summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/themes/theme-manager.ts
blob: 4a8cc32c99180b643390bf0ed460e0f9220b510c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { AtomOneDark } from './atom-one-dark.js';
import { Dracula } from './dracula.js';
import { GitHub } from './github.js';
import { GoogleCode } from './googlecode.js';
import { VS } from './vs.js';
import { VS2015 } from './vs2015.js';
import { XCode } from './xcode.js';
import { Theme } from './theme.js';
import { ANSI } from './ansi.js';

export interface ThemeDisplay {
  name: string;
  active: boolean;
}

export const DEFAULT_THEME: Theme = VS2015;

class ThemeManager {
  private readonly availableThemes: Theme[];
  private activeTheme: Theme;

  constructor() {
    this.availableThemes = [
      AtomOneDark,
      Dracula,
      VS, // Light mode.
      VS2015,
      GitHub,
      GoogleCode,
      XCode,
      ANSI,
    ];
    this.activeTheme = DEFAULT_THEME;
  }

  /**
   * Returns a list of available theme names.
   */
  getAvailableThemes(): ThemeDisplay[] {
    return this.availableThemes.map((theme) => ({
      name: theme.name,
      active: theme === this.activeTheme,
    }));
  }

  /**
   * Sets the active theme.
   * @param themeName The name of the theme to activate.
   */
  setActiveTheme(themeName: string | undefined): void {
    const foundTheme = this.findThemeByName(themeName);

    if (foundTheme) {
      this.activeTheme = foundTheme;
    } else {
      throw new Error(`Theme "${themeName}" not found.`);
    }
  }

  findThemeByName(themeName: string | undefined): Theme | undefined {
    if (!themeName) {
      return DEFAULT_THEME;
    }
    return this.availableThemes.find((theme) => theme.name === themeName);
  }

  /**
   * Returns the currently active theme object.
   */
  getActiveTheme(): Theme {
    return this.activeTheme;
  }
}

// Export an instance of the ThemeManager
export const themeManager = new ThemeManager();