Refactor Ansi module so that it works as expected
All checks were successful
timw4mail/scroll/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2024-07-24 15:19:32 -04:00
parent d59900a895
commit 46a0314ce6
3 changed files with 41 additions and 42 deletions

View File

@ -224,11 +224,11 @@ const highlightToColorTest = {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const ANSITest = () => { const ANSITest = () => {
const { AnsiColor, Ground } = _Ansi; const { Ground } = _Ansi;
return { return {
'color()': () => { 'color()': () => {
assertEquals(Ansi.color(AnsiColor.FgBlue), '\x1b[34m'); assertEquals(Ansi.color.Blue, '\x1b[34m');
}, },
'color256()': () => { 'color256()': () => {
assertEquals(Ansi.color256(128, Ground.Back), '\x1b[48;5;128m'); assertEquals(Ansi.color256(128, Ground.Back), '\x1b[48;5;128m');

View File

@ -108,7 +108,6 @@ const moveCursorForward = (col: number): string => code(col, 'C');
const moveCursorDown = (row: number): string => code(row, 'B'); const moveCursorDown = (row: number): string => code(row, 'B');
const textFormat = (param: string | number | string[] | number[]): string => const textFormat = (param: string | number | string[] | number[]): string =>
code(param, 'm'); code(param, 'm');
const color = (value: AnsiColor): string => textFormat(value);
const color256 = (value: number, ground: Ground = Ground.Fore): string => const color256 = (value: number, ground: Ground = Ground.Fore): string =>
textFormat([ground, AnsiColor.Type256, value]); textFormat([ground, AnsiColor.Type256, value]);
const rgb = ( const rgb = (
@ -131,48 +130,48 @@ export const Ansi = {
moveCursorForward, moveCursorForward,
moveCursorDown, moveCursorDown,
textFormat, textFormat,
color, color: {
colors: { Black: textFormat(AnsiColor.FgBlack),
rgb, Red: textFormat(AnsiColor.FgRed),
Black: AnsiColor.FgBlack, Green: textFormat(AnsiColor.FgGreen),
Red: AnsiColor.FgRed, Yellow: textFormat(AnsiColor.FgYellow),
Green: AnsiColor.FgGreen, Blue: textFormat(AnsiColor.FgBlue),
Yellow: AnsiColor.FgYellow, Magenta: textFormat(AnsiColor.FgMagenta),
Blue: AnsiColor.FgBlue, Cyan: textFormat(AnsiColor.FgCyan),
Magenta: AnsiColor.FgMagenta, White: textFormat(AnsiColor.FgWhite),
Cyan: AnsiColor.FgCyan, Default: textFormat(AnsiColor.FgDefault),
White: AnsiColor.FgWhite, BrightBlack: textFormat(AnsiColor.FgBrightBlack),
BrightBlack: AnsiColor.FgBrightBlack, BrightRed: textFormat(AnsiColor.FgBrightRed),
BrightRed: AnsiColor.FgBrightRed, BrightGreen: textFormat(AnsiColor.FgBrightGreen),
BrightGreen: AnsiColor.FgBrightGreen, BrightYellow: textFormat(AnsiColor.FgBrightYellow),
BrightYellow: AnsiColor.FgBrightYellow, BrightBlue: textFormat(AnsiColor.FgBrightBlue),
BrightBlue: AnsiColor.FgBrightBlue, BrightMagenta: textFormat(AnsiColor.FgBrightMagenta),
BrightMagenta: AnsiColor.FgBrightMagenta, BrightCyan: textFormat(AnsiColor.FgBrightCyan),
BrightCyan: AnsiColor.FgBrightCyan, BrightWhite: textFormat(AnsiColor.FgBrightWhite),
BrightWhite: AnsiColor.FgBrightWhite, Invert: textFormat(AnsiColor.Invert),
Invert: AnsiColor.Invert,
background: { background: {
rgb: (r: number, g: number, b: number) => rgb(r, g, b, Ground.Back), Black: textFormat(AnsiColor.BgBlack),
Black: AnsiColor.BgBlack, Red: textFormat(AnsiColor.BgRed),
Red: AnsiColor.BgRed, Green: textFormat(AnsiColor.BgGreen),
Green: AnsiColor.BgGreen, Yellow: textFormat(AnsiColor.BgYellow),
Yellow: AnsiColor.BgYellow, Blue: textFormat(AnsiColor.BgBlue),
Blue: AnsiColor.BgBlue, Magenta: textFormat(AnsiColor.BgMagenta),
Magenta: AnsiColor.BgMagenta, Cyan: textFormat(AnsiColor.BgCyan),
Cyan: AnsiColor.BgCyan, White: textFormat(AnsiColor.BgWhite),
White: AnsiColor.BgWhite, Default: textFormat(AnsiColor.BgDefault),
BrightBlack: AnsiColor.BgBrightBlack, BrightBlack: textFormat(AnsiColor.BgBrightBlack),
BrightRed: AnsiColor.BgBrightRed, BrightRed: textFormat(AnsiColor.BgBrightRed),
BrightGreen: AnsiColor.BgBrightGreen, BrightGreen: textFormat(AnsiColor.BgBrightGreen),
BrightYellow: AnsiColor.BgBrightYellow, BrightYellow: textFormat(AnsiColor.BgBrightYellow),
BrightBlue: AnsiColor.BgBrightBlue, BrightBlue: textFormat(AnsiColor.BgBrightBlue),
BrightMagenta: AnsiColor.BgBrightMagenta, BrightMagenta: textFormat(AnsiColor.BgBrightMagenta),
BrightCyan: AnsiColor.BgBrightCyan, BrightCyan: textFormat(AnsiColor.BgBrightCyan),
BrightWhite: AnsiColor.BgBrightWhite, BrightWhite: textFormat(AnsiColor.BgBrightWhite),
Invert: AnsiColor.Invert, Invert: textFormat(AnsiColor.Invert),
}, },
}, },
color256, color256,
rgb,
}; };
export default Ansi; export default Ansi;

View File

@ -14,7 +14,7 @@ export const defaultTerminalSize: ITerminalSize = {
}; };
export const SCROLL_COLOR_SCHEME: Map<HighlightType, string> = new Map([ export const SCROLL_COLOR_SCHEME: Map<HighlightType, string> = new Map([
[HighlightType.Match, Ansi.colors.Invert.toString()], // Inverted color [HighlightType.Match, Ansi.color.Invert], // Inverted color
[HighlightType.Number, Ansi.color256(196)], // Bright Red [HighlightType.Number, Ansi.color256(196)], // Bright Red
[HighlightType.Character, Ansi.color256(207)], // Magenta [HighlightType.Character, Ansi.color256(207)], // Magenta
[HighlightType.String, Ansi.color256(45)], // Cyan [HighlightType.String, Ansi.color256(45)], // Cyan