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
*/
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';
@ -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)) {