diff --git a/demo/colors.ts b/demo/colors.ts index b5aae39..42bc0ab 100644 --- a/demo/colors.ts +++ b/demo/colors.ts @@ -1,34 +1,63 @@ /** * This is a test file and a terminal color table program */ -import Ansi, { Ground } from '../src/common/ansi.ts'; +import Ansi, { AnsiColor, Ground } from '../src/common/ansi.ts'; // ---------------------------------------------------------------------------- // Display table of the 256 type color console escape codes // ---------------------------------------------------------------------------- + +const addColor = (fore: number | string, back: number | string): string => { + let output = ''; + output += (typeof fore === 'number') ? Ansi.color(fore) : fore; + output += (typeof back === 'number') ? Ansi.color(back) : back; + + return output; +}; + +const padNum = (num: number): string => + String(num).padStart(3, ' ').padEnd(5, ' '); + +const colorBlock = ( + start: number, + end: number, + block: (i: number) => [string | number, string | number], +): string => { + let output = ''; + + for (let i = start; i < end; i++) { + const [fg, bg] = block(i); + + output += addColor(fg, bg); + output += padNum(i); + output += Ansi.ResetFormatting; + } + return output; +}; + function print16colorTable(): void { - let colorTable = ''; - - [30, 90].forEach((start) => { - let i = 0; + const drawRow = (start: number): string => { let end = start + 8; - for (i = start; i < end; i++) { - colorTable += Ansi.color(i); - colorTable += String(i).padStart(3, ' ').padEnd(4, ' '); - colorTable += Ansi.ResetFormatting; - } - colorTable += ' '.repeat(5); + let blocks = [ + colorBlock(start, end, (i: number) => [i, AnsiColor.BgBlack]), + colorBlock(start, end, (i: number) => [i, AnsiColor.BgBrightWhite]), + ]; start += 10; end += 10; - for (i = start; i < end; i++) { - colorTable += Ansi.color(i); - colorTable += String(i).padStart(4, ' ').padEnd(5, ' '); - colorTable += Ansi.ResetFormatting; - } - colorTable += '\n'; - }); + blocks.push(colorBlock(start, end, (i: number) => [AnsiColor.FgBlack, i])); + blocks.push( + colorBlock(start, end, (i: number) => [AnsiColor.FgBrightWhite, i]), + ); + + return blocks.join(' '.repeat(5)); + }; + + let colorTable = [ + drawRow(30), + drawRow(90), + ].join('\n'); colorTable += '\n'; @@ -38,7 +67,7 @@ function print256colorTable(): void { let colorTable = ''; // deno-fmt-ignore const breaks = [ - 7, 15, + 7, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105, 111, 117, 123, @@ -53,22 +82,30 @@ function print256colorTable(): void { const start = (n > 0) ? breaks[n - 1] + 1 : 0; const end = line + 1; - let i = 0; - - for (i = start; i < end; i++) { - colorTable += Ansi.color256(i, Ground.Fore); - colorTable += String(i).padStart(4, ' ').padEnd(5, ' '); - colorTable += Ansi.ResetFormatting; - } - - colorTable += ' '.repeat(5); - - for (i = start; i < end; i++) { - colorTable += Ansi.color256(i, Ground.Back); - colorTable += String(i).padStart(4, ' ').padEnd(5, ' '); - colorTable += Ansi.ResetFormatting; - } + const blocks = [ + colorBlock( + start, + end, + (i: number) => [Ansi.color256(i, Ground.Fore), AnsiColor.BgBlack], + ), + colorBlock( + start, + end, + (i: number) => [Ansi.color256(i, Ground.Fore), AnsiColor.BgBrightWhite], + ), + colorBlock( + start, + end, + (i: number) => [AnsiColor.FgBlack, Ansi.color256(i, Ground.Back)], + ), + colorBlock( + start, + end, + (i: number) => [AnsiColor.FgBrightWhite, Ansi.color256(i, Ground.Back)], + ), + ]; + colorTable += blocks.join(' '.repeat(5)); colorTable += '\n'; if (doubleBreaks.includes(line)) {