File size: 20,209 Bytes
52efc7b | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { CSSProperties } from 'react';
import type { SemanticColors } from './semantic-tokens.js';
import type { CustomTheme } from '@google/gemini-cli-core';
import {
DEFAULT_INPUT_BACKGROUND_OPACITY,
DEFAULT_SELECTION_OPACITY,
DEFAULT_BORDER_OPACITY,
} from '../constants.js';
import tinygradient from 'tinygradient';
import tinycolor from 'tinycolor2';
// Define the set of Ink's named colors for quick lookup
export const INK_SUPPORTED_NAMES = new Set([
'black',
'red',
'green',
'yellow',
'blue',
'cyan',
'magenta',
'white',
'gray',
'grey',
'blackbright',
'redbright',
'greenbright',
'yellowbright',
'bluebright',
'cyanbright',
'magentabright',
'whitebright',
]);
// Use tinycolor's built-in names map for CSS colors, excluding ones Ink supports
export const CSS_NAME_TO_HEX_MAP = Object.fromEntries(
Object.entries(tinycolor.names)
.filter(([name]) => !INK_SUPPORTED_NAMES.has(name))
.map(([name, hex]) => [name, `#${hex}`]),
);
// Mapping for ANSI bright colors that are not in tinycolor's standard CSS names
export const INK_NAME_TO_HEX_MAP: Readonly<Record<string, string>> = {
blackbright: '#555555',
redbright: '#ff5555',
greenbright: '#55ff55',
yellowbright: '#ffff55',
bluebright: '#5555ff',
magentabright: '#ff55ff',
cyanbright: '#55ffff',
whitebright: '#ffffff',
};
/**
* Calculates the relative luminance of a color.
* See https://www.w3.org/TR/WCAG20/#relativeluminancedef
*
* @param color Color string (hex or Ink-supported name)
* @returns Luminance value (0-255)
*/
export function getLuminance(color: string): number {
const resolved = color.toLowerCase();
const hex = INK_NAME_TO_HEX_MAP[resolved] || resolved;
const colorObj = tinycolor(hex);
if (!colorObj.isValid()) {
return 0;
}
// tinycolor returns 0-1, we need 0-255
return colorObj.getLuminance() * 255;
}
/**
* Resolves a CSS color value (name or hex) into an Ink-compatible color string.
* @param colorValue The raw color string (e.g., 'blue', '#ff0000', 'darkkhaki').
* @returns An Ink-compatible color string (hex or name), or undefined if not resolvable.
*/
export function resolveColor(colorValue: string): string | undefined {
const lowerColor = colorValue.toLowerCase();
// 1. Check if it's already a hex code and valid
if (lowerColor.startsWith('#')) {
if (/^#[0-9A-Fa-f]{3}([0-9A-Fa-f]{3})?$/.test(colorValue)) {
return lowerColor;
} else {
return undefined;
}
}
// Handle hex codes without #
if (/^[0-9A-Fa-f]{3}([0-9A-Fa-f]{3})?$/.test(colorValue)) {
return `#${lowerColor}`;
}
// 2. Check if it's an Ink supported name (lowercase)
if (INK_SUPPORTED_NAMES.has(lowerColor)) {
return lowerColor; // Use Ink name directly
}
// 3. Check if it's a known CSS name we can map to hex
// We can't import CSS_NAME_TO_HEX_MAP here due to circular deps,
// but we can use tinycolor directly for named colors.
const colorObj = tinycolor(lowerColor);
if (colorObj.isValid()) {
return colorObj.toHexString();
}
// 4. Could not resolve
return undefined;
}
export function interpolateColor(
color1: string,
color2: string,
factor: number,
) {
if (factor <= 0 && color1) {
return color1;
}
if (factor >= 1 && color2) {
return color2;
}
if (!color1 || !color2) {
return '';
}
try {
const gradient = tinygradient(color1, color2);
const color = gradient.rgbAt(factor);
return color.toHexString();
} catch {
return color1;
}
}
export function getThemeTypeFromBackgroundColor(
backgroundColor: string | undefined,
): 'light' | 'dark' | undefined {
if (!backgroundColor) {
return undefined;
}
const resolvedColor = resolveColor(backgroundColor);
if (!resolvedColor) {
return undefined;
}
const luminance = getLuminance(resolvedColor);
return luminance > 128 ? 'light' : 'dark';
}
export type { CustomTheme };
export type ThemeType = 'light' | 'dark' | 'ansi' | 'custom';
export interface ColorsTheme {
type: ThemeType;
Background: string;
Foreground: string;
LightBlue: string;
AccentBlue: string;
AccentPurple: string;
AccentCyan: string;
AccentGreen: string;
AccentYellow: string;
AccentRed: string;
DiffAdded: string;
DiffRemoved: string;
Comment: string;
Gray: string;
DarkGray: string;
InputBackground?: string;
MessageBackground?: string;
FocusBackground?: string;
FocusColor?: string;
GradientColors?: string[];
}
export const lightTheme: ColorsTheme = {
type: 'light',
Background: '#FFFFFF',
Foreground: '#000000',
LightBlue: '#005FAF',
AccentBlue: '#005FAF',
AccentPurple: '#5F00FF',
AccentCyan: '#005F87',
AccentGreen: '#005F00',
AccentYellow: '#875F00',
AccentRed: '#AF0000',
DiffAdded: '#D7FFD7',
DiffRemoved: '#FFD7D7',
Comment: '#008700',
Gray: '#5F5F5F',
DarkGray: '#5F5F5F',
InputBackground: '#E4E4E4',
MessageBackground: '#FAFAFA',
FocusBackground: '#D7FFD7',
GradientColors: ['#4796E4', '#847ACE', '#C3677F'],
};
export const darkTheme: ColorsTheme = {
type: 'dark',
Background: '#000000',
Foreground: '#FFFFFF',
LightBlue: '#AFD7D7',
AccentBlue: '#87AFFF',
AccentPurple: '#D7AFFF',
AccentCyan: '#87D7D7',
AccentGreen: '#D7FFD7',
AccentYellow: '#FFFFAF',
AccentRed: '#FF87AF',
DiffAdded: '#005F00',
DiffRemoved: '#5F0000',
Comment: '#AFAFAF',
Gray: '#AFAFAF',
DarkGray: '#878787',
InputBackground: '#5F5F5F',
MessageBackground: '#5F5F5F',
FocusBackground: '#005F00',
GradientColors: ['#4796E4', '#847ACE', '#C3677F'],
};
export const ansiTheme: ColorsTheme = {
type: 'ansi',
Background: 'black',
Foreground: '',
LightBlue: 'blue',
AccentBlue: 'blue',
AccentPurple: 'magenta',
AccentCyan: 'cyan',
AccentGreen: 'green',
AccentYellow: 'yellow',
AccentRed: 'red',
DiffAdded: 'green',
DiffRemoved: 'red',
Comment: 'gray',
Gray: 'gray',
DarkGray: 'gray',
InputBackground: 'black',
MessageBackground: 'black',
FocusBackground: 'black',
};
export class Theme {
/**
* The default foreground color for text when no specific highlight rule applies.
* This is an Ink-compatible color string (hex or name).
*/
readonly defaultColor: string;
/**
* Stores the mapping from highlight.js class names (e.g., 'hljs-keyword')
* to Ink-compatible color strings (hex or name).
*/
protected readonly _colorMap: Readonly<Record<string, string>>;
readonly semanticColors: SemanticColors;
/**
* Creates a new Theme instance.
* @param name The name of the theme.
* @param rawMappings The raw CSSProperties mappings from a react-syntax-highlighter theme object.
*/
constructor(
readonly name: string,
readonly type: ThemeType,
rawMappings: Record<string, CSSProperties>,
readonly colors: ColorsTheme,
semanticColors?: SemanticColors,
) {
this.semanticColors = semanticColors ?? {
text: {
primary: this.colors.Foreground,
secondary: this.colors.Gray,
link: this.colors.AccentBlue,
accent: this.colors.AccentPurple,
response: this.colors.Foreground,
},
background: {
primary: this.colors.Background,
message:
this.colors.MessageBackground ??
interpolateColor(
this.colors.Background,
this.colors.Gray,
DEFAULT_INPUT_BACKGROUND_OPACITY,
),
input:
this.colors.InputBackground ??
interpolateColor(
this.colors.Background,
this.colors.Gray,
DEFAULT_INPUT_BACKGROUND_OPACITY,
),
focus:
this.colors.FocusBackground ??
interpolateColor(
this.colors.Background,
this.colors.FocusColor ?? this.colors.AccentGreen,
DEFAULT_SELECTION_OPACITY,
),
diff: {
added: this.colors.DiffAdded,
removed: this.colors.DiffRemoved,
},
},
border: {
default: this.colors.DarkGray,
},
ui: {
comment: this.colors.Gray,
symbol: this.colors.AccentCyan,
active: this.colors.AccentBlue,
dark: this.colors.DarkGray,
focus: this.colors.FocusColor ?? this.colors.AccentGreen,
gradient: this.colors.GradientColors,
},
status: {
error: this.colors.AccentRed,
success: this.colors.AccentGreen,
warning: this.colors.AccentYellow,
},
};
this._colorMap = Object.freeze(this._buildColorMap(rawMappings)); // Build and freeze the map
// Determine the default foreground color
const rawDefaultColor = rawMappings['hljs']?.color;
this.defaultColor =
(rawDefaultColor ? Theme._resolveColor(rawDefaultColor) : undefined) ??
''; // Default to empty string if not found or resolvable
}
/**
* Gets the Ink-compatible color string for a given highlight.js class name.
* @param hljsClass The highlight.js class name (e.g., 'hljs-keyword', 'hljs-string').
* @returns The corresponding Ink color string (hex or name) if it exists.
*/
getInkColor(hljsClass: string): string | undefined {
return this._colorMap[hljsClass];
}
/**
* Resolves a CSS color value (name or hex) into an Ink-compatible color string.
* @param colorValue The raw color string (e.g., 'blue', '#ff0000', 'darkkhaki').
* @returns An Ink-compatible color string (hex or name), or undefined if not resolvable.
*/
private static _resolveColor(colorValue: string): string | undefined {
return resolveColor(colorValue);
}
/**
* Builds the internal map from highlight.js class names to Ink-compatible color strings.
* This method is protected and primarily intended for use by the constructor.
* @param hljsTheme The raw CSSProperties mappings from a react-syntax-highlighter theme object.
* @returns An Ink-compatible theme map (Record<string, string>).
*/
protected _buildColorMap(
hljsTheme: Record<string, CSSProperties>,
): Record<string, string> {
const inkTheme: Record<string, string> = {};
for (const key in hljsTheme) {
// Ensure the key starts with 'hljs-' or is 'hljs' for the base style
if (!key.startsWith('hljs-') && key !== 'hljs') {
continue; // Skip keys not related to highlighting classes
}
const style = hljsTheme[key];
if (style?.color) {
const resolvedColor = Theme._resolveColor(style.color);
if (resolvedColor !== undefined) {
// Use the original key from the hljsTheme (e.g., 'hljs-keyword')
inkTheme[key] = resolvedColor;
}
// If color is not resolvable, it's omitted from the map,
// this enables falling back to the default foreground color.
}
// We currently only care about the 'color' property for Ink rendering.
// Other properties like background, fontStyle, etc., are ignored.
}
return inkTheme;
}
}
/**
* Creates a Theme instance from a custom theme configuration.
* @param customTheme The custom theme configuration.
* @returns A new Theme instance.
*/
export function createCustomTheme(customTheme: CustomTheme): Theme {
const colors: ColorsTheme = {
type: 'custom',
Background: customTheme.background?.primary ?? customTheme.Background ?? '',
Foreground: customTheme.text?.primary ?? customTheme.Foreground ?? '',
LightBlue: customTheme.text?.link ?? customTheme.LightBlue ?? '',
AccentBlue: customTheme.text?.link ?? customTheme.AccentBlue ?? '',
AccentPurple: customTheme.text?.accent ?? customTheme.AccentPurple ?? '',
AccentCyan: customTheme.text?.link ?? customTheme.AccentCyan ?? '',
AccentGreen: customTheme.status?.success ?? customTheme.AccentGreen ?? '',
AccentYellow: customTheme.status?.warning ?? customTheme.AccentYellow ?? '',
AccentRed: customTheme.status?.error ?? customTheme.AccentRed ?? '',
DiffAdded:
customTheme.background?.diff?.added ?? customTheme.DiffAdded ?? '',
DiffRemoved:
customTheme.background?.diff?.removed ?? customTheme.DiffRemoved ?? '',
Comment: customTheme.ui?.comment ?? customTheme.Comment ?? '',
Gray: customTheme.text?.secondary ?? customTheme.Gray ?? '',
DarkGray:
customTheme.DarkGray ??
interpolateColor(
customTheme.background?.primary ?? customTheme.Background ?? '',
customTheme.text?.secondary ?? customTheme.Gray ?? '',
DEFAULT_BORDER_OPACITY,
),
InputBackground: interpolateColor(
customTheme.background?.primary ?? customTheme.Background ?? '',
customTheme.text?.secondary ?? customTheme.Gray ?? '',
DEFAULT_INPUT_BACKGROUND_OPACITY,
),
MessageBackground: interpolateColor(
customTheme.background?.primary ?? customTheme.Background ?? '',
customTheme.text?.secondary ?? customTheme.Gray ?? '',
DEFAULT_INPUT_BACKGROUND_OPACITY,
),
FocusBackground: interpolateColor(
customTheme.background?.primary ?? customTheme.Background ?? '',
customTheme.status?.success ?? customTheme.AccentGreen ?? '#3CA84B', // Fallback to a default green if not found
DEFAULT_SELECTION_OPACITY,
),
FocusColor: customTheme.ui?.focus ?? customTheme.AccentGreen,
GradientColors: customTheme.ui?.gradient ?? customTheme.GradientColors,
};
// Generate CSS properties mappings based on the custom theme colors
const rawMappings: Record<string, CSSProperties> = {
hljs: {
display: 'block',
overflowX: 'auto',
padding: '0.5em',
background: colors.Background,
color: colors.Foreground,
},
'hljs-keyword': {
color: colors.AccentBlue,
},
'hljs-literal': {
color: colors.AccentBlue,
},
'hljs-symbol': {
color: colors.AccentBlue,
},
'hljs-name': {
color: colors.AccentBlue,
},
'hljs-link': {
color: colors.AccentBlue,
textDecoration: 'underline',
},
'hljs-built_in': {
color: colors.AccentCyan,
},
'hljs-type': {
color: colors.AccentCyan,
},
'hljs-number': {
color: colors.AccentGreen,
},
'hljs-class': {
color: colors.AccentGreen,
},
'hljs-string': {
color: colors.AccentYellow,
},
'hljs-meta-string': {
color: colors.AccentYellow,
},
'hljs-regexp': {
color: colors.AccentRed,
},
'hljs-template-tag': {
color: colors.AccentRed,
},
'hljs-subst': {
color: colors.Foreground,
},
'hljs-function': {
color: colors.Foreground,
},
'hljs-title': {
color: colors.Foreground,
},
'hljs-params': {
color: colors.Foreground,
},
'hljs-formula': {
color: colors.Foreground,
},
'hljs-comment': {
color: colors.Comment,
fontStyle: 'italic',
},
'hljs-quote': {
color: colors.Comment,
fontStyle: 'italic',
},
'hljs-doctag': {
color: colors.Comment,
},
'hljs-meta': {
color: colors.Gray,
},
'hljs-meta-keyword': {
color: colors.Gray,
},
'hljs-tag': {
color: colors.Gray,
},
'hljs-variable': {
color: colors.AccentPurple,
},
'hljs-template-variable': {
color: colors.AccentPurple,
},
'hljs-attr': {
color: colors.LightBlue,
},
'hljs-attribute': {
color: colors.LightBlue,
},
'hljs-builtin-name': {
color: colors.LightBlue,
},
'hljs-section': {
color: colors.AccentYellow,
},
'hljs-emphasis': {
fontStyle: 'italic',
},
'hljs-strong': {
fontWeight: 'bold',
},
'hljs-bullet': {
color: colors.AccentYellow,
},
'hljs-selector-tag': {
color: colors.AccentYellow,
},
'hljs-selector-id': {
color: colors.AccentYellow,
},
'hljs-selector-class': {
color: colors.AccentYellow,
},
'hljs-selector-attr': {
color: colors.AccentYellow,
},
'hljs-selector-pseudo': {
color: colors.AccentYellow,
},
'hljs-addition': {
backgroundColor: colors.AccentGreen,
display: 'inline-block',
width: '100%',
},
'hljs-deletion': {
backgroundColor: colors.AccentRed,
display: 'inline-block',
width: '100%',
},
};
const semanticColors: SemanticColors = {
text: {
primary: customTheme.text?.primary ?? colors.Foreground,
secondary: customTheme.text?.secondary ?? colors.Gray,
link: customTheme.text?.link ?? colors.AccentBlue,
accent: customTheme.text?.accent ?? colors.AccentPurple,
response:
customTheme.text?.response ??
customTheme.text?.primary ??
colors.Foreground,
},
background: {
primary: customTheme.background?.primary ?? colors.Background,
message: colors.MessageBackground!,
input: colors.InputBackground!,
focus: colors.FocusBackground!,
diff: {
added: customTheme.background?.diff?.added ?? colors.DiffAdded,
removed: customTheme.background?.diff?.removed ?? colors.DiffRemoved,
},
},
border: {
default: colors.DarkGray,
},
ui: {
comment: customTheme.ui?.comment ?? colors.Comment,
symbol: customTheme.ui?.symbol ?? colors.Gray,
active: customTheme.ui?.active ?? colors.AccentBlue,
dark: colors.DarkGray,
focus: colors.FocusColor ?? colors.AccentGreen,
gradient: customTheme.ui?.gradient ?? colors.GradientColors,
},
status: {
error: customTheme.status?.error ?? colors.AccentRed,
success: customTheme.status?.success ?? colors.AccentGreen,
warning: customTheme.status?.warning ?? colors.AccentYellow,
},
};
return new Theme(
customTheme.name,
'custom',
rawMappings,
colors,
semanticColors,
);
}
/**
* Validates a custom theme configuration.
* @param customTheme The custom theme to validate.
* @returns An object with isValid boolean and error message if invalid.
*/
export function validateCustomTheme(customTheme: Partial<CustomTheme>): {
isValid: boolean;
error?: string;
warning?: string;
} {
// Since all fields are optional, we only need to validate the name.
if (customTheme.name && !isValidThemeName(customTheme.name)) {
return {
isValid: false,
error: `Invalid theme name: ${customTheme.name}`,
};
}
return {
isValid: true,
};
}
/**
* Checks if a theme name is valid.
* @param name The theme name to validate.
* @returns True if the theme name is valid.
*/
function isValidThemeName(name: string): boolean {
// Theme name should be non-empty and not contain invalid characters
return name.trim().length > 0 && name.trim().length <= 50;
}
/**
* Picks a default theme name based on terminal background color.
* It first tries to find a theme with an exact background color match.
* If no match is found, it falls back to a light or dark theme based on the
* luminance of the background color.
* @param terminalBackground The hex color string of the terminal background.
* @param availableThemes A list of available themes to search through.
* @param defaultDarkThemeName The name of the fallback dark theme.
* @param defaultLightThemeName The name of the fallback light theme.
* @returns The name of the chosen theme.
*/
export function pickDefaultThemeName(
terminalBackground: string | undefined,
availableThemes: readonly Theme[],
defaultDarkThemeName: string,
defaultLightThemeName: string,
): string {
if (terminalBackground) {
const lowerTerminalBackground = terminalBackground.toLowerCase();
for (const theme of availableThemes) {
if (!theme.colors.Background) continue;
// resolveColor can return undefined
const themeBg = resolveColor(theme.colors.Background)?.toLowerCase();
if (themeBg === lowerTerminalBackground) {
return theme.name;
}
}
}
const themeType = getThemeTypeFromBackgroundColor(terminalBackground);
if (themeType === 'light') {
return defaultLightThemeName;
}
return defaultDarkThemeName;
}
|