Skip to content

Commit e9976a9

Browse files
committed
chore: wip
1 parent 914d1f4 commit e9976a9

File tree

8 files changed

+486
-126
lines changed

8 files changed

+486
-126
lines changed

src/colors.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* ANSI Color codes for terminal output
3+
*/
4+
5+
// Basic colors
6+
export const reset = '\x1B[0m'
7+
export const bold = '\x1B[1m'
8+
export const dim = '\x1B[2m'
9+
10+
// Foreground colors
11+
export const black = '\x1B[30m'
12+
export const red = '\x1B[31m'
13+
export const green = '\x1B[32m'
14+
export const yellow = '\x1B[33m'
15+
export const blue = '\x1B[34m'
16+
export const magenta = '\x1B[35m'
17+
export const cyan = '\x1B[36m'
18+
export const white = '\x1B[37m'
19+
export const gray = '\x1B[90m'
20+
21+
// Background colors
22+
export const bgBlack = '\x1B[40m'
23+
export const bgRed = '\x1B[41m'
24+
export const bgGreen = '\x1B[42m'
25+
export const bgYellow = '\x1B[43m'
26+
export const bgBlue = '\x1B[44m'
27+
export const bgMagenta = '\x1B[45m'
28+
export const bgCyan = '\x1B[46m'
29+
export const bgWhite = '\x1B[47m'
30+
31+
// Log level specific colors
32+
export const levels = {
33+
debug: gray,
34+
info: blue,
35+
success: green,
36+
warning: yellow,
37+
error: red,
38+
}
39+
40+
/**
41+
* Wrap text with color codes
42+
*/
43+
export function colorize(text: string, color: string): string {
44+
return `${color}${text}${reset}`
45+
}
46+
47+
/**
48+
* Remove ANSI color codes from text
49+
*/
50+
export function stripColors(text: string): string {
51+
return text.replace(/\x1B\[\d+m/g, '')
52+
}

src/formatters/text.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class TextFormatter implements Formatter {
3232
constructor(private config: ClarityConfig) { }
3333

3434
async format(entry: LogEntry, forFile: boolean = false): Promise<string> {
35-
const timestamp = this.config.timestamp ? `${colors.gray(entry.timestamp.toISOString())} ` : ''
36-
const name = colors.gray(`[${entry.name}]`)
35+
const timestamp = this.config.timestamp ? `${colors.colorize(entry.timestamp.toISOString(), colors.gray)} ` : ''
36+
const name = colors.colorize(`[${entry.name}]`, colors.gray)
3737

3838
const levelSymbols: Record<LogLevel, string> = {
3939
debug: s('🔍', 'D'),
@@ -43,7 +43,7 @@ export class TextFormatter implements Formatter {
4343
error: s('❌', '×'),
4444
}
4545

46-
const levelColors: Record<LogLevel, (text: string) => string> = {
46+
const levelColors: Record<LogLevel, string> = {
4747
debug: colors.gray,
4848
info: colors.blue,
4949
success: colors.green,
@@ -67,7 +67,7 @@ export class TextFormatter implements Formatter {
6767

6868
const symbol = this.config.colors ? levelSymbols[entry.level] : ''
6969
message = this.config.colors
70-
? levelColors[entry.level](message)
70+
? colors.colorize(message, levelColors[entry.level])
7171
: message
7272

7373
// For file output, put timestamp at beginning
@@ -108,10 +108,10 @@ export class TextFormatter implements Formatter {
108108
if (funcLocationParts.length > 1) {
109109
const fnName = funcLocationParts[0]
110110
const location = funcLocationParts[1].replace(')', '')
111-
return ` ${colors.gray(`at ${ANSI.cyan}${fnName}${ANSI.reset} (${location})`)}`
111+
return ` ${colors.colorize(`at ${colors.colorize(fnName, ANSI.cyan)} (${location})`, colors.gray)}`
112112
}
113113
}
114-
return ` ${colors.gray(line.trim())}`
114+
return ` ${colors.colorize(line.trim(), colors.gray)}`
115115
}
116116
return ` ${line}`
117117
})

0 commit comments

Comments
 (0)