Make text safe to write to a terminal.
A TUI usually renders data its user did not author - filenames, log lines, HTTP responses. Escape sequences in that data are instructions to the terminal, not characters: OSC 52 writes the attacker's data into the user's system clipboard, OSC 8 hyperlinks text to somewhere other than what it says, OSC 2 rewrites the window title, and CSI 2K with a carriage return erases the line the content just drew.
sanitize keeps the one sequence family that only paints characters - SGR,
the styling codes - and drops the rest. strip drops those too, leaving the
text that will actually be displayed.
Both return the argument unchanged when there is nothing to remove, so the common case of plain or styled text costs one scan and no allocation.
Make text safe to write to a terminal. A TUI usually renders data its user did not author - filenames, log lines, HTTP responses. Escape sequences in that data are instructions to the terminal, not characters: OSC 52 writes the attacker's data into the user's system clipboard, OSC 8 hyperlinks text to somewhere other than what it says, OSC 2 rewrites the window title, and CSI 2K with a carriage return erases the line the content just drew. `sanitize` keeps the one sequence family that only paints characters - SGR, the styling codes - and drops the rest. `strip` drops those too, leaving the text that will actually be displayed. Both return the argument unchanged when there is nothing to remove, so the common case of plain or styled text costs one scan and no allocation.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |