8000 fix: correct terminal display width for Thai sara am and similar SpacingMark characters by Copilot · Pull Request #2 · admannon/gemini-cli · GitHub
[go: up one dir, main page]

Skip to content
Draft
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
28 changes: 27 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions packages/cli/src/ui/components/InputPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import {
cpLen,
toCodePoints,
cpIndexToOffset,
getStringDisplayWidth,
} from '../utils/textUtils.js';
import chalk from 'chalk';
import stringWidth from 'string-width';
import { useShellHistory } from '../hooks/useShellHistory.js';
import { useReverseSearchCompletion } from '../hooks/useReverseSearchCompletion.js';
import {
Expand Down Expand Up @@ -518,7 +518,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
if (!visualLine) return;

// Even if we click past the end of the line, we might want to collapse an expanded paste
const isPastEndOfLine = relX >= stringWidth(visualLine);
const isPastEndOfLine = relX >= getStringDisplayWidth(visualLine);

const logicalPos = isPastEndOfLine
? null
Expand Down Expand Up @@ -1229,7 +1229,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
const cursorCol = buffer.cursor[1];

const textBeforeCursor = cpSlice(currentLogicalLine, 0, cursorCol);
const usedWidth = stringWidth(textBeforeCursor);
const usedWidth = getStringDisplayWidth(textBeforeCursor);
const remainingWidth = Math.max(0, inputWidth - usedWidth);

const ghostTextLinesRaw = ghostSuffix.split('\n');
Expand All @@ -1238,15 +1238,15 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
let inlineGhost = '';
let remainingFirstLine = '';

if (stringWidth(firstLineRaw) <= remainingWidth) {
if (getStringDisplayWidth(firstLineRaw) <= remainingWidth) {
inlineGhost = firstLineRaw;
} else {
const words = firstLineRaw.split(' ');
let currentLine = '';
let wordIdx = 0;
for (const word of words) {
const prospectiveLine = currentLine ? `${currentLine} ${word}` : word;
if (stringWidth(prospectiveLine) > remainingWidth) {
if (getStringDisplayWidth(prospectiveLine) > remainingWidth) {
break;
}
currentLine = prospectiveLine;
Expand Down Expand Up @@ -1274,22 +1274,22 @@ export const InputPrompt: React.FC<InputPromptProps> = ({

for (const word of words) {
const prospectiveLine = currentLine ? `${currentLine} ${word}` : word;
const prospectiveWidth = stringWidth(prospectiveLine);
const prospectiveWidth = getStringDisplayWidth(prospectiveLine);

if (prospectiveWidth > inputWidth) {
if (currentLine) {
additionalLines.push(currentLine);
}

let wordToProcess = word;
while (stringWidth(wordToProcess) > inputWidth) {
while (getStringDisplayWidth(wordToProcess) > inputWidth) {
let part = '';
const wordCP = toCodePoints(wordToProcess);
let partWidth = 0;
let splitIndex = 0;
for (let i = 0; i < wordCP.length; i++) {
const char = wordCP[i];
const charWidth = stringWidth(char);
const charWidth = getStringDisplayWidth(char);
if (partWidth + charWidth > inputWidth) {
break;
}
Expand Down Expand Up @@ -1616,7 +1616,7 @@ export const InputPrompt: React.FC<InputPromptProps> = ({
additionalLines.map((ghostLine, index) => {
const padding = Math.max(
0,
inputWidth - stringWidth(ghostLine),
inputWidth - getStringDisplayWidth(ghostLine),
);
return (
<Text
Expand Down
44 changes: 44 additions & 0 deletions packages/cli/src/ui/utils/textUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
escapeAnsiCtrlCodes,
stripUnsafeCharacters,
getCachedStringWidth,
getStringDisplayWidth,
sanitizeForDisplay,
} from './textUtils.js';

Expand Down Expand Up @@ -57,6 +58,49 @@ describe('textUtils', () => {
});
});

describe('getStringDisplayWidth', () => {
it('should return 0 for empty string', () => {
expect(getStringDisplayWidth('')).toBe(0);
});

it('should return correct width for ASCII', () => {
expect(getStringDisplayWidth('hello')).toBe(5);
});

it('should return 2 for CJK wide characters', () => {
expect(getStringDisplayWidth('世界')).toBe(4);
});

it('should return 2 for Thai ทำ (sara am occupies its own terminal column)', () => {
expect(getStringDisplayWidth('ทำ')).toBe(2);
});

it('should return 5 for Thai word ทำงาน', () => {
expect(getStringDisplayWidth('ทำงาน')).toBe(5);
});

it('should return correct width for mixed ASCII and Thai text', () => {
expect(getStringDisplayWidth('Hi ทำ!')).toBe(6);
});

it('should return 2 for ZWJ emoji family sequence', () => {
// Emoji clusters are handled at cluster level via getCachedStringWidth,
// which delegates to string-width's RGI-emoji detection → width 2.
expect(getStringDisplayWidth('👨‍👩‍👧‍👦')).toBe(2);
});

it('should return 2 for emoji with skin tone modifier', () => {
// Skin-tone modifier sequences are detected as emoji clusters and
// handled at cluster level → width 2.
expect(getStringDisplayWidth('👍🏿')).toBe(2);
});

it('should handle Latin combining diacritics correctly', () => {
// Decomposed "é" (e + combining acute accent) should be width 1
expect(getStringDisplayWidth('e\u0301')).toBe(1);
});
});

describe('stripUnsafeCharacters', () => {
describe('preserved characters', () => {
it('should preserve TAB (0x09)', () => {
Expand Down
Loading
0