The detail view had no scrolling at all: no offset on Board, nothing passed to Paragraph::scroll, so anything past the height of the overlay was unreachable. Every ticket in this repo is longer than that. The footer read "↑↓ scroll" while up and down moved the selection, so it named a key that did nothing and sent the reader looking for their own mistake. The scroll goes on pgup and pgdn rather than taking up and down. Up and down stay on the selection with the modal open, so a whole column can be read without closing it and the modal follows the selection. That is worth more than the two keys. The offset is clamped against the wrapped line count from Paragraph::line_count, which is why ratatui now carries the unstable-rendered-line-info feature. The paragraph wraps on words, so a clamp on the raw lines would stop short of the end or run past it into blank space, and counting the wrapping by hand would only agree with the widget until one of the two changed. The clamp runs on the keypress and again in the draw, since the terminal can be resized between them and a tick can shorten the ticket under a reader who is already at the end. The offset resets when the selection moves and when the modal is closed, so a ticket opens at its top. It deliberately does not reset on a tick: an edit elsewhere in the tree must not throw the reader back to the top of what they are reading. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
190 lines
6.7 KiB
Rust
190 lines
6.7 KiB
Rust
use anyhow::Result;
|
|
use clap::Parser;
|
|
|
|
mod command;
|
|
mod handlers;
|
|
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.
|
|
None | Some(Command::Tui) => run_tui(&start),
|
|
Some(Command::Init) => cmd_init(&start),
|
|
Some(Command::Add {
|
|
title,
|
|
description,
|
|
acceptance,
|
|
plan,
|
|
tags,
|
|
due,
|
|
owner,
|
|
priority,
|
|
}) => cmd_add(
|
|
&title,
|
|
&description,
|
|
&acceptance,
|
|
&plan,
|
|
&tags,
|
|
&due,
|
|
&owner,
|
|
priority,
|
|
&start,
|
|
),
|
|
Some(Command::Ls { status, priority }) => {
|
|
let worklane_dir = find_worklane_dir(&start)?;
|
|
cmd_ls(&status, priority, &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::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),
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------- 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(start: &std::path::Path) -> Result<()> {
|
|
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
|
|
use std::io::IsTerminal;
|
|
|
|
// The board is 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 worklane_dir = find_worklane_dir(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(&worklane_dir)?;
|
|
|
|
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;
|
|
}
|
|
|
|
match key.code {
|
|
KeyCode::Char('q') => board.should_quit = true,
|
|
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
|
board.should_quit = true
|
|
}
|
|
// Esc closes the detail view, and only quits when there is nothing
|
|
// left to dismiss.
|
|
KeyCode::Esc => match board.detail {
|
|
Some(_) => board.toggle_detail(),
|
|
None => board.should_quit = true,
|
|
},
|
|
KeyCode::Enter | KeyCode::Char(' ') => board.toggle_detail(),
|
|
KeyCode::Left | KeyCode::Char('h') => board.select_column(-1),
|
|
KeyCode::Right | KeyCode::Char('l') => board.select_column(1),
|
|
// 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),
|
|
KeyCode::Down | KeyCode::Char('j') => board.select_card(1),
|
|
KeyCode::PageUp => board.scroll_detail(-1, screen),
|
|
KeyCode::PageDown => board.scroll_detail(1, screen),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
})();
|
|
|
|
// Before the error surfaces, so a failure does not leave the shell in raw mode.
|
|
restore_terminal()?;
|
|
result
|
|
}
|