From a1a94f402d81b8587204f46dd0df6589f88ca7f1 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 25 Jul 2024 00:52:03 +0300 Subject: [PATCH] feat: add relative path property --- src/main.rs | 2 +- src/tasks.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 802d13b..3dbf8fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -158,7 +158,7 @@ async fn main() { } tasks.print_current_tasks(); - print!(" {}> ", tasks.taskpath(tasks.get_position())); + print!(" {}) ", tasks.taskpath(tasks.get_position())); stdout().flush().unwrap(); match stdin().lines().next() { Some(Ok(input)) => { diff --git a/src/tasks.rs b/src/tasks.rs index 3086881..822cb77 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -112,6 +112,7 @@ impl Tasks { .iter() .map(|p| match p.as_str() { "path" => self.taskpath(Some(task.event.id)), + "rpath" => join_tasks(self.traverse_up_from(Some(task.event.id)).take_while(|t| Some(t.event.id) != self.position)), "ttime" => self.total_time_tracked(&task.event.id).to_string(), prop => task.get(prop).unwrap_or(String::new()), }) @@ -213,9 +214,7 @@ impl Tasks { } pub(crate) fn taskpath(&self, id: Option) -> String { - self.traverse_up_from(id) - .map(|t| t.event.content.clone()) - .fold(String::new(), |acc, val| format!("{} {}", val, acc)) + join_tasks(self.traverse_up_from(id)) } pub(crate) fn traverse_up_from(&self, id: Option) -> ParentIterator { @@ -253,6 +252,14 @@ impl Tasks { } } +pub(crate) fn join_tasks<'a>(iter: impl IntoIterator) -> String{ + iter.into_iter() + .map(|t| t.event.content.clone()) + .fold(None, |acc, val| Some(acc.map_or_else(|| val.clone(), |cur| format!("{}>{}", val, cur)))) + .unwrap_or(String::new()) +} + + struct ParentIterator<'a> { tasks: &'a TaskMap, current: Option,