feat: add relative path property

This commit is contained in:
xeruf 2024-07-25 00:52:03 +03:00
parent 6ed2299ee0
commit a1a94f402d
2 changed files with 11 additions and 4 deletions

View File

@ -158,7 +158,7 @@ async fn main() {
} }
tasks.print_current_tasks(); tasks.print_current_tasks();
print!(" {}> ", tasks.taskpath(tasks.get_position())); print!(" {}) ", tasks.taskpath(tasks.get_position()));
stdout().flush().unwrap(); stdout().flush().unwrap();
match stdin().lines().next() { match stdin().lines().next() {
Some(Ok(input)) => { Some(Ok(input)) => {

View File

@ -112,6 +112,7 @@ impl Tasks {
.iter() .iter()
.map(|p| match p.as_str() { .map(|p| match p.as_str() {
"path" => self.taskpath(Some(task.event.id)), "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(), "ttime" => self.total_time_tracked(&task.event.id).to_string(),
prop => task.get(prop).unwrap_or(String::new()), prop => task.get(prop).unwrap_or(String::new()),
}) })
@ -213,9 +214,7 @@ impl Tasks {
} }
pub(crate) fn taskpath(&self, id: Option<EventId>) -> String { pub(crate) fn taskpath(&self, id: Option<EventId>) -> String {
self.traverse_up_from(id) join_tasks(self.traverse_up_from(id))
.map(|t| t.event.content.clone())
.fold(String::new(), |acc, val| format!("{} {}", val, acc))
} }
pub(crate) fn traverse_up_from(&self, id: Option<EventId>) -> ParentIterator { pub(crate) fn traverse_up_from(&self, id: Option<EventId>) -> ParentIterator {
@ -253,6 +252,14 @@ impl Tasks {
} }
} }
pub(crate) fn join_tasks<'a>(iter: impl IntoIterator<Item=&'a Task>) -> 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> { struct ParentIterator<'a> {
tasks: &'a TaskMap, tasks: &'a TaskMap,
current: Option<EventId>, current: Option<EventId>,