src — themes

Module: src-themes Cohesion: 0.80 Members: 0

src — themes

The src/themes module provides a robust and extensible system for managing the visual appearance of the Code Buddy application. It encompasses definitions for themes and avatars, a collection of built-in options, validation schemas, and a central manager for loading, saving, and applying user preferences.

Purpose

The primary goal of this module is to allow users to customize the application's look and feel through:

  1. Color Themes: Defining a comprehensive set of colors for various UI elements, messages, and syntax highlighting.
  2. Avatars: Customizing the visual identifiers for different message sources (user, assistant, tool, system).
  3. Persistence: Ensuring that user-selected themes and custom preferences are saved and reloaded across sessions.
  4. Extensibility: Enabling the creation, import, and management of custom themes.

Core Concepts

The module revolves around several key data structures:

Module Structure

The src/themes module is organized into several files, each with a distinct responsibility:

graph TD
    A[src/themes/index.ts] --> B[src/themes/theme.ts]
    A --> C[src/themes/default-themes.ts]
    A --> D[src/themes/theme-manager.ts]
    D --> B
    D --> C
    D --> E[src/themes/theme-schema.ts]
    D --> F[src/utils/logger.ts]
    D --> G[Node.js fs, path, os modules]

Key Components

theme.ts: Data Structures

This file is the foundation, defining the contracts for all theme-related data. It ensures consistency across built-in and custom themes. The ThemeColor type is flexible, supporting both standard terminal colors and hex codes, allowing for rich customization. The various AvatarConfig presets offer quick visual changes for message roles.

default-themes.ts: Built-in Themes

This file is a large collection of predefined Theme objects. Each theme is a constant export (e.g., DEFAULT_THEME, DRACULA_THEME) and includes a unique id, name, description, a full colors palette, and a default avatars configuration. These themes are marked with isBuiltin: true to prevent accidental deletion by users. The BUILTIN_THEMES array aggregates all these themes for easy iteration and lookup.

theme-schema.ts: Data Validation

Leveraging the Zod library, this file defines schemas to validate the structure and content of Theme and ThemePreferences objects. This is critical for:

For example, themeColorSchema ensures colors are either valid Ink names or hex codes, and avatarConfigSchema checks that avatar strings are within a reasonable length.

ThemeManager: The Core Logic

The ThemeManager class is a singleton (getInstance()) that orchestrates the entire theme system.

Initialization (initializeThemes):

  1. Loads all BUILTIN_THEMES into its internal themes map.
  2. Calls loadCustomThemes() to discover and load any user-defined themes from ~/.codebuddy/themes/.
  3. Calls loadPreferences() to load the user's last active theme, custom avatars, and custom colors from ~/.codebuddy/theme-preferences.json.

Persistence:

Theme and Avatar Management:

Custom Theme Operations:

Error Handling: The ThemeManager uses src/utils/logger.ts to log warnings for issues like invalid JSON in theme files or preferences, or failures during file system operations, ensuring the application remains stable even with corrupted user data.

How it Works

  1. Initialization: When the application starts, getThemeManager() is called, which instantiates the ThemeManager singleton. The manager immediately loads all built-in themes, then scans ~/.codebuddy/themes/ for custom JSON theme files, validating them with Zod schemas. Finally, it loads ~/.codebuddy/theme-preferences.json to determine the user's last active theme and any custom avatar/color overrides.
  2. Active Theme Resolution: The currentTheme property holds the active theme. getColors() and getAvatars() dynamically merge the currentTheme's properties with any customColors or customAvatars stored in preferences, providing the final, effective styling.
  3. Persistence: Any changes made via setTheme(), setCustomAvatars(), setCustomColors(), applyAvatarPreset(), createCustomTheme(), deleteCustomTheme(), or importTheme() trigger a save operation to the respective JSON files in the user's ~/.codebuddy/ directory. This ensures that customizations persist across application restarts.

Connections to the Codebase

The src/themes module is a foundational service, primarily consumed by UI components and command handlers:

The module's reliance on Node.js fs, path, and os modules for file system interactions, and src/utils/logger.ts for internal logging, highlights its role as a backend service for managing user-specific configuration.