Skip to content

Commit 6663174

Browse files
committed
feat(themes): integrate Logger with theme system
Make LOG_SYMBOLS theme-aware by reading colors from the active theme's color palette. Symbols now use theme.colors.success, theme.colors.error, theme.colors.warning, theme.colors.info, and theme.colors.step instead of hardcoded colors. Changes: - Added getTheme() import to logger.ts - Created applyColor() helper to handle both named colors and RGB tuples - Modified LOG_SYMBOLS init() to read from theme colors - Updated YoctoColors type definition with rgb/bgRgb methods and bright colors - Updated LOG_SYMBOLS documentation to reflect theme integration Colors are determined by the theme active at initialization time.
1 parent 68a2f06 commit 6663174

2 files changed

Lines changed: 57 additions & 12 deletions

File tree

src/external/yoctocolors-cjs.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ interface YoctoColors {
2222
gray: (text: string) => string
2323
grey: (text: string) => string
2424

25+
// Bright colors
26+
blackBright: (text: string) => string
27+
redBright: (text: string) => string
28+
greenBright: (text: string) => string
29+
yellowBright: (text: string) => string
30+
blueBright: (text: string) => string
31+
magentaBright: (text: string) => string
32+
cyanBright: (text: string) => string
33+
whiteBright: (text: string) => string
34+
2535
// Background colors
2636
bgBlack: (text: string) => string
2737
bgRed: (text: string) => string
@@ -31,6 +41,10 @@ interface YoctoColors {
3141
bgMagenta: (text: string) => string
3242
bgCyan: (text: string) => string
3343
bgWhite: (text: string) => string
44+
45+
// RGB colors
46+
rgb: (r: number, g: number, b: number) => (text: string) => string
47+
bgRgb: (r: number, g: number, b: number) => (text: string) => string
3448
}
3549

3650
declare const yoctocolorsCjs: YoctoColors

src/logger.ts

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import isUnicodeSupported from './external/@socketregistry/is-unicode-supported'
77
import yoctocolorsCjs from './external/yoctocolors-cjs'
88
import { objectAssign, objectFreeze } from './objects'
99
import { applyLinePrefix, isBlankString } from './strings'
10+
import type { ColorValue } from './spinner'
11+
import { getTheme } from './themes/context'
1012

1113
/**
1214
* Log symbols for terminal output with colored indicators.
@@ -115,24 +117,44 @@ function getYoctocolors() {
115117
return yoctocolorsCjs
116118
}
117119

120+
/**
121+
* Apply a color to text using yoctocolors.
122+
* Handles both named colors and RGB tuples.
123+
* @private
124+
*/
125+
/*@__NO_SIDE_EFFECTS__*/
126+
function applyColor(
127+
text: string,
128+
color: ColorValue,
129+
colors: typeof yoctocolorsCjs,
130+
): string {
131+
if (typeof color === 'string') {
132+
// Named color like 'green', 'red', etc.
133+
return (colors as any)[color](text)
134+
}
135+
// RGB tuple [r, g, b]
136+
return colors.rgb(color[0], color[1], color[2])(text)
137+
}
138+
118139
/**
119140
* Log symbols for terminal output with colored indicators.
120141
*
121142
* Provides colored Unicode symbols (✔, ✖, ⚠, ℹ, →) with ASCII fallbacks (√, ×, ‼, i, >)
122-
* for terminals that don't support Unicode. Symbols are color-coded: green for
123-
* success, red for failure, yellow for warnings, blue for info, cyan for step.
143+
* for terminals that don't support Unicode. Symbols are colored according to the active
144+
* theme's color palette (success, error, warning, info, step).
124145
*
125146
* The symbols are lazily initialized on first access and then frozen for immutability.
147+
* Colors are determined by the theme active at initialization time.
126148
*
127149
* @example
128150
* ```typescript
129151
* import { LOG_SYMBOLS } from '@socketsecurity/lib'
130152
*
131-
* console.log(`${LOG_SYMBOLS.success} Build completed`) // Green
132-
* console.log(`${LOG_SYMBOLS.fail} Build failed`) // Red
133-
* console.log(`${LOG_SYMBOLS.warn} Deprecated API used`) // Yellow
134-
* console.log(`${LOG_SYMBOLS.info} Starting process`) // Blue
135-
* console.log(`${LOG_SYMBOLS.step} Processing files`) // Cyan
153+
* console.log(`${LOG_SYMBOLS.success} Build completed`) // Theme success color
154+
* console.log(`${LOG_SYMBOLS.fail} Build failed`) // Theme error color
155+
* console.log(`${LOG_SYMBOLS.warn} Deprecated API used`) // Theme warning color
156+
* console.log(`${LOG_SYMBOLS.info} Starting process`) // Theme info color
157+
* console.log(`${LOG_SYMBOLS.step} Processing files`) // Theme step color
136158
* ```
137159
*/
138160
export const LOG_SYMBOLS = /*@__PURE__*/ (() => {
@@ -146,12 +168,21 @@ export const LOG_SYMBOLS = /*@__PURE__*/ (() => {
146168
const init = () => {
147169
const supported = isUnicodeSupported()
148170
const colors = getYoctocolors()
171+
const theme = getTheme()
172+
173+
// Get colors from theme
174+
const successColor = theme.colors.success
175+
const errorColor = theme.colors.error
176+
const warningColor = theme.colors.warning
177+
const infoColor = theme.colors.info
178+
const stepColor = theme.colors.step
179+
149180
objectAssign(target, {
150-
fail: colors.red(supported ? '✖' : '×'),
151-
info: colors.blue(supported ? 'ℹ' : 'i'),
152-
step: colors.cyan(supported ? '→' : '>'),
153-
success: colors.green(supported ? '✔' : '√'),
154-
warn: colors.yellow(supported ? '⚠' : '‼'),
181+
fail: applyColor(supported ? '✖' : '×', errorColor, colors),
182+
info: applyColor(supported ? 'ℹ' : 'i', infoColor, colors),
183+
step: applyColor(supported ? '→' : '>', stepColor, colors),
184+
success: applyColor(supported ? '✔' : '√', successColor, colors),
185+
warn: applyColor(supported ? '⚠' : '‼', warningColor, colors),
155186
})
156187
objectFreeze(target)
157188
// The handler of a Proxy is mutable after proxy instantiation.

0 commit comments

Comments
 (0)