Skip to content

Pane viewer: cursor lands past wide characters such as CJK #156

Open

Description

@tony

Summary

The pane viewer (ui://libtmux-mcp/pane-viewer.html, added in #149) draws the cursor in the wrong column on any row holding a character whose cell width differs from its length as a JavaScript string. snapshot_pane reports cursor_x in terminal cells, as tmux counts them; the page uses cursor_x as an index into the row's string. A CJK character fills two cells but is one string element, so on a row reading ab日本語 tmux reports cursor_x 8, the string has 5 elements, and the page draws the cursor 3 columns past the text. Zero-width combining marks shift it the other way.

Reproduction

  1. Start a pane whose cursor stays right after wide characters:
$ tmux -L cjkrepro -f /dev/null new-session -d -x 30 -y 4 "printf 'ab日本語'; sleep 30"
  1. From a libtmux-mcp checkout, take a snapshot and compare cursor_x with the row's length in UTF-16 code units, the unit JavaScript strings index by:
$ uv run python -c "from libtmux_mcp.tools.pane_tools.meta import snapshot_pane; s = snapshot_pane(socket_name='cjkrepro'); row = s.content.split(chr(10))[s.cursor_y]; print(repr(row), s.cursor_x, len(row.encode('utf-16-le')) // 2)"
  1. Repeat with printf 'e\314\201x' (an e followed by U+0301 COMBINING ACUTE ACCENT, then x) and printf '\342\234\214x' (U+270C VICTORY HAND, then x).

Expected

The cursor on the cell after the last character: string index 5 for ab日本語, 3 for éx, and 2 for ✌x.

Actual

Row cursor_x (cells) String length Where the page draws the cursor
ab日本語 8 5 3 columns past the text
e + U+0301 + x 2 3 on the x
✌x 3 2 1 column past the text

The page pads the row to cursor_x + 1 elements and marks element cursor_x:

https://github.com/tmux-python/libtmux-mcp/blob/d503159/src/libtmux_mcp/resources/viewer.py#L71-L77

Environment

Versions
  • tmux 3.8a
  • libtmux-mcp main at d503159
  • Linux

Proposal

Map cells to string positions with the widths tmux uses, and draw each wide character two cells wide (for example an inline-block span 2ch wide) so the columns after it line up. tmux 3.8 resolves a width in utf8_width: first from its override table, which holds built-in entries such as U+270C at width 2 (utf8_default_width_cache) plus the codepoint-widths server option, then from utf8proc_wcwidth or the system wcwidth. A width table in the page will disagree with tmux wherever those sources differ, so the map's source is a design choice for the fix.

References

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions