textual icon indicating copy to clipboard operation
textual copied to clipboard

Outline trimming can remove trailing CJK wide characters

Open Timotty opened this issue 1 month ago • 1 comments

Have you checked closed issues? Yes. I searched open and closed issues and pull requests for line_trim, CJK/wide-character trimming, outlines, and double-width truncation. #6357 also involves disappearing CJK text, but describes toast overlap and partial redraw over another widget's border; this reproduction is deterministic trimming of a widget's own outlined line.

Have you checked against the most recent version of Textual? Yes. PyPI currently lists 8.2.8. This also reproduces with Textual installed editable from current main, commit 5e5b7ef58b8c5572c13a3ff9027d756623661495.

The bug

When an outlined content line ends in double-width characters and has no trailing padding, Textual trims the end by character count rather than terminal-cell count. The cut can land inside a wide glyph and remove visible CJK text.

Complete minimal example

import asyncio

from rich.cells import cell_len
from rich.segment import Segment

from textual._segment_tools import line_trim
from textual.app import App, ComposeResult
from textual.widgets import Static


def segment_text(segments: list[Segment]) -> str:
    return "".join(segment.text for segment in segments)


for segments in (
    [Segment("ab"), Segment("中中")],
    [Segment("ab"), Segment("中")],
):
    result = segment_text(line_trim(segments, start=False, end=True))
    print(repr(result), cell_len(result))


class Repro(App[None]):
    def compose(self) -> ComposeResult:
        yield Static("xxxxxx\nab中中\nyyyyyy")


async def main() -> None:
    app = Repro()
    async with app.run_test(size=(6, 5)) as pilot:
        app.query_one(Static).styles.outline = ("round", "red")
        await pilot.pause()
        lines = [
            "".join(segment.text for segment in strip)
            for strip in app.screen._compositor.render_strips(app.screen.size)
        ]
        print(repr(lines[1]))


asyncio.run(main())

Actual output:

'ab ' 3
'ab' 2
'│b │'

Removing one terminal cell should retain five cells from ab中中 ('ab中 ') and three cells from ab中 ('ab '). In the end-to-end render, both 中 characters disappear; the corresponding content line should retain one of them ('│b中 │').

Investigation and proposed direction

The end branch of line_trim() in src/textual/_segment_tools.py currently calls:

last_segment.split_cells(len(last_segment.text) - 1)

Segment.split_cells() expects a cell offset, but len(text) counts Unicode code points. The start branch already passes a cell count. A focused correction appears to be:

last_segment.split_cells(cell_len(last_segment.text) - 1)

cell_len is already imported by the module. Regression coverage can exercise ASCII, one and two trailing wide characters, and the existing start behavior. Could @willmcgugan confirm that preserving the remaining cells with Rich's space placeholder is the intended behavior? No PR has been opened pending approval under the project's AI policy.

Environment

  • Windows
  • CPython 3.14.6
  • Textual 8.2.8 (editable from the main commit above)
  • Rich 15.0.0
  • Reproduced both with direct segment calls and headless App.run_test()

Disclosure: this report and its reproduction were prepared and verified with AI assistance (OpenCode, GPT-5.6 Sol).

Timotty avatar Oct 13 '26 00:10 Timotty

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Oct 13 '26 00:10 github-actions[bot]