More fun with colors
All checks were successful
timw4mail/scroll/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2024-07-18 17:25:21 -04:00
parent 0c13942aae
commit 5b40d16999

View File

@ -1,34 +1,63 @@
/** /**
* This is a test file and a terminal color table program * 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 // Display table of the 256 type color console escape codes
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
function print16colorTable(): void {
let colorTable = '';
[30, 90].forEach((start) => { const addColor = (fore: number | string, back: number | string): string => {
let i = 0; let output = '';
let end = start + 8; output += (typeof fore === 'number') ? Ansi.color(fore) : fore;
for (i = start; i < end; i++) { output += (typeof back === 'number') ? Ansi.color(back) : back;
colorTable += Ansi.color(i);
colorTable += String(i).padStart(3, ' ').padEnd(4, ' '); return output;
colorTable += Ansi.ResetFormatting; };
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;
};
colorTable += ' '.repeat(5); function print16colorTable(): void {
const drawRow = (start: number): string => {
let end = start + 8;
let blocks = [
colorBlock(start, end, (i: number) => [i, AnsiColor.BgBlack]),
colorBlock(start, end, (i: number) => [i, AnsiColor.BgBrightWhite]),
];
start += 10; start += 10;
end += 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'; colorTable += '\n';
@ -53,22 +82,30 @@ function print256colorTable(): void {
const start = (n > 0) ? breaks[n - 1] + 1 : 0; const start = (n > 0) ? breaks[n - 1] + 1 : 0;
const end = line + 1; const end = line + 1;
let i = 0; const blocks = [
colorBlock(
for (i = start; i < end; i++) { start,
colorTable += Ansi.color256(i, Ground.Fore); end,
colorTable += String(i).padStart(4, ' ').padEnd(5, ' '); (i: number) => [Ansi.color256(i, Ground.Fore), AnsiColor.BgBlack],
colorTable += Ansi.ResetFormatting; ),
} colorBlock(
start,
colorTable += ' '.repeat(5); end,
(i: number) => [Ansi.color256(i, Ground.Fore), AnsiColor.BgBrightWhite],
for (i = start; i < end; i++) { ),
colorTable += Ansi.color256(i, Ground.Back); colorBlock(
colorTable += String(i).padStart(4, ' ').padEnd(5, ' '); start,
colorTable += Ansi.ResetFormatting; 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'; colorTable += '\n';
if (doubleBreaks.includes(line)) { if (doubleBreaks.includes(line)) {