Skip to content

Fix(tui): control sequences in a note are not sent to the terminal - #44

Merged
adibhanna merged 4 commits into
mainfrom
claude/tui-note-control-sequences
Nov 4, 2026
Merged

adibhanna merged 4 commits into
mainfrom
claude/tui-note-control-sequences

Conversation

@adibhanna

@adibhanna adibhanna commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Note text is data, so it should show as text and never act on the terminal. This covers the TUI and the plain CLI, and fixes two older bugs in the same area.

What changes

A shared filter. internal/termsafe holds it. Clean keeps text, newlines, tabs and SGR colours, and drops other CSI sequences, OSC, DCS, APC, PM and SOS strings, single-character escapes, C0 and C1 controls, and bidirectional overrides. An unterminated string stops at the end of its row, so later rows stay.

The CLI. When stdout or stderr is a terminal, both go through the filter. That covers every command that prints through them, zn task watch included. Pipes and files keep every byte.

The TUI. App.View runs Clean on every frame, and note text is also handled where it enters a frame. Visible draws each control rune as a one-cell picture such as ␛, in the editor and both reading views. So:

  • The insert cursor stays on its rune.
  • A note's own colours do not reach the terminal.
  • A note cannot place a picture's placeholder rune.
  • File names stay on one row in the sidebar and tabs.
  • The Tasks list, the board, comments, the outline and Home show note text the same way, through truncateCells, and so does every other view that uses it. Messages in the status line and toasts do too, and so do errors on a terminal's stderr.

Older bugs.

  • Opening a note with bytes that are not valid UTF-8, or with CRLF line ends, no longer marks it changed, so :qa no longer rewrites the file. The bytes stay until a real edit.
  • A very long word no longer stalls the reading view. Glamour's word wrap is quadratic in a word's length, so words over 2 KB get a space every 1 KB before rendering. Lines stay lines, so source mapping holds.

Cost

About 47 microseconds to clean a 60-row frame, measured once on the first commit. Visible returns early when a note has nothing to change.

Testing

  • go test ./... and go vet ./... pass.
  • Real runs in tmux: each CLI command checked, on a terminal, writes nothing to the clipboard buffer; the editor and reading view show pictures; :qa on a note with an invalid byte leaves the file's hash unchanged; a pipe still carries the original bytes.

Known limits

  • CLI lines that do not go through emitLine, zn read or --pretty keep a note's own SGR colours on a terminal, because the filter keeps colours for zn's help and pretty output. Other control sequences are still dropped.
  • A word over 2 KB shows spaces in the reading view, inside code too.
  • Text a view draws without truncateCells, wrapCommentText or the editor and reading views, such as a menu's title, only gets the frame filter. It cannot send other control sequences, but it can keep a note's SGR colours.
  • A real edit to a note with invalid bytes or CRLF line ends saves it as UTF-8 with LF, as before.

Bytes in a note reached the terminal as they were, so an escape sequence in a
note acted on the terminal instead of showing as text. Every frame now passes
through one filter at the end of `App.View`, which keeps text, newlines, tabs
and colours and drops everything else. zn's own cursor and picture sequences are
written apart from the frame and are not affected.
…e keeps its bytes

The filter moves to internal/termsafe and now also covers the plain CLI: when
stdout or stderr is a terminal, control sequences in what zn prints are dropped.
Pipes and files keep every byte.

In the TUI, control runes are now handled where note text enters a frame. The
editor and both reading views draw each as a one-cell picture such as ␛, so the
insert cursor stays on its rune, a note's own colours do not reach the terminal,
and a note cannot place a picture's placeholder rune. File names in the sidebar
and tabs stay on one row, and bidirectional overrides are dropped from what is
written.

Very long words are broken before Glamour, whose word wrap is quadratic in a
word's length, so a long run in a note no longer stalls the reading view.

Opening a note holding bytes that are not valid UTF-8, or CRLF line ends, no
longer marks it changed, so quitting no longer rewrites the file. The file stays
as it is until a real edit.
A note's own colour still reached the terminal in the Tasks list, the board, the
comments panel and thread, the outline panel and Home, since those views take
note text without going through the editor or the reading view.

Truncating a row's text is where these views take it in, so truncateCells now
makes it safe, and that covers every other view using it. Comment threads and
the text reader are covered where they wrap, and a task's note name directly.
Messages that name a note, in the status line and toasts, and an error on a
terminal's stderr, show the name the same way.
Windows refuses an escape in a file name, so the test could not create its file
there; the escaped title is now set in the index. The comments and outline
checks were also passing on rows from another view, so each view now names the
text it must show.
@adibhanna
adibhanna merged commit 18a6ca0 into main Nov 4, 2026
7 checks passed
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.

1 participant