(eadaca9) Add check and uncheck for one acceptance criterion, and bump to 0.6.0
Criteria are stored as - [ ] lines and nothing could tick one, so marking progress meant replacing the whole list through set, losing the state of every other item. check and uncheck are separate commands rather than one verb with a flag, sharing CheckArgs and cmd_check. The criterion is an index first and a substring second, so a criterion whose text is literally "2" is reachable only by substring. A number out of range is reported as a bad index rather than as a substring that missed. Only the character between the brackets is spliced, so the text keeps whatever indentation and spacing it had. Criteria are counted inside the Acceptance Criteria section only, so a checkbox written into a description is not a criterion and a handwritten line in the section takes no number. With the criterion omitted, the criteria are listed and the number asked for when stdin is a terminal. Where it is not, the same list comes back as an error instead. Prompting regardless would leave the opencode wrapper blocked on a stdin that never closes, and that is the choice resolve_ticket already makes for an ambiguous match, for the same reason: the wrapper keeps stderr and throws on the code, so a list printed to stdout would be discarded. Any listing of a subset keeps the real numbers. Renumbering the matches from one would contradict the advice to use the number and point at the wrong criterion. show now numbers the criteria as "1. [x] text", so the index is visible where the ticket is read. That also drops a manual prefix strip, so clippy is one warning below where it started. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
3810e442c6
commit
bbb1568a0f
@@ -140,6 +140,12 @@ pub enum Command {
|
||||
/// Add to a section of an existing ticket
|
||||
Append(AppendArgs),
|
||||
|
||||
/// Tick an acceptance criterion
|
||||
Check(CheckArgs),
|
||||
|
||||
/// Untick an acceptance criterion
|
||||
Uncheck(CheckArgs),
|
||||
|
||||
/// Add a comment to a ticket
|
||||
Comment {
|
||||
/// Ticket UUID or title
|
||||
@@ -225,3 +231,19 @@ pub struct AppendArgs {
|
||||
#[arg(short, long, allow_hyphen_values = PROSE)]
|
||||
pub plan: Vec<String>,
|
||||
}
|
||||
|
||||
/// `check` and `uncheck` are separate commands rather than one verb with a flag,
|
||||
/// because the two read as opposites at the call site and neither needs the other's
|
||||
/// name to make sense.
|
||||
///
|
||||
/// The criterion is optional: without it the criteria are listed and the index asked
|
||||
/// for, or, where there is no terminal to ask at, returned as an error carrying the
|
||||
/// same list.
|
||||
#[derive(clap::Args)]
|
||||
pub struct CheckArgs {
|
||||
/// Ticket UUID or title
|
||||
pub ticket: String,
|
||||
|
||||
/// Criterion index as shown by `show`, or a substring of its text. Prompts if omitted
|
||||
pub criterion: Option<String>,
|
||||
}
|
||||
|
||||
+120
-2
@@ -1,10 +1,10 @@
|
||||
use anyhow::{Result, anyhow};
|
||||
use chrono::Local;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::io::{BufRead, IsTerminal, Write};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::command::{AppendArgs, SetArgs};
|
||||
use crate::command::{AppendArgs, CheckArgs, SetArgs};
|
||||
use crate::util::*;
|
||||
|
||||
pub fn cmd_init(start: &Path) -> Result<()> {
|
||||
@@ -749,3 +749,121 @@ pub fn cmd_append(args: &AppendArgs, start: &Path) -> Result<()> {
|
||||
report(&changed, &title, "Appended to");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Which criterion to act on, asked for when the caller did not say.
|
||||
///
|
||||
/// A terminal gets the list and a prompt. Anything else gets the same list as an
|
||||
/// error, which is what `resolve_ticket` does for an ambiguous match and for the same
|
||||
/// reason: the opencode wrapper throws on the exit code and keeps stderr, so a list
|
||||
/// printed to stdout would be discarded and an agent would learn nothing. Prompting
|
||||
/// regardless would leave it blocked on a stdin that never closes.
|
||||
fn ask_which_criterion(criteria: &[Criterion], title: &str) -> Result<usize> {
|
||||
let listing = criteria_list(criteria);
|
||||
|
||||
if !std::io::stdin().is_terminal() {
|
||||
return Err(anyhow!(
|
||||
"Which criterion of '{}'?\n{}\n\nRe-run with the number, or a substring of the text.",
|
||||
title,
|
||||
listing
|
||||
));
|
||||
}
|
||||
|
||||
println!("Acceptance criteria of '{}':", color_text(title, "green"));
|
||||
println!("{}", listing);
|
||||
// Either form, since find_criteria takes both.
|
||||
print!("Number or text: ");
|
||||
std::io::stdout().flush()?;
|
||||
|
||||
let mut answer = String::new();
|
||||
std::io::stdin().lock().read_line(&mut answer)?;
|
||||
|
||||
// An empty needle matches every criterion, so it would otherwise be reported as
|
||||
// ambiguous rather than as nothing having been chosen.
|
||||
if answer.trim().is_empty() {
|
||||
return Err(anyhow!("No criterion chosen"));
|
||||
}
|
||||
|
||||
let chosen = find_criteria(criteria, &answer);
|
||||
match chosen.len() {
|
||||
1 => Ok(chosen[0]),
|
||||
0 => Err(anyhow!(
|
||||
"'{}' matches none of the {} criteria",
|
||||
answer.trim(),
|
||||
criteria.len()
|
||||
)),
|
||||
_ => Err(anyhow!(
|
||||
"'{}' matches more than one criterion, use the number",
|
||||
answer.trim()
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cmd_check(args: &CheckArgs, checked: bool, start: &Path) -> Result<()> {
|
||||
let worklane_dir = find_worklane_dir(start)?;
|
||||
let (status, title, content) = resolve_ticket(&args.ticket, &worklane_dir)?;
|
||||
|
||||
let criteria = criteria(&content);
|
||||
if criteria.is_empty() {
|
||||
return Err(anyhow!("Ticket '{}' has no acceptance criteria", title));
|
||||
}
|
||||
|
||||
let chosen = match &args.criterion {
|
||||
None => ask_which_criterion(&criteria, &title)?,
|
||||
Some(search) => {
|
||||
let found = find_criteria(&criteria, search);
|
||||
match found.len() {
|
||||
1 => found[0],
|
||||
0 if is_index(search) => {
|
||||
return Err(anyhow!(
|
||||
"'{}' is not one of the {} criteria of '{}':\n{}",
|
||||
search.trim(),
|
||||
criteria.len(),
|
||||
title,
|
||||
criteria_list(&criteria)
|
||||
));
|
||||
}
|
||||
0 => {
|
||||
return Err(anyhow!(
|
||||
"No criterion of '{}' matches '{}'. Its criteria are:\n{}",
|
||||
title,
|
||||
search,
|
||||
criteria_list(&criteria)
|
||||
));
|
||||
}
|
||||
_ => {
|
||||
return Err(anyhow!(
|
||||
"'{}' matches {} criteria of '{}':\n{}\n\nUse the number instead.",
|
||||
search,
|
||||
found.len(),
|
||||
title,
|
||||
numbered_criteria(&criteria, &found)
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let criterion = &criteria[chosen];
|
||||
let verb = if checked { "checked" } else { "unchecked" };
|
||||
|
||||
if criterion.checked == checked {
|
||||
println!(
|
||||
"Already {}: {}",
|
||||
verb,
|
||||
color_text(&criterion.text, "green")
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let updated = set_criterion(&content, criterion.line, checked);
|
||||
let uuid = parse_ticket_field(&content, "ID");
|
||||
save_ticket(&worklane_dir, &status, &uuid, &title, None, &updated)?;
|
||||
|
||||
println!(
|
||||
"{} {}: {}",
|
||||
if checked { "Checked" } else { "Unchecked" },
|
||||
chosen + 1,
|
||||
color_text(&criterion.text, "green")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -72,6 +72,8 @@ fn main() -> Result<()> {
|
||||
} => cmd_mv(&ticket, &status, force, &start),
|
||||
Command::Set(args) => cmd_set(&args, &start),
|
||||
Command::Append(args) => cmd_append(&args, &start),
|
||||
Command::Check(args) => cmd_check(&args, true, &start),
|
||||
Command::Uncheck(args) => cmd_check(&args, false, &start),
|
||||
Command::Comment { ticket, body, owner } => cmd_comment(&ticket, &body, &owner, &start),
|
||||
}
|
||||
}
|
||||
|
||||
+161
-4
@@ -290,6 +290,7 @@ pub fn content_trim_colored(content: &str) -> String {
|
||||
let colored = should_use_colors();
|
||||
let mut result = String::new();
|
||||
let mut current_section = "";
|
||||
let mut criterion = 0;
|
||||
let mut i = 0;
|
||||
|
||||
while i < lines.len() {
|
||||
@@ -342,10 +343,22 @@ pub fn content_trim_colored(content: &str) -> String {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Acceptance Criteria checkboxes
|
||||
if line.starts_with("- [ ]") {
|
||||
result.push_str(&paint("- [ ]", "grey", colored));
|
||||
result.push_str(&line[5..]);
|
||||
// Acceptance Criteria checkboxes, numbered so the index `check` takes is
|
||||
// visible where the ticket is read. The number replaces the list marker
|
||||
// rather than joining it, and is cyan like the numbers in the plan section.
|
||||
if current_section == ACCEPTANCE.heading
|
||||
&& let Some((checked, text)) = checkbox(line)
|
||||
{
|
||||
criterion += 1;
|
||||
result.push_str(&paint(&format!("{}.", criterion), "cyan", colored));
|
||||
result.push(' ');
|
||||
result.push_str(&paint(
|
||||
if checked { "[x]" } else { "[ ]" },
|
||||
"grey",
|
||||
colored,
|
||||
));
|
||||
result.push(' ');
|
||||
result.push_str(&colorize_line(text));
|
||||
result.push('\n');
|
||||
continue;
|
||||
}
|
||||
@@ -849,6 +862,150 @@ pub fn append_section_body(content: &str, section: &Section, items: &[String]) -
|
||||
Ok(join_lines(&out))
|
||||
}
|
||||
|
||||
/// One acceptance criterion as it sits in the file.
|
||||
pub struct Criterion {
|
||||
/// Index into `content.lines()`, so the one line can be rewritten in place.
|
||||
pub line: usize,
|
||||
pub checked: bool,
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
/// A checkbox line's state and text, or `None` when the line is not one.
|
||||
///
|
||||
/// Both `[x]` and `[X]` count as ticked. Anything else between the brackets is not a
|
||||
/// criterion, so a handwritten line in the section is left out of the numbering.
|
||||
pub fn checkbox(line: &str) -> Option<(bool, &str)> {
|
||||
let rest = line.trim_start().strip_prefix("- [")?;
|
||||
let mut chars = rest.char_indices();
|
||||
|
||||
let (_, mark) = chars.next()?;
|
||||
let (closes_at, close) = chars.next()?;
|
||||
if close != ']' {
|
||||
return None;
|
||||
}
|
||||
|
||||
let checked = match mark {
|
||||
' ' => false,
|
||||
'x' | 'X' => true,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
Some((checked, rest[closes_at + 1..].trim()))
|
||||
}
|
||||
|
||||
/// The acceptance criteria, numbered in the order they appear.
|
||||
///
|
||||
/// Scoped to the section, so a checkbox written into a description is not a criterion,
|
||||
/// and non-checkbox lines inside the section are skipped rather than numbered.
|
||||
pub fn criteria(content: &str) -> Vec<Criterion> {
|
||||
let lines: Vec<&str> = content.lines().collect();
|
||||
let Some(body) = section_body(&lines, ACCEPTANCE.heading) else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
lines[body.clone()]
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(offset, line)| {
|
||||
checkbox(line).map(|(checked, text)| Criterion {
|
||||
line: body.start + offset,
|
||||
checked,
|
||||
text: text.to_string(),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The criteria at `shown`, numbered by their real position in the list.
|
||||
///
|
||||
/// The position matters: a subset renumbered from one would contradict the advice to
|
||||
/// use the number, pointing at whichever criterion happens to sit there instead.
|
||||
pub fn numbered_criteria(criteria: &[Criterion], shown: &[usize]) -> String {
|
||||
let colored = should_use_colors();
|
||||
|
||||
shown
|
||||
.iter()
|
||||
.filter_map(|offset| criteria.get(*offset).map(|criterion| (offset, criterion)))
|
||||
.map(|(offset, criterion)| {
|
||||
format!(
|
||||
" {} {} {}",
|
||||
paint(&format!("{}.", offset + 1), "cyan", colored),
|
||||
paint(
|
||||
if criterion.checked { "[x]" } else { "[ ]" },
|
||||
"grey",
|
||||
colored
|
||||
),
|
||||
criterion.text
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
/// The whole list, numbered as `show` numbers it.
|
||||
pub fn criteria_list(criteria: &[Criterion]) -> String {
|
||||
numbered_criteria(criteria, &(0..criteria.len()).collect::<Vec<_>>())
|
||||
}
|
||||
|
||||
/// Whether `search` is a number rather than a substring, so a value out of range can
|
||||
/// be reported as a bad index instead of a fruitless text search.
|
||||
pub fn is_index(search: &str) -> bool {
|
||||
search.trim().parse::<usize>().is_ok()
|
||||
}
|
||||
|
||||
/// Which criterion `search` names: a one-based index as `show` prints, or a
|
||||
/// case-insensitive substring of the text.
|
||||
///
|
||||
/// The index is tried first, so a criterion whose text is literally "2" is reachable
|
||||
/// only by substring. Returns every match, leaving the caller to refuse more than one.
|
||||
pub fn find_criteria(criteria: &[Criterion], search: &str) -> Vec<usize> {
|
||||
if let Ok(index) = search.trim().parse::<usize>() {
|
||||
if index >= 1 && index <= criteria.len() {
|
||||
return vec![index - 1];
|
||||
}
|
||||
// A number outside the range is a bad index, not a substring to hunt for.
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
// Trimmed, like the index path: an answer typed at the prompt arrives with its
|
||||
// newline attached, and no criterion text contains one.
|
||||
let needle = search.trim().to_lowercase();
|
||||
criteria
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, criterion)| criterion.text.to_lowercase().contains(&needle))
|
||||
.map(|(offset, _)| offset)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Tick or untick the criterion on `line`, leaving every other byte alone.
|
||||
///
|
||||
/// Only the character between the brackets is spliced. Rebuilding the line would
|
||||
/// normalise its indentation and whatever spacing it carried after the checkbox.
|
||||
pub fn set_criterion(content: &str, line: usize, checked: bool) -> String {
|
||||
let mark = if checked { 'x' } else { ' ' };
|
||||
let mut out = String::with_capacity(content.len());
|
||||
|
||||
for (index, current) in content.lines().enumerate() {
|
||||
match (index == line, current.find("- [")) {
|
||||
(true, Some(at)) => {
|
||||
let opens = at + "- [".len();
|
||||
out.push_str(¤t[..opens]);
|
||||
out.push(mark);
|
||||
// Past the one character the brackets hold. Nothing follows on a line
|
||||
// that ends mid-checkbox, which is why this is not unconditional.
|
||||
if let Some((offset, _)) = current[opens..].char_indices().nth(1) {
|
||||
out.push_str(¤t[opens + offset..]);
|
||||
}
|
||||
}
|
||||
_ => out.push_str(current),
|
||||
}
|
||||
out.push('\n');
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
/// The slice with blank values trimmed from both ends, interior ones kept.
|
||||
///
|
||||
/// Description values are joined with a single newline, so an empty value is how a
|
||||
|
||||
Reference in New Issue
Block a user