Repository navigation
feat(extensions): add support for custom themes in extensions #17327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
dd57de4
e1ea92e
92bad52
6a09428
c9ec1c4
5f204d9
7f3ad14
33dd1e1
686599f
a44d084
d1ab25e
e660329
b331faf
127e02f
3d4eafe
0e7a796
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Themes Example | ||
|
|
||
| This is an example of a Gemini CLI extension that adds a custom theme. | ||
|
|
||
| ## How to use | ||
|
|
||
| 1. Link this extension: | ||
|
|
||
| ```bash | ||
| gemini extensions link packages/cli/src/commands/extensions/examples/themes-example | ||
| ``` | ||
|
|
||
| 2. Set the theme in your settings file (`~/.gemini/settings.json`): | ||
|
|
||
| ```json | ||
| { | ||
| "ui": { | ||
| "theme": "themes-example: My-Awesome-Theme" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Alternatively, you can set it through the UI by running `gemini` and then | ||
| pressing `t`. | ||
|
|
||
| 3. **Observe the Changes:** | ||
|
|
||
| After setting the theme, you should see the changes reflected in the Gemini | ||
| CLI's UI. The primary text color should now be magenta (`#FF00FF`), as | ||
| defined in the `themes-example`'s `gemini-extension.json` file. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "name": "themes-example", | ||
| "version": "1.0.0", | ||
| "themes": [ | ||
| { | ||
| "name": "My-Awesome-Theme", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make this theme reasonable. For example, do a shades of green theme where everything is green. Will be clearer than this one where all that changes is the primary color is different which is odd but not really recognizable.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, I updated the example. |
||
| "type": "custom", | ||
| "text": { | ||
| "primary": "#FF00FF" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as fs from 'node:fs'; | ||
| import * as path from 'node:path'; | ||
| import { | ||
| beforeAll, | ||
| afterAll, | ||
| beforeEach, | ||
| describe, | ||
| expect, | ||
| it, | ||
| vi, | ||
| afterEach, | ||
| } from 'vitest'; | ||
| import { createExtension } from '../test-utils/createExtension.js'; | ||
| import { ExtensionManager } from './extension-manager.js'; | ||
| import { themeManager, DEFAULT_THEME } from '../ui/themes/theme-manager.js'; | ||
| import { GEMINI_DIR, type Config } from '@google/gemini-cli-core'; | ||
| import { createTestMergedSettings, SettingScope } from './settings.js'; | ||
|
|
||
| describe('ExtensionManager theme loading', () => { | ||
| let extensionManager: ExtensionManager; | ||
| let userExtensionsDir: string; | ||
| let tempHomeDir: string; | ||
|
|
||
| beforeAll(async () => { | ||
| tempHomeDir = await fs.promises.mkdtemp( | ||
| path.join(fs.realpathSync('/tmp'), 'gemini-cli-test-'), | ||
| ); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| if (tempHomeDir) { | ||
| await fs.promises.rm(tempHomeDir, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| process.env['GEMINI_CLI_HOME'] = tempHomeDir; | ||
| userExtensionsDir = path.join(tempHomeDir, GEMINI_DIR, 'extensions'); | ||
| // Ensure userExtensionsDir is clean for each test | ||
| fs.rmSync(userExtensionsDir, { recursive: true, force: true }); | ||
| fs.mkdirSync(userExtensionsDir, { recursive: true }); | ||
|
|
||
| extensionManager = new ExtensionManager({ | ||
| settings: createTestMergedSettings({ | ||
| experimental: { extensionConfig: true }, | ||
| security: { blockGitExtensions: false }, | ||
| admin: { extensions: { enabled: true }, mcp: { enabled: true } }, | ||
| tools: { enableHooks: true }, | ||
| }), | ||
| requestConsent: async () => true, | ||
| requestSetting: async () => '', | ||
| workspaceDir: tempHomeDir, | ||
| enabledExtensionOverrides: [], | ||
| }); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete process.env['GEMINI_CLI_HOME']; | ||
| }); | ||
|
|
||
| it('should register themes from an extension when started', async () => { | ||
| const registerSpy = vi.spyOn(themeManager, 'registerExtensionThemes'); | ||
| createExtension({ | ||
| extensionsDir: userExtensionsDir, | ||
| name: 'my-theme-extension', | ||
| themes: [ | ||
| { | ||
| name: 'My-Awesome-Theme', | ||
| type: 'custom', | ||
| text: { | ||
| primary: '#FF00FF', | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| await extensionManager.loadExtensions(); | ||
|
|
||
| const mockConfig = { | ||
| getEnableExtensionReloading: () => false, | ||
| getMcpClientManager: () => ({ | ||
| startExtension: vi.fn().mockResolvedValue(undefined), | ||
| }), | ||
| getGeminiClient: () => ({ | ||
| isInitialized: () => false, | ||
| updateSystemInstruction: vi.fn(), | ||
| setTools: vi.fn(), | ||
| }), | ||
| getHookSystem: () => undefined, | ||
| getWorkingDir: () => tempHomeDir, | ||
| shouldLoadMemoryFromIncludeDirectories: () => false, | ||
| getDebugMode: () => false, | ||
| getFileExclusions: () => ({ | ||
| isIgnored: () => false, | ||
| }), | ||
| getGeminiMdFilePaths: () => [], | ||
| getMcpServers: () => ({}), | ||
| getAllowedMcpServers: () => [], | ||
| getSanitizationConfig: () => ({ | ||
| allowedEnvironmentVariables: [], | ||
| blockedEnvironmentVariables: [], | ||
| enableEnvironmentVariableRedaction: false, | ||
| }), | ||
| getShellExecutionConfig: () => ({ | ||
| terminalWidth: 80, | ||
| terminalHeight: 24, | ||
| showColor: false, | ||
| pager: 'cat', | ||
| sanitizationConfig: { | ||
| allowedEnvironmentVariables: [], | ||
| blockedEnvironmentVariables: [], | ||
| enableEnvironmentVariableRedaction: false, | ||
| }, | ||
| }), | ||
| getToolRegistry: () => ({ | ||
| getTools: () => [], | ||
| }), | ||
| getProxy: () => undefined, | ||
| getFileService: () => ({ | ||
| findFiles: async () => [], | ||
| }), | ||
| getExtensionLoader: () => ({ | ||
| getExtensions: () => [], | ||
| }), | ||
| isTrustedFolder: () => true, | ||
| getImportFormat: () => 'tree', | ||
| } as unknown as Config; | ||
|
|
||
| await extensionManager.start(mockConfig); | ||
|
|
||
| expect(registerSpy).toHaveBeenCalledWith('my-theme-extension', [ | ||
| { | ||
| name: 'My-Awesome-Theme', | ||
| type: 'custom', | ||
| text: { | ||
| primary: '#FF00FF', | ||
| }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should revert to default theme when extension is stopped', async () => { | ||
| const extensionName = 'my-theme-extension'; | ||
| const themeName = 'My-Awesome-Theme'; | ||
| const namespacedThemeName = `${extensionName}: ${themeName}`; | ||
|
|
||
| createExtension({ | ||
| extensionsDir: userExtensionsDir, | ||
| name: extensionName, | ||
| themes: [ | ||
| { | ||
| name: themeName, | ||
| type: 'custom', | ||
| text: { | ||
| primary: '#FF00FF', | ||
| }, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| await extensionManager.loadExtensions(); | ||
|
|
||
| const mockConfig = { | ||
| getWorkingDir: () => tempHomeDir, | ||
| shouldLoadMemoryFromIncludeDirectories: () => false, | ||
| getWorkspaceContext: () => ({ | ||
| getDirectories: () => [], | ||
| }), | ||
| getDebugMode: () => false, | ||
| getFileService: () => ({ | ||
| findFiles: async () => [], | ||
| }), | ||
| getExtensionLoader: () => ({ | ||
| getExtensions: () => [], | ||
| }), | ||
| isTrustedFolder: () => true, | ||
| getImportFormat: () => 'tree', | ||
| getFileFilteringOptions: () => ({ | ||
| respectGitIgnore: true, | ||
| respectGeminiIgnore: true, | ||
| }), | ||
| getDiscoveryMaxDirs: () => 200, | ||
| getMcpClientManager: () => ({ | ||
| getMcpInstructions: () => '', | ||
| startExtension: vi.fn().mockResolvedValue(undefined), | ||
| stopExtension: vi.fn().mockResolvedValue(undefined), | ||
| }), | ||
| setUserMemory: vi.fn(), | ||
| setGeminiMdFileCount: vi.fn(), | ||
| setGeminiMdFilePaths: vi.fn(), | ||
| getEnableExtensionReloading: () => true, | ||
| getGeminiClient: () => ({ | ||
| isInitialized: () => false, | ||
| updateSystemInstruction: vi.fn(), | ||
| setTools: vi.fn(), | ||
| }), | ||
| getHookSystem: () => undefined, | ||
| getProxy: () => undefined, | ||
| getAgentRegistry: () => ({ | ||
| reload: vi.fn().mockResolvedValue(undefined), | ||
| }), | ||
| } as unknown as Config; | ||
|
|
||
| await extensionManager.start(mockConfig); | ||
|
|
||
| // Set the active theme to the one from the extension | ||
| themeManager.setActiveTheme(namespacedThemeName); | ||
| expect(themeManager.getActiveTheme().name).toBe(namespacedThemeName); | ||
|
|
||
| // Stop the extension | ||
| await extensionManager.disableExtension(extensionName, SettingScope.User); | ||
|
|
||
| // Check that the active theme has reverted to the default | ||
| expect(themeManager.getActiveTheme().name).toBe(DEFAULT_THEME.name); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,7 @@ import { | |
| ExtensionSettingScope, | ||
| } from './extensions/extensionSettings.js'; | ||
| import type { EventEmitter } from 'node:stream'; | ||
| import { themeManager } from '../ui/themes/theme-manager.js'; | ||
|
|
||
| interface ExtensionManagerParams { | ||
| enabledExtensionOverrides?: string[]; | ||
|
|
@@ -467,6 +468,20 @@ Would you like to attempt to install via "git clone" instead?`, | |
| ); | ||
| } | ||
|
|
||
| protected override async startExtension(extension: GeminiCLIExtension) { | ||
| await super.startExtension(extension); | ||
| if (extension.themes) { | ||
| themeManager.registerExtensionThemes(extension.name, extension.themes); | ||
| } | ||
| } | ||
|
|
||
| protected override async stopExtension(extension: GeminiCLIExtension) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a test verifying that stopping an extension with a theme causes the theme to change back to the default theme if that custom theme was in use.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a "Should revert to default theme when extension is stopped" spec in |
||
| await super.stopExtension(extension); | ||
| if (extension.themes) { | ||
| themeManager.unregisterExtensionThemes(extension.name, extension.themes); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Loads all installed extensions, should only be called once. | ||
| */ | ||
|
|
@@ -656,6 +671,7 @@ Would you like to attempt to install via "git clone" instead?`, | |
| resolvedSettings, | ||
| skills, | ||
| agents: agentLoadResult.agents, | ||
| themes: config.themes, | ||
| }; | ||
| this.loadedExtensions = [...this.loadedExtensions, extension]; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not correct. pressing t does not change the theme. Please update to the correct shortcut.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.