From e7bfc720dbab3e6fd18f1894856fbc94dd679cc4 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Mon, 29 Jul 2024 21:27:50 +0300 Subject: [PATCH] fix(tasks): relative path last parent --- src/tasks.rs | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/tasks.rs b/src/tasks.rs index 20231b0..84dc103 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -92,7 +92,7 @@ impl Tasks { } pub(crate) fn get_task_path(&self, id: Option) -> String { - join_tasks(self.traverse_up_from(id)) + join_tasks(self.traverse_up_from(id), true) .filter(|s| !s.is_empty()) .or_else(|| id.map(|id| id.to_string())) .unwrap_or(String::new()) @@ -106,6 +106,14 @@ impl Tasks { } } + fn relative_path(&self, id: EventId) -> String { + join_tasks( + self.traverse_up_from(Some(id)) + .take_while(|t| Some(t.event.id) != self.position), + false, + ).unwrap_or(id.to_string()) + } + // Helpers fn resolve_tasks<'a>(&self, iter: impl IntoIterator) -> Vec<&Task> { @@ -192,11 +200,7 @@ impl Tasks { .iter() .map(|p| match p.as_str() { "path" => self.get_task_path(Some(task.event.id)), - "rpath" => join_tasks( - self.traverse_up_from(Some(task.event.id)) - .take_while(|t| Some(t.event.id) != self.position) - ) - .unwrap_or(task.event.id.to_string()), + "rpath" => self.relative_path(task.event.id), "rtime" => { let time = self.total_time_tracked(&task.event.id); format!("{:02}:{:02}", time / 3600, time / 60 % 60) @@ -357,17 +361,21 @@ impl Tasks { } } -pub(crate) fn join_tasks<'a>(iter: impl Iterator) -> Option { +pub(crate) fn join_tasks<'a>(iter: impl Iterator, include_last_id: bool) -> Option { let tasks: Vec<&Task> = iter.collect(); tasks .iter() .map(|t| t.get_title()) .chain( - tasks - .last() - .and_then(|t| t.parent_id()) - .map(|id| id.to_string()) - .into_iter(), + if include_last_id { + tasks + .last() + .and_then(|t| t.parent_id()) + .map(|id| id.to_string()) + .into_iter() + } else { + None.into_iter() + } ) .fold(None, |acc, val| { Some(acc.map_or_else(|| val.clone(), |cur| format!("{}>{}", val, cur))) @@ -417,6 +425,8 @@ fn test_depth() { assert_eq!(tasks.current_tasks().len(), 0); let t2 = tasks.make_task("t2"); assert_eq!(tasks.current_tasks().len(), 1); + assert_eq!(tasks.get_task_path(t2), "t1>t2"); + assert_eq!(tasks.relative_path(t2.unwrap()), "t2"); let t3 = tasks.make_task("t3"); assert_eq!(tasks.current_tasks().len(), 2); @@ -424,12 +434,15 @@ fn test_depth() { assert_eq!(tasks.current_tasks().len(), 0); let t4 = tasks.make_task("t4"); assert_eq!(tasks.current_tasks().len(), 1); + assert_eq!(tasks.get_task_path(t4), "t1>t2>t4"); + assert_eq!(tasks.relative_path(t4.unwrap()), "t4"); tasks.depth = 2; assert_eq!(tasks.current_tasks().len(), 1); tasks.depth = -1; assert_eq!(tasks.current_tasks().len(), 1); tasks.move_to(t1); + assert_eq!(tasks.relative_path(t4.unwrap()), "t2>t4"); assert_eq!(tasks.current_tasks().len(), 2); tasks.depth = 2; assert_eq!(tasks.current_tasks().len(), 3); @@ -465,6 +478,10 @@ fn test_depth() { let zero = EventId::all_zeros(); assert_eq!(tasks.get_task_path(Some(zero)), zero.to_string()); + tasks.move_to(Some(zero)); + let dangling = tasks.make_task("test"); + assert_eq!(tasks.get_task_path(dangling), "0000000000000000000000000000000000000000000000000000000000000000>test"); + assert_eq!(tasks.relative_path(dangling.unwrap()), "test"); use itertools::Itertools; assert_eq!("test toast".split(' ').collect_vec().len(), 3);