Skip to content

Fixed(terminal-emulator): Keep cell styles when inserting characters (CSI @) - #5424

Open
kukirin-ui wants to merge 2 commits into
termux:masterfrom
kukirin-ui:fix-5415-prompt-color
Open

kukirin-ui wants to merge 2 commits into
termux:masterfrom
kukirin-ui:fix-5415-prompt-color

Conversation

@kukirin-ui

@kukirin-ui kukirin-ui commented Nov 3, 2026 •

Copy link
Copy Markdown

Fixes #5415

When bash redraws the line after the cursor ends up in the wrong place (typeahead like ^[[B echoed before the prompt), typing a character sends CSI 1 @ (insert character). ICH shifts the rest of the row right with TerminalBuffer.blockCopy(), which ends up in TerminalRow.copyInterval() copying a row onto itself.

copyInterval() already takes a copy of mText for that case, but it still read styles with line.getStyle() from the live row. Copying left to right, every cell after the insert point picked up a style that had just been overwritten, so the style at the cursor got smeared over the rest of the line. That's why the green ~ turned default colored.

The fix takes a copy of mStyle as well when source and destination are the same row.

Test: added testInsertCharactersKeepsStyleOfShiftedCells to ControlSequenceIntroducerTest. It feeds the exact output bash 5.2 produced for the steps in the issue (captured in a pty) plus a simpler case. It fails before the change and passes after; the other terminal-emulator tests still pass (run with a local JUnit setup, not the Gradle build; not yet tried on a device).

AI disclosure: I used an AI assistant to test the fix.

…(CSI @)

TerminalRow.copyInterval() copied the source text when copying within the
same row but read styles from the row being overwritten, so shifting
cells right smeared the style of the insert position over the rest of
the line. This made colored shell prompts lose their color after line
editing.

Closes termux#5415
@kukirin-ui kukirin-ui changed the title Fixed(terminal-emulator): Keep cell styles when inserting characters … Fixed(terminal-emulator): Keep cell styles when inserting characters (CSI @) Nov 3, 2026
@dev12124

dev12124 commented Nov 8, 2026

Copy link
Copy Markdown

The logic regarding state isolation for self-overlapping row copies in copyInterval() safely fixes the boundary blurring issue. However, allocating a brand-new heap slice via Arrays.copyOf every single time this == line evaluates to true can trigger minor GC overhead during heavy shell redraw stress.

Since the terminal line width bounds are predictable, could we potentially optimize this pipeline by bypassing the full array duplication if the displacement shift intervals (sourceX1 vs destinationX) do not introduce pointers collision or read-after-write conflicts?

Either way, the current code accurately prevents the crash state. Good work on the test coverage.

mhhridoy87190-wq

This comment was marked as off-topic.

arhbarhb81-afk

This comment was marked as spam.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Prompt color lost after cursor movement in Termux built-in terminal

4 participants