MCPcopy Create free account
hub / github.com/Stevenic/codepilot / Colorize

Class Colorize

src/internals/Colorize.ts:7–101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5 * Colorizes text for the console.
6 */
7export class Colorize {
8 /**
9 * Replaces the current line with the given text.
10 * @param text The text to replace the current line with.
11 */
12 public static replaceLine(text: string): string {
13 return '\x1b[A\x1b[2K' + text;
14 }
15
16 /**
17 * Renders the given text as error text.
18 * @param error Error text to render.
19 */
20 public static error(error: Error|string): string {
21 if (typeof error === 'string') {
22 return `\x1b[31;1m${error}\x1b[0m`;
23 } else {
24 return `\x1b[31;1m${error.message}\x1b[0m`;
25 }
26 }
27
28 /**
29 * Renders the given text with a highlight to call attention.
30 * @param message Text to highlight.
31 */
32 public static highlight(message: string): string {
33 return `\x1b[34;1m${message}\x1b[0m`;
34 }
35
36 /**
37 * Renders the given text as general output text.
38 * @param output Text to render.
39 * @param quote Optional. Quote to use for strings. Defaults to `''`.
40 * @param units Optional. Units to use for numbers. Defaults to `''`.
41 */
42 public static output(output: object | string, quote: string = '', units: string = ''): string {
43 if (typeof output === 'string') {
44 return `\x1b[32m${quote}${output}${quote}\x1b[0m`;
45 } else if (typeof output === 'object' && output !== null) {
46 return colorizer(output, {
47 pretty: true,
48 colors: {
49 BRACE: 'white',
50 BRACKET: 'white',
51 COLON: 'white',
52 COMMA: 'white',
53 STRING_KEY: 'white',
54 STRING_LITERAL: 'green',
55 NUMBER_LITERAL: 'blue',
56 BOOLEAN_LITERAL: 'blue',
57 NULL_LITERAL: 'blue'
58 }
59 });
60 } else if (typeof output == 'number') {
61 return `\x1b[34m${output}${units}\x1b[0m`;
62 } else {
63 return `\x1b[34m${output}\x1b[0m`;
64 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected