use std::collections::HashMap;
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Default)]
pub struct TaskTitles(HashMap<String, String>);
impl TaskTitles {
pub fn title_for(&self, task_id: &str) -> Option<&str> {
self.0.get(task_id).map(|s| s.as_str())
}
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[cfg(test)]
pub fn from_pairs(pairs: impl IntoIterator<Item = (String, String)>) -> Self {
TaskTitles(pairs.into_iter().collect())
}
}
pub fn load_task_titles(home: &Path) -> TaskTitles {
let mut map: HashMap<String, String> = HashMap::new();
if let Ok(entries) = fs::read_dir(home.join("data")) {
for entry in entries.flatten() {
let dir = entry.path();
if !dir.is_dir() {
continue;
}
let Some(task_id) = dir.file_name().and_then(|s| s.to_str()) else {
continue;
};
let brief = dir.join("brief.md");
if let Ok(content) = fs::read_to_string(&brief) {
if let Some(title) = extract_brief_title(&content) {
map.entry(task_id.to_string()).or_insert(title);
}
}
}
}
let backlog_path = home.join("data").join("backlog.md");
if let Ok(content) = fs::read_to_string(&backlog_path) {
for (id, title) in parse_backlog(&content) {
map.insert(id, title);
}
}
TaskTitles(map)
}
pub fn parse_backlog(content: &str) -> Vec<(String, String)> {
let mut out = Vec::new();
for line in content.lines() {
let trimmed = line.trim_start();
let after_box = trimmed
.strip_prefix("- [")
.and_then(|s| {
let m = s.chars().next()?;
if m == ' ' || m == 'x' || m == 'X' {
Some(&s[1..])
} else {
None
}
})
.and_then(|s| s.strip_prefix("] "));
let Some(rest) = after_box else {
continue;
};
let Some((id, title_part)) = rest.split_once(" - ") else {
continue;
};
let title = strip_trailing_markers(title_part.trim());
if !title.is_empty() {
out.push((id.trim().to_string(), title));
}
}
out
}
fn strip_trailing_markers(title: &str) -> String {
const MARKERS: &[&str] = &[" (repo:", " (kind:", " (since", " (done", " blocked-by:"];
let mut cut = title.len();
for &m in MARKERS {
if let Some(idx) = title.find(m) {
if idx < cut {
cut = idx;
}
}
}
title[..cut].trim_end().to_string()
}
pub fn extract_brief_title(content: &str) -> Option<String> {
let mut lines = content.lines();
let mut found = false;
for line in lines.by_ref() {
let t = line.trim();
if t.eq_ignore_ascii_case("# task") || t.eq_ignore_ascii_case("#task") {
found = true;
break;
}
}
if !found {
return None;
}
let mut paragraph: Vec<&str> = Vec::new();
for line in lines.by_ref() {
if line.trim().is_empty() {
if !paragraph.is_empty() {
break;
}
continue;
}
if line.trim_start().starts_with('#') {
break;
}
paragraph.push(line);
break;
}
let line = paragraph.first()?.trim();
if line.is_empty() {
return None;
}
Some(first_sentence(line))
}
fn first_sentence(line: &str) -> String {
if let Some(idx) = line.find(". ") {
return line[..=idx].trim().to_string();
}
line.trim().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
const BACKLOG_SAMPLE: &str = "\
# Backlog
## In flight
- [ ] crew-watch-model-task-cols - crew-watch: add MODEL column (parse from argv) and TASK title (repo: crew-watch) (kind: ship) (since 2026-08-11)
- [ ] fm-afk-resurface-loop - away mode unusable: resurface-after-rearm fires ~1/sec (repo: firstmate) (kind: ship) (since 2026-08-11)
Some continuation paragraph on the next line.
- [x] crew-watch-v1 - build crew-watch v1: Rust TUI fleet monitor https://github.com/x/y (repo: crew-watch) (kind: ship) (done 2026-08-11)
- [ ] short-task - just a title with no markers
- [ ] edge - multi (parenthesized) title (kind: here)
- not a checkbox
- [ ] no-separator here
## Queued
";
#[test]
fn parse_backlog_extracts_titles() {
let pairs = parse_backlog(BACKLOG_SAMPLE);
let map: HashMap<String, String> = pairs.into_iter().collect();
assert_eq!(
map.get("crew-watch-model-task-cols").map(|s| s.as_str()),
Some("crew-watch: add MODEL column (parse from argv) and TASK title")
);
assert_eq!(
map.get("fm-afk-resurface-loop").map(|s| s.as_str()),
Some("away mode unusable: resurface-after-rearm fires ~1/sec")
);
}
#[test]
fn parse_backlog_done_items_with_url() {
let pairs = parse_backlog(BACKLOG_SAMPLE);
let map: HashMap<String, String> = pairs.into_iter().collect();
assert_eq!(
map.get("crew-watch-v1").map(|s| s.as_str()),
Some("build crew-watch v1: Rust TUI fleet monitor https://github.com/x/y")
);
}
#[test]
fn parse_backlog_title_with_no_markers() {
let pairs = parse_backlog(BACKLOG_SAMPLE);
let map: HashMap<String, String> = pairs.into_iter().collect();
assert_eq!(
map.get("short-task").map(|s| s.as_str()),
Some("just a title with no markers")
);
}
#[test]
fn parse_backlog_strips_only_trailing_marker_keywords() {
let pairs = parse_backlog(BACKLOG_SAMPLE);
let map: HashMap<String, String> = pairs.into_iter().collect();
assert_eq!(
map.get("edge").map(|s| s.as_str()),
Some("multi (parenthesized) title")
);
}
#[test]
fn parse_backlog_skips_non_checkbox_lines() {
let pairs = parse_backlog(BACKLOG_SAMPLE);
let map: HashMap<String, String> = pairs.into_iter().collect();
assert!(!map.contains_key("not"));
assert!(!map.contains_key("no-separator"));
}
#[test]
fn parse_backlog_handles_uppercase_checkbox_mark() {
let content = "- [X] done-task - a finished thing (repo: x) (done 2026-01-01)";
let pairs = parse_backlog(content);
assert_eq!(pairs.len(), 1);
assert_eq!(pairs[0].0, "done-task");
assert_eq!(pairs[0].1, "a finished thing");
}
#[test]
fn parse_backlog_empty_is_empty() {
assert!(parse_backlog("").is_empty());
}
#[test]
fn parse_backlog_strips_blocked_by() {
let content =
"- [ ] blocked-task - needs a thing blocked-by: other-task (repo: x) (kind: y)";
let pairs = parse_backlog(content);
assert_eq!(pairs[0].1, "needs a thing");
}
const BRIEF_SAMPLE: &str = "\
You are a crewmate.
# Task
Build v1 of crew-watch: a Rust TUI monitor for the captain's workstation. The repo is fresh and contains only a README, so you build from scratch.
## Why it exists
The captain uses htop.
";
#[test]
fn extract_brief_title_first_sentence() {
let title = extract_brief_title(BRIEF_SAMPLE);
assert_eq!(
title.as_deref(),
Some("Build v1 of crew-watch: a Rust TUI monitor for the captain's workstation.")
);
}
#[test]
fn extract_brief_title_skips_blank_lines_after_heading() {
let content = "# Task\n\n\nFirst sentence here. More text.\n";
assert_eq!(
extract_brief_title(content).as_deref(),
Some("First sentence here.")
);
}
#[test]
fn extract_brief_title_case_insensitive_heading() {
assert_eq!(
extract_brief_title("# TASK\nDo the thing. Then more.\n").as_deref(),
Some("Do the thing.")
);
}
#[test]
fn extract_brief_title_no_heading_returns_none() {
assert!(extract_brief_title("No heading here.\nDo things.\n").is_none());
}
#[test]
fn extract_brief_title_heading_with_no_body_returns_none() {
assert!(extract_brief_title("# Task\n\n## Next\nbody\n").is_none());
}
#[test]
fn extract_brief_title_single_sentence_no_period() {
let title = extract_brief_title("# Task\nA short description with no period\n");
assert_eq!(title.as_deref(), Some("A short description with no period"));
}
#[test]
fn extract_brief_title_version_number_not_sentence_boundary() {
let title = extract_brief_title("# Task\nShip v1.0.0 today. Then rest.\n");
assert_eq!(title.as_deref(), Some("Ship v1.0.0 today."));
}
#[test]
fn load_missing_home_is_empty() {
let t = load_task_titles(Path::new("/nonexistent/fm-home-99999"));
assert!(t.is_empty());
}
#[test]
fn backlog_title_overrides_brief_title() {
let tmp = tempfile_dir();
let data = tmp.join("data");
fs::create_dir_all(data.join("demo-task")).unwrap();
fs::write(
data.join("backlog.md"),
"- [ ] demo-task - backlog version of the title (repo: x) (kind: y)\n",
)
.unwrap();
fs::write(
data.join("demo-task").join("brief.md"),
"# Task\nBrief version. More.\n",
)
.unwrap();
let titles = load_task_titles(&tmp);
assert_eq!(
titles.title_for("demo-task"),
Some("backlog version of the title")
);
}
#[test]
fn brief_title_used_when_not_in_backlog() {
let tmp = tempfile_dir();
let data = tmp.join("data");
fs::create_dir_all(data.join("brief-only")).unwrap();
fs::write(
data.join("brief-only").join("brief.md"),
"# Task\nDo the thing.\n",
)
.unwrap();
let titles = load_task_titles(&tmp);
assert_eq!(titles.title_for("brief-only"), Some("Do the thing."));
}
fn tempfile_dir() -> std::path::PathBuf {
let dir = std::env::temp_dir().join(format!(
"crew-watch-titles-test-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
fs::create_dir_all(&dir).unwrap();
dir
}
}