| | |
| | |
| | |
| | |
| | |
| |
|
| | import { AyuDark } from './ayu.js'; |
| | import { AyuLight } from './ayu-light.js'; |
| | import { AtomOneDark } from './atom-one-dark.js'; |
| | import { Dracula } from './dracula.js'; |
| | import { GitHubDark } from './github-dark.js'; |
| | import { GitHubLight } from './github-light.js'; |
| | import { GoogleCode } from './googlecode.js'; |
| | import { DefaultLight } from './default-light.js'; |
| | import { DefaultDark } from './default.js'; |
| | import { ShadesOfPurple } from './shades-of-purple.js'; |
| | import { XCode } from './xcode.js'; |
| | import * as fs from 'node:fs'; |
| | import * as path from 'node:path'; |
| | import * as os from 'node:os'; |
| | import type { Theme, ThemeType, CustomTheme } from './theme.js'; |
| | import { createCustomTheme, validateCustomTheme } from './theme.js'; |
| | import type { SemanticColors } from './semantic-tokens.js'; |
| | import { ANSI } from './ansi.js'; |
| | import { ANSILight } from './ansi-light.js'; |
| | import { NoColorTheme } from './no-color.js'; |
| | import process from 'node:process'; |
| |
|
| | export interface ThemeDisplay { |
| | name: string; |
| | type: ThemeType; |
| | isCustom?: boolean; |
| | } |
| |
|
| | export const DEFAULT_THEME: Theme = DefaultDark; |
| |
|
| | class ThemeManager { |
| | private readonly availableThemes: Theme[]; |
| | private activeTheme: Theme; |
| | private customThemes: Map<string, Theme> = new Map(); |
| |
|
| | constructor() { |
| | this.availableThemes = [ |
| | AyuDark, |
| | AyuLight, |
| | AtomOneDark, |
| | Dracula, |
| | DefaultLight, |
| | DefaultDark, |
| | GitHubDark, |
| | GitHubLight, |
| | GoogleCode, |
| | ShadesOfPurple, |
| | XCode, |
| | ANSI, |
| | ANSILight, |
| | ]; |
| | this.activeTheme = DEFAULT_THEME; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | loadCustomThemes(customThemesSettings?: Record<string, CustomTheme>): void { |
| | this.customThemes.clear(); |
| |
|
| | if (!customThemesSettings) { |
| | return; |
| | } |
| |
|
| | for (const [name, customThemeConfig] of Object.entries( |
| | customThemesSettings, |
| | )) { |
| | const validation = validateCustomTheme(customThemeConfig); |
| | if (validation.isValid) { |
| | if (validation.warning) { |
| | console.warn(`Theme "${name}": ${validation.warning}`); |
| | } |
| | const themeWithDefaults: CustomTheme = { |
| | ...DEFAULT_THEME.colors, |
| | ...customThemeConfig, |
| | name: customThemeConfig.name || name, |
| | type: 'custom', |
| | }; |
| |
|
| | try { |
| | const theme = createCustomTheme(themeWithDefaults); |
| | this.customThemes.set(name, theme); |
| | } catch (error) { |
| | console.warn(`Failed to load custom theme "${name}":`, error); |
| | } |
| | } else { |
| | console.warn(`Invalid custom theme "${name}": ${validation.error}`); |
| | } |
| | } |
| | |
| | if ( |
| | this.activeTheme && |
| | this.activeTheme.type === 'custom' && |
| | this.customThemes.has(this.activeTheme.name) |
| | ) { |
| | this.activeTheme = this.customThemes.get(this.activeTheme.name)!; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | setActiveTheme(themeName: string | undefined): boolean { |
| | const theme = this.findThemeByName(themeName); |
| | if (!theme) { |
| | return false; |
| | } |
| | this.activeTheme = theme; |
| | return true; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getActiveTheme(): Theme { |
| | if (process.env['NO_COLOR']) { |
| | return NoColorTheme; |
| | } |
| |
|
| | if (this.activeTheme) { |
| | const isBuiltIn = this.availableThemes.some( |
| | (t) => t.name === this.activeTheme.name, |
| | ); |
| | const isCustom = [...this.customThemes.values()].includes( |
| | this.activeTheme, |
| | ); |
| |
|
| | if (isBuiltIn || isCustom) { |
| | return this.activeTheme; |
| | } |
| | } |
| |
|
| | |
| | this.activeTheme = DEFAULT_THEME; |
| | return this.activeTheme; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getSemanticColors(): SemanticColors { |
| | return this.getActiveTheme().semanticColors; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getCustomThemeNames(): string[] { |
| | return Array.from(this.customThemes.keys()); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | isCustomTheme(themeName: string): boolean { |
| | return this.customThemes.has(themeName); |
| | } |
| |
|
| | |
| | |
| | |
| | getAvailableThemes(): ThemeDisplay[] { |
| | const builtInThemes = this.availableThemes.map((theme) => ({ |
| | name: theme.name, |
| | type: theme.type, |
| | isCustom: false, |
| | })); |
| |
|
| | const customThemes = Array.from(this.customThemes.values()).map( |
| | (theme) => ({ |
| | name: theme.name, |
| | type: theme.type, |
| | isCustom: true, |
| | }), |
| | ); |
| |
|
| | const allThemes = [...builtInThemes, ...customThemes]; |
| |
|
| | const sortedThemes = allThemes.sort((a, b) => { |
| | const typeOrder = (type: ThemeType): number => { |
| | switch (type) { |
| | case 'dark': |
| | return 1; |
| | case 'light': |
| | return 2; |
| | case 'ansi': |
| | return 3; |
| | case 'custom': |
| | return 4; |
| | default: |
| | return 5; |
| | } |
| | }; |
| |
|
| | const typeComparison = typeOrder(a.type) - typeOrder(b.type); |
| | if (typeComparison !== 0) { |
| | return typeComparison; |
| | } |
| | return a.name.localeCompare(b.name); |
| | }); |
| |
|
| | return sortedThemes; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | getTheme(themeName: string): Theme | undefined { |
| | return this.findThemeByName(themeName); |
| | } |
| |
|
| | private isPath(themeName: string): boolean { |
| | return ( |
| | themeName.endsWith('.json') || |
| | themeName.startsWith('.') || |
| | path.isAbsolute(themeName) |
| | ); |
| | } |
| |
|
| | private loadThemeFromFile(themePath: string): Theme | undefined { |
| | try { |
| | |
| | const canonicalPath = fs.realpathSync(path.resolve(themePath)); |
| |
|
| | |
| | if (this.customThemes.has(canonicalPath)) { |
| | return this.customThemes.get(canonicalPath); |
| | } |
| |
|
| | |
| | const homeDir = path.resolve(os.homedir()); |
| | if (!canonicalPath.startsWith(homeDir)) { |
| | console.warn( |
| | `Theme file at "${themePath}" is outside your home directory. ` + |
| | `Only load themes from trusted sources.`, |
| | ); |
| | return undefined; |
| | } |
| |
|
| | |
| | const themeContent = fs.readFileSync(canonicalPath, 'utf-8'); |
| | const customThemeConfig = JSON.parse(themeContent) as CustomTheme; |
| |
|
| | const validation = validateCustomTheme(customThemeConfig); |
| | if (!validation.isValid) { |
| | console.warn( |
| | `Invalid custom theme from file "${themePath}": ${validation.error}`, |
| | ); |
| | return undefined; |
| | } |
| |
|
| | if (validation.warning) { |
| | console.warn(`Theme from "${themePath}": ${validation.warning}`); |
| | } |
| |
|
| | |
| | const themeWithDefaults: CustomTheme = { |
| | ...DEFAULT_THEME.colors, |
| | ...customThemeConfig, |
| | name: customThemeConfig.name || canonicalPath, |
| | type: 'custom', |
| | }; |
| |
|
| | const theme = createCustomTheme(themeWithDefaults); |
| | this.customThemes.set(canonicalPath, theme); |
| | return theme; |
| | } catch (error) { |
| | |
| | |
| | if ( |
| | !(error instanceof Error && 'code' in error && error.code === 'ENOENT') |
| | ) { |
| | console.warn(`Could not load theme from file "${themePath}":`, error); |
| | } |
| | return undefined; |
| | } |
| | } |
| |
|
| | findThemeByName(themeName: string | undefined): Theme | undefined { |
| | if (!themeName) { |
| | return DEFAULT_THEME; |
| | } |
| |
|
| | |
| | const builtInTheme = this.availableThemes.find( |
| | (theme) => theme.name === themeName, |
| | ); |
| | if (builtInTheme) { |
| | return builtInTheme; |
| | } |
| |
|
| | |
| | if (this.isPath(themeName)) { |
| | return this.loadThemeFromFile(themeName); |
| | } |
| |
|
| | if (this.customThemes.has(themeName)) { |
| | return this.customThemes.get(themeName); |
| | } |
| |
|
| | |
| | |
| | return undefined; |
| | } |
| | } |
| |
|
| | |
| | export const themeManager = new ThemeManager(); |
| |
|