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) <noreply@anthropic.com>
91 lines
3.6 KiB
Rust
91 lines
3.6 KiB
Rust
//! 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");
|
|
}
|
|
}
|