From 108e21ad7fc4070b08978e010c39d89c9b1696eb Mon Sep 17 00:00:00 2001 From: Ansho Rei Date: Mon, 24 Aug 2026 11:13:33 +0200 Subject: [PATCH] (25d3018) Fix the markdown link colorization branch, and bump to 0.27.2 The branch searched for a closing square bracket after the ]( sequence instead of a closing parenthesis, so it fired only on text that was not a link, and when it fired it pushed the matched span and dropped the rest of the line. Folded into colorize_bold, now colorize_inline: one walk over the source line recognising both **bold** and [text](url). A second pass over the first one's output would be scanning text that already holds escape sequences, and \x1b[36m carries an opening bracket, so the link scan would take the escape for markup. Unterminated markup of either kind falls through to the ordinary character push, so the rest of the line survives as literal text. Criteria text and hand-written metadata rows get links too, since they already went through this function. The whole span is painted, markers and url kept: show prints to a terminal worklane cannot assume will make a link clickable. --- AGENTS.md | 6 +- Cargo.lock | 2 +- Cargo.toml | 2 +- src/util.rs | 167 ++++++++++++++++++++++++++++++++++++++-------------- 4 files changed, 128 insertions(+), 49 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 64c69bb..b098db4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -398,7 +398,7 @@ and this file is for what someone changing worklane's own code needs. ### Color and Output - Use `color_text()` for coloured output — it respects `should_use_colors()` (terminal detection), and `paint()` is the same with the decision passed in - **`color_text()` is for the CLI's own printing only. Anything reachable from `content_trim` takes the decision as an argument and calls `paint()`**, because the board renders with colour off and an escape sequence in a ratatui paragraph is drawn as literal bytes. `nothing_the_body_renders_asks_the_terminal_for_the_colour_decision` scans this file and fails on a new caller; the allowlist is `ambiguous`, which builds a failed-search message for stderr -- A function that takes an `enabled` flag must use it all the way down. `colorize_bold` honoured its flag on the early return and then asked `should_use_colors()` for the bold run, which made the flag a lie in half the function +- A function that takes an `enabled` flag must use it all the way down. `colorize_inline` honoured its flag on the early return and then asked `should_use_colors()` for the bold run, which made the flag a lie in half the function - **A test that asserts an absence has to be paired with one that asserts the presence.** The guard for this, an assert that the detail view holds no escape byte, could not fail: a test binary has no tty, so `should_use_colors()` was false and the leaking branch was never taken. The pair is now `content_trim(.., false)` has no escape and `content_trim(.., true)` still colours, which pins the flag as the only thing that decides - **The palette is the `Color` enum, not a string.** `Color::ansi()` gives the CLI its SGR sequence and `From for ratatui::style::Color` gives the TUI its equivalent, so a colour that exists for one renderer and not the other is a compile error. The stringly-typed version needed a test for exactly that, and a missing entry silently produced an empty prefix, which is how `magenta` came to be undefined - Reset color with `\x1b[0m` after colored text @@ -415,6 +415,7 @@ and this file is for what someone changing worklane's own code needs. - Priority ramp → brightred (1), red (2), orange (3), cyan (4), grey (5). Green is deliberately absent: it means title - Relation kind mark → brightred `×` blocked by and blocks, cyan `→` relates to, orange `←` regression of and regressed by. A small ramp inside one column, the way the priority meter is: the mark says which kind of edge the row is, so none of these three is a colour standing in for the uuid or the title beside it. Brightred on the cross rather than red, because the id one cell to its right is red and the two would blend. It is the colour and the glyph a held-up card already carries, which is the point: one vocabulary for blocking - Relation status mark → grey `○` backlog, orange `◐` committed, cyan `◐` reviewing, green `●` done, grey `?` for a target the board does not hold. The second ramp in that column: the shape says how far the target has got and the colour tells the two middle stages apart, since both are half circles. **Green is here and deliberately absent from the priority ramp**, where it would sit on a whole line and read as a title; on a one-cell mark with no title behind it, it is the obvious colour for finished and nothing else wants it. Orange keeps the meaning it already has, Status, and grey keeps unset for the one row with no status to show +- Markdown link → cyan, markers and url kept. Not a new meaning for the colour: the plan numbers, the criterion numbers and the `relates to` mark are already cyan inside a rendered body, and a link is the same kind of thing, a pointer the reader may follow rather than a value. The url is not hidden behind the text the way a renderer would hide it, because `show` prints to a terminal worklane cannot assume will make it clickable, and a link whose target cannot be read is worse than a long line - Chrome → grey. The footer's key names, a rendered `[ ]`, and the overlay's page indicator. Not a new slot: grey already means "what a reader skips", which is the other half of the unset-value meaning, and none of these is a value. The indicator in orange would read as the title it faces across the overlay, in cyan as a timestamp, and in the priority ramp's colours as a level - Campaign label → its codename through `tag_color`, bold, marked `~`. The colour is what groups a campaign across the cards that carry it, which is exactly what a tag's colour is for, and one word is one colour in every view because `tag_color` is a pure function of it. The mark and the bold are what tell it from a tag on a row that holds both: the mark is not `#`, or the row would read as a list of tags with a stranger in it, and not `@`, which reads as a person on every board anyone has used. `Codename` in a metadata table takes the colour without the mark, since the field name has already said what it is - Campaign label with no codename → grey. The label is then the short id, and grey keeps a reserved meaning off a `TAG_COLORS` slot in the one case where it would land on one: red is the uuid's everywhere else, and grey already means what a reader skips, which is what a campaign with no codename has to say for itself @@ -426,7 +427,8 @@ and this file is for what someone changing worklane's own code needs. - Grey is indexed colour 8 in the 256-colour form, not SGR 90 or 37. Both of those failed on a real palette: 90 leans on the bright-black slot and 37 can sit on top of the default foreground. `\x1b[38;5;8m` is byte for byte what crossterm emits for ratatui's `Color::DarkGray`, the grey frig and pantop use for dimmed rows ### Code Organization -- Colorization functions (`colorize_ordered`, `colorize_bold`, `content_trim_colored`) handle markdown formatting. `colorize_line` is gone: it was a `pub` alias for `colorize_bold` with one caller, and the only thing it added was asking the terminal +- Colorization functions (`colorize_ordered`, `colorize_inline`, `content_trim_colored`) handle markdown formatting. `colorize_line` is gone: it was a `pub` alias for the bold pass with one caller, and the only thing it added was asking the terminal +- **`colorize_inline` walks the line once for both inline markers**, `**bold**` and `[text](url)`, rather than running one pass over the output of the other. A second pass would be scanning text that already holds escape sequences, and `\x1b[36m` carries an opening bracket, so the link scan would take the escape for markup. The source is the only string in which either marker means what it says. It was `colorize_bold` while links lived in a branch of their own in `content_parts`; that branch is gone, which is also what gives the criteria text and the metadata `Other` rows their links, since those already went through this function - `show` renders the Metadata section as a two column table via `render_metadata_table`, not line by line. Column width is measured on the raw field names in characters and padding is applied before anything is styled: measuring a coloured string counts its escapes towards its width. `paint()` is `color_text()` with the colour decision passed in, which is what lets a test render both ways in one process - The title is printed once, by `cmd_show`. `content_trim_colored` skips the `# ` heading in the file content - Rules under headings are measured on the displayed name in characters, never on the raw line or in bytes diff --git a/Cargo.lock b/Cargo.lock index 6b224f4..0109be7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1503,7 +1503,7 @@ dependencies = [ [[package]] name = "worklane" -version = "0.27.1" +version = "0.27.2" dependencies = [ "anyhow", "assert_cmd", diff --git a/Cargo.toml b/Cargo.toml index 06526f3..9705472 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "worklane" -version = "0.27.1" +version = "0.27.2" edition = "2024" [dependencies] diff --git a/src/util.rs b/src/util.rs index 3c69f52..d8f63d8 100644 --- a/src/util.rs +++ b/src/util.rs @@ -841,31 +841,13 @@ pub fn content_parts(content: &str, colored: bool, resolved: &Resolved) -> Vec Strin // The decision this row is rendered under, not the one the terminal // would give: `colorize_line` asked `should_use_colors()` here, which is // how escapes reached the board's detail view. - MetadataRow::Other(text) => result.push_str(&colorize_bold(text, colored)), + MetadataRow::Other(text) => result.push_str(&colorize_inline(text, colored)), } result.push('\n'); } @@ -1077,13 +1059,20 @@ pub fn is_unknown_value(value: &str) -> bool { trimmed == NONE || trimmed == NOT_SET } -/// Replace `**bold**` with colour, or leave the line exactly as it is. +/// Replace `**bold**` with colour and paint `[text](url)`, or leave the line exactly +/// as it is. /// /// Taking `enabled` rather than asking makes both paths testable, and the /// disabled path matters: stripping the markers when there is no colour to /// replace them with loses the emphasis altogether, which is what happens to /// every piped `show` and to everything the opencode tools read. -fn colorize_bold(line: &str, enabled: bool) -> String { +/// +/// One walk for both markers rather than two passes. A pass over the output of +/// another would be scanning text that already holds escape sequences, and `\x1b[36m` +/// carries an opening bracket: a link scan run afterwards would take the escape for +/// markup. Walking the source once is the only place either marker can be recognised +/// for what it is. +fn colorize_inline(line: &str, enabled: bool) -> String { if !enabled { return line.to_string(); } @@ -1093,27 +1082,34 @@ fn colorize_bold(line: &str, enabled: bool) -> String { let mut i = 0; while i < chars.len() { - if !is_delimiter(&chars, i) { - result.push(chars[i]); - i += 1; + // Unterminated markup falls through to the push below, one character at a + // time, so the rest of the line survives as the literal text it is. The old + // link branch pushed its match and dropped everything around it. + if is_delimiter(&chars, i) + && let Some(close) = (i + 2..chars.len()).find(|&at| is_delimiter(&chars, at)) + { + let bold: String = chars[i + 2..close].iter().collect(); + // `enabled`, not `should_use_colors()`. Asking again here made the + // flag a lie: the early return above respected it and this did not. + result.push_str(&paint(&bold, Color::Magenta, enabled)); + i = close + 2; continue; } - match (i + 2..chars.len()).find(|&at| is_delimiter(&chars, at)) { - Some(close) => { - let bold: String = chars[i + 2..close].iter().collect(); - // `enabled`, not `should_use_colors()`. Asking again here made the - // flag a lie: the early return above respected it and this did not. - result.push_str(&paint(&bold, Color::Magenta, enabled)); - i = close + 2; - } - // Unterminated: the rest is literal. The old scan walked off the end - // here and dropped the final character. - None => { - result.extend(&chars[i..]); - return result; - } + if chars[i] == '[' + && let Some(end) = link_end(&chars, i) + { + // Markers and url kept, unlike the bold pair. The url is content: a + // reader of `show` wants to see where the link goes, and the terminal + // that would make it clickable is not something worklane can count on. + let link: String = chars[i..end].iter().collect(); + result.push_str(&paint(&link, Color::Cyan, enabled)); + i = end; + continue; } + + result.push(chars[i]); + i += 1; } result @@ -1123,6 +1119,22 @@ fn is_delimiter(chars: &[char], at: usize) -> bool { chars.get(at) == Some(&'*') && chars.get(at + 1) == Some(&'*') } +/// The index one past the end of the `[text](url)` opening at `open`, or nothing when +/// the line does not close one. +/// +/// The closing parenthesis is what ends a link. The old branch looked for a second +/// square bracket there, which a well-formed link does not have, so the whole branch +/// fired only on text that was not a link at all. +fn link_end(chars: &[char], open: usize) -> Option { + let close_text = (open + 1..chars.len()).find(|&at| chars[at] == ']')?; + if chars.get(close_text + 1) != Some(&'(') { + return None; + } + + let close_url = (close_text + 2..chars.len()).find(|&at| chars[at] == ')')?; + Some(close_url + 1) +} + pub const METADATA_HEADING: &str = "## Metadata"; fn is_metadata_heading(line: &str) -> bool { @@ -4559,12 +4571,12 @@ mod tests { #[test] fn bold_markers_survive_when_colour_is_off() { let line = "Some **bold** text"; - assert_eq!(colorize_bold(line, false), line); + assert_eq!(colorize_inline(line, false), line); } #[test] fn bold_markers_are_replaced_when_colour_is_on() { - let out = colorize_bold("Some **bold** text", true); + let out = colorize_inline("Some **bold** text", true); assert!(out.contains("bold"), "was {out:?}"); assert!(!out.contains("**"), "markers left behind in {out:?}"); } @@ -4573,17 +4585,82 @@ mod tests { #[test] fn an_unterminated_marker_keeps_the_whole_line() { for line in ["a **bold", "**", "***", "trailing **"] { - assert_eq!(colorize_bold(line, true), line, "for {line:?}"); + assert_eq!(colorize_inline(line, true), line, "for {line:?}"); } } #[test] fn several_bold_runs_on_one_line_all_survive() { - let out = colorize_bold("**one** and **two** and rest", true); + let out = colorize_inline("**one** and **two** and rest", true); assert!(out.contains("one") && out.contains("two") && out.ends_with("and rest")); assert!(!out.contains("**"), "was {out:?}"); } + /// The old branch pushed the span it matched and dropped the rest of the line. + #[test] + fn a_link_keeps_the_text_on_either_side_of_it() { + let out = colorize_inline("see [the docs](https://x.test/y) for more", true); + + assert!(out.starts_with("see "), "was {out:?}"); + assert!(out.ends_with(" for more"), "was {out:?}"); + assert!(out.contains("[the docs](https://x.test/y)"), "was {out:?}"); + assert!(out.contains(Color::Cyan.ansi()), "not painted in {out:?}"); + } + + /// With no colour to carry it, the markup is the markup. + #[test] + fn a_link_survives_unchanged_when_colour_is_off() { + let line = "see [the docs](https://x.test/y) for more"; + assert_eq!(colorize_inline(line, false), line); + } + + /// The closing parenthesis ends a link. Looking for a second square bracket there, + /// as the old branch did, meant it fired on anything but a link. + #[test] + fn text_that_is_not_a_closed_link_is_left_alone() { + for line in [ + "a [label] and (a note)", + "unterminated [text](http://x.test", + "[a][b] reference style", + "bare [", + ] { + assert_eq!(colorize_inline(line, true), line, "for {line:?}"); + } + } + + #[test] + fn several_links_on_one_line_all_survive() { + let out = colorize_inline("[a](1) then [b](2) then end", true); + + assert!(out.contains("[a](1)") && out.contains("[b](2)"), "was {out:?}"); + assert!(out.ends_with(" then end"), "was {out:?}"); + } + + /// Both markers are recognised in the same walk, so a line does not have to pick + /// which of the two it gets. + #[test] + fn bold_and_a_link_on_one_line_are_both_coloured() { + let out = colorize_inline("**note**: read [this](http://x.test) first", true); + + assert!(!out.contains("**"), "markers left behind in {out:?}"); + assert!(out.contains("[this](http://x.test)"), "was {out:?}"); + assert!(out.contains(Color::Magenta.ansi()), "no bold in {out:?}"); + assert!(out.contains(Color::Cyan.ansi()), "no link in {out:?}"); + assert!(out.ends_with(" first"), "was {out:?}"); + } + + /// End to end: the body a `show` prints keeps every word of a line with a link. + #[test] + fn a_body_line_with_a_link_keeps_all_of_its_text() { + let content = "# T\n\n## Description\n\nbefore [docs](http://x.test/a) after\n"; + let rendered = content_trim(content, false, &Resolved::default()); + + assert!( + rendered.ends_with("before [docs](http://x.test/a) after\n"), + "{rendered:?}" + ); + } + #[test] fn short_id_shortens_a_full_uuid() { assert_eq!(short_id("c230d2c6-d78a-44a5-8f5f-88fb28f84493"), "c230d2c");