diff --git a/src/util.rs b/src/util.rs index 5b73255..d5f2c41 100644 --- a/src/util.rs +++ b/src/util.rs @@ -59,6 +59,9 @@ pub fn get_color(color: &str) -> &'static str { "orange" => "\x1b[33m", "green" => "\x1b[32m", "grey" => "\x1b[90m", + // Bold, per the guideline in AGENTS.md. Its absence meant colorize_line + // asked for a colour that did not exist and got an empty prefix. + "magenta" => "\x1b[1;35m", _ => "", } } @@ -379,37 +382,53 @@ pub fn is_unknown_value(value: &str) -> bool { } pub fn colorize_line(line: &str) -> String { - let mut result = String::new(); + colorize_bold(line, should_use_colors()) +} + +/// Replace `**bold**` with colour, 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 { + if !enabled { + return line.to_string(); + } + let chars: Vec = line.chars().collect(); - let len = chars.len(); + let mut result = String::new(); let mut i = 0; - while i < len { - // Check for **bold** markdown - if i + 1 < len && chars[i] == '*' && chars[i + 1] == '*' { - // Find closing ** - let start = i + 2; - let mut end = start; - while end + 1 < len && !(chars[end] == '*' && chars[end + 1] == '*') { - if chars[end] == '\n' { - break; - } - end += 1; - } - - let bold_text: String = chars[start..end].iter().collect(); - result.push_str(&color_text(&bold_text, "magenta")); - i = end + 2; + while i < chars.len() { + if !is_delimiter(&chars, i) { + result.push(chars[i]); + i += 1; continue; } - result.push(chars[i]); - i += 1; + match (i + 2..chars.len()).find(|&at| is_delimiter(&chars, at)) { + Some(close) => { + let bold: String = chars[i + 2..close].iter().collect(); + result.push_str(&color_text(&bold, "magenta")); + 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; + } + } } result } +fn is_delimiter(chars: &[char], at: usize) -> bool { + chars.get(at) == Some(&'*') && chars.get(at + 1) == Some(&'*') +} + pub fn parse_ticket_field(content: &str, field: &str) -> String { let pattern = format!("**{}**:", field); content @@ -557,6 +576,42 @@ mod tests { assert!(colors_wanted(true, None, None)); } + #[test] + fn every_colour_the_code_asks_for_exists() { + for colour in ["red", "cyan", "orange", "green", "grey", "magenta"] { + assert!(!get_color(colour).is_empty(), "{colour} is not defined"); + } + } + + /// With no colour to carry the emphasis, the markers are the emphasis. + #[test] + fn bold_markers_survive_when_colour_is_off() { + let line = "Some **bold** text"; + assert_eq!(colorize_bold(line, false), line); + } + + #[test] + fn bold_markers_are_replaced_when_colour_is_on() { + let out = colorize_bold("Some **bold** text", true); + assert!(out.contains("bold"), "was {out:?}"); + assert!(!out.contains("**"), "markers left behind in {out:?}"); + } + + /// The old scan ran past the end here and dropped the last character. + #[test] + fn an_unterminated_marker_keeps_the_whole_line() { + for line in ["a **bold", "**", "***", "trailing **"] { + assert_eq!(colorize_bold(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); + assert!(out.contains("one") && out.contains("two") && out.ends_with("and rest")); + assert!(!out.contains("**"), "was {out:?}"); + } + #[test] fn short_id_shortens_a_full_uuid() { assert_eq!(short_id("c230d2c6-d78a-44a5-8f5f-88fb28f84493"), "c230d2c");