Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/named-color-violation-tui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Replace chalk named color with theme-aware hex in session-directory warning.
2 changes: 1 addition & 1 deletion apps/kimi-code/src/cli/run-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async function resolvePromptSession(
}
if (target.workDir !== workDir) {
stderr.write(
`${chalk.yellow(
`${chalk.hex('#E8A838')(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use a light-safe warning color for print mode

When kimi -p -r <session> hits the wrong-directory path on a light/white terminal, this hard-coded dark-theme warning color has only about 2.1:1 contrast against white; the palette already uses lightColors.warning (#92660A, about 5.1:1) for this case. Because print mode has no theme detection here, replacing ANSI yellow with the dark palette hex makes this warning difficult to read for light-terminal users instead of fixing the contrast problem.

Useful? React with 👍 / 👎.

`Session "${opts.session}" was created under a different directory.\n` +
` cd "${target.workDir}" && kimi -r ${opts.session}`,
)}\n\n`,
Expand Down
2 changes: 1 addition & 1 deletion apps/kimi-code/src/tui/kimi-tui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ export class KimiTUI {
if (target.workDir !== workDir) {
this.state.ui.stop();
process.stderr.write(
`${chalk.yellow(
`${chalk.hex(this.state.theme.colors.warning)(
`Session "${startup.sessionFlag}" was created under a different directory.\n` +
` cd "${target.workDir}" && kimi -r ${startup.sessionFlag}`,
)}\n\n`,
Expand Down
74 changes: 74 additions & 0 deletions apps/kimi-code/test/tui/chalk-named-color-guard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { readFileSync, readdirSync } from 'node:fs';
import { join, relative } from 'node:path';

import { describe, expect, it } from 'vitest';

const SRC_ROOT = join(__dirname, '..', '..', 'src');

const NAMED_COLORS = [
'red', 'green', 'yellow', 'blue', 'magenta', 'cyan',
'white', 'gray', 'grey', 'black',
'blackBright', 'whiteBright', 'redBright', 'greenBright',
'yellowBright', 'blueBright', 'magentaBright', 'cyanBright',
];

const CHALK_NAMED_PATTERN = new RegExp(
`chalk\\.(${NAMED_COLORS.join('|')})\\(`,
);

function walk(dir: string, files: string[] = []): string[] {
try {
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const p = join(dir, entry.name);
if (entry.isDirectory()) {
walk(p, files);
} else if (entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts') && !entry.name.endsWith('.spec.ts')) {
files.push(p);
}
}
} catch { /* skip */ }
return files;
}

describe('chalk named color guard', () => {
it('forbids chalk named colors in production source code', () => {
const offenders: { file: string; line: number; snippet: string }[] = [];
const files = walk(SRC_ROOT);
let inBlockComment = false;
for (const file of files) {
const content = readFileSync(file, 'utf8');
const lines = content.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i] ?? '';
const trimmed = line.trimStart();

if (inBlockComment) {
if (trimmed.includes('*/')) inBlockComment = false;
continue;
}

if (trimmed.startsWith('/*')) {
if (!trimmed.includes('*/')) inBlockComment = true;
continue;
}

if (trimmed.startsWith('//') || trimmed.startsWith('*')) continue;

CHALK_NAMED_PATTERN.lastIndex = 0;
const m = CHALK_NAMED_PATTERN.exec(line);
if (m) {
offenders.push({
file: relative(SRC_ROOT, file),
line: i + 1,
snippet: line.trim(),
});
}
}
}
expect(
offenders,
`Found chalk named color usages. Use chalk.hex(colors.<token>) or theme styles instead.\n` +
offenders.map((o) => ` ${o.file}:${String(o.line)} ${o.snippet}`).join('\n'),
).toEqual([]);
});
});
37 changes: 37 additions & 0 deletions apps/kimi-code/test/tui/terminal-theme.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { describe, expect, it, vi } from "vitest";

import type { TUIState } from "#/tui/kimi-tui";
import { darkColors, lightColors, getColorPalette } from "#/tui/theme/colors";
import { createThemeStyles } from "#/tui/theme/styles";
import {
DISABLE_TERMINAL_THEME_REPORTING,
ENABLE_TERMINAL_THEME_REPORTING,
Expand Down Expand Up @@ -158,3 +160,38 @@ describe("terminal theme tracking", () => {
expect(state.terminal.write).toHaveBeenCalledWith(DISABLE_TERMINAL_THEME_REPORTING);
});
});

describe('ColorPalette warning token', () => {
it('has a defined warning color in both themes', () => {
expect(darkColors.warning).toBeTruthy();
expect(lightColors.warning).toBeTruthy();
expect(darkColors.warning).not.toBe(lightColors.warning);
});

it('resolves the correct palette by theme name', () => {
expect(getColorPalette('dark')).toBe(darkColors);
expect(getColorPalette('light')).toBe(lightColors);
});
});

describe('ThemeStyles warning helper', () => {
it('wraps text and includes the input', () => {
const styles = createThemeStyles(darkColors);
const result = styles.warning('test');
expect(result).toContain('test');
});

it('is a function that returns a string', () => {
const darkStyles = createThemeStyles(darkColors);
expect(typeof darkStyles.warning).toBe('function');
expect(typeof darkStyles.warning('hello')).toBe('string');
});

it('creates independent style sets per palette', () => {
const darkStyles = createThemeStyles(darkColors);
const lightStyles = createThemeStyles(lightColors);
expect(darkStyles.colors.warning).toBe(darkColors.warning);
expect(lightStyles.colors.warning).toBe(lightColors.warning);
expect(darkStyles.colors.warning).not.toBe(lightStyles.colors.warning);
});
});