Every argument worklane takes names a ticket, a status, a shell or a skill, and the shell offered a directory listing for all of them. `completions <shell>` prints a script for fish, bash, zsh, elvish and powershell, following what frig does, which is where the shape of this comes from. Two halves, because neither alone is enough. `clap_complete` generates the structure from the clap definition, so a flag added to `command.rs` is completed without anyone touching `completions.rs`, and fish gets a hand-written block appended for what only the board knows. That block runs `worklane ls --ids` in a command substitution, one run per keypress, 2.5ms in release on a board of seventy-five tickets. A status is a `util::Status` value enum now rather than a string, which was the plan's third step and pays for itself three ways. clap refuses an unknown one at parse time naming the four, so `cmd_mv`'s `valid_statuses` and the status half of `cmd_ls`'s error are gone and exit 2 is what an unreal status gets, the bargain `--due` and `--priority` already take. `status_dirs()` and `DONE` are derived from `Status::ALL`, so the flow is written once. And clap emits the four for `mv --status` itself. Only half free: the fish generator emits nothing for the possible values of a *positional*, so `ls <status>` and `completions <shell>` are listed in the appended half after all, read off the enums with `possible_values` rather than by hand. `ls --ids` is the machine-readable listing, and prints the short id, a tab and the title rather than the id alone. Both readers want both fields: fish takes what follows a tab as the description, which is what makes `worklane show <TAB>` a list of titles instead of hex, and a script takes the id back with `cut -f1`. It includes done, where the decorated listing leaves done out, because a completion has to be able to name a finished ticket and `show` is most often pointed at one. It skips a missing status directory rather than refusing, since an error printed over a shell prompt is worse than no candidates, and the listing still refuses one so a person can act on it. The filters all still apply, and an unknown `--campaign` is still refused ahead of everything. Adding the mode tipped `cmd_ls` over the boundary AGENTS.md names, so it takes an `LsArgs`, the way `add` took `AddArgs`. More than the criteria asked, since the cost of each was a name in a list: tickets are completed for `set`, `append`, `relate`, `check`, `uncheck`, the second argument of `chown` and `assign`, and `relate`'s three target flags, plus the skill names read off `install::SKILLS`. The three lists are kept honest by tests that walk the clap definition and fail on a ticket-taking subcommand missing from them, or listed at the wrong position. Nested subcommands are not walked: `campaign` is the only one, none of its three takes a ticket, and the token counting could not tell them apart if one did. Position is counted rather than inferred. `__worklane_first_arg_of` reads `$tokens[2]` and the token count instead of `__fish_seen_subcommand_from`, because `campaign ls` and `campaign show` are also an `ls` and a `show` that fish has seen, and matching against the ticket list to find the position would run worklane again inside the condition. Files are turned off for the command and back on for `tui`, which is the one argument that really is a path. The ticket said worklane takes no file arguments; it takes those. Campaigns and tags are completed nowhere, which needs a listing they do not have: 55ee6e3. And since fish runs whatever is on PATH, the completions are only as current as the installed binary, an older one without `--ids` completing nothing. That is in the README beside the install line. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
234 lines
10 KiB
Rust
234 lines
10 KiB
Rust
use anyhow::Result;
|
|
use clap::Parser;
|
|
|
|
mod campaign;
|
|
mod clipboard;
|
|
mod command;
|
|
mod completions;
|
|
mod handlers;
|
|
mod install;
|
|
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()?;
|
|
|
|
match cli.command {
|
|
// 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::Init) => cmd_init(&start),
|
|
Some(Command::Add(args)) => cmd_add(&args, &start),
|
|
Some(Command::Ls(args)) => {
|
|
let worklane_dir = find_worklane_dir(&start)?;
|
|
cmd_ls(&args, &worklane_dir)
|
|
}
|
|
Some(Command::Show { id }) => {
|
|
let worklane_dir = find_worklane_dir(&start)?;
|
|
cmd_show(&id, &worklane_dir)
|
|
}
|
|
Some(Command::Mv {
|
|
ticket,
|
|
status,
|
|
force,
|
|
}) => cmd_mv(&ticket, status, force, &start),
|
|
Some(Command::Set(args)) => cmd_set(&args, &start),
|
|
Some(Command::Append(args)) => cmd_append(&args, &start),
|
|
Some(Command::Relate(args)) => cmd_relate(&args, &start),
|
|
Some(Command::Chown { user, ticket }) => cmd_chown(&user, &ticket, &start),
|
|
Some(Command::Check(args)) => cmd_check(&args, true, &start),
|
|
Some(Command::Uncheck(args)) => cmd_check(&args, false, &start),
|
|
Some(Command::Comment {
|
|
ticket,
|
|
body,
|
|
owner,
|
|
}) => cmd_comment(&ticket, &body, &owner, &start),
|
|
Some(Command::Campaign { command }) => match command {
|
|
CampaignCommand::Add(args) => cmd_campaign_add(&args, &start),
|
|
CampaignCommand::Ls => {
|
|
let worklane_dir = find_worklane_dir(&start)?;
|
|
cmd_campaign_ls(&worklane_dir)
|
|
}
|
|
CampaignCommand::Show { campaign } => {
|
|
let worklane_dir = find_worklane_dir(&start)?;
|
|
cmd_campaign_show(&campaign, &worklane_dir)
|
|
}
|
|
},
|
|
Some(Command::Assign { campaign, ticket }) => cmd_assign(&campaign, &ticket, &start),
|
|
// 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.
|
|
Some(Command::Completions { shell }) => {
|
|
completions::generate(shell, &mut std::io::stdout()).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.
|
|
Some(Command::InstallSkill { skill, force, print }) => {
|
|
cmd_install_skill(skill.as_deref(), force, print)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------- 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;
|
|
}
|
|
|
|
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(),
|
|
// 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
|
|
}
|
|
KeyCode::Enter | KeyCode::Char(' ') => board.toggle_detail(),
|
|
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.
|
|
KeyCode::Up | KeyCode::Char('k') => board.select_card(-1, screen),
|
|
KeyCode::Down | KeyCode::Char('j') => board.select_card(1, screen),
|
|
KeyCode::PageUp => board.scroll_detail(-1, screen),
|
|
KeyCode::PageDown => board.scroll_detail(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 it in both states.
|
|
KeyCode::Char('d') => board.toggle_done(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, 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),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
})();
|
|
|
|
// Before the error surfaces, so a failure does not leave the shell in raw mode.
|
|
restore_terminal()?;
|
|
result
|
|
}
|