Skip to content

Commit 62b1e47

Browse files
committed
refactor(themes): polish theme system with elegant sophistication
Three rounds of refinement inspired by premium design: Round 1 - Color Palettes & Theme Essence: - Upgraded all themes to use Bright color variants for terminal clarity - Refined theme descriptions with sophisticated, evocative language - Socket: "Signature theme with refined violet and subtle shimmer" - Coana: "Analytical theme with crisp azure for precision" - Firewall: "Protective theme with warm ember and balanced contrast" - Python: "Python-inspired theme with steel blue and golden harmony" - Ultra: "Premium theme with prismatic shimmer for deep analysis" Round 2 - Documentation & Types: - Streamlined all JSDoc comments for clarity and elegance - Simplified function descriptions while maintaining precision - Reduced verbosity without sacrificing information - Enhanced type documentation with concise, purposeful descriptions Round 3 - API Surface & Philosophy: - Added design philosophy comment: "Elegance in restraint" - Refined export section labels for better code organization - Ensured consistent terminology across all theme files - Polished every detail for a premium developer experience Result: 180 lines removed through refinement, not deletion. Every word earns its place. Sophisticated color choices, minimal documentation, maximum clarity—the Mercedes experience for terminal theming.
1 parent 76f4582 commit 62b1e47

5 files changed

Lines changed: 157 additions & 180 deletions

File tree

src/themes/context.ts

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
2-
* @fileoverview Theme context management using AsyncLocalStorage.
3-
* Provides async-aware theme management with automatic context isolation.
2+
* @fileoverview Elegant theme context management.
3+
* Async-aware theming with automatic context isolation via AsyncLocalStorage.
44
*/
55

66
import { AsyncLocalStorage } from 'node:async_hooks'
@@ -9,39 +9,33 @@ import type { Theme } from './types'
99
import { SOCKET_THEME, THEMES, type ThemeName } from './themes'
1010

1111
/**
12-
* Theme change event listener.
12+
* Theme change event listener signature.
1313
*/
1414
export type ThemeChangeListener = (theme: Theme) => void
1515

1616
/**
17-
* AsyncLocalStorage for theme context.
18-
* Automatically isolates theme state across async boundaries.
17+
* AsyncLocalStorage for theme context isolation.
1918
*/
2019
const themeStorage = new AsyncLocalStorage<Theme>()
2120

2221
/**
23-
* Fallback theme when no async context is active.
22+
* Fallback theme for global context.
2423
*/
2524
let fallbackTheme: Theme = SOCKET_THEME
2625

27-
// Event listeners
26+
/**
27+
* Registered theme change listeners.
28+
*/
2829
const listeners: Set<ThemeChangeListener> = new Set()
2930

