(00a14e7) Suggest the selected ticket where a ticket argument goes, and accept it with tab, and bump to 0.33.3

The reader has the ticket selected, which is the whole reason to type a
command on the board rather than in a shell, so where the line is naming a
ticket the selected card's short id is drawn after the cursor in grey and
tab takes it into the line.

Where a ticket goes is completions.rs's answer, through a new
`ticket_argument` over the three tables the fish half is generated from. A
copy of the positions on the board would go wrong the next time a command
grew a ticket argument, and silently, since the tests guarding the first
copy would not read the second.

TICKET_FLAGS holds (command, flag) pairs now, the shape TAG_FLAGS already
had, and the `chown --clear` rule the fish block wrote out by hand is
generated from it. Only chown's --clear names a ticket: relate's takes a
kind and set's a field, and the flag alone cannot tell the three apart.

The suggestion is derived on every call rather than held on the Prompt, so
what the row draws and what tab takes are one answer, and it is offered
only with the cursor at the end of the line, while a card is selected, and
while the id still starts with what is typed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ansho Rei
2026-09-15 15:01:15 +02:00
co-authored by Claude Opus 5
parent a017f9002e
commit c714c73a75
6 changed files with 472 additions and 49 deletions
Generated
+1 -1
View File
@@ -1563,7 +1563,7 @@ dependencies = [
[[package]]
name = "worklane"
version = "0.33.2"
version = "0.33.3"
dependencies = [
"anyhow",
"assert_cmd",
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "worklane"
version = "0.33.2"
version = "0.33.3"
edition = "2024"
[dependencies]
+3 -1
View File
@@ -10,8 +10,10 @@ The generated half, the fish half, and what keeps the two honest.
- **The strongest completion check loads the script into fish and asks it for candidates.** `fish_offers_the_commands_the_statuses_and_the_values` in `tests/cmd_completions.rs` sources the generated script into a throwaway shell standing in a real board, with this build's directory on the front of PATH, and compares `complete -C` against the ticket and the tag it put there. Every other test in the file reads the script as text, which cannot tell a rule that fires from one whose condition stopped matching, and that is how a completion actually breaks. **Assert the values, not the flag names**: clap emits every option's name on its own, so `--tags` and `--blocked-by` are offered with every hand-written rule deleted; what the dynamic half adds is what comes *after* the flag. Two traps, both paid for once: the shell must have `fish_complete_path` emptied, or `complete -C` autoloads `~/.config/fish/completions/worklane.fish` and answers from whatever version wrote that, which is a guard that cannot fail; and the script is written beside the board rather than in it, since a rule that fires nothing falls back to listing the directory
- **`fish --no-execute` is the floor under that, not an oracle.** It catches a script that does not parse, quotes still open at the end of the file or an unclosed block. Measured, not assumed: an apostrophe interpolated into a `-d` description partway down re-balances against the next quote and parses, corrupting every rule in between while the check passes; the same apostrophe in the last rule reaches the end unbalanced and is caught. Only fish gets this, because only fish gets a hand-written half
- **The rule counts in the completion tests are hardcoded on purpose.** The walkers go clap → list and never see an entry naming something that does not exist, so a bogus pair in `TAG_FLAGS` is caught by `every_tag_rule_takes_the_annotation_as_its_description` counting one rule too many. Deriving that number from the lists it checks would grow with the bogus entry and pass. The `seen` totals in the walkers are hardcoded for the mirror reason, and both carry the note
- **The five completion lists are kept honest by tests** that walk the clap definition and fail on an argument left out of them. `TICKET_COMMANDS` and `TICKET_SECOND_COMMANDS` by position, `TICKET_FLAGS`, `CAMPAIGN_COMMANDS`, `CAMPAIGN_NESTED_COMMANDS`, `CAMPAIGN_FLAGS` and `TAG_FLAGS` likewise, the last against the test-local `TAG_LIST_FLAGS` and `TAG_NEW_COMMANDS` for the tag arguments that deliberately get no rule. The walk is one function, `every_argument`, since the tests differ only in the id they look for. Each also asserts *how many* it found, so a walker that stopped finding arguments fails rather than passing every assertion by never running them
- **The completion lists are kept honest by tests** that walk the clap definition and fail on an argument left out of them. `TICKET_COMMANDS` and `TICKET_SECOND_COMMANDS` by position, `TICKET_FLAGS`, `CAMPAIGN_COMMANDS`, `CAMPAIGN_NESTED_COMMANDS`, `CAMPAIGN_FLAGS` and `TAG_FLAGS` likewise, the last against the test-local `TAG_LIST_FLAGS` and `TAG_NEW_COMMANDS` for the tag arguments that deliberately get no rule. The walk is one function, `every_argument`, since the tests differ only in the id they look for. Each also asserts *how many* it found, so a walker that stopped finding arguments fails rather than passing every assertion by never running them
- **A campaign is named in three shapes and a tag in one**, which is why the lists are not one list: a campaign is `assign`'s first positional, the first argument of `campaign show` and `campaign set`, and `ls --campaign`'s value, and each needs a different fish rule. A tag is only ever an option value, and its values come from a different command
- **`TICKET_FLAGS` holds pairs, the command and the flag, and every ticket rule in the fish half is generated from it.** Three commands carry a `--clear` and each takes something different: `chown`'s names the ticket, since with no user to give there is no positional for it; `relate`'s takes the kind and then the target; `set`'s takes a field name clap already offers. So the command is half of the fact, and a list of bare flag names could only be scoped by hand, which is what the `chown --clear` rule used to be. `every_ticket_flag_is_a_real_option` walks each pair against the command it names, which a flat list could not do
- **The lists answer the board as well as the shell.** `ticket_argument` reads the three ticket lists to say whether the argument at a given index of a typed line is a ticket, which is what the command prompt's suggestion is built on, see [tui-prompts.md](tui-prompts.md). Position is counted there exactly as it is counted here, so the two cannot come to disagree about `mv --status done <ticket>`; a second table on the board would be a copy the tests above never read
- **`TAG_FLAGS` holds pairs where the others hold names**, because the two options that take one tag are spelled differently: `ls --tag` filters on one and `append --tags` adds them, so a single rule naming both would tell fish that each command accepts the other's flag. One rule per pair, generated in `fish_dynamic`. `add --tags` and `set --tags` are in the test's `TAG_LIST_FLAGS` instead and get no rule: they take the *whole* list, so the token at the cursor is `bug,tui` and fish replaces a token entire, which would throw away what is already typed. The test walks both `tag` and `tags`, so the next one has to say which of the two it is
- **`ls --ids` is the machine-readable listing**, and one of the three outputs in the tree meant to be parsed: the short id, a tab, the title, unstyled. Two fields because fish reads what follows a tab as the description and a script reads `cut -f1`. It includes done, which the decorated listing leaves out, because a completion has to be able to name a finished ticket, and it skips a missing status directory rather than refusing, because an error printed over a shell prompt is worse than no candidates. The filters still apply, and an unknown `--campaign` is still refused
- **`campaign ls --ids` is the same flag and the same two fields for the other object**, and it deliberately skips the member counting the decorated listing does: `members()` reads every ticket on the board, and the caller here is a shell filling in candidates on a keypress. It also prints nothing on an empty board, where the decorated listing prints a sentence, because a sentence offered as a candidate is worse than no candidates
+14 -1
View File
@@ -19,7 +19,7 @@ The three input modes on the board and the key that copies an id off it.
## 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
- **`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. `tab` is the mode's too, for the reason the recall walk is: what it takes is made of the selected card and the line is only where it lands
- **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
@@ -46,6 +46,19 @@ The three input modes on the board and the key that copies an id off it.
- **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
## The Prompt's Suggestion
- **The reader has the ticket selected, which is the whole reason to type a command on the board rather than in a shell.** So where the line is naming a ticket, the selected card's short id is drawn after the cursor and `tab` takes it into the line. Retyping seven characters off the card in front of you is the friction the prompt exists to remove
- **Where a ticket goes is `completions.rs`'s answer, not a table written here.** `ticket_argument` reads `TICKET_COMMANDS`, `TICKET_SECOND_COMMANDS` and `TICKET_FLAGS`, which `no_ticket_taking_subcommand_is_missing` and `every_ticket_flag_is_a_real_option` keep honest against clap's own definition. A second copy of the positions on the board would go wrong the next time a command grew a ticket argument, and it would go wrong *silently*, since the test guarding the first copy would not read the second
- **The ticket is not always the first argument, which is what a naive version gets wrong.** `chown alice <ticket>` and `assign v1 <ticket>` put it second on purpose, `relate x --blocked-by <ticket>` puts it in a flag, and `chown --clear <ticket>` does too, since with no user to give there is no positional for it. Nothing is offered where the argument is something else: `relate --clear` wants a kind, `set --clear` wants a field name, and `chown`'s first argument is a user
- **Position is counted, exactly as the fish half counts it**, so `mv --status done <ticket>` is not recognised: the ticket is `mv`'s first positional and the third argument on the line. That is the fish rules' limit as well, and one table answering both is worth more than two tables disagreeing about the corner
- **`TICKET_FLAGS` holds pairs, command and flag, where it used to hold bare flag names.** Three commands carry a `--clear` and only `chown`'s names a ticket, so the command is half of the fact. It also took the last hand-written ticket rule out of the fish block: every one of them is generated from the table now, and `every_ticket_flag_is_a_real_option` walks each pair against the command it names
- **It is derived on every call and held nowhere.** `Board::suggestion` is asked once by the draw and once by `tab`, so what is on the row and what the key takes cannot disagree, and there is no field to go stale when the line is edited or the selection moves. That is also what "any other key drops it" means: nothing is dropped, because nothing was kept
- **Only with the cursor at the end of the line.** The suggestion is drawn after the cursor and `tab` inserts it there, so with a command continuing past the cursor both would land in the middle of something already typed. A shell's own suggestion keeps the same rule
- **Only while the selected id still starts with what is typed**, and what is drawn is the rest of it rather than the whole. A suggestion that no longer matches the argument under it describes nothing, and once the whole id is typed there is nothing left to add, so a second `tab` cannot double it
- **Nothing with no card under the selection.** On the frame the selection is on a campaign or a tag, and neither is a ticket: `y` copies what the frame names there, and this deliberately does not
- **Grey, which is what the palette already spends on a value that is not one.** It is not in the line until `tab` puts it there, and the reader has to tell the two apart at a glance. It takes what the line leaves of the field rather than a field of its own, so a long command loses the offer instead of pushing `esc` and `enter` off the row. The terminal cursor sits on its first cell, which is where the cursor already was
- **The row does not name `tab`.** The prompt names its two mode keys and no more — `ctrl-w`, the arrows and home and end are unnamed there as well — and the suggestion under the cursor is its own affordance
## 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
+168 -33
View File
@@ -39,8 +39,54 @@ pub const TICKET_COMMANDS: [&str; 8] = [
/// campaign, and neither is completed.
pub const TICKET_SECOND_COMMANDS: [&str; 2] = ["chown", "assign"];
/// Options whose value is a ticket, as `relate` takes three of.
pub const TICKET_FLAGS: [&str; 3] = ["blocked-by", "regression-of", "relates-to"];
/// Options whose value is a ticket, as the command that takes it and the flag's long name.
///
/// Pairs rather than the flat list this was, for the reason [`TAG_FLAGS`] holds pairs: the
/// commands spell their flags differently and one of the spellings is shared. `relate`'s
/// three take a ticket, `chown --clear` takes one because there is no user to give it, and
/// `set --clear` takes a field name and `relate --clear` a relation kind — so a list of
/// bare flag names would offer tickets to those two as well.
pub const TICKET_FLAGS: [(&str, &str); 4] = [
("relate", "blocked-by"),
("relate", "regression-of"),
("relate", "relates-to"),
("chown", "clear"),
];
/// Whether the argument at `index` of a typed line names a ticket, `index` counting from
/// the verb at 0.
///
/// The three tables above read as one question, which is what the board's command prompt
/// asks to decide whether to suggest the selected ticket. Here rather than in `tui.rs`
/// because the tables are here and the tests that keep them honest against clap are here:
/// a second copy of the positions on the board would go wrong the next time a command grew
/// a ticket argument, and `no_ticket_taking_subcommand_is_missing` would not notice.
///
/// Position is counted rather than inferred, exactly as the fish half counts it, so
/// `mv --status done <ticket>` is not recognised: the ticket is `mv`'s first *positional*
/// and the third argument on the line. That is the fish rules' limit too, and one table
/// answering both is worth more than two tables disagreeing about the corner.
pub fn ticket_argument(args: &[String], index: usize) -> bool {
// The verb is not a ticket, and index 0 read against the tables below would make
// `show` its own first argument.
if index == 0 {
return false;
}
let Some(verb) = args.first().map(String::as_str) else {
return false;
};
// A flag's value is the argument after it, which is where the flag sits whether the
// value is half typed or has not been started.
if let Some(flag) = args.get(index - 1).and_then(|arg| arg.strip_prefix("--"))
&& TICKET_FLAGS.contains(&(verb, flag))
{
return true;
}
(index == 1 && TICKET_COMMANDS.contains(&verb))
|| (index == 2 && TICKET_SECOND_COMMANDS.contains(&verb))
}
/// Subcommands whose first positional is a campaign.
///
@@ -107,7 +153,16 @@ pub fn generate(shell: Shell, out: &mut dyn io::Write) -> io::Result<()> {
fn fish_dynamic() -> String {
let ticket_commands = TICKET_COMMANDS.join(" ");
let ticket_second_commands = TICKET_SECOND_COMMANDS.join(" ");
let ticket_flags = flag_list(&TICKET_FLAGS);
let ticket_flags = TICKET_FLAGS
.iter()
.map(|(command, flag)| {
format!(
"complete -c worklane -n '__worklane_command_is {command}' -l {flag} \\\n \
-x -a '(worklane ls --ids)'"
)
})
.collect::<Vec<_>>()
.join("\n");
let relation_kinds = RelationKind::ALL
.iter()
.map(|kind| kind.flag())
@@ -207,17 +262,19 @@ complete -c worklane \
-n '__worklane_first_arg_of {ticket_commands}; or __worklane_second_arg_of {ticket_second_commands}' \
-a '(worklane ls --ids)'
# `relate` takes its targets as option values, so they are completed by flag rather
# than by position.
complete -c worklane -n '__worklane_command_is relate' {ticket_flags} \
-x -a '(worklane ls --ids)'
# The options whose value is a ticket, one rule per command so that a flag is offered
# only under the command that has it. `relate` takes its three targets this way rather
# than by position, and `chown --clear` names the ticket in the flag because with no user
# to give there is no second positional to put it in. Its `--clear` is scoped like every
# other: `set` has one that takes a field name, which clap already offers.
{ticket_flags}
# `--clear` is the one flag here whose first value is not a ticket, so it is not in
# `TICKET_FLAGS`: it takes the kind and then the target, and offering a board listing where
# the kind goes would be offering values it refuses. The kinds come from `RelationKind`
# rather than being spelled again. The target gets no rule at all, deliberately: fish
# completes the value after the flag, and the case this flag exists for is a target whose
# ticket is gone, which no listing of the board can offer.
# `relate --clear` is the one `--clear` whose first value is not a ticket, which is why
# that pair is not in `TICKET_FLAGS`: it takes the kind and then the target, and offering a
# board listing where the kind goes would be offering values it refuses. The kinds come
# from `RelationKind` rather than being spelled again. The target gets no rule at all,
# deliberately: fish completes the value after the flag, and the case this flag exists for
# is a target whose ticket is gone, which no listing of the board can offer.
complete -c worklane -n '__worklane_command_is relate' -l clear -x -a '{relation_kinds}'
# Campaigns, from `campaign ls --ids`, in the three places one is named: `assign`'s first
@@ -247,11 +304,6 @@ complete -c worklane -n '__worklane_command_is ls' {campaign_flags} \
# here are the values it refuses.
complete -c worklane -n '{tag_nested}' -a '(worklane tags | cut -f1,3)'
# `chown --clear` names the ticket in the flag too, since with no user to give there is
# no second positional to put it in. Scoped to `chown`, unlike the rule above: `set`
# has a `--clear` of its own that takes a field name, and clap already offers those.
complete -c worklane -n '__worklane_command_is chown' -l clear -x -a '(worklane ls --ids)'
# clap emits the statuses for `mv --status`, which is an option, but the fish generator
# emits nothing for the possible values of a positional. So the one place a status is
# positional needs them listed, and the same goes for the shell below.
@@ -489,6 +541,70 @@ mod tests {
);
}
/// One line, split the way the prompt splits it, and the argument the cursor is in.
///
/// The splitter is the prompt's own, so a row here states a line as a reader types it
/// rather than as a hand-written argument vector, and the two cannot come apart.
fn at_cursor(line: &str) -> bool {
let split = crate::split::split_line(line, line.len());
ticket_argument(&split.args, split.cursor_arg)
}
/// Every position a ticket is taken in, as the line that reaches it.
#[test]
fn a_ticket_is_recognised_in_every_position_a_command_takes_one() {
for line in [
// The first positional, begun and half typed.
"show ",
"mv 1b7",
"comment ",
"check ",
// The second, where the first argument is a user or a campaign.
"chown alice ",
"assign v1 1b7",
// The three of `relate`'s flags, and `chown --clear`, which names the ticket
// in the flag because there is no user to give.
"relate 1b78a24 --blocked-by ",
"relate 1b78a24 --regression-of 417",
"relate 1b78a24 --relates-to ",
"chown --clear ",
] {
assert!(
at_cursor(line),
"`{line}` takes a ticket at the cursor and was not offered one"
);
}
}
/// And every position that takes something else, which is the half a naive version
/// gets wrong.
#[test]
fn nothing_is_recognised_where_the_argument_is_not_a_ticket() {
for line in [
// The verb, on an empty line and on a half typed one.
"",
"m",
// `chown`'s first argument is a user and `assign`'s is a campaign.
"chown ",
"assign ",
// `ls` takes a status and `campaign show` a campaign.
"ls ",
"campaign show ",
// The two `--clear` flags that are spelled like `chown`'s and take something
// else: a relation kind, and a field name.
"relate 1b78a24 --clear ",
"set 1b78a24 --clear ",
// `relate --clear`'s second value is a ticket the board no longer holds, which
// is what the flag exists for, so nothing on the board is offered there.
"relate 1b78a24 --clear blocked-by ",
// One past the ticket, which is `mv`'s status and `comment`'s body.
"mv 1b78a24 ",
"comment 1b78a24 the ",
] {
assert!(!at_cursor(line), "`{line}` was offered a ticket where none goes");
}
}
/// **Every rule naming an option has to say which command it belongs to.** A bare
/// `complete -c worklane -l tag` tells fish the whole tool takes `--tag`, so
/// `worklane init --<TAB>` offered six options that only `relate` and `ls` have. This
@@ -522,6 +638,7 @@ mod tests {
.chain(TICKET_SECOND_COMMANDS.iter())
.chain(CAMPAIGN_COMMANDS.iter())
.copied()
.chain(TICKET_FLAGS.iter().map(|(command, _)| *command))
.chain(TAG_FLAGS.iter().map(|(command, _)| *command));
for name in top {
@@ -545,7 +662,9 @@ mod tests {
/// bare search for `-l clear` cannot tell them apart, which is what this used to do.
#[test]
fn each_clear_flag_is_completed_for_its_own_command() {
let script = fish_dynamic();
// Continuations joined first: a rule is one logical line, and the generated ones
// put the flag and its values on either side of a trailing backslash.
let script = fish_dynamic().replace("\\\n", " ");
let cli = command();
let rule_for = |name: &str| {
@@ -558,6 +677,7 @@ mod tests {
&& line.contains(&scope)
})
.unwrap_or_else(|| panic!("no --clear rule for {name}:\n{script}"))
.to_string()
};
let has_clear = |name: &str| {
@@ -584,26 +704,41 @@ mod tests {
"tickets offered where the kind goes: {relate}"
);
// `set --clear` takes a field name, which clap already offers, and `relate --clear`
// takes a kind. Only `chown`'s names a ticket, so only that pair is in the table.
assert!(
!TICKET_FLAGS.contains(&"clear"),
"--clear in TICKET_FLAGS would offer tickets to `set --clear`, which takes a field"
!TICKET_FLAGS.contains(&("set", "clear")),
"`set --clear` in TICKET_FLAGS would offer tickets where a field name goes"
);
assert!(
!TICKET_FLAGS.contains(&("relate", "clear")),
"`relate --clear` in TICKET_FLAGS would offer tickets where the kind goes"
);
assert!(
TICKET_FLAGS.contains(&("chown", "clear")),
"`chown --clear` names the ticket in the flag, so it belongs in TICKET_FLAGS"
);
}
/// The flags completed as tickets have to be `relate`'s actual flags, or the rule
/// completes an option nothing accepts.
/// Each flag completed as a ticket has to be an option its own command actually takes,
/// or the rule completes something nothing accepts.
///
/// The command is half of what is checked, which is what the pairs bought: a flat list
/// could only be walked against one command, and `--clear` is spelled the same on three.
#[test]
fn every_ticket_flag_is_a_real_option() {
let relate = command()
.find_subcommand("relate")
.expect("relate exists")
.clone();
let known: Vec<String> = relate
.get_arguments()
.filter_map(|arg| arg.get_long().map(str::to_string))
.collect();
for flag in TICKET_FLAGS {
assert!(known.contains(&flag.to_string()), "`relate` has no --{flag}");
let cli = command();
for (name, flag) in TICKET_FLAGS {
let subcommand = cli
.find_subcommand(name)
.unwrap_or_else(|| panic!("no such subcommand: {name}"));
assert!(
subcommand
.get_arguments()
.any(|arg| arg.get_long() == Some(flag)),
"`worklane {name}` has no --{flag}"
);
}
}
+285 -12
View File
@@ -28,6 +28,7 @@ use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph, Tabs, Wrap};
use crate::campaign::{CAMPAIGNS_DIR, CampaignIndex, Standing};
use crate::command::{Cli, Command};
use crate::completions::ticket_argument;
use crate::history::{HistoryPart, history_parts, history_root, read_history};
use crate::split::{Unclosed, split_line};
use crate::tag::{NO_ANNOTATION, TAGS_FILE, TagIndex};
@@ -748,11 +749,11 @@ impl Prompt {
/// `esc` and `enter`: those two are the mode's rather than the line's, and they are
/// answered by [`Board::prompt_key`] before it gets here.
///
/// Left, right, home, end, backspace and `ctrl-w` behave as they do in a shell. There
/// is no `tab`: suggesting the selected ticket is 00a14e7, and until then it is a key
/// that must not reach the project tabs. Every other control combination lands on the
/// last arm and does nothing, which is what stops `ctrl-d` reaching the action bound
/// to `d`.
/// Left, right, home, end, backspace and `ctrl-w` behave as they do in a shell. `tab`
/// is not here either: it takes the suggestion, which is the board's rather than the
/// line's, since the selected card is what it is made of. Every other control
/// combination lands on the last arm and does nothing, which is what stops `ctrl-d`
/// reaching the action bound to `d`.
fn key(&mut self, key: KeyEvent, control: bool) {
match key.code {
KeyCode::Left if !control => self.cursor = self.left_of(),
@@ -763,15 +764,22 @@ impl Prompt {
// with it. Movement above deliberately does not clear it.
KeyCode::Backspace if !control => self.cut(self.left_of()),
KeyCode::Char('w') if control => self.cut(self.word_start()),
KeyCode::Char(typed) if !control => {
self.line.insert(self.cursor, typed);
self.cursor += typed.len_utf8();
self.refused = None;
}
KeyCode::Char(typed) if !control => self.insert(typed.encode_utf8(&mut [0; 4])),
_ => {}
}
}
/// Put `text` into the line at the cursor and leave the cursor after it, which is what
/// typing it would have done.
///
/// The message about the last enter goes with it, for the reason every other edit takes
/// it away: it was about a line that is no longer on the row.
fn insert(&mut self, text: &str) {
self.line.insert_str(self.cursor, text);
self.cursor += text.len();
self.refused = None;
}
/// The offset one character to the left of the cursor, or the cursor itself at the
/// start of the line. By characters, so a cursor never lands inside one.
fn left_of(&self) -> usize {
@@ -2355,6 +2363,9 @@ impl Board {
// the whole board rather than the prompt alone.
KeyCode::Up if !control => self.recall_back(),
KeyCode::Down if !control => self.recall_forward(),
// The mode's too, and never the tabs': every key belongs to the prompt while it
// is open, and what this one takes is made of the selected card.
KeyCode::Tab if !control => self.accept_suggestion(),
_ => {
if let Some(prompt) = &mut self.prompt {
prompt.key(key, control);
@@ -2365,6 +2376,64 @@ impl Board {
true
}
/// The rest of the selected card's short id where the line is naming a ticket, or
/// nothing anywhere else.
///
/// Derived on every call rather than held on the [`Prompt`], so it cannot come to
/// describe a line that has since been edited or a card the selection has left: what
/// the row draws and what `tab` takes are one answer, asked twice.
///
/// Where a ticket goes is [`ticket_argument`]'s, off the tables the fish completions
/// are generated from. A copy of those positions here would go wrong the next time a
/// command grew a ticket argument, and the test that keeps them honest against clap
/// would not notice.
///
/// Three things have to hold, and each is its own answer to the reader:
///
/// - **The cursor is at the end of the line.** The suggestion is drawn after it and
/// `tab` inserts it there, so with the line continuing past the cursor both would be
/// in the middle of something already typed. That is the rule a shell's own
/// suggestion keeps.
/// - **A card is selected.** On the frame there is none, and the frame's own id is a
/// campaign or a tag rather than a ticket.
/// - **What is already typed is still a prefix of the id.** A suggestion that no longer
/// matches the argument under it describes nothing, and what is left is empty once
/// the whole id is typed.
pub fn suggestion(&self) -> Option<&str> {
let prompt = self.prompt.as_ref()?;
if prompt.cursor != prompt.line.len() {
return None;
}
let id = self.current().selected()?.ticket.id.as_str();
let split = split_line(&prompt.line, prompt.cursor);
if !ticket_argument(&split.args, split.cursor_arg) {
return None;
}
// Empty where the argument has been opened and nothing of it is behind the cursor,
// which is the whole id offered.
let typed = match split.at_new_arg {
true => "",
false => split.args[split.cursor_arg].as_str(),
};
id.strip_prefix(typed).filter(|rest| !rest.is_empty())
}
/// `tab`: take the suggestion into the line, or do nothing where there is none.
///
/// The whole board rather than the prompt alone, for the reason the recall walk is:
/// the suggestion is made of the selected card and the line is only where it lands.
fn accept_suggestion(&mut self) {
let Some(rest) = self.suggestion().map(str::to_string) else {
return;
};
if let Some(prompt) = &mut self.prompt {
prompt.insert(&rest);
}
}
/// `up`: one line further back, or the newest line when the reader is still on their
/// own.
///
@@ -4554,7 +4623,7 @@ fn draw_footer(frame: &mut Frame, board: &Board, area: Rect) {
// and is asked first: an input cannot be covered, and it is the newest of the three
// things that can hold the row. Only one of the two prompts can be open at a time.
if let Some(prompt) = &board.prompt {
draw_prompt(frame, prompt, area);
draw_prompt(frame, prompt, board.suggestion(), area);
return;
}
@@ -4760,7 +4829,14 @@ const PROMPT_OPENING: &str = " :";
///
/// `run` and not `check`: enter hands the line to the same handler the shell reaches, and
/// the row has to say which of the two it is about to do.
fn draw_prompt(frame: &mut Frame, prompt: &Prompt, area: Rect) {
///
/// The suggestion is drawn after the line in grey, which is what the palette already spends
/// on a value that is not one: it is not in the line until `tab` puts it there, and the
/// reader has to be able to tell the two apart at a glance. It takes what the line leaves
/// of the field rather than a field of its own, so a long command loses the offer instead
/// of pushing the keys off the row. The terminal cursor sits on its first cell, which is
/// where the cursor already was: what is under a cursor is still what will be typed over.
fn draw_prompt(frame: &mut Frame, prompt: &Prompt, suggestion: Option<&str>, area: Rect) {
let keys = key_hints(&[("↵", "run"), ("esc", "cancel")]);
let spent: usize = PROMPT_OPENING.chars().count()
+ KEY_GAP.chars().count()
@@ -4775,6 +4851,18 @@ fn draw_prompt(frame: &mut Frame, prompt: &Prompt, area: Rect) {
let (visible, at) = prompt_window(&prompt.line, prompt.cursor, room);
let mut spans = vec![Span::raw(format!("{PROMPT_OPENING}{visible}"))];
// Only ever drawn where the cursor is at the end of the line, which is what `visible`
// then ends at, so this follows the cursor rather than landing in front of the tail of
// a command.
if let Some(rest) = suggestion {
let left = room.saturating_sub(visible.chars().count());
let shown: String = rest.chars().take(left).collect();
if !shown.is_empty() {
spans.push(Span::styled(shown, Style::default().fg(Color::Grey.into())));
}
}
match &prompt.refused {
Some(message) => spans.push(Span::styled(
format!("{KEY_GAP}{message}"),
@@ -9047,6 +9135,191 @@ approach";
assert_eq!(prompt_line(&board), Some("ls backlog"), "the walk did not happen");
}
// ---------------------------------------------------------------- the suggestion --
/// The id of the card the selection is on, which is what every suggestion is made of.
fn selected_id(board: &Board) -> String {
board.selected().expect("a card is selected").ticket.id.clone()
}
/// Open a fresh prompt, type `line` into it, and answer what is suggested at the end
/// of it.
fn suggested(board: &mut Board, line: &str) -> Option<String> {
board.open_prompt();
type_line(board, line);
let rest = board.suggestion().map(str::to_string);
board.prompt = None;
rest
}
/// Every position a command takes a ticket in, driven as a line typed at the prompt.
///
/// The whole id where the argument has been opened and nothing typed into it, which is
/// the ordinary case: the reader has the card in front of them and presses tab.
#[test]
fn the_selected_id_is_suggested_wherever_a_ticket_argument_goes() {
let (_root, mut board) = runnable();
let id = selected_id(&board);
for line in [
// The first positional.
"show ",
"mv ",
"comment ",
// The second, which is where `chown` and `assign` put it on purpose.
"chown alice ",
"assign v1 ",
// `relate`'s three flags, and `chown --clear`, which names the ticket in the
// flag since there is no user to give.
"relate 2222222 --blocked-by ",
"relate 2222222 --regression-of ",
"relate 2222222 --relates-to ",
"chown --clear ",
] {
assert_eq!(
suggested(&mut board, line).as_deref(),
Some(id.as_str()),
"`{line}` takes a ticket and was offered nothing"
);
}
}
/// And every position that takes something else, which is the half a version counting
/// the first argument alone gets wrong.
#[test]
fn nothing_is_suggested_where_the_argument_is_not_a_ticket() {
let (_root, mut board) = runnable();
for line in [
// The verb itself, empty and half typed.
"",
"sh",
// `chown` takes a user first and `assign` a campaign.
"chown ",
"assign ",
// A status, and a campaign under its own command.
"ls ",
"campaign show ",
// The two `--clear` flags spelled like `chown`'s: a relation kind, and a field.
"relate 2222222 --clear ",
"set 2222222 --clear ",
// And `relate --clear`'s second value, which is a target the board may no
// longer hold.
"relate 2222222 --clear blocked-by ",
// One past the ticket, where `mv` wants a status.
"mv 2222222 ",
] {
assert_eq!(
suggested(&mut board, line),
None,
"`{line}` was offered a ticket where none goes"
);
}
}
/// A partly typed argument is completed rather than restated, and only while the
/// selected id still starts with what is on the row.
#[test]
fn a_half_typed_argument_is_suggested_only_while_the_id_starts_with_it() {
let (_root, mut board) = runnable();
let id = selected_id(&board);
let head = &id[..3];
assert_eq!(
suggested(&mut board, &format!("show {head}")).as_deref(),
Some(&id[3..]),
"the part already typed was offered a second time"
);
// The whole id typed leaves nothing to add, which is not a suggestion of nothing.
assert_eq!(suggested(&mut board, &format!("show {id}")), None);
// And a prefix the selection does not match describes another ticket entirely.
assert_eq!(suggested(&mut board, "show 9"), None);
}
/// Nothing is suggested with no card under the selection: on the frame, where the
/// selection is on a campaign or a tag rather than a ticket, and on an empty board.
#[test]
fn nothing_is_suggested_when_no_card_is_selected() {
let (_root, mut board) = runnable();
board.current_mut().focus = Focus::Frame;
assert_eq!(suggested(&mut board, "show "), None, "the frame offered a ticket");
let empty = board_dir();
let mut bare = one_board(&empty);
assert_eq!(suggested(&mut bare, "show "), None, "an empty board offered a ticket");
}
/// What the row shows: the suggestion after the cursor, in grey, and not in the line.
#[test]
fn the_suggestion_is_drawn_after_the_cursor_in_grey_and_is_not_yet_the_line() {
let (_root, mut board) = runnable();
let id = selected_id(&board);
board.open_prompt();
type_line(&mut board, "show ");
let row = footer(&board, SCREEN.width, SCREEN.height);
assert!(row.contains(&format!(":show {id}")), "the suggestion is not drawn: {row}");
assert_eq!(prompt_line(&board), Some("show "), "the suggestion went into the line");
let (colour, _) = footer_style(&board, SCREEN.width, SCREEN.height, &id);
assert_eq!(colour, Color::Grey.into(), "the suggestion is not drawn as a placeholder");
// The cursor stays where the line ends, which is the first cell of the suggestion.
let mut terminal = Terminal::new(TestBackend::new(SCREEN.width, SCREEN.height)).unwrap();
terminal.draw(|frame| draw(frame, &board)).unwrap();
let at = terminal.get_cursor_position().unwrap();
assert_eq!(at.x as usize, PROMPT_OPENING.chars().count() + "show ".len());
}
/// Tab takes it, and any other key leaves the line to say what was typed.
#[test]
fn tab_takes_the_suggestion_into_the_line_and_another_key_drops_it() {
let (_root, mut board) = runnable();
let id = selected_id(&board);
board.open_prompt();
type_line(&mut board, "show ");
press_prompt(&mut board, KeyCode::Tab);
assert_eq!(prompt_line(&board), Some(format!("show {id}").as_str()));
// Nothing is left to offer once the whole id is on the row, so a second press
// cannot double it.
press_prompt(&mut board, KeyCode::Tab);
assert_eq!(prompt_line(&board), Some(format!("show {id}").as_str()));
// And a key that takes the line off the id drops the offer rather than holding it.
board.prompt = None;
board.open_prompt();
type_line(&mut board, "show 9");
assert_eq!(board.suggestion(), None);
press_prompt(&mut board, KeyCode::Tab);
assert_eq!(prompt_line(&board), Some("show 9"), "tab typed a suggestion that had gone");
}
/// The swallow contract for the one key this ticket gives the prompt: `tab` is the
/// project key on the board and must not be while a command is being typed.
#[test]
fn tab_in_the_prompt_never_changes_the_project_tab() {
let alpha = board_dir();
let beta = board_dir();
write_ticket(&alpha, "backlog", "11111111-0000-4000-8000-000000000000", "Alpha", "");
write_ticket(&beta, "backlog", "22222222-0000-4000-8000-000000000000", "Beta", "");
let mut board = boards(&[("alpha", &alpha), ("beta", &beta)]);
assert!(board.tabbed(), "the fixture has nothing to switch between");
board.open_prompt();
// Both with something to take and with nothing, since the arm has to be the
// prompt's either way.
for line in ["show ", "ls "] {
board.prompt = Some(Prompt::default());
type_line(&mut board, line);
assert!(board.prompt_key(typed(KeyCode::Tab), SCREEN), "tab was not taken");
assert_eq!(board.tab, 0, "`{line}` let tab through to the tabs");
}
}
// ------------------------------------------------------------------ the copy key --
/// A sink that records what it was handed, standing in for the clipboard.