diff --git a/src/tui.rs b/src/tui.rs index e1d9469..db73fa3 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -30,6 +30,19 @@ pub struct Column { pub tickets: Vec, } +/// What the detail view holds while it is open: the ticket it belongs to, then its +/// body and comments as one plain text. +/// +/// The title travels beside the text rather than inside it, because it does not +/// scroll: it names the overlay from the border, where no amount of reading can +/// carry it off the screen. +#[derive(Debug)] +pub struct Detail { + pub title: String, + pub id: String, + pub text: String, +} + /// What is on screen. pub struct Board { pub columns: Vec, @@ -38,8 +51,8 @@ pub struct Board { /// First visible line of each column, counted in card lines. Parallel to /// `columns`, so a tick that reshapes the board resizes it in step. pub offsets: Vec, - /// The body and comments of the selected ticket, when the detail view is open. - pub detail: Option, + /// The open detail view, when one is open. + pub detail: Option, /// First visible line of the detail view, counted in wrapped lines. pub offset: u16, /// Whether the done column is drawn. Off at startup, and not remembered between @@ -274,7 +287,7 @@ impl Board { let text = detail_text_area(Rect::new(0, 0, screen.width, screen.height)); // A screenful less a line, so the eye has something to land on after the jump. let step = text.height.saturating_sub(1).max(1) as isize; - let last = last_offset(detail, text) as isize; + let last = last_offset(&detail.text, text) as isize; self.offset = (self.offset as isize + pages.saturating_mul(step)).clamp(0, last) as u16; } @@ -289,11 +302,12 @@ impl Board { } } -/// The whole ticket as plain text: the body as `show` renders it, then the comments. +/// The ticket as the detail view holds it: its name and short id, then the body as +/// `show` renders it and the comments. /// /// Plain rather than coloured, since ratatui draws styled spans and an escape sequence /// in a paragraph would be printed as literal bytes. -fn detail_of(ticket: &TicketItem) -> String { +fn detail_of(ticket: &TicketItem) -> Detail { let content = std::fs::read_to_string(&ticket.path).unwrap_or_default(); let mut out = content_trim(&content, false); @@ -307,7 +321,11 @@ fn detail_of(ticket: &TicketItem) -> String { out.push('\n'); out.push_str(&format_comments_with(&comments, false)); - out + Detail { + title: ticket.title.clone(), + id: ticket.id.clone(), + text: out, + } } /// A fingerprint of every ticket and comment file: name, length and modified time. @@ -581,24 +599,24 @@ fn last_offset(detail: &str, text: Rect) -> u16 { .saturating_sub(text.height) } -fn draw_detail(frame: &mut Frame, detail: &str, offset: u16, area: Rect) { +fn draw_detail(frame: &mut Frame, detail: &Detail, offset: u16, area: Rect) { let overlay = detail_overlay(area); // Clamped here as well as on the keypress: the terminal may have been resized since, // and a ticket read to its end can grow shorter under a tick. - let offset = offset.min(last_offset(detail, detail_text_area(area))); + let offset = offset.min(last_offset(&detail.text, detail_text_area(area))); let block = Block::default() .borders(Borders::ALL) .border_style(Style::default().fg(Color::Cyan.into())) .title(Span::styled( - " ticket ", + detail_title(detail, overlay.width), Style::default().fg(Color::Orange.into()), )); frame.render_widget(Clear, overlay); frame.render_widget( - Paragraph::new(detail.to_string()) + Paragraph::new(detail.text.to_string()) .block(block) .wrap(Wrap { trim: false }) .scroll((offset, 0)), @@ -606,6 +624,35 @@ fn draw_detail(frame: &mut Frame, detail: &str, offset: u16, area: Rect) { ); } +/// The overlay's title: the short id and the name of the ticket it has open. +/// +/// The id goes first because that is how every other view addresses a ticket, and the +/// name after it because that is what the id is shorthand for. Both sit on the border +/// rather than at the head of the paragraph, so reading to the end of a long ticket +/// cannot carry them off the screen. +/// +/// The string is cut here rather than left to the renderer, which truncates an +/// overlong title at whatever cell it reaches and would take the id with it. Cut +/// against the inner width — a border column on either side is not the title's to +/// write on — the id survives whole and the name ends in an ellipsis where it was +/// trimmed. On an overlay too narrow for even the id, what fits is what shows. +fn detail_title(detail: &Detail, overlay_width: u16) -> String { + let room = overlay_width.saturating_sub(2) as usize; + let full = format!(" ({}) · {} ", detail.id, detail.title); + if full.chars().count() <= room { + return full; + } + + let prefix = format!(" ({}) · ", detail.id); + match room.checked_sub(prefix.chars().count() + 1) { + Some(keep) if keep > 0 => { + let name: String = detail.title.chars().take(keep).collect(); + format!("{prefix}{name}…") + } + _ => full.chars().take(room).collect(), + } +} + #[cfg(test)] mod tests { use super::*; @@ -680,6 +727,18 @@ mod tests { render(board, width, height).pop().expect("no rows rendered") } + /// The overlay's top border row, where the block title sits. + /// + /// Counted in characters rather than bytes: the rows carry multi-byte glyphs. + fn overlay_title_row(lines: &[String], width: u16, height: u16) -> String { + let overlay = detail_overlay(Rect::new(0, 0, width, height)); + lines[overlay.y as usize] + .chars() + .skip(overlay.x as usize) + .take(overlay.width as usize) + .collect() + } + #[test] fn the_board_draws_the_four_statuses_as_columns_once_done_is_shown() { let dir = board_dir(); @@ -1229,20 +1288,98 @@ mod tests { assert!(board.detail.is_none()); board.toggle_detail(); - let detail = board.detail.clone().expect("no detail"); + let detail = board.detail.as_ref().expect("no detail"); - assert!(detail.contains("A description."), "{detail}"); - assert!(detail.contains("Crit one"), "{detail}"); - assert!(detail.contains("A note from elsewhere."), "{detail}"); - assert!(detail.contains("bob"), "{detail}"); + assert!(detail.text.contains("A description."), "{}", detail.text); + assert!(detail.text.contains("Crit one"), "{}", detail.text); + assert!(detail.text.contains("A note from elsewhere."), "{}", detail.text); + assert!(detail.text.contains("bob"), "{}", detail.text); // Plain text, since ratatui would print an escape as literal bytes - assert!(!detail.contains('\x1b'), "escapes reached the detail view: {detail:?}"); + assert!( + !detail.text.contains('\x1b'), + "escapes reached the detail view: {:?}", + detail.text + ); board.toggle_detail(); assert!(board.detail.is_none()); } + /// The reported bug: the modal showed a description, criteria and a metadata + /// table belonging to a ticket it never named. + #[test] + fn the_detail_view_names_the_ticket_it_has_open() { + let dir = board_dir(); + write_ticket(&dir, "backlog", "11111111-0000-4000-8000-000000000000", "First one", ""); + + let mut board = Board::new(&dir).unwrap(); + board.toggle_detail(); + + let screen = render(&board, SCREEN.width, SCREEN.height); + let row = overlay_title_row(&screen, SCREEN.width, SCREEN.height); + assert!(row.contains("(1111111)"), "no short id on the border:\n{row}"); + assert!(row.contains("First one"), "no title on the border:\n{row}"); + + // The heading stays out of the text: `show` prints it and here the border + // carries it, so it is on screen once rather than twice. + assert!( + !board.detail.as_ref().unwrap().text.contains("First one"), + "{:?}", + board.detail + ); + } + + /// Scrolled to the end of a long ticket, the name is still where it started. + #[test] + fn the_name_stays_on_screen_at_the_end_of_a_long_ticket() { + let dir = board_dir(); + write_ticket( + &dir, + "backlog", + "11111111-0000-4000-8000-000000000000", + "First one", + &long_section(60), + ); + + let mut board = Board::new(&dir).unwrap(); + board.toggle_detail(); + for _ in 0..20 { + board.scroll_detail(1, SCREEN); + } + + let screen = render(&board, SCREEN.width, SCREEN.height); + assert!(screen.join("\n").contains("Line 060"), "nothing scrolled:\n{}", screen.join("\n")); + + let row = overlay_title_row(&screen, SCREEN.width, SCREEN.height); + assert!(row.contains("(1111111)"), "the id scrolled away:\n{row}"); + assert!(row.contains("First one"), "the title scrolled away:\n{row}"); + } + + /// A title longer than the overlay is cut to the border — corners intact, id + /// whole, ellipsis where the name gave up — not overrun and not dropped whole. + #[test] + fn a_long_title_is_cut_to_the_border_it_sits_on() { + let dir = board_dir(); + // Long enough to pass the overlay's inner width many times over, short enough + // that its slug still fits a filename. + let title = format!("Head {} tailmarker", "filler ".repeat(20)); + write_ticket(&dir, "backlog", "11111111-0000-4000-8000-000000000000", &title, ""); + + let mut board = Board::new(&dir).unwrap(); + board.toggle_detail(); + + let screen = render(&board, SCREEN.width, SCREEN.height); + let row = overlay_title_row(&screen, SCREEN.width, SCREEN.height); + + assert!(row.starts_with('┌'), "the left corner gave way:\n{row}"); + assert!(row.ends_with('┐'), "the right corner gave way:\n{row}"); + assert!(row.contains("(1111111)"), "the id did not survive the cut:\n{row}"); + assert!(row.contains("Head"), "the border was left blank:\n{row}"); + assert!(row.contains('…'), "the cut read as a complete title:\n{row}"); + assert!(!row.contains("tailmarker"), "the title overran the border:\n{row}"); + } + /// A comment added while the detail view is open shows up on the next tick. #[test] fn a_tick_picks_up_a_comment_added_under_an_open_detail_view() { @@ -1252,7 +1389,7 @@ mod tests { let mut board = Board::new(&dir).unwrap(); board.toggle_detail(); - assert!(!board.detail.clone().unwrap().contains("A late note.")); + assert!(!board.detail.as_ref().unwrap().text.contains("A late note.")); let comments = dir.join("comments").join(uuid); std::fs::create_dir_all(&comments).unwrap(); @@ -1263,7 +1400,11 @@ mod tests { .unwrap(); assert!(board.tick().unwrap(), "the tick saw no change"); - assert!(board.detail.clone().unwrap().contains("A late note."), "{:?}", board.detail); + assert!( + board.detail.as_ref().unwrap().text.contains("A late note."), + "{:?}", + board.detail + ); } #[test] @@ -1384,13 +1525,21 @@ mod tests { let mut board = Board::new(&dir).unwrap(); board.toggle_detail(); - // By id rather than title: the body leaves the title to the caller that prints - // the heading, so the detail text does not carry it. - assert!(board.detail.clone().unwrap().contains("11111111"), "{:?}", board.detail); + // By id in the text: the heading is stripped from the body, and the title + // travels beside it for the block title instead. + assert!( + board.detail.as_ref().unwrap().text.contains("11111111"), + "{:?}", + board.detail + ); board.select_card(1, SCREEN); assert_eq!(board.selected().map(|t| t.title.clone()), Some("Beta".to_string())); - assert!(board.detail.clone().unwrap().contains("22222222"), "{:?}", board.detail); + assert!( + board.detail.as_ref().unwrap().text.contains("22222222"), + "{:?}", + board.detail + ); } /// A newly selected ticket starts at its own top, not where the last one was left.