3031
/**
31-
* Set the fallback theme (used when no async context is active).
32-
* This replaces the previous global theme setter.
32+
* Set the global fallback theme.
3333
*
34-
* @param theme - Theme object or theme name
34+
* @param theme - Theme name or object
3535
*
3636
* @example
3737
* ```ts
38-
* import { setTheme } from '@socketsecurity/lib/themes'
39-
*
40-
* // Set by name
4138
* setTheme('socket-firewall')
42-
*
43-
* // Set by object
44-
* setTheme(customTheme)
4539
* ```
4640
*/
4741
export function setTheme(theme: Theme | ThemeName): void {
@@ -50,44 +44,34 @@ export function setTheme(theme: Theme | ThemeName): void {
5044
}
5145

5246
/**
53-
* Get the current theme from async context or fallback.
47+
* Get the active theme from context.
5448
*
5549
* @returns Current theme
5650
*
5751
* @example
5852
* ```ts
59-
* import { getTheme } from '@socketsecurity/lib/themes'
60-
*
6153
* const theme = getTheme()
62-
* console.log(theme.displayName) // "Socket Security"
54+
* console.log(theme.displayName)
6355
* ```
6456
*/
6557
export function getTheme(): Theme {
6658
return themeStorage.getStore() ?? fallbackTheme
6759
}
6860

6961
/**
70-
* Execute an async operation with a temporary theme.
71-
* Uses AsyncLocalStorage for automatic context isolation.
72-
* Theme is automatically restored when the operation completes.
62+
* Execute async operation with scoped theme.
63+
* Theme automatically restored on completion.
7364
*
74-
* @template T - Return type of the operation
75-
* @param theme - Theme to use during operation
76-
* @param fn - Async function to execute
77-
* @returns Result of the operation
65+
* @template T - Return type
66+
* @param theme - Scoped theme
67+
* @param fn - Async operation
68+
* @returns Operation result
7869
*
7970
* @example
8071
* ```ts
81-
* import { withTheme } from '@socketsecurity/lib/themes'
82-
* import { Spinner } from '@socketsecurity/lib/spinner'
83-
*
8472
* await withTheme('ultra', async () => {
85-
* const spinner = Spinner({ text: 'Rainbow mode!' })
86-
* spinner.start()
87-
* await heavyOperation()
88-
* spinner.stop()
73+
* // Operations use Ultra theme
8974
* })
90-
* // Theme automatically restored via AsyncLocalStorage
9175
* ```
9276
*/
9377
export async function withTheme<T>(
@@ -102,21 +86,17 @@ export async function withTheme<T>(
10286
}
10387

10488
/**
105-
* Execute a synchronous operation with a temporary theme.
106-
* Uses AsyncLocalStorage for automatic context isolation.
107-
* Theme is automatically restored when the operation completes.
89+
* Execute sync operation with scoped theme.
90+
* Theme automatically restored on completion.
10891
*
109-
* @template T - Return type of the operation
110-
* @param theme - Theme to use during operation
111-
* @param fn - Synchronous function to execute
112-
* @returns Result of the operation
92+
* @template T - Return type
93+
* @param theme - Scoped theme
94+
* @param fn - Sync operation
95+
* @returns Operation result
11396
*
11497
* @example
11598
* ```ts
116-
* import { withThemeSync } from '@socketsecurity/lib/themes'
117-
*
11899
* const result = withThemeSync('coana', () => {
119-
* // Sync operations with coana theme
120100
* return processData()
121101
* })
122102
* ```
@@ -130,20 +110,18 @@ export function withThemeSync<T>(theme: Theme | ThemeName, fn: () => T): T {
130110
}
131111

132112
/**
133-
* Register a listener for theme change events.
113+
* Subscribe to theme change events.
134114
*
135-
* @param listener - Function to call when theme changes
115+
* @param listener - Change handler
136116
* @returns Unsubscribe function
137117
*
138118
* @example
139119
* ```ts
140-
* import { onThemeChange } from '@socketsecurity/lib/themes'
141-
*
142120
* const unsubscribe = onThemeChange((theme) => {
143-
* console.log('Theme changed to:', theme.displayName)
121+
* console.log('Theme:', theme.displayName)
144122
* })
145123
*
146-
* // Later: stop listening
124+
* // Cleanup
147125
* unsubscribe()
148126
* ```
149127
*/
@@ -155,7 +133,7 @@ export function onThemeChange(listener: ThemeChangeListener): () => void {
155133
}
156134

157135
/**
158-
* Emit theme change event to all listeners.
136+
* Emit theme change event to listeners.
159137
* @private
160138
*/
161139
function emitThemeChange(theme: Theme): void {

src/themes/index.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
/**
2-
* @fileoverview Theme system for Socket libraries.
3-
* Provides unified theming across spinners, logger, prompts, and links.
2+
* @fileoverview Elegant theming system for Socket libraries.
3+
* Unified visual language across spinners, loggers, prompts, and links.
44
*
55
* @example
66
* ```ts
7-
* import { setTheme, THEMES, Spinner } from '@socketsecurity/lib'
7+
* import { setTheme, THEMES } from '@socketsecurity/lib/themes'
88
*
99
* // Set global theme
1010
* setTheme('socket-firewall')
11-
*
12-
* // Create themed spinner
13-
* const spinner = Spinner({ text: 'Loading...' })
14-
* spinner.start() // Uses firewall theme (orange)
1511
* ```
1612
*
1713
* @example
1814
* ```ts
19-
* import { withTheme, createTheme } from '@socketsecurity/lib/themes'
15+
* import { withTheme } from '@socketsecurity/lib/themes'
2016
*
21-
* // Scoped theme usage
17+
* // Scoped theme context
2218
* await withTheme('ultra', async () => {
23-
* // All operations use ultra theme (rainbow)
19+
* // All operations inherit Ultra theme
2420
* })
21+
* ```
22+
*
23+
* @example
24+
* ```ts
25+
* import { createTheme } from '@socketsecurity/lib/themes'
2526
*
26-
* // Custom theme
27+
* // Custom theme creation
2728
* const myTheme = createTheme({
28-
* name: 'my-theme',
29-
* displayName: 'My Theme',
29+
* name: 'custom',
30+
* displayName: 'Custom Theme',
3031
* colors: {
3132
* primary: [255, 100, 200],
32-
* success: 'green',
33-
* error: 'red',
34-
* warning: 'yellow',
35-
* info: 'blue',
36-
* step: 'cyan',
33+
* success: 'greenBright',
34+
* error: 'redBright',
35+
* warning: 'yellowBright',
36+
* info: 'blueBright',
37+
* step: 'cyanBright',
3738
* text: 'white',
3839
* textDim: 'gray',
39-
* link: 'cyan',
40+
* link: 'cyanBright',
4041
* prompt: 'primary'
4142
* }
4243
* })
43-
* setTheme(myTheme)
4444
* ```
4545
*/
4646

47-
// Re-export types
47+
// Type system
4848
export type {
4949
ColorReference,
5050
Theme,
@@ -53,7 +53,7 @@ export type {
5353
ThemeMeta,
5454
} from './types'
5555

56-
// Re-export theme definitions
56+
// Curated themes
5757
export {
5858
COANA_THEME,
5959
FIREWALL_THEME,
@@ -64,7 +64,7 @@ export {
6464
type ThemeName,
6565
} from './themes'
6666

67-
// Re-export context management
67+
// Context management
6868
export {
6969
getTheme,
7070
onThemeChange,
@@ -74,7 +74,7 @@ export {
7474
type ThemeChangeListener,
7575
} from './context'
7676

77-
// Re-export utilities
77+
// Composition utilities
7878
export {
7979
createTheme,
8080
extendTheme,

0 commit comments

Comments
 (0)