diff --git a/src/handlers.rs b/src/handlers.rs index 13724ec..80c7d34 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -294,8 +294,10 @@ pub fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> { let (_status, title, content) = resolve_ticket(search, worklane_dir)?; let ticket_uuid = parse_ticket_field(&content, "ID"); - println!("{}", title); - // In characters, so a non-ASCII title is not over-drawn. + // The only title in the output: content_trim_colored used to print it a second + // time from the file content. Green per the colour guideline, and the rule is + // measured in characters so a non-ASCII title is not over-drawn. + println!("{}", color_text(&title, "green")); println!("{}", "─".repeat(title.chars().count())); println!("\n{}", content_trim_colored(&content)); diff --git a/src/util.rs b/src/util.rs index 1796bca..6030190 100644 --- a/src/util.rs +++ b/src/util.rs @@ -253,8 +253,14 @@ pub fn content_trim_colored(content: &str) -> String { let lines: Vec<&str> = content.trim().lines().collect(); let mut result = String::new(); let mut current_section = ""; + let mut i = 0; + + // Indexed rather than a plain iteration: dropping the title means also dropping + // the blank line under it, which needs a look at what comes next. + while i < lines.len() { + let line = lines[i]; + i += 1; - for line in &lines { // Track current section if let Some(section_name) = line.strip_prefix("## ") { current_section = line; @@ -269,11 +275,14 @@ pub fn content_trim_colored(content: &str) -> String { continue; } - // First line is the title header - color it in green + // The title belongs to cmd_show, which prints it with its rule. Printing it + // again from the file content is what repeated it in every show output. if line.starts_with("# ") && current_section.is_empty() { - let header = &line[2..]; // Remove "# " - result.push_str(&color_text(header, "green")); - result.push('\n'); + // Along with the blank line the template puts under it, so dropping the + // title does not leave a gap where it used to be. + while i < lines.len() && lines[i].trim().is_empty() { + i += 1; + } continue; } @@ -841,6 +850,15 @@ mod tests { assert!(set_status_field(content, "committed", "T").is_err()); } + #[test] + fn the_body_leaves_the_title_to_cmd_show() { + let rendered = content_trim_colored("# Only Once\n\n## Description\nbody\n"); + + assert!(!rendered.contains("Only Once"), "{rendered:?}"); + // And no gap left where it was + assert!(rendered.starts_with("Description"), "{rendered:?}"); + } + #[test] fn a_section_rule_matches_the_width_of_its_name() { let rendered = content_trim_colored("# T\n\n## Description\nbody\n"); diff --git a/tests/cmd_show.rs b/tests/cmd_show.rs index def21fa..8b9d993 100644 --- a/tests/cmd_show.rs +++ b/tests/cmd_show.rs @@ -394,6 +394,35 @@ fn show_searches_across_all_statuses() { assert!(stdout.contains("This is in committed.")); } +#[test] +fn show_prints_the_title_once() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + create_ticket( + &worklane, + "backlog", + "Once Only", + "A description that does not name the title.", + "abc123def456789012345678901234567890", + "alice", + "2026-12-31", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()).arg("show").arg("Once Only"); + let stdout = get_stdout(&mut cmd); + + assert_eq!( + stdout.matches("Once Only").count(), + 1, + "title repeated in {stdout:?}" + ); + // The rule under it is still the width of the title + assert!(stdout.contains("Once Only\n─────────\n"), "{stdout:?}"); +} + #[test] fn show_renders_the_priority_mark_beside_the_number() { let temp = TempDir::new().unwrap();