334 lines
16 KiB
Rust
334 lines
16 KiB
Rust
use std::path::Path;
|
|
|
|
use anyhow::Result;
|
|
use clap::Parser;
|
|
|
|
mod board;
|
|
mod campaign;
|
|
mod clipboard;
|
|
mod command;
|
|
mod completions;
|
|
mod handlers;
|
|
mod history;
|
|
mod install;
|
|
/// The command prompt reads `split_line` and `unclosed`; `cursor_arg` and `at_new_arg` are
|
|
/// what completing an argument needs and nothing reads them yet, so the allow stays until
|
|
/// 00a14e7 and 952a9f8 land. The tests are what drive them in the meantime.
|
|
#[cfg_attr(not(test), allow(dead_code))]
|
|
mod split;
|
|
mod tag;
|
|
mod tui;
|
|
mod util;
|
|
|
|
use command::*;
|
|
use handlers::*;
|
|
use util::*;
|
|
|
|
/// Die quietly when stdout is closed early, the way every other Unix tool does.
|
|
///
|
|
/// Rust sets `SIGPIPE` to `SIG_IGN` at startup, which turns a closed pipe into an
|
|
/// io error that `println!` panics on. So `worklane ls | head -3` printed a panic
|
|
/// and a backtrace instead of three lines, which is both alarming and easy to
|
|
/// mistake for a real fault. Restoring the default handler makes `| head` behave.
|
|
///
|
|
/// The cost is that a truncated pipe now kills the process with signal 13 rather
|
|
/// than returning a code, so `pipefail` sees 141 instead of 101. That is what
|
|
/// `head` and `sort` do, and it is recorded in the README.
|
|
fn quiet_broken_pipe() {
|
|
// SAFETY: setting a signal disposition to the default is always valid, and
|
|
// this runs before any thread is spawned.
|
|
unsafe {
|
|
libc::signal(libc::SIGPIPE, libc::SIG_DFL);
|
|
}
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
// Before any output, so no write can beat it to a closed pipe.
|
|
quiet_broken_pipe();
|
|
|
|
let cli = Cli::parse();
|
|
|
|
let start = std::env::current_dir()?;
|
|
|
|
let mut sink = Sink::std();
|
|
|
|
let result = match cli.command {
|
|
// The board is the one thing `dispatch` does not run, and it is matched here
|
|
// rather than there because it is the *caller* of that table rather than an entry
|
|
// in it: a `tui` reached from the command prompt would enter the alternate screen
|
|
// a second time from inside the draw loop. `dispatch` refuses it in those words.
|
|
//
|
|
// No subcommand opens the board, the way a bare `frig` does, and with no path
|
|
// it opens the board above the working directory.
|
|
None => run_tui(&[], &start),
|
|
Some(Command::Tui { paths }) => run_tui(&paths, &start),
|
|
Some(command) => dispatch(&command, &start, &mut sink),
|
|
};
|
|
|
|
// The board (and every handler) own their own writing, so flush what the last one
|
|
// handed the sink before returning; an error path printed nothing to flush.
|
|
sink.flush()?;
|
|
result
|
|
}
|
|
|
|
/// Run one parsed command against the board at or above `start`, saying what it did
|
|
/// through `sink`.
|
|
///
|
|
/// **The one mapping from a parsed command to the function that performs it.** `main`
|
|
/// calls it with the process's stdout and the working directory; the command prompt on
|
|
/// the board calls it with a buffer and the selected tab's own root, and reads back what
|
|
/// the handler said. A second copy of this match inside `tui.rs` would be a copy that
|
|
/// drifted the first time a command was added, and it would drift *silently*, since both
|
|
/// halves would go on compiling.
|
|
///
|
|
/// A command added to [`Command`] therefore lands here once and is runnable from both.
|
|
/// The match is exhaustive for exactly that reason: there is no `_` arm for a new verb to
|
|
/// fall into and do nothing.
|
|
pub fn dispatch(command: &Command, start: &Path, sink: &mut Sink) -> Result<()> {
|
|
match command {
|
|
// Reachable only from the prompt, since `main` takes this arm before it gets
|
|
// here. A board is not something a board can open.
|
|
Command::Tui { .. } => Err(anyhow::anyhow!("`tui` opens a board, and you are on one")),
|
|
Command::Init { create } => cmd_init(*create, start, sink),
|
|
Command::Add(args) => cmd_add(args, start, sink),
|
|
Command::Ls(args) => {
|
|
let worklane_dir = find_worklane_dir(start)?;
|
|
cmd_ls(args, &worklane_dir, sink)
|
|
}
|
|
Command::Show { id } => {
|
|
let worklane_dir = find_worklane_dir(start)?;
|
|
cmd_show(id, &worklane_dir, sink)
|
|
}
|
|
Command::Mv(args) => cmd_mv(args, start, sink),
|
|
Command::Set(args) => cmd_set(args, start, sink),
|
|
Command::Append(args) => cmd_append(args, start, sink),
|
|
Command::Relate(args) => cmd_relate(args, start, sink),
|
|
Command::Chown(args) => cmd_chown(args, start, sink),
|
|
Command::Check(args) => cmd_check(args, true, start, sink),
|
|
Command::Uncheck(args) => cmd_check(args, false, start, sink),
|
|
Command::Comment {
|
|
ticket,
|
|
body,
|
|
actor,
|
|
actor_from_git,
|
|
} => cmd_comment(ticket, body, actor, *actor_from_git, start, sink),
|
|
Command::Campaign { command } => match command {
|
|
CampaignCommand::Add(args) => cmd_campaign_add(args, start, sink),
|
|
CampaignCommand::Ls { ids } => {
|
|
let worklane_dir = find_worklane_dir(start)?;
|
|
cmd_campaign_ls(&worklane_dir, *ids, sink)
|
|
}
|
|
CampaignCommand::Set(args) => cmd_campaign_set(args, start, sink),
|
|
CampaignCommand::Append(args) => cmd_campaign_append(args, start, sink),
|
|
CampaignCommand::Show { campaign } => {
|
|
let worklane_dir = find_worklane_dir(start)?;
|
|
cmd_campaign_show(campaign, &worklane_dir, sink)
|
|
}
|
|
},
|
|
Command::Assign {
|
|
campaign,
|
|
ticket,
|
|
actor,
|
|
actor_from_git,
|
|
} => cmd_assign(campaign, ticket, actor, *actor_from_git, start, sink),
|
|
// The bare form and `ls` are one arm: `worklane tags` was the listing before the
|
|
// group existed and the fish completion still calls it that way, so the two
|
|
// spellings cannot be allowed to drift into two behaviours.
|
|
Command::Tags { command } => match command {
|
|
None | Some(TagCommand::Ls) => {
|
|
let worklane_dir = find_worklane_dir(start)?;
|
|
cmd_tags(&worklane_dir, sink)
|
|
}
|
|
Some(TagCommand::Add(args)) => cmd_tag_add(args, start, sink),
|
|
Some(TagCommand::Set(args)) => cmd_tag_set(args, start, sink),
|
|
},
|
|
// No board either, for the reason `install-skill` has none: a completion script
|
|
// is generated from the clap definition and is the same wherever it is run.
|
|
Command::Completions { shell } => {
|
|
completions::generate(*shell, sink.as_write()).map_err(Into::into)
|
|
}
|
|
// No board is looked for: the point of the skill is to teach an agent worklane
|
|
// before there is a `.worklane` anywhere.
|
|
Command::InstallSkill { skill, force, print } => {
|
|
cmd_install_skill(skill.as_deref(), *force, *print, sink)
|
|
}
|
|
// No board either, and one agent rather than both: opencode loads the tool file,
|
|
// Claude Code has no path for one.
|
|
Command::InstallTool { force, print } => cmd_install_tool(*force, *print, sink),
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------- tui --
|
|
|
|
/// Restore the terminal whatever happens.
|
|
///
|
|
/// A panic inside the draw loop with raw mode still on leaves the shell unusable and
|
|
/// the backtrace unreadable, so the hook runs before the default one.
|
|
fn install_panic_hook() {
|
|
let previous = std::panic::take_hook();
|
|
std::panic::set_hook(Box::new(move |info| {
|
|
let _ = restore_terminal();
|
|
previous(info);
|
|
}));
|
|
}
|
|
|
|
fn restore_terminal() -> std::io::Result<()> {
|
|
crossterm::terminal::disable_raw_mode()?;
|
|
crossterm::execute!(std::io::stdout(), crossterm::terminal::LeaveAlternateScreen)
|
|
}
|
|
|
|
fn run_tui(paths: &[std::path::PathBuf], start: &std::path::Path) -> Result<()> {
|
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
|
|
use std::io::IsTerminal;
|
|
|
|
// The boards are looked for before the terminal is, so running this outside a
|
|
// worklane directory gives the same message every other command gives rather than
|
|
// a complaint about the terminal.
|
|
let projects = tui::projects(paths, start)?;
|
|
|
|
// Without this, entering raw mode on a pipe fails with a bare "No such device or
|
|
// address (os error 6)", which says nothing about the actual mistake. A bare
|
|
// `worklane` is how the board is opened, so a script that reaches it by accident
|
|
// has to be pointed at a subcommand.
|
|
if !std::io::stdin().is_terminal() {
|
|
return Err(anyhow::anyhow!(
|
|
"`worklane` with no subcommand opens the board, which needs a terminal.\n\
|
|
For scripted use try `worklane ls` or `worklane show`; `worklane --help` \
|
|
lists everything."
|
|
));
|
|
}
|
|
|
|
let mut board = tui::Board::new(projects)?;
|
|
|
|
install_panic_hook();
|
|
crossterm::terminal::enable_raw_mode()?;
|
|
crossterm::execute!(std::io::stdout(), crossterm::terminal::EnterAlternateScreen)?;
|
|
let mut terminal =
|
|
ratatui::Terminal::new(ratatui::backend::CrosstermBackend::new(std::io::stdout()))?;
|
|
|
|
let result = (|| -> Result<()> {
|
|
while !board.should_quit {
|
|
board.tick()?;
|
|
terminal.draw(|frame| tui::draw(frame, &board))?;
|
|
|
|
// Taken after the draw, so a scroll measures the overlay that is on screen.
|
|
let screen = terminal.size()?;
|
|
|
|
// Bounded, so the tick still runs when nobody is typing.
|
|
if !event::poll(tui::POLL)? {
|
|
continue;
|
|
}
|
|
let Event::Key(key) = event::read()? else {
|
|
continue;
|
|
};
|
|
if key.kind != KeyEventKind::Press {
|
|
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
|
|
// match on the keys it did not, and it takes nothing when no prompt is open.
|
|
// The rule is `Board::search_key`, where a test can reach it.
|
|
if board.search_key(key, screen) {
|
|
continue;
|
|
}
|
|
|
|
// The same contract for the campaign picker, which is the other mode that owns
|
|
// the keyboard while it is open. Two calls rather than one combined pre-branch,
|
|
// because only one of the two can be open at a time and each reports for
|
|
// itself; the rules are `Board::search_key` and `Board::picker_key`, where a
|
|
// test can reach them.
|
|
if board.picker_key(key, screen) {
|
|
continue;
|
|
}
|
|
|
|
// And the command prompt, third, on the same contract: while a command is being
|
|
// typed `q` is a letter, `d` leaves the columns alone and `tab` stays out of the
|
|
// project tabs. It takes the screen because enter runs the command and then has
|
|
// to bring the ticket it moved back into view. The rule is `Board::prompt_key`,
|
|
// where a test can reach it.
|
|
if board.prompt_key(key, screen) {
|
|
continue;
|
|
}
|
|
|
|
match key.code {
|
|
// One arm, because the two keys mean one thing: close what is open, and
|
|
// quit when there is nothing left to close. Two arms carrying one rule
|
|
// is how a later edit changes it for one key and not the other, which is
|
|
// what `q` quitting out from under an open ticket already was. The rule
|
|
// itself is `Board::dismiss`, where a test can reach it.
|
|
KeyCode::Esc | KeyCode::Char('q') => board.dismiss(screen),
|
|
// The only key that leaves the board from an open ticket, which is why
|
|
// the footer names it there.
|
|
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
|
board.should_quit = true
|
|
}
|
|
// The frame's own height while it holds the selection, and the detail
|
|
// overlay on a card. `Board::toggle_focused` rather than a condition here,
|
|
// for the reason the scroll keys route there.
|
|
KeyCode::Enter | KeyCode::Char(' ') => board.toggle_focused(screen),
|
|
KeyCode::Left | KeyCode::Char('h') => board.select_column(-1, screen),
|
|
KeyCode::Right | KeyCode::Char('l') => board.select_column(1, screen),
|
|
// Up and down stay on the selection, open detail view or not, so a
|
|
// column can be read through without closing the modal. Scrolling the
|
|
// open ticket is a page at a time, and so is scrolling the campaign
|
|
// frame that up reaches from the top card of a column.
|
|
KeyCode::Up | KeyCode::Char('k') => board.select_card(-1, screen),
|
|
KeyCode::Down | KeyCode::Char('j') => board.select_card(1, screen),
|
|
// The open ticket first and the campaign frame behind it, which is
|
|
// `Board::scroll_page` rather than a condition written here: the arms in
|
|
// this dispatch are not reachable from a test.
|
|
KeyCode::PageUp => board.scroll_page(-1, screen),
|
|
KeyCode::PageDown => board.scroll_page(1, screen),
|
|
// Tabs cycle rather than clamp, the way tabs everywhere do, and are
|
|
// a no-op with a single board. Free of every other binding: the
|
|
// columns took hjkl and the arrows.
|
|
KeyCode::Tab => board.select_tab(1, screen),
|
|
KeyCode::BackTab => board.select_tab(-1, screen),
|
|
// Free of the other bindings, and the footer names what the next press
|
|
// does in every state of the rotation.
|
|
KeyCode::Char('d') => board.rotate_visibility(screen),
|
|
// An ordinary single-shot action like the rest: it opens the prompt, and
|
|
// from the next keypress on `search_key` above has the keyboard. A `/`
|
|
// typed into an open prompt is taken there as a character, and one pressed
|
|
// at an open picker is taken by `picker_key` to open the picker's own
|
|
// query, so this arm is only ever the way in to the board's prompt.
|
|
KeyCode::Char('/') => board.open_search(),
|
|
// The way in to the command prompt, and the same single-shot arm `/` is:
|
|
// from the next keypress on, `prompt_key` above has the keyboard. A `:`
|
|
// typed into either open prompt is a character there and one pressed at an
|
|
// open picker is swallowed by it, so this arm is only ever the way in from
|
|
// the board itself. Free of every existing binding.
|
|
KeyCode::Char(':') => board.open_prompt(),
|
|
// `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),
|
|
// The way in to the campaign picker, like `/` is to the search prompt: from
|
|
// the next keypress on `picker_key` above has the keyboard. Free of every
|
|
// other binding, and `ctrl-c` is matched above this so quitting still wins.
|
|
KeyCode::Char('c') => board.open_picker(tui::PickerKind::Campaign, screen),
|
|
// The other axis the board is cut along, and the one `ls --tag` has
|
|
// answered since 2f3796e. Free of every other binding: `tab` is its own
|
|
// key code, not this letter.
|
|
KeyCode::Char('t') => board.open_picker(tui::PickerKind::Tag, screen),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
})();
|
|
|
|
// Before the error surfaces, so a failure does not leave the shell in raw mode.
|
|
restore_terminal()?;
|
|
result
|
|
}
|