(dca0b61) Add priority as a ticket field

Priority lived in tags, as p1 to p3, which ls could neither sort nor
filter on, nothing validated, and a typo like P1 passed silently. ls
sorted by title only, so a backlog of any size gave no ordering signal.

Levels are 1 to 5 with 1 most urgent, matching what the tags meant.
Omitting the flag leaves the field _Not set_, like Due and Owner: there is
no implicit middle level, because a priority nobody chose is worse than
none. clap rejects anything outside the range before a ticket is written.

Priority parses to three states rather than two. A stored value outside
the range is Invalid, kept distinct from Unset, so a typo cannot read as
"no priority": it shows as ? and sorts just ahead of the unset tail.

ls sorts on priority and falls back to the old title sort, and --priority
filters, composing with the status filter in both the grouped and the
filtered listing. TicketItem becomes a struct, since a sixth tuple field
was past reading.

Migrated the 31 tickets already carrying p1 to p3 tags: the level moves to
the field and the tag is dropped.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ansho Rei
2026-08-20 15:36:19 +02:00
co-authored by Claude Opus 5
parent b7781a830c
commit ddbd45e332
10 changed files with 325 additions and 34 deletions
+130 -20
View File
@@ -496,7 +496,54 @@ pub fn status_dirs() -> Vec<&'static str> {
vec!["backlog", "committed", "reviewing", "done"]
}
pub type TicketItem = (String, String, String, String, String);
/// Lowest and highest level a ticket can carry. 1 is the most urgent, which is
/// what the `p1` to `p3` tags this replaced already meant.
pub const PRIORITY_RANGE: std::ops::RangeInclusive<u8> = 1..=5;
/// A ticket's priority as it comes off disk.
///
/// Three states, not two: a value outside the range has to stay distinguishable
/// from no value at all, or a typo silently reads as "no priority".
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Priority {
Level(u8),
Unset,
Invalid,
}
impl Priority {
/// Sort weight. Levels first in order, then anything wrong, then anything
/// absent, so a typo surfaces above the long tail of unset tickets.
pub fn rank(&self) -> u8 {
match self {
Priority::Level(level) => *level,
Priority::Invalid => u8::MAX - 1,
Priority::Unset => u8::MAX,
}
}
}
pub fn parse_priority(content: &str) -> Priority {
let raw = parse_ticket_field(content, "Priority");
if is_unknown_value(&raw) {
return Priority::Unset;
}
match raw.trim().parse::<u8>() {
Ok(level) if PRIORITY_RANGE.contains(&level) => Priority::Level(level),
_ => Priority::Invalid,
}
}
pub struct TicketItem {
pub title: String,
pub owner: String,
pub due: String,
pub id: String,
pub created: String,
pub priority: Priority,
}
/// Grey for a value that is not set, per the colour guideline.
///
@@ -510,22 +557,32 @@ fn dim_if_unset(value: &str) -> String {
}
}
pub fn display_ticket_items(items: &Vec<TicketItem>) {
for (title, owner, due, uuid, ts) in items {
let uuid_colored = color_text(uuid, "red");
let owner = dim_if_unset(owner);
let due = dim_if_unset(due);
/// How a level reads in a listing.
fn show_priority(priority: Priority) -> String {
match priority {
Priority::Level(level) => level.to_string(),
Priority::Unset => color_text("-", "grey"),
Priority::Invalid => color_text("?", "red"),
}
}
if ts.is_empty() {
println!(
" ({}) {} | Owner: {} | Due: {}",
uuid_colored, title, owner, due
);
} else {
let ts_colored = color_text(ts, "cyan");
pub fn display_ticket_items(items: &[TicketItem]) {
for item in items {
let id = color_text(&item.id, "red");
let owner = dim_if_unset(&item.owner);
let due = dim_if_unset(&item.due);
let priority = show_priority(item.priority);
if item.created.is_empty() {
println!(
" {} ({}) {} | Owner: {} | Due: {}",
ts_colored, uuid_colored, title, owner, due
priority, id, item.title, owner, due
);
} else {
let created = color_text(&item.created, "cyan");
println!(
" {} {} ({}) {} | Owner: {} | Due: {}",
priority, created, id, item.title, owner, due
);
}
}
@@ -547,19 +604,25 @@ pub fn collect_ticket_items(status_path: impl AsRef<Path>) -> Result<Vec<TicketI
let due = parse_ticket_field(&content, "Due");
let filename = e.path().file_stem()?.to_string_lossy().to_string();
let created_at = parse_ticket_field(&content, "Created At");
let short_date = format_created_at(&created_at);
Some((
Some(TicketItem {
title,
owner,
due,
short_id(&filename).to_string(),
short_date,
))
id: short_id(&filename).to_string(),
created: format_created_at(&created_at),
priority: parse_priority(&content),
})
})
.collect();
items.sort_by(|a, b| a.0.to_lowercase().cmp(&b.0.to_lowercase()));
// Priority first, title as the tiebreak, which is what the sort used to be.
items.sort_by(|a, b| {
a.priority
.rank()
.cmp(&b.priority.rank())
.then_with(|| a.title.to_lowercase().cmp(&b.title.to_lowercase()))
});
Ok(items)
}
@@ -610,6 +673,53 @@ mod tests {
}
}
fn ticket_with_priority(raw: &str) -> String {
format!("# T\n\n## Metadata\n\n- **Priority**: {}\n", raw)
}
#[test]
fn a_level_in_range_parses() {
for level in 1..=5u8 {
let content = ticket_with_priority(&level.to_string());
assert_eq!(parse_priority(&content), Priority::Level(level));
}
}
#[test]
fn a_missing_or_unset_level_is_unset() {
assert_eq!(parse_priority("# T\n\n## Metadata\n"), Priority::Unset);
assert_eq!(parse_priority(&ticket_with_priority("_Not set_")), Priority::Unset);
assert_eq!(parse_priority(&ticket_with_priority("_None_")), Priority::Unset);
}
/// Distinct from unset on purpose: a typo must not read as "no priority".
#[test]
fn an_out_of_range_or_unparseable_level_is_invalid() {
for raw in ["0", "6", "42", "high", "-1", "1.5", "999999999999"] {
assert_eq!(
parse_priority(&ticket_with_priority(raw)),
Priority::Invalid,
"for {raw:?}"
);
}
}
#[test]
fn levels_sort_ahead_of_invalid_which_sorts_ahead_of_unset() {
let mut ranks = [Priority::Unset, Priority::Level(5), Priority::Invalid, Priority::Level(1)]
.map(|p| p.rank());
ranks.sort();
assert_eq!(
ranks,
[
Priority::Level(1).rank(),
Priority::Level(5).rank(),
Priority::Invalid.rank(),
Priority::Unset.rank()
]
);
}
#[test]
fn every_colour_the_code_asks_for_exists() {
for colour in ["red", "cyan", "orange", "green", "grey", "magenta"] {