Repository navigation
Conversation
Tiny Code Reviewthe AI provider rate-limited the request This is advice, not a gate. Re-run with the |
unRekable
left a comment
There was a problem hiding this comment.
Code Review
🟡 2 findings (1 blocking).
Adds per-language colors for fenced code blocks, drawn in the shell: a self-contained highlighter runs on the background executor, is cached per message and block, has a 300-line plain fallback, and wires the e2e prove driver the way the repo already does it. The shape is right, but the ANSI scanner panics on text the other side chose: while scanning SGR parameters it consumes only the first byte of a non-ASCII character and then slices the text at a non-char-boundary (finding 1). The #[path] source share also compiles the window from a file inside chatd's Discord adapter, with an unwritten keep-it-dependency-free constraint (finding 2).
Residual risk
- The module is a new hand-rolled parser over peer-controlled fence language and text; the tests cover happy paths only. A fuzz pass over highlight(), especially the ANSI scanner, would be cheap insurance beyond what this diff ships.
- ANSI SGR 256-color parameters fold into the 16-color table (sgr256 maps n to 30 + n % 8) and truecolor is ignored, so pasted terminal output in an ansi block shows approximated colors.
- While a block's coloring is in flight, an edited message with the same key draws the stale cached text until the old task finishes, because the coloring guard returns the cached Lit for a stamp that no longer matches. Rare and self-correcting.
| if open.is_none() && (fg != 0 || bg != 0 || bold) { | ||
| open = Some(text.len()); | ||
| } | ||
| let c = source[i..].chars().next().unwrap(); |
There was a problem hiding this comment.
🔴 Blocker: Crash on input the other side controls. take_sgr() bails after reading one byte when it meets a byte that is not a digit, ';' or an ASCII letter, so an escape sequence ESC-[ followed by any non-ASCII character leaves i pointing at a UTF-8 continuation byte; this line then slices source[i..] at a non-char-boundary and panics. The fence language and text are the peer's, Message::bound caps lengths only and strips nothing, and this runs on the shell's background executor, so one message with an ```ansi block containing ESC-[é panics the coloring task. Fix take_sgr() to round j up past UTF-8 continuation bytes when it bails (or guard this line with source.get(i..) and step to the next boundary), and add that input to the tests.
| let c = source[i..].chars().next().unwrap(); | |
| let Some(c) = source.get(i..).and_then(|rest| rest.chars().next()) else { i += 1; continue }; |
| mod viewer; | ||
| mod when; | ||
| mod wiki; | ||
| #[path = "../../../chatd/src/adapters/discord/highlight.rs"] |
There was a problem hiding this comment.
use crate::... in that file, or a move of it within adapters/discord, breaks the shipped window build out of nowhere. The highlighter also colors code blocks from every network, not Discord only. Give it a neutral home both crates include (or a small workspace crate) and state the shared-file constraint in the module doc.
75310f7 to
1419d1d
Compare
…anics the coloring task take_sgr bailed after one byte of a multi-byte character, so ansi() sliced the source mid-character. It now leaves a non-ASCII byte for the caller, and a test covers escapes followed by accented, emoji and CJK text. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… ANSI UTF-8 tests
1419d1d to
519b4b7
Compare
What changed and why
A fenced code block is coloured from the language on its fence. Diff lines that start with + or - take a green or red background, and an ansi block keeps the SGR colours with the escapes removed. A block longer than 300 lines stays plain. Colouring runs off the UI thread and is cached per message and block.
Tested with chatd unit tests for a rust keyword, diff lines, ansi escapes, a long block, and the lab check.
cargo clippy -p chatd --no-default-features --all-targetsreported nothing new in this change. The GUI crate typechecks with the gui feature. The window was not opened, and this was not tried on a live Discord account.Type
Platforms
Proof
Scenario: e2e/scenarios/discord-code-colors
Lab run:
Compatibility
No protocol change. An older client and an older server are unchanged. The colours are drawn from the language the message already carried.
Risk and rollback
A wrong colour is only on screen. Revert this commit.
Release note
Code blocks are coloured by language, including diff and ansi.