fix(tui): correct textarea width for full-width chars #2922
+3
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
double-width glyphs (e.g., Japanese) report accurate offsets and cursor positioning.
inserting spaces to prevent duplicated padding for wide whitespace.
rune loop while preserving their display width.
account for prompt/line-number padding internally.
enumerating each rune or attachment width.
matching double-width semantics across lines.
Fixes #2920
Addresses #2593, #2013, #379
Testing
components/chat/editor.go
This issue occurs because macOS’s native Terminal handles CJK full-width characters and combining marks more consistently, while VS Code and Cursor (VS Code–based integrated terminals, powered by xterm.js) are more prone to misalignment depending on configuration.
(CJK stands for Chinese, Japanese, and Korean — languages that use full-width characters which typically occupy two cells in a fixed-width terminal.)
Fix for Japanese Text Clipping in OpenCode TUI
Problem Description
When entering Japanese (full-width) characters in the OpenCode TUI editor, some characters were not visible, even though width calculations appeared correct. The issues observed:
Root Cause
1. Double Width Reduction in
SetWidth()
Location:
packages/tui/internal/components/chat/editor.go
, around line 441Problem: The editor pre-subtracted 6 characters from the available width before passing it to
textarea.SetWidth()
. However,SetWidth()
itself is responsible for subtracting prompt, line number, and border widths internally.Why this failed:
2. Excessive Top/Bottom Padding
Location:
packages/tui/internal/components/chat/editor.go
, textarea style configurationProblem: The textarea was styled with both
PaddingTop(1)
andPaddingBottom(1)
.Why this failed:
Solutions
Fix 1: Pass Full Width to
SetWidth()
File:
packages/tui/internal/components/chat/editor.go
Change:
Rationale:
SetWidth()
is designed to receive the full available width. It internally subtracts reserved widths for prompts, line numbers, and borders. Passing the unmodified width ensures the textarea’s visible area matches the container width.Fix 2: Remove Extra Vertical Padding
File:
packages/tui/internal/components/chat/editor.go
Change:
Rationale:
Removing top and bottom padding eliminates the unintended two-line increase in height, resulting in a compact and consistent input area.
Testing
After these fixes, Japanese text in the TUI editor:
Technical Details
SetWidth()
is now always passed the full container width, avoiding double subtraction.go-runewidth
anduniseg
) remains correct, ensuring CJK text displays with accurate widths.Why This Matters
For users working in Japanese (or other CJK languages), correct handling of full-width characters is essential for usability. Fixing width calculation and unnecessary padding ensures: