Code review cleanup for thinking display (#18720)

This commit is contained in:
Jacob Richman
2026-02-10 11:12:40 -08:00
committed by GitHub
parent 9813531f81
commit f9fc9335f5
16 changed files with 125 additions and 346 deletions

View File

@@ -1,77 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { isITerm2, resetITerm2Cache, shouldUseEmoji } from './terminalUtils.js';
describe('terminalUtils', () => {
beforeEach(() => {
vi.stubEnv('TERM_PROGRAM', '');
vi.stubEnv('LC_ALL', '');
vi.stubEnv('LC_CTYPE', '');
vi.stubEnv('LANG', '');
vi.stubEnv('TERM', '');
resetITerm2Cache();
});
afterEach(() => {
vi.unstubAllEnvs();
vi.restoreAllMocks();
});
describe('isITerm2', () => {
it('should detect iTerm2 via TERM_PROGRAM', () => {
vi.stubEnv('TERM_PROGRAM', 'iTerm.app');
expect(isITerm2()).toBe(true);
});
it('should return false if not iTerm2', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
expect(isITerm2()).toBe(false);
});
it('should cache the result', () => {
vi.stubEnv('TERM_PROGRAM', 'iTerm.app');
expect(isITerm2()).toBe(true);
// Change env but should still be true due to cache
vi.stubEnv('TERM_PROGRAM', 'vscode');
expect(isITerm2()).toBe(true);
resetITerm2Cache();
expect(isITerm2()).toBe(false);
});
});
describe('shouldUseEmoji', () => {
it('should return true when UTF-8 is supported', () => {
vi.stubEnv('LANG', 'en_US.UTF-8');
expect(shouldUseEmoji()).toBe(true);
});
it('should return true when utf8 (no hyphen) is supported', () => {
vi.stubEnv('LANG', 'en_US.utf8');
expect(shouldUseEmoji()).toBe(true);
});
it('should check LC_ALL first', () => {
vi.stubEnv('LC_ALL', 'en_US.UTF-8');
vi.stubEnv('LANG', 'C');
expect(shouldUseEmoji()).toBe(true);
});
it('should return false when UTF-8 is not supported', () => {
vi.stubEnv('LANG', 'C');
expect(shouldUseEmoji()).toBe(false);
});
it('should return false on linux console (TERM=linux)', () => {
vi.stubEnv('LANG', 'en_US.UTF-8');
vi.stubEnv('TERM', 'linux');
expect(shouldUseEmoji()).toBe(false);
});
});
});

View File

@@ -43,25 +43,3 @@ export function isITerm2(): boolean {
export function resetITerm2Cache(): void {
cachedIsITerm2 = undefined;
}
/**
* Returns true if the terminal likely supports emoji.
*/
export function shouldUseEmoji(): boolean {
const locale = (
process.env['LC_ALL'] ||
process.env['LC_CTYPE'] ||
process.env['LANG'] ||
''
).toLowerCase();
const supportsUtf8 = locale.includes('utf-8') || locale.includes('utf8');
if (!supportsUtf8) {
return false;
}
if (process.env['TERM'] === 'linux') {
return false;
}
return true;
}

View File

@@ -143,6 +143,13 @@ export function sanitizeForDisplay(str: string, maxLength?: number): string {
return sanitized;
}
/**
* Normalizes escaped newline characters (e.g., "\\n") into actual newline characters.
*/
export function normalizeEscapedNewlines(value: string): string {
return value.replace(/\\r\\n/g, '\n').replace(/\\n/g, '\n');
}
const stringWidthCache = new LRUCache<string, number>(
LRU_BUFFER_PERF_CACHE_LIMIT,
);