(e97d55) Add show command

This commit is contained in:
2026-05-14 22:28:00 +02:00
parent fd216b056c
commit 7d40159959
5 changed files with 398 additions and 8 deletions
+91 -7
View File
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use anyhow::{Result, anyhow};
use clap::Parser;
use std::fs;
use std::path::Path;
@@ -40,7 +40,10 @@ fn cmd_init(start: &Path) -> Result<()> {
let worklane_dir = dir.join(".worklane");
if worklane_dir.exists() {
println!("worklane already initialized in: {}", worklane_dir.display());
println!(
"worklane already initialized in: {}",
worklane_dir.display()
);
return Ok(());
}
@@ -61,7 +64,12 @@ fn parse_ticket_field(content: &str, field: &str) -> String {
.lines()
.find(|line| line.contains(&pattern))
.map(|line| {
line.split(':').nth(1).unwrap_or("").trim().trim_matches('_').trim()
line.split(':')
.nth(1)
.unwrap_or("")
.trim()
.trim_matches('_')
.trim()
})
.unwrap_or("_Not set_")
.to_string()
@@ -81,9 +89,7 @@ fn cmd_ls(status_filter: &Option<String>, worklane_dir: &Path) -> Result<()> {
));
}
let entries: Vec<_> = fs::read_dir(&status_path)?
.filter_map(|e| e.ok())
.collect();
let entries: Vec<_> = fs::read_dir(&status_path)?.filter_map(|e| e.ok()).collect();
if entries.is_empty() {
println!("No tickets in '{}' status.", status);
@@ -253,6 +259,73 @@ fn cmd_add(
Ok(())
}
fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
let statuses = status_dirs();
let mut matches: Vec<(String, String, String)> = Vec::new();
for status in &statuses {
let status_path = worklane_dir.join(status);
if !status_path.exists() {
continue;
}
let entries = fs::read_dir(&status_path)?;
for entry in entries.filter_map(|e| e.ok()) {
let path = entry.path();
if !path.extension().map_or(false, |e| e == "md") {
continue;
}
let content = match fs::read_to_string(&path) {
Ok(c) => c,
Err(_) => continue,
};
let title = content
.lines()
.find(|l| l.starts_with("# "))
.map(|l| l[2..].trim())
.unwrap_or("")
.to_string();
let filename = path
.file_stem()
.map(|s| s.to_string_lossy().to_string())
.unwrap_or_default();
let id = filename.split('_').next().unwrap_or("");
let search_lower = search.to_lowercase();
let title_lower = title.to_lowercase();
let filename_lower = filename.to_lowercase();
let id_match = id.to_lowercase().starts_with(&search_lower) && id.len() >= 8;
let title_match = title_lower.contains(&search_lower);
let filename_match = filename_lower.contains(&search_lower);
if id_match || title_match || filename_match {
matches.push((status.to_string(), title, content));
}
}
}
if matches.is_empty() {
return Err(anyhow!("No ticket found matching '{}'", search));
}
if matches.len() > 1 {
println!("Multiple tickets found matching '{}':", search);
for (i, (status, title, _)) in matches.iter().enumerate() {
println!(" {}. {} ({})", i + 1, title, status);
}
println!("\nTry a more specific search term.");
std::process::exit(1);
}
let (_status, title, content) = matches.remove(0);
println!("{}\n", title);
println!("{} ", "─".repeat(title.len()));
println!("\n{}", content.trim());
Ok(())
}
fn main() -> Result<()> {
let cli = Cli::parse();
@@ -269,11 +342,22 @@ fn main() -> Result<()> {
due,
owner,
} => cmd_add(
&title, &description, &acceptance, &plan, &tags, &due, &owner, &start,
&title,
&description,
&acceptance,
&plan,
&tags,
&due,
&owner,
&start,
),
Command::Ls { status } => {
let worklane_dir = find_worklane_dir(&start)?;
cmd_ls(&status, &worklane_dir)
}
Command::Show { id } => {
let worklane_dir = find_worklane_dir(&start)?;
cmd_show(&id, &worklane_dir)
}
}
}