From 0ce3fdb246c3bebbe1de004f1977eab30dbc2248 Mon Sep 17 00:00:00 2001 From: Ansho Rei Date: Sat, 22 Aug 2026 14:36:09 +0200 Subject: [PATCH] (0546fc5) Copy the selected id with y in the tui, and bump to 0.13.7 The board is where a ticket is found and the CLI is where it is acted on, and the only thing joining them was seven characters retyped off the screen. y reads as yank to the readers a keyboard board is for, and was the letter left after q, esc, enter, space, hjkl, the arrows, pgup, pgdn, tab, backtab, d and / took theirs. The mechanism was chosen against ssh, which is where a board is often read. OSC 52 crosses the connection, because it is the terminal at the reader's own end that owns the clipboard. A crate wants X11 and Wayland libraries at build time and, on X11, hands the selection to the process that set it: copy an id, quit, and the paste buffer is empty, which is precisely the flow this key exists for. Shelling out to wl-copy, xclip or pbcopy is platform sniffing plus a spawn, and does nothing where none of them is installed. crossterm 0.28 is what ratatui 0.29 pins and ships no clipboard command, so the sequence and its base64 are twenty lines in src/clipboard.rs, pinned on the RFC 4648 vectors. The cost is recorded rather than hidden, in AGENTS.md under The Copy Key: a terminal that ignores OSC 52 ignores it silently, and tmux needs set-clipboard on. What clipboard::copy reports is whether the sequence was written, and the footer says no more than that. There is no reply to read either, since the query form is refused by most emulators. What is copied is ticket.id, the seven characters the card already prints and every command already resolves, decided by Board::copy_text so the string is a question the suite can ask without a clipboard. None on an empty column: an empty string would take away whatever the reader had there and give nothing back. The project is left off even on the merge tab, since a second word would have to be deleted out of every paste. Board::copy_selected_id takes the clipboard as an argument, clipboard::copy from the dispatch and a closure from a test, which is what makes the hit and the refusal both drivable. A failure is said in the footer rather than returned: there is nobody above it to handle one, and an error swallowed there would look exactly like a copy that worked until the paste came out wrong. Notice is the board's first channel for saying something in words, and it has to be, because a copy leaves nothing on screen to look at. failed means nothing reached the clipboard, so an empty column and a refused write are one answer to the reader, both in brightred like the search's no match. It takes the whole footer row, since a message squeezed in beside the keys would move every key in it. Cleared by the next keypress rather than on a timer: no clock on the board, no wakeup in the loop, and never in tick, which runs four times a second. clear_notice goes ahead of search_key as well as the arms, because a key typed into a query is still a key. Co-Authored-By: Claude Opus 5 (1M context) --- AGENTS.md | 15 +++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- src/clipboard.rs | 90 ++++++++++++++++ src/main.rs | 12 +++ src/tui.rs | 273 ++++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 391 insertions(+), 5 deletions(-) create mode 100644 src/clipboard.rs diff --git a/AGENTS.md b/AGENTS.md index dd80021..5927bc4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -7,6 +7,7 @@ - `src/util.rs` - the ticket format, resolution, rendering and the palette - `src/campaign.rs` - the campaign file format, its resolution, its index and its members - `src/tui.rs` - board state, the change digest, and the ratatui draw +- `src/clipboard.rs` - the OSC 52 write and the base64 it carries, the board's only way out to the clipboard - `src/install.rs` - the embedded agent assets and the compare-and-refuse install - `assets/SKILL.md` - the agent skill for the CLI itself, embedded with `include_str!` - `assets/REVIEW.md` - the agent skill for reviewing a finished ticket, shipped the same way. Assets are named after the role they teach; `SKILL.md` keeps its name because the role it teaches is the CLI @@ -177,6 +178,20 @@ and this file is for what someone changing worklane's own code needs. - Both closed footers name `/`, since the jump works with a ticket open as well: it moves the selection and the overlay follows - Out of scope on purpose, each its own ticket once the input mode exists: `n` and `N` for the next and previous match, searching the description, tags or owner, regex, and filtering the board down to matches rather than selecting one +### The Copy Key +- **`y` copies the selected card's short id, over OSC 52 written by hand.** The board is where a ticket is found and the CLI is where it is acted on, and the only thing joining them was seven characters retyped off the screen. `y` reads as yank to the readers a keyboard board is for, and was the letter left: `q`, `esc`, `enter`, `space`, `hjkl`, the arrows, `pgup`, `pgdn`, `tab`, `backtab`, `d` and `/` were taken +- **The mechanism was chosen against ssh**, which is where a board is often read. An escape sequence is the only one of the three ways that crosses the connection, since it is the terminal at the reader's end that owns the clipboard. A crate (`arboard` and its kind) wants X11 and Wayland libraries at build time, and on X11 the selection belongs to the process that set it: copy an id, quit, and the paste buffer is empty, which is precisely this flow. Shelling out to `wl-copy`, `xclip` or `pbcopy` is platform sniffing plus a process spawn, and does nothing on a machine where none of them is installed or on the far end of a connection. crossterm 0.28 is what ratatui 0.29 pins and it ships no clipboard command, and the backend types have to match whatever ratatui pins, so a newer crossterm cannot be picked up on its own +- **The cost is paid knowingly: a terminal that ignores OSC 52 ignores it silently**, and tmux needs `set-clipboard on`. What `clipboard::copy` reports is whether the sequence was written, not whether it was honoured, and the footer message says no more than that. There is no reply to read: the query form of OSC 52 is refused by most emulators as a security matter, so nothing better is available to us +- Base64 is twenty lines here rather than a dependency, because the only thing that goes through it is a seven-character id. Pinned on the RFC 4648 vectors, which cover both padding forms, since a wrong encoder would put plausible-looking rubbish on the clipboard +- **`c` and not `p`**: the clipboard proper, not the X11 primary selection, since this is pasted into a command line. Terminated with BEL, which is what tmux documents and what every emulator that implements this at all accepts +- **What is copied is `ticket.id`, the seven characters the card prints**, not the full uuid: they are what stops the retyping and what `show`, `mv` and `comment` all resolve. Decided by `Board::copy_text` rather than inside the arm that copies, so the string is a question the suite can ask without a clipboard. `None` on an empty column, since an empty string would take away whatever the reader had there and give nothing back +- The project is left off even on the merge tab, where the id alone is not an address. This goes onto a clipboard to be pasted into a command line, and a second word in it would have to be deleted every time; running the command in the wrong project fails to resolve rather than acting on the wrong ticket +- **The clipboard is handed to `Board::copy_selected_id` as an argument**, `clipboard::copy` from the dispatch and a closure from a test. That is what makes both answers drivable, and it keeps the terminal write out of the board state +- **A failure is said in the footer, not returned.** There is nobody above it to handle one: the board goes on being drawn, and an error swallowed there would look exactly like a copy that worked until the paste came out wrong +- **`Notice` is the board's one channel for saying something in words**, since a copy leaves nothing on screen to look at. `failed` means "nothing reached the clipboard" rather than "an error occurred", so an empty column and a refused write are one answer to the reader, both in brightred for the reason `no match` is +- **It takes the whole footer row**, on a weaker reason than the prompt's: every key still works while a notice is up. It is that a message squeezed in beside the key list would move every key in it, and the row is the only part of the board that is not a ticket. Drawn after the prompt, which is an input and cannot be covered +- **The notice is cleared by the next keypress, not by a timer.** No clock on the board and no wakeup in the loop, and the moment the reader does anything else the message has been read or was never going to be. Doing nothing leaves it up, which is the one state where it is still worth reading. `Board::clear_notice` runs ahead of `search_key` as well as the arms, since a key typed into a query is still a key, and it must never move into `tick`: the board redraws four times a second and a notice cleared there would never be read + ### Monitoring Several Boards - `worklane tui ...` takes any number of paths, each resolved through `find_worklane_dir` the way the working directory is: the first `.worklane` at or above it. No path is the working directory, so a bare `worklane` and `worklane tui` are unchanged - The failure **names the path** that had no board, since with several arguments "No .worklane directory found" does not say which one was wrong. Only when a path was given: a bare `worklane` has no path to name and keeps the message every other command gives, which is what its integration test asserts diff --git a/Cargo.lock b/Cargo.lock index aea2e2f..4c6bc27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1494,7 +1494,7 @@ dependencies = [ [[package]] name = "worklane" -version = "0.13.6" +version = "0.13.7" dependencies = [ "anyhow", "assert_cmd", diff --git a/Cargo.toml b/Cargo.toml index 1053794..0cc4e03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "worklane" -version = "0.13.6" +version = "0.13.7" edition = "2024" [dependencies] diff --git a/README.md b/README.md index 80c4983..8bd93c0 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Worklane is a local Kanban flow for agentic software development. ## Commands | Command | Description | |---------|-------------| -| `worklane` or `worklane tui [path...]` | Opens the board: four status columns, redrawn when the tree changes. Read only. Opens on backlog, committed and reviewing; `d` shows or hides done, whose count stays in the footer either way. Arrows or `hjkl` to move, `enter` for detail, `pgup`/`pgdn` to scroll an open ticket, `q` to quit. Each path opens the first `.worklane` at or above it, the working directory by default. Several paths are monitored together, as a tab per project plus an `all` tab that merges them, with `tab` and `shift+tab` between them | +| `worklane` or `worklane tui [path...]` | Opens the board: four status columns, redrawn when the tree changes. Read only. Opens on backlog, committed and reviewing; `d` shows or hides done, whose count stays in the footer either way. Arrows or `hjkl` to move, `enter` for detail, `pgup`/`pgdn` to scroll an open ticket, `/` to jump to the first ticket whose id or title matches, `y` to copy the selected ticket's short id to the clipboard, `q` to quit. Each path opens the first `.worklane` at or above it, the working directory by default. Several paths are monitored together, as a tab per project plus an `all` tab that merges them, with `tab` and `shift+tab` between them | | `worklane init` | Creates `.worklane/` with backlog, committed, reviewing, done directories | | `worklane add` | Creates a ticket in backlog with `--title`, `--description`, `--acceptance`, `--plan`, `--tags`, `--due`, `--owner`, `--priority` flags. `--due` takes `YYYY-MM-DD` and refuses anything else at parse time | | `worklane ls [status]` | Lists tickets, sorted by priority then title. Optional positional status, and `--priority <1-5>` | diff --git a/src/clipboard.rs b/src/clipboard.rs new file mode 100644 index 0000000..7942579 --- /dev/null +++ b/src/clipboard.rs @@ -0,0 +1,90 @@ +//! Putting text on the clipboard of whatever terminal is drawing the board. +//! +//! OSC 52, written by hand, and the reasoning is in AGENTS.md under The Copy Key. The +//! short of it: the board is often being read over ssh, and an escape sequence is the +//! only one of the three ways that crosses the connection. crossterm 0.28 is what +//! ratatui 0.29 pins and it ships no clipboard command, so the sequence and the base64 +//! it carries are written here rather than pulled in. +//! +//! The honest limit is that a terminal which ignores OSC 52 ignores it silently: what +//! this reports is whether the sequence was written, not whether it was honoured. + +use std::io::Write; + +use anyhow::{Context, Result}; + +/// Ask the terminal to put `text` on the system clipboard. +/// +/// `52;c` is the clipboard proper rather than `p`, the X11 primary selection, because +/// this is for pasting into a command line with ctrl-shift-v or cmd-v. Terminated with +/// BEL rather than ST: both are accepted, and BEL is what tmux's `set-clipboard` +/// documents and what every emulator that implements this at all takes. +/// +/// Written straight to stdout, which is where ratatui's own frames go, and flushed +/// before returning so a failure belongs to this call rather than to the next draw. +pub fn copy(text: &str) -> Result<()> { + let mut out = std::io::stdout(); + write!(out, "\x1b]52;c;{}\x07", base64(text.as_bytes())) + .and_then(|()| out.flush()) + .context("could not write to the terminal") +} + +/// Standard base64, the alphabet OSC 52 requires, padding included. +/// +/// Twenty lines against a dependency: the only thing encoded here is a seven-character +/// id, and the crate would be carried for its own sake. +fn base64(bytes: &[u8]) -> String { + const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4); + for chunk in bytes.chunks(3) { + // The missing bytes of a short chunk read as zero, and the characters they + // would have produced are the ones that become padding below. + let triple = chunk + .iter() + .chain([0u8; 2].iter()) + .take(3) + .fold(0u32, |acc, byte| (acc << 8) | u32::from(*byte)); + + for slot in 0..4 { + match slot <= chunk.len() { + true => out.push(char::from(ALPHABET[(triple >> (18 - 6 * slot) & 0x3f) as usize])), + false => out.push('='), + } + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The vectors from RFC 4648, which is what a terminal decodes with. Every chunk + /// length is in here, so the padding is covered in both of its forms. + #[test] + fn base64_encodes_the_rfc_vectors() { + assert_eq!(base64(b""), ""); + assert_eq!(base64(b"f"), "Zg=="); + assert_eq!(base64(b"fo"), "Zm8="); + assert_eq!(base64(b"foo"), "Zm9v"); + assert_eq!(base64(b"foob"), "Zm9vYg=="); + assert_eq!(base64(b"fooba"), "Zm9vYmE="); + assert_eq!(base64(b"foobar"), "Zm9vYmFy"); + } + + /// What actually goes on the wire for a short id, which is the whole use. + #[test] + fn base64_encodes_a_short_id() { + assert_eq!(base64(b"0546fc5"), "MDU0NmZjNQ=="); + } + + /// The high bits reach the end of the alphabet, which an off-by-one in the shift + /// would miss entirely on ascii input. + #[test] + fn base64_reaches_the_whole_alphabet() { + assert_eq!(base64(&[0xff, 0xff, 0xff]), "////"); + assert_eq!(base64(&[0xfb, 0xff, 0xfe]), "+//+"); + assert_eq!(base64(&[0x00, 0x00, 0x00]), "AAAA"); + } +} diff --git a/src/main.rs b/src/main.rs index 3e4d234..2ff3888 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use anyhow::Result; use clap::Parser; mod campaign; +mod clipboard; mod command; mod handlers; mod install; @@ -163,6 +164,13 @@ fn run_tui(paths: &[std::path::PathBuf], start: &std::path::Path) -> Result<()> continue; } + // Every keypress takes away what the last one said, so a notice never + // outlives the moment it belongs to. Ahead of the prompt as well as the + // arms: a key typed into a query is still a key. The rule is + // `Board::clear_notice`, where a test can reach it, and the arm below that + // sets one runs after this. + board.clear_notice(); + // Ahead of the arms, because a prompt has to swallow every key it is // given: typing `d` into a query must not toggle the done column and `q` // must not quit. It reports what it took, so the arms below stay a plain @@ -206,6 +214,10 @@ fn run_tui(paths: &[std::path::PathBuf], start: &std::path::Path) -> Result<()> // typed into an open prompt is taken there as a character, so this arm // is only ever the way in. KeyCode::Char('/') => board.open_search(), + // `y` as in yank, for the readers this board is for, and the only letter + // left that a vim hand would reach for. The clipboard is handed in rather + // than reached for inside `Board`, so the suite can drive both answers. + KeyCode::Char('y') => board.copy_selected_id(clipboard::copy), _ => {} } } diff --git a/src/tui.rs b/src/tui.rs index 2d4c0ef..538bd07 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -218,6 +218,37 @@ pub struct Search { pub missed: bool, } +/// A line the footer carries in place of the key list until the next keypress. +/// +/// The board otherwise says everything by drawing it, and a copy leaves nothing on the +/// screen to look at: the id was already there and the clipboard is somewhere else. So +/// this is the one thing the board has to say in words. +/// +/// `failed` is "nothing reached the clipboard", not "an error occurred": an empty column +/// and a terminal that refused the write are the same answer to the reader, who pressed +/// a key and has nothing to paste. Both are drawn in red for the reason `no match` is. +#[derive(Debug)] +pub struct Notice { + pub text: String, + pub failed: bool, +} + +impl Notice { + fn done(text: impl Into) -> Self { + Notice { + text: text.into(), + failed: false, + } + } + + fn failed(text: impl Into) -> Self { + Notice { + text: text.into(), + failed: true, + } + } +} + /// What is on screen. pub struct Board { /// The monitored boards as tabs, the merge first when there is more than one. @@ -230,6 +261,8 @@ pub struct Board { pub offset: u16, /// The search prompt while it is open. `None` is the board reading keys as actions. pub search: Option, + /// What the last action said, until the next keypress takes it away. + pub notice: Option, /// Whether the done column is drawn. Off at startup, and not remembered between /// runs: worklane has no config file, and one flag is not a reason to start one. /// Board-wide rather than per tab: it is a preference about the flow, not about a @@ -266,6 +299,7 @@ impl Board { detail: None, offset: 0, search: None, + notice: None, show_done: false, should_quit: false, projects, @@ -599,6 +633,59 @@ impl Board { } } + /// The string the copy key puts on the clipboard: the selected card's short id. + /// + /// A method here rather than a line inside the arm that copies, so what gets copied + /// is a question a test can ask without a clipboard or a terminal to answer it. + /// + /// The seven characters the card already prints and every command already takes, + /// `ticket.id` rather than the full uuid: the point of the key is to stop those seven + /// being retyped, and a `worklane show` resolves them. The project is left off on the + /// merge tab even though the id alone is not an address there. This goes on a + /// clipboard to be pasted into a command line, and a second word in it would have to + /// be deleted every time. + /// + /// `None` on an empty column, so nothing is sent rather than an empty string put on + /// the clipboard over whatever the reader had there. + pub fn copy_text(&self) -> Option { + self.selected().map(|card| card.ticket.id.clone()) + } + + /// Copy the selected id through `copy`, and say what happened. + /// + /// The clipboard comes in as an argument rather than being reached for here, so the + /// suite can drive both answers: `crate::clipboard::copy` in the dispatch, a closure + /// in a test. Nothing else about the board changes, since a copy is not a move. + /// + /// A failure is said in the footer rather than returned. There is nobody above this + /// to handle it: the board goes on being drawn, and an error swallowed here would + /// look exactly like a copy that worked until the paste came out wrong. + pub fn copy_selected_id(&mut self, copy: impl FnOnce(&str) -> Result<()>) { + let Some(text) = self.copy_text() else { + self.notice = Some(Notice::failed("nothing to copy")); + return; + }; + + self.notice = Some(match copy(&text) { + Ok(()) => Notice::done(format!("copied {text}")), + Err(err) => Notice::failed(format!("copy failed: {err}")), + }); + } + + /// Forget the notice, which is what every keypress does before it is dispatched. + /// + /// A keypress rather than a timer: it needs no clock on the board and no wakeup in + /// the loop, and the moment the reader does anything else the message has been read + /// or was never going to be. Doing nothing leaves it up, which is the one state where + /// it is still worth reading. + /// + /// Here rather than in the loop for the reason `dismiss` is: the dispatch in + /// `main.rs` is not reachable from a test, so a rule written into it is a rule + /// nothing can check. + pub fn clear_notice(&mut self) { + self.notice = None; + } + /// Scroll the open detail view by whole screenfuls, one line kept for overlap. /// /// Up and down move the selection, so the scroll is on PageUp and PageDown. That is @@ -1357,6 +1444,16 @@ fn draw_footer(frame: &mut Frame, board: &Board, area: Rect) { return; } + // And a notice takes it for the one keypress it lives, for a weaker reason than the + // prompt's: every key still works while it is up. It is that a message squeezed in + // beside the list would move every key in it, and the row is the only place on the + // board that is not a ticket. Ordered after the prompt, which is an input and cannot + // be covered, and a keypress clears the notice before it can open one anyway. + if let Some(notice) = &board.notice { + draw_notice(frame, notice, area); + return; + } + // While done is hidden its count goes here, which is the board's version of the // summary line `ls` prints above a listing that leaves done out. let done = match board.show_done { @@ -1376,10 +1473,10 @@ fn draw_footer(frame: &mut Frame, board: &Board, area: Rect) { // would be the fault 1b78a24 was, a footer describing a key by what it used to do. let keys = match board.detail { Some(_) => format!( - " {tabs}←→↑↓ select pgup/pgdn scroll / search enter/q close {done} ctrl-c quit " + " {tabs}←→↑↓ select pgup/pgdn scroll / search y copy id enter/q close {done} ctrl-c quit " ), None => format!( - " {tabs}←→ column ↑↓ card / search enter detail {done} q quit " + " {tabs}←→ column ↑↓ card / search y copy id enter detail {done} q quit " ), }; @@ -1389,6 +1486,23 @@ fn draw_footer(frame: &mut Frame, board: &Board, area: Rect) { ); } +/// What the last keypress did, where the keys would otherwise be. +/// +/// Red when nothing reached the clipboard, so a copy that did not happen does not read +/// like one that did, and the default foreground when it did: the same division the +/// prompt draws its query and its miss with. +fn draw_notice(frame: &mut Frame, notice: &Notice, area: Rect) { + let style = match notice.failed { + true => Style::default().fg(Color::BrightRed.into()), + false => Style::default(), + }; + + frame.render_widget( + Paragraph::new(Span::styled(format!(" {}", notice.text), style)), + area, + ); +} + /// The prompt, in place of the key list it would otherwise describe wrongly. /// /// The real terminal cursor goes after the query rather than a glyph standing in for @@ -3264,6 +3378,161 @@ mod tests { ); } + // ------------------------------------------------------------------ the copy key -- + + /// A sink that records what it was handed, standing in for the clipboard. + fn recorder(taken: &mut Option) -> impl FnOnce(&str) -> Result<()> + '_ { + move |text: &str| { + *taken = Some(text.to_string()); + Ok(()) + } + } + + /// What the card prints and what every command takes: the seven characters, so the + /// point of the key is that they are not retyped. + #[test] + fn the_copy_key_puts_the_selected_short_id_on_the_clipboard() { + let dir = searchable(); + let mut board = one_board(&dir); + + assert_eq!(board.copy_text().as_deref(), Some("1111111")); + + let mut taken = None; + board.copy_selected_id(recorder(&mut taken)); + assert_eq!(taken.as_deref(), Some("1111111")); + + // Said in words, because a copy leaves nothing on the screen to look at. + let row = footer(&board, 120, 20); + assert!(row.contains("copied 1111111"), "the copy was silent: {row}"); + + // The selection follows the copy: it is the card that was copied from. + let mut taken = None; + board.select_card(1, SCREEN); + board.copy_selected_id(recorder(&mut taken)); + assert_eq!(taken.as_deref(), Some("2222222")); + } + + /// The reader who has a ticket open is exactly the one who has decided to act on it, + /// so the key has to work there too, and the overlay stays where it was. + #[test] + fn the_copy_key_works_with_the_detail_overlay_open() { + let dir = searchable(); + let mut board = one_board(&dir); + board.toggle_detail(); + + let mut taken = None; + board.copy_selected_id(recorder(&mut taken)); + + assert_eq!(taken.as_deref(), Some("1111111")); + assert!(board.detail.is_some(), "the copy closed the ticket it copied from"); + } + + /// Nothing selected is nothing to copy, and above all not an empty string: that would + /// take away whatever the reader had on the clipboard and give nothing back. + #[test] + fn an_empty_column_copies_nothing_at_all() { + let dir = searchable(); + let mut board = one_board(&dir); + // reviewing, which `searchable` leaves empty. + board.select_column(2, SCREEN); + assert!(board.selected().is_none(), "the column was not empty"); + assert_eq!(board.copy_text(), None); + + let mut taken = None; + board.copy_selected_id(recorder(&mut taken)); + assert_eq!(taken, None, "an empty column reached the clipboard"); + + // Said rather than ignored, and in red: the reader pressed a key and has nothing + // to paste, which is the same answer a failed write gives. + let notice = board.notice.as_ref().expect("nothing was said"); + assert_eq!(notice.text, "nothing to copy"); + assert!(notice.failed, "a copy that did not happen read as one that did"); + let row = footer(&board, 120, 20); + assert!(row.contains("nothing to copy"), "{row}"); + } + + /// A copy that did not happen must not read like one that did, or the reader pastes + /// whatever was on the clipboard before and acts on the wrong ticket. + #[test] + fn a_clipboard_that_refuses_the_write_says_so() { + let dir = searchable(); + let mut board = one_board(&dir); + + board.copy_selected_id(|_| Err(anyhow::anyhow!("could not write to the terminal"))); + + let notice = board.notice.as_ref().expect("the failure was swallowed"); + assert!(notice.failed, "a failed copy was reported as a success"); + let row = footer(&board, 120, 20); + assert!(row.contains("copy failed"), "{row}"); + // The reason travels with it: a bare "failed" leaves nothing to act on. + assert!(row.contains("could not write to the terminal"), "{row}"); + assert!(!row.contains("copied 1111111"), "both answers were drawn: {row}"); + } + + /// The message belongs to the keypress that made it. It survives the tick, since the + /// board redraws four times a second and a notice cleared there would never be read, + /// and it goes the moment the reader does anything else. + #[test] + fn the_notice_outlives_the_tick_and_not_the_next_keypress() { + let dir = searchable(); + let mut board = one_board(&dir); + + let mut taken = None; + board.copy_selected_id(recorder(&mut taken)); + board.tick().unwrap(); + assert!(board.notice.is_some(), "the tick took the message away"); + assert!(footer(&board, 120, 20).contains("copied"), "the message was not drawn"); + + board.clear_notice(); + assert!(board.notice.is_none(), "the message outlived its keypress"); + // And the keys are back, rather than the row staying spent on a read message. + let row = footer(&board, 120, 20); + assert!(row.contains("q quit"), "the key list did not come back: {row}"); + } + + /// Both footers name it, since the key works with a ticket open as well. + #[test] + fn both_footers_name_the_copy_key() { + let dir = searchable(); + let mut board = one_board(&dir); + + assert!(footer(&board, 120, 20).contains("y copy id")); + board.toggle_detail(); + assert!(footer(&board, 120, 20).contains("y copy id")); + } + + /// The rules are `copy_selected_id` and `clear_notice`, which the tests above reach. + /// What is left is the dispatch, which none of them can: that `y` is bound at all, + /// that it is handed the real clipboard, and that the clearing runs ahead of every + /// key rather than only ahead of the arms. + /// + /// Scanned over whitespace-collapsed source, the way the other two dispatch tests + /// scan, so rustfmt cannot fail it. + #[test] + fn the_dispatch_binds_y_to_the_clipboard_and_clears_the_notice_on_every_key() { + let source = include_str!("main.rs") + .split_whitespace() + .collect::>() + .join(" "); + + assert!( + source.contains("KeyCode::Char('y') => board.copy_selected_id(clipboard::copy),"), + "`y` has to reach `Board::copy_selected_id` with the real clipboard: the \ + argument is what the suite replaces, so the dispatch is the only place \ + the OSC 52 write is named" + ); + + let cleared = source.find("board.clear_notice();").expect( + "a notice that nothing clears sits in the footer for the rest of the session", + ); + let prompt = source.find("if board.search_key(key, screen)").unwrap(); + assert!( + cleared < prompt, + "the notice has to be cleared ahead of the prompt as well as the arms: a \ + key typed into a query is still a key" + ); + } + // ------------------------------------------------------- several boards at once -- /// A project tree: a directory holding a `.worklane`, which is what a path argument