feat(todo): add numbered tasks, edit, done, and future filtering

Implement smoother todo editing with the following features:
- Display numbered tasks [1], [2], etc. in `streamd todo`
- Add `streamd todo N edit` to open editor at task line
- Add `streamd todo N done` to insert @Done after @Task
- Add `--show-future` flag to include future tasks (hidden by default)
This commit is contained in:
Konstantin Fickel 2026-04-02 18:27:19 +02:00
parent a8c41ec833
commit 124a5b7e2a
Signed by: kfickel
GPG key ID: A793722F9933C1A5
5 changed files with 198 additions and 7 deletions

View file

@ -1,5 +1,7 @@
use std::fs;
use std::process::Command;
use chrono::Utc;
use walkdir::WalkDir;
use crate::config::Settings;
@ -33,10 +35,26 @@ fn all_files() -> Result<Vec<LocalizedShard>, StreamdError> {
Ok(shards)
}
pub fn run() -> Result<(), StreamdError> {
pub fn collect_open_tasks(show_future: bool) -> Result<Vec<LocalizedShard>, StreamdError> {
let all_shards = all_files()?;
let now = Utc::now();
for task_shard in find_shard_by_position(&all_shards, "task", "open") {
let mut tasks: Vec<LocalizedShard> = find_shard_by_position(&all_shards, "task", "open")
.into_iter()
.filter(|shard| show_future || shard.moment <= now)
.collect();
// Sort by moment ascending (oldest first = task 1)
tasks.sort_by(|a, b| a.moment.cmp(&b.moment));
Ok(tasks)
}
pub fn run_list(show_future: bool) -> Result<(), StreamdError> {
let tasks = collect_open_tasks(show_future)?;
for (index, task_shard) in tasks.iter().enumerate() {
let task_number = index + 1; // 1-indexed
if let Some(file_path) = task_shard.location.get("file") {
let content = fs::read_to_string(file_path)?;
let lines: Vec<&str> = content.lines().collect();
@ -44,7 +62,10 @@ pub fn run() -> Result<(), StreamdError> {
let start = task_shard.start_line.saturating_sub(1);
let end = std::cmp::min(task_shard.end_line, lines.len());
println!("--- {}:{} ---", file_path, task_shard.start_line);
println!(
"[{}] --- {}:{} ---",
task_number, file_path, task_shard.start_line
);
for line in &lines[start..end] {
println!("{}", line);
}
@ -54,3 +75,130 @@ pub fn run() -> Result<(), StreamdError> {
Ok(())
}
pub fn run_edit(number: usize) -> Result<(), StreamdError> {
// Always include all tasks for edit (user might want to edit a future task)
let tasks = collect_open_tasks(true)?;
if number == 0 || number > tasks.len() {
return Err(StreamdError::InvalidTaskNumber(number, tasks.len()));
}
let task = &tasks[number - 1]; // Convert to 0-indexed
let file_path = task
.location
.get("file")
.ok_or(StreamdError::MissingFilePath)?;
let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
let line_arg = format!("+{}", task.start_line);
let status = Command::new(&editor)
.arg(&line_arg)
.arg(file_path)
.status()?;
if !status.success() {
return Err(StreamdError::IoError(std::io::Error::other(
"Editor exited with non-zero status",
)));
}
Ok(())
}
pub fn run_done(number: usize) -> Result<(), StreamdError> {
// Always include all tasks for done (user might want to mark a future task as done)
let tasks = collect_open_tasks(true)?;
if number == 0 || number > tasks.len() {
return Err(StreamdError::InvalidTaskNumber(number, tasks.len()));
}
let task = &tasks[number - 1];
let file_path = task
.location
.get("file")
.ok_or(StreamdError::MissingFilePath)?;
let content = fs::read_to_string(file_path)?;
let mut lines: Vec<String> = content.lines().map(String::from).collect();
// Find the line containing @Task (should be at start_line)
let task_line_idx = task.start_line.saturating_sub(1);
if task_line_idx >= lines.len() {
return Err(StreamdError::InvalidLineNumber);
}
let line = &lines[task_line_idx];
// Check for multiple @Task occurrences
let task_count = line.matches("@Task").count();
if task_count > 1 {
return Err(StreamdError::MultipleTaskMarkers(
file_path.clone(),
task.start_line,
));
}
if task_count == 0 {
return Err(StreamdError::NoTaskMarker(
file_path.clone(),
task.start_line,
));
}
// Insert @Done after @Task
let new_line = line.replacen("@Task", "@Task @Done", 1);
lines[task_line_idx] = new_line;
// Write back to file, preserving trailing newline if present
let new_content = if content.ends_with('\n') {
format!("{}\n", lines.join("\n"))
} else {
lines.join("\n")
};
fs::write(file_path, new_content)?;
println!("Marked task {} as done", number);
Ok(())
}
pub fn run() -> Result<(), StreamdError> {
run_list(false)
}
#[cfg(test)]
mod tests {
#[test]
fn test_insert_done_after_task() {
let line = "Some content @Task with more text";
let result = line.replacen("@Task", "@Task @Done", 1);
assert_eq!(result, "Some content @Task @Done with more text");
}
#[test]
fn test_insert_done_at_line_end() {
let line = "Some content @Task";
let result = line.replacen("@Task", "@Task @Done", 1);
assert_eq!(result, "Some content @Task @Done");
}
#[test]
fn test_task_count_single() {
let line = "Some content @Task with more text";
assert_eq!(line.matches("@Task").count(), 1);
}
#[test]
fn test_task_count_multiple() {
let line = "Some @Task content @Task again";
assert_eq!(line.matches("@Task").count(), 2);
}
#[test]
fn test_task_count_none() {
let line = "Some content without task marker";
assert_eq!(line.matches("@Task").count(), 0);
}
}