mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-10-24 06:10:38 +08:00
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import type { ColorName } from "chalk";
|
|
|
|
import chalk from "chalk";
|
|
|
|
const PADDING_WIDTH = 12;
|
|
|
|
// The order here is important, we want successive prefixes to have
|
|
// high contrast.
|
|
const availableColors: ColorName[] = [
|
|
"blue",
|
|
"yellowBright",
|
|
"greenBright",
|
|
"gray",
|
|
"green",
|
|
"blueBright",
|
|
"redBright",
|
|
"white",
|
|
"yellow",
|
|
"red",
|
|
"magenta",
|
|
"cyan",
|
|
];
|
|
|
|
let nextColorIndex = 0;
|
|
|
|
const assignedColors: Record<string, ColorName> = {};
|
|
|
|
export function logPrefix(label: string): string {
|
|
if (assignedColors[label] === undefined) {
|
|
const color = availableColors[nextColorIndex];
|
|
nextColorIndex = (nextColorIndex + 1) % availableColors.length;
|
|
assignedColors[label] = color;
|
|
}
|
|
|
|
// We reset colors at the beginning of each line to avoid styles from previous
|
|
// lines messing up prefix colors. This is noticable in rust stack traces
|
|
// where the `in` and `with` keywords have a white background color.
|
|
return chalk.reset[assignedColors[label]](
|
|
`${label.padEnd(PADDING_WIDTH)} | `,
|
|
);
|
|
}
|