Misc refactoring
All checks were successful
timw4mail/scroll/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2024-07-24 15:44:28 -04:00
parent 46a0314ce6
commit 501f5e10d5
4 changed files with 41 additions and 19 deletions

View File

@ -6,10 +6,10 @@ import Ansi, { AnsiColor, Ground } from '../src/common/ansi.ts';
// Display table of the 256 type color console escape codes // Display table of the 256 type color console escape codes
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const addColor = (fore: number | string, back: number | string): string => { const addColor = (fore: string, back: string): string => {
let output = ''; let output = '';
output += (typeof fore === 'number') ? Ansi.color(fore) : fore; output += fore;
output += (typeof back === 'number') ? Ansi.color(back) : back; output += back;
return output; return output;
}; };
@ -20,7 +20,7 @@ const padNum = (num: number): string =>
const colorBlock = ( const colorBlock = (
start: number, start: number,
end: number, end: number,
block: (i: number) => [string | number, string | number], block: (i: number) => [string, string],
): string => { ): string => {
let output = ''; let output = '';
@ -39,16 +39,34 @@ function print16colorTable(): void {
let end = start + 8; let end = start + 8;
let blocks = [ let blocks = [
colorBlock(start, end, (i: number) => [i, AnsiColor.BgBlack]), colorBlock(
colorBlock(start, end, (i: number) => [i, AnsiColor.BgBrightWhite]), start,
end,
(i: number) => [Ansi.textFormat(i), Ansi.color.background.Black],
),
colorBlock(
start,
end,
(i: number) => [Ansi.textFormat(i), Ansi.color.background.BrightWhite],
),
]; ];
start += 10; start += 10;
end += 10; end += 10;
blocks.push(colorBlock(start, end, (i: number) => [AnsiColor.FgBlack, i]));
blocks.push( blocks.push(
colorBlock(start, end, (i: number) => [AnsiColor.FgBrightWhite, i]), colorBlock(
start,
end,
(i: number) => [Ansi.color.Black, Ansi.textFormat(i)],
),
);
blocks.push(
colorBlock(
start,
end,
(i: number) => [Ansi.color.BrightWhite, Ansi.textFormat(i)],
),
); );
return blocks.join(' '.repeat(5)); return blocks.join(' '.repeat(5));
@ -87,22 +105,26 @@ function print256colorTable(): void {
colorBlock( colorBlock(
start, start,
end, end,
(i: number) => [Ansi.color256(i, Ground.Fore), AnsiColor.BgBlack], (
i: number,
) => [Ansi.color256(i, Ground.Fore), Ansi.color.background.Black],
), ),
colorBlock( colorBlock(
start, start,
end, end,
(i: number) => [Ansi.color256(i, Ground.Fore), AnsiColor.BgBrightWhite], (
i: number,
) => [Ansi.color256(i, Ground.Fore), Ansi.color.background.BrightWhite],
), ),
colorBlock( colorBlock(
start, start,
end, end,
(i: number) => [AnsiColor.FgBlack, Ansi.color256(i, Ground.Back)], (i: number) => [Ansi.color.Black, Ansi.color256(i, Ground.Back)],
), ),
colorBlock( colorBlock(
start, start,
end, end,
(i: number) => [AnsiColor.FgBrightWhite, Ansi.color256(i, Ground.Back)], (i: number) => [Ansi.color.BrightWhite, Ansi.color256(i, Ground.Back)],
), ),
]; ];

View File

@ -644,12 +644,12 @@ const RowTest = {
const normalRow = Row.from('\tFor whom the bell tolls'); const normalRow = Row.from('\tFor whom the bell tolls');
assertEquivalent( assertEquivalent(
normalRow.find('who', 0, SearchDirection.Forward), normalRow.find('who', 0, SearchDirection.Forward),
Some(8), Some(5),
); );
assertEquals(normalRow.find('foo', 0, SearchDirection.Forward), None); assertEquals(normalRow.find('foo', 0, SearchDirection.Forward), None);
const emojiRow = Row.from('\t😺😸😹'); const emojiRow = Row.from('\t😺😸😹');
assertEquivalent(emojiRow.find('😹', 0, SearchDirection.Forward), Some(6)); assertEquivalent(emojiRow.find('😹', 0, SearchDirection.Forward), Some(3));
assertEquals(emojiRow.find('🤰🏼', 10, SearchDirection.Forward), None); assertEquals(emojiRow.find('🤰🏼', 10, SearchDirection.Forward), None);
}, },
'.find backwards': () => { '.find backwards': () => {

View File

@ -98,7 +98,7 @@ export class Document {
for (let y = at.y; y >= 0 && y < this.numRows; y += direction) { for (let y = at.y; y >= 0 && y < this.numRows; y += direction) {
const maybeMatch = this.#rows[y].find(q, position.x, direction); const maybeMatch = this.#rows[y].find(q, position.x, direction);
if (maybeMatch.isSome()) { if (maybeMatch.isSome()) {
position.x = this.#rows[y].rxToCx(maybeMatch.unwrap()); position.x = maybeMatch.unwrap();
return Some(position); return Some(position);
} }

View File

@ -132,7 +132,7 @@ export class Row {
/** /**
* Search the current row for the specified string, and return * Search the current row for the specified string, and return
* the render 'character' index of the start of that match * the 'character' index of the start of that match
*/ */
public find( public find(
s: string, s: string,
@ -158,11 +158,11 @@ export class Row {
// equal the number of characters. So // equal the number of characters. So
// searching is fairly easy // searching is fairly easy
if (thisStr.length === this.chars.length) { if (thisStr.length === this.chars.length) {
return Some(this.cxToRx(byteIndex)); return Some(byteIndex);
} }
// Emoji/Extended Unicode-friendly search // Emoji/Extended Unicode-friendly search
return Some(this.cxToRx(this.byteIndexToCharIndex(byteIndex))); return Some(this.byteIndexToCharIndex(byteIndex));
} }
/** /**
@ -532,7 +532,7 @@ export class Row {
// Highlight character literals // Highlight character literals
const ch = this.rchars[i]; const ch = this.rchars[i];
if (ch === SINGLE_QUOTE) { if (ch === SINGLE_QUOTE && this.rIndexOf(SINGLE_QUOTE, i + 1).isSome()) {
while (true) { while (true) {
this.hl.push(HighlightType.Character); this.hl.push(HighlightType.Character);
i += 1; i += 1;