(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
+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