Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4534eb86d | ||
|
|
96cddc14ad | ||
|
|
f16e2aba0c |
@@ -1,7 +1,7 @@
|
||||
# AGENTS.md
|
||||
|
||||
## Project Structure
|
||||
- `src/main.rs` - dispatch, the SIGPIPE reset, and the TUI event loop
|
||||
- `src/main.rs` - `dispatch`, the one mapping from a parsed command to its handler, the SIGPIPE reset, and the TUI event loop
|
||||
- `src/command.rs` - CLI argument definitions using clap derive
|
||||
- `src/completions.rs` - the generated completion script and the fish half that asks the board
|
||||
- `src/handlers.rs` - one `cmd_*` per subcommand
|
||||
@@ -351,6 +351,35 @@ 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 Command Prompt
|
||||
- **`prompt: Option<Prompt>` is the whole mode**, the shape `search` and `detail` use: open and content cannot disagree, and closing it leaves nothing to reset. `Prompt` carries the line, the cursor as a byte offset into it, and `refused`, which is `Search::missed` with something to say
|
||||
- **`Board::prompt_key` is third in the pre-dispatch**, beside `search_key` and `picker_key` and 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, and `ctrl-c` is the one key declined. It takes a `Size` for enter's sake: running a command reloads the board and then has to bring the ticket it moved back into view, which is the measurement `follow_selection` takes. Only one of the two prompts can be open at once: `:` is reachable from the action arms alone, and while either has the keyboard the other's key is a character in a line
|
||||
- **`esc` and `enter` are the mode's own keys and are answered in `prompt_key`; everything else belongs to the line and goes to `Prompt::key`.** That is also what keeps the borrow straight, since closing the prompt takes the whole board and editing takes only the prompt. Left, right, home, end, backspace and `ctrl-w` behave as they do in a shell, and `ctrl-w` eats the separators behind the cursor before the word behind those, so one press at the end of `mv 1b78a24 ` takes the id. There is deliberately no `tab`: suggesting the selected ticket is 00a14e7, and until then it is a key that must not reach the tabs
|
||||
- **The grammar is clap's, with nothing restated.** The line is tokenised by `split_line`, the binary's own name goes in front, and the arguments go to the same `Cli` definition the shell reaches, so a flag added to a command is typeable here the day it is added. Through `Cli::command().try_get_matches_from_mut` rather than `try_parse_from`, for what the matches give back: `command_name` walks the subcommands to name what resolved (`campaign show`, not `campaign`) instead of a `match` over every variant, which would be a second list of every verb the CLI has and one that goes stale silently
|
||||
- **`refused` is cleared by an edit and not by a movement.** The message is about the line, and moving the cursor does not change the line. `Prompt::key` clears it in the three arms that write to the line
|
||||
- **A line that ends inside a quote never reaches clap.** That is what `Unclosed` is for: a grammar complaint about a sentence nobody finished describes the wrong thing. Enter on an empty line, or one of nothing but separators, closes the prompt with nothing said, the way `esc` does — the bare form of the CLI opens a board and there is one on the screen
|
||||
- **Enter runs it, through `main.rs`'s `dispatch` and never through a handler this file names.** That match is the one mapping from a parsed command to the function that performs it: `main` calls it with its own stdout and the working directory, `Board::run` calls it with a buffer and the selected tab's root. A second copy inside `tui.rs` would drift the first time a command was added, and it would drift *silently*, since both halves go on compiling. The match is exhaustive so a new verb cannot fall into an `_` arm and do nothing, and `main_and_the_prompt_run_one_dispatch_table` reads both files as text to pin it. Only `tui` is matched outside the table, in `main`, because the board is that table's caller rather than an entry in it; reached from the prompt it is refused in those words
|
||||
- **`from_arg_matches` on the same matches gives `run` the typed values.** One parse, two readings: `command_name` walks the subcommands to name what resolved and `from_arg_matches` reconstructs the `Cli`. A `match` over every `Command` variant to get one or the other would be a second list of every verb the CLI has
|
||||
- **The handler's own words are what the board reports, word for word.** Those messages are written for a person already, and a second set kept here would go stale the first time one of them changed. The sink is a buffer, so the words come back as text and the board styles them; it is plain for the same reason `Detail` holds spans, since a ratatui paragraph prints an escape rather than obeying it. An error never goes through a sink at all — `color_text` asks the *process* about its stdout, which under the board answers yes about a terminal the message is not going to — so `error_lines` runs `without_escapes` over it and puts each cause on a line of its own
|
||||
- **A handler that errors leaves the prompt open with the line intact.** The usual cause is an ambiguous id, and the fix is two more characters rather than the whole line retyped from `:`. Ambiguity is an `Err` carrying the candidates, not a print and an `exit`, which is what makes it safe to reach from here at all
|
||||
- **The reload after a command is unconditional, and runs whether it failed or not.** `tick` reloads on the digest, which is right for a change made in another pane and needless latency for one the board just made: the reader would watch their own `mv` take up to a poll interval to appear. Either way, because a handler that failed part way through has still written
|
||||
- **The selection is restored by uuid across that reload, never by index.** `mv` puts the card in another column, `set --title` renames its file, and a filter can drop it out of view entirely, so the slot is not the ticket. `Board::reselect` has three answers: the card is in a drawn column and the selection goes to it; it is on the board but in a column the rotation is hiding, which is where `--status done` lands by default, so the notice says where it went and which key shows it; or the filter no longer holds it, which is said too, since a card vanishing from under the highlight reads as a bug in the board
|
||||
- **The merge tab is refused, for now.** It is the one tab that is not a board — its cards came from several `.worklane` directories and it is none of them — so `Board::current_project` answers `None` there and the command is refused rather than landing on whichever project came first. Routing by the card a command names is ebc5755. The project is resolved by index and not by the tab's name, which is a label two projects can share: e1d79c3 is exactly that, and a write routed by name would be that bug with a file changing at the end of it
|
||||
- **`Notice` holds as many lines as the command used, and `Board::notice_on_row` decides where they go.** One line with nothing else holding the row draws on the footer, where a copy's notice has always drawn. Anything else — several lines, or any answer at all while the prompt is still open on an error — draws as a box at the foot of the columns, cut to what the columns leave with the dropped count on its last row. A box rather than more rows of footer, and that is the whole reason it is an overlay: a footer that grew and shrank as notices came and went would reflow the columns on every keypress, moving every card the reader is looking at. `draw` and `draw_footer` both ask `notice_on_row`, so the two cannot come to disagree and draw the message twice or not at all
|
||||
- **What a refused line shows is clap's own message with the usage block cut off**, which is also what drops the `--help` line after it. `--help` is the exception: it parses as an error of the kind that means *print this* and what it would print is a page, so it is caught and answered in a row's worth of words, or clap's help lands on the alternate screen the way an unsunk `println!` would. `--version` goes through the ordinary path, because what it prints is one line by construction. `ColorChoice::Never` on the command, so nothing that reaches the row carries an escape
|
||||
- **The keys' width comes off the row before the line is windowed**, so a long command does not push them off the end; a message takes the row from the keys instead, since the reader has just been told something and `esc` and `enter` still do what they did. `prompt_window` anchors the window on the cursor's right edge, which costs the text *after* the cursor on a long line: keeping both ends needs a scroll offset in state, and the draw is where the width is known and the state is not writable
|
||||
- `: command` is named in every footer state, because the key works in every one of them, and the prompt's own key is drawn as `↵ run`
|
||||
|
||||
### The Prompt's Recall
|
||||
- **`up` and `down` walk the lines already typed, and the lines are on disk.** `.wlhistory` in the board's own folder, appended as each line is entered. It is the one file under `.worklane` the board writes rather than a command, and the only one that is not markdown, which is the cost of a recall worth having: a board is opened and closed all day, and one that emptied on quit would only help inside a single sitting
|
||||
- **`Recall` is per tab, parallel to `Board::tabs`**, so the tab in view decides which list `up` walks. A project's lines are the ones naming its ticket ids, and handing another board's `mv` back on this one would offer an id that resolves to nothing there. The tabs are fixed for the run, so the two vectors cannot drift, and the same index that picks the tab picks the recall
|
||||
- **The merge tab has no file.** It is the one tab that is not a board, `current_project` answers `None` there and a command typed there is refused for that reason, so there is nothing to attribute a line to. Those lines are still recalled for the run: every one of them was refused, and a refused line is the one most worth getting back. When ebc5755 routes a merge line to the board its card names, that board's file is the one to append to
|
||||
- **The position is on `Prompt` and the lines are on `Recall`**, so closing the prompt forgets the position and not the lines: the next `:` opens at the end of the list again, which is where a shell opens. `pending` holds the half typed line `up` walked away from, and walking `down` past the newest entry puts it back, so leaving a command to look for an older one never costs it
|
||||
- **The ends are stops, not wraps.** `up` on the oldest line leaves it up rather than cycling round to the newest, which is what a shell does with it; `down` with no walk under way does nothing, since there is nothing in front of a line still being typed and emptying the row would be a keypress that destroys work. **An empty recall does nothing at all**, and that includes leaving the line alone, or `up` on a board nobody has typed at would be a way to lose what is half written
|
||||
- **The line is kept before anything can refuse it.** A refused line never reached a handler and the fix is usually two characters, which is what the walk exists to save. Consecutive duplicates are kept once, against what is already held, so the rule spans runs as well as one sitting; a blank line is no command and is not kept. Nothing is trimmed
|
||||
- **A failed write is silent, and an unreadable file is an empty recall.** The line is in memory either way, so the walk works for the run on a board whose folder cannot be written, and a board with no history yet is the ordinary case rather than something to say. A notice about a history file would be about the one thing the reader did not ask for
|
||||
- **`.wlhistory` is not in `digest`.** Appending to it must not read as the tree moving, or every command typed would provoke a reload of every board on top of the one `run` already does
|
||||
|
||||
### Splitting a Typed Line
|
||||
- **`split_line` is the whole module, and it knows nothing about `Board`.** The command prompt reads one string and clap reads an argv, so `comment 1b78a24 the offset was never passed` has to be tokenised before anything can parse it: split on whitespace it is five arguments and `comment` takes two. A pure function over the line and the cursor is what makes every rule reachable from a unit test, and it is in a file of its own rather than in `tui.rs`, which is eleven thousand lines already
|
||||
- **The rules are the shell's, because that is what the prompt promises**: the line is spelled there exactly as it is spelled in the shell. Runs of whitespace separate arguments, single and double quotes group one, a backslash escapes the next character *except* inside single quotes where it is a backslash, and an explicitly empty `''` is an argument that exists and is empty — which is what `--title ""` being a usage error rather than a silent clear depends on. A leading hyphen is not special here: whether `--foo` is a flag or the text of a criterion is clap's question, and `PROSE` already answers it
|
||||
|
||||
Generated
+1
-1
@@ -1503,7 +1503,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "worklane"
|
||||
version = "0.32.14"
|
||||
version = "0.32.17"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"assert_cmd",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "worklane"
|
||||
version = "0.32.14"
|
||||
version = "0.32.17"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
+113
-68
@@ -1,3 +1,5 @@
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Parser;
|
||||
|
||||
@@ -8,8 +10,9 @@ mod completions;
|
||||
mod handlers;
|
||||
mod history;
|
||||
mod install;
|
||||
/// Nothing in the binary splits a line yet: the prompt that will is 3686dd6, and the
|
||||
/// allow goes when it lands. The tests are what drive it in the meantime.
|
||||
/// 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;
|
||||
@@ -49,76 +52,16 @@ fn main() -> Result<()> {
|
||||
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::Init) => cmd_init(&start, &mut sink),
|
||||
Some(Command::Add(args)) => cmd_add(&args, &start, &mut sink),
|
||||
Some(Command::Ls(args)) => {
|
||||
let worklane_dir = find_worklane_dir(&start)?;
|
||||
cmd_ls(&args, &worklane_dir, &mut sink)
|
||||
}
|
||||
Some(Command::Show { id }) => {
|
||||
let worklane_dir = find_worklane_dir(&start)?;
|
||||
cmd_show(&id, &worklane_dir, &mut sink)
|
||||
}
|
||||
Some(Command::Mv(args)) => cmd_mv(&args, &start, &mut sink),
|
||||
Some(Command::Set(args)) => cmd_set(&args, &start, &mut sink),
|
||||
Some(Command::Append(args)) => cmd_append(&args, &start, &mut sink),
|
||||
Some(Command::Relate(args)) => cmd_relate(&args, &start, &mut sink),
|
||||
Some(Command::Chown(args)) => cmd_chown(&args, &start, &mut sink),
|
||||
Some(Command::Check(args)) => cmd_check(&args, true, &start, &mut sink),
|
||||
Some(Command::Uncheck(args)) => cmd_check(&args, false, &start, &mut sink),
|
||||
Some(Command::Comment {
|
||||
ticket,
|
||||
body,
|
||||
actor,
|
||||
actor_from_git,
|
||||
}) => cmd_comment(&ticket, &body, &actor, actor_from_git, &start, &mut sink),
|
||||
Some(Command::Campaign { command }) => match command {
|
||||
CampaignCommand::Add(args) => cmd_campaign_add(&args, &start, &mut sink),
|
||||
CampaignCommand::Ls { ids } => {
|
||||
let worklane_dir = find_worklane_dir(&start)?;
|
||||
cmd_campaign_ls(&worklane_dir, ids, &mut sink)
|
||||
}
|
||||
CampaignCommand::Set(args) => cmd_campaign_set(&args, &start, &mut sink),
|
||||
CampaignCommand::Append(args) => cmd_campaign_append(&args, &start, &mut sink),
|
||||
CampaignCommand::Show { campaign } => {
|
||||
let worklane_dir = find_worklane_dir(&start)?;
|
||||
cmd_campaign_show(&campaign, &worklane_dir, &mut sink)
|
||||
}
|
||||
},
|
||||
Some(Command::Assign {
|
||||
campaign,
|
||||
ticket,
|
||||
actor,
|
||||
actor_from_git,
|
||||
}) => cmd_assign(&campaign, &ticket, &actor, actor_from_git, &start, &mut 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.
|
||||
Some(Command::Tags { command }) => match command {
|
||||
None | Some(TagCommand::Ls) => {
|
||||
let worklane_dir = find_worklane_dir(&start)?;
|
||||
cmd_tags(&worklane_dir, &mut sink)
|
||||
}
|
||||
Some(TagCommand::Add(args)) => cmd_tag_add(&args, &start, &mut sink),
|
||||
Some(TagCommand::Set(args)) => cmd_tag_set(&args, &start, &mut 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.
|
||||
Some(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.
|
||||
Some(Command::InstallSkill { skill, force, print }) => {
|
||||
cmd_install_skill(skill.as_deref(), force, print, &mut sink)
|
||||
}
|
||||
// No board either, and one agent rather than both: opencode loads the tool file,
|
||||
// Claude Code has no path for one.
|
||||
Some(Command::InstallTool { force, print }) => cmd_install_tool(force, print, &mut sink),
|
||||
Some(command) => dispatch(&command, &start, &mut sink),
|
||||
};
|
||||
|
||||
// The board (and every handler) own their own writing, so flush what the last one
|
||||
@@ -127,6 +70,93 @@ fn main() -> Result<()> {
|
||||
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 => cmd_init(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.
|
||||
@@ -219,6 +249,15 @@ fn run_tui(paths: &[std::path::PathBuf], start: &std::path::Path) -> Result<()>
|
||||
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
|
||||
@@ -262,6 +301,12 @@ fn run_tui(paths: &[std::path::PathBuf], start: &std::path::Path) -> Result<()>
|
||||
// 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.
|
||||
|
||||
+1976
-5
File diff suppressed because it is too large
Load Diff
+40
-1
@@ -303,6 +303,42 @@ pub fn color_text(text: &str, color: Color) -> String {
|
||||
paint(text, color, should_use_colors())
|
||||
}
|
||||
|
||||
/// `text` with every ANSI escape sequence taken out of it.
|
||||
///
|
||||
/// The inverse of what [`paint`] writes, and the guard at the seam where a handler's
|
||||
/// words become something the board draws: ratatui prints an escape as literal bytes
|
||||
/// rather than obeying it, so a painted status reaches the screen as `\x1b[33m` and
|
||||
/// the word after it.
|
||||
///
|
||||
/// A [`Sink`] the board owns is plain, so this is not about a handler's ordinary
|
||||
/// output. It is about the strings that never go through one: an error message is
|
||||
/// built with `color_text`, which asks the *process* about its stdout, and under the
|
||||
/// board that answers yes about a terminal the message is not going to.
|
||||
pub fn without_escapes(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
let mut chars = text.chars();
|
||||
|
||||
while let Some(c) = chars.next() {
|
||||
if c != '\x1b' {
|
||||
out.push(c);
|
||||
continue;
|
||||
}
|
||||
|
||||
// A CSI sequence is `\x1b[`, then parameter bytes, closed by a final byte in
|
||||
// `@` to `~`. That is every sequence `Color` writes. Anything else is a short
|
||||
// escape whose one following character goes with it, which `next` has taken.
|
||||
if chars.next() == Some('[') {
|
||||
for c in chars.by_ref() {
|
||||
if ('\x40'..='\x7e').contains(&c) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
/// A destination for a command's output, chosen by the caller.
|
||||
///
|
||||
/// `main` hands a handler its own stdout and nothing about the CLI changes; the
|
||||
@@ -332,7 +368,10 @@ impl<'a> Sink<'a> {
|
||||
|
||||
/// An in-memory buffer, plain, for a test or a caller that colours the text
|
||||
/// itself.
|
||||
#[allow(dead_code)]
|
||||
///
|
||||
/// What the board's command prompt passes: the handler's words come back as text
|
||||
/// and the board styles them with ratatui, which is also why it must not be handed
|
||||
/// a coloured one — a paragraph prints an escape rather than obeying it.
|
||||
pub fn buffer(target: &'a mut Vec<u8>) -> Sink<'a> {
|
||||
Sink {
|
||||
writer: Box::new(target),
|
||||
|
||||
Reference in New Issue
Block a user