Skip to content

Conversation

@majiayu000
Copy link
Contributor

Summary

Fix garbled display and incorrect backspace behavior for Chinese/Unicode characters in the TUI.

Closes #233

Problem

Chinese and other multi-byte Unicode characters were displaying as garbled text (e.g., 是的分身艺术�) because:

  1. String truncation used byte slicing (str[:n]), which cuts UTF-8 characters in the middle
  2. Backspace deleted only 1 byte instead of 1 character, corrupting multi-byte characters
  3. Width calculations used len() which returns byte count, not display width

Solution

Use github.com/mattn/go-runewidth (already a dependency) for proper Unicode handling:

  • runewidth.StringWidth() - calculate actual display width
  • runewidth.Truncate() - safely truncate strings without breaking characters
  • []rune() conversion - for character-based operations like backspace

Changes

File Change
ui/list.go Use runewidth for title/branch truncation and width calculation
ui/err.go Use runewidth for error message truncation
app/app.go Convert to []rune for backspace, use runewidth for length check

Testing

  • Tested with Chinese characters in instance names
  • Tested backspace deletion of Chinese characters
  • All existing tests pass (go test ./...)
  • Build succeeds (go build .)

Screenshots

Before (Bug):

Instance names with Chinese characters show garbled text when truncated or after backspace.

After (Fixed):

Chinese characters display correctly and backspace removes whole characters.

Fix garbled display of Chinese/Unicode characters in instance list and
error messages by replacing byte-based string operations with proper
Unicode-aware width calculations.

Changes:
- ui/list.go: Use runewidth.StringWidth() for width calculations and
  runewidth.Truncate() for safe string truncation in title and branch
- ui/err.go: Same fix for error message truncation

The issue was that Go's len() returns byte count, not character count.
When truncating UTF-8 strings (like Chinese characters which are 3 bytes
each) at byte boundaries, multi-byte characters get cut in the middle,
resulting in invalid UTF-8 sequences displayed as garbled text.

Fixes display of non-ASCII characters including Chinese, Japanese,
Korean, emoji, etc.
When deleting characters with backspace, convert string to []rune first
to delete by character instead of by byte. This fixes the issue where
pressing backspace on Chinese/Unicode characters would only delete part
of the multi-byte sequence, causing garbled text.

Also use runewidth.StringWidth() for title length validation to properly
count display width instead of byte count.
@github-actions
Copy link

github-actions bot commented Dec 24, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@majiayu000
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

Copy link
Member

@mufeez-amjad mufeez-amjad left a comment

Choose a reason for hiding this comment

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

thanks!

@mufeez-amjad mufeez-amjad merged commit 81d66d7 into smtg-ai:main Dec 24, 2025
7 of 8 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Dec 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chinese/Unicode characters display as garbled text and backspace deletes incorrectly

2 participants