forked from janek/mostr
1
0
Fork 0

fix(tasks): relative path last parent

This commit is contained in:
xeruf 2024-07-29 21:27:50 +03:00
parent eb5117e21f
commit e7bfc720db
1 changed files with 29 additions and 12 deletions

View File

@ -92,7 +92,7 @@ impl Tasks {
} }
pub(crate) fn get_task_path(&self, id: Option<EventId>) -> String { pub(crate) fn get_task_path(&self, id: Option<EventId>) -> String {
join_tasks(self.traverse_up_from(id)) join_tasks(self.traverse_up_from(id), true)
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())
.or_else(|| id.map(|id| id.to_string())) .or_else(|| id.map(|id| id.to_string()))
.unwrap_or(String::new()) .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 // Helpers
fn resolve_tasks<'a>(&self, iter: impl IntoIterator<Item = &'a EventId>) -> Vec<&Task> { fn resolve_tasks<'a>(&self, iter: impl IntoIterator<Item = &'a EventId>) -> Vec<&Task> {
@ -192,11 +200,7 @@ impl Tasks {
.iter() .iter()
.map(|p| match p.as_str() { .map(|p| match p.as_str() {
"path" => self.get_task_path(Some(task.event.id)), "path" => self.get_task_path(Some(task.event.id)),
"rpath" => join_tasks( "rpath" => self.relative_path(task.event.id),
self.traverse_up_from(Some(task.event.id))
.take_while(|t| Some(t.event.id) != self.position)
)
.unwrap_or(task.event.id.to_string()),
"rtime" => { "rtime" => {
let time = self.total_time_tracked(&task.event.id); let time = self.total_time_tracked(&task.event.id);
format!("{:02}:{:02}", time / 3600, time / 60 % 60) format!("{:02}:{:02}", time / 3600, time / 60 % 60)
@ -357,17 +361,21 @@ impl Tasks {
} }
} }
pub(crate) fn join_tasks<'a>(iter: impl Iterator<Item = &'a Task>) -> Option<String> { pub(crate) fn join_tasks<'a>(iter: impl Iterator<Item = &'a Task>, include_last_id: bool) -> Option<String> {
let tasks: Vec<&Task> = iter.collect(); let tasks: Vec<&Task> = iter.collect();
tasks tasks
.iter() .iter()
.map(|t| t.get_title()) .map(|t| t.get_title())
.chain( .chain(
tasks if include_last_id {
.last() tasks
.and_then(|t| t.parent_id()) .last()
.map(|id| id.to_string()) .and_then(|t| t.parent_id())
.into_iter(), .map(|id| id.to_string())
.into_iter()
} else {
None.into_iter()
}
) )
.fold(None, |acc, val| { .fold(None, |acc, val| {
Some(acc.map_or_else(|| val.clone(), |cur| format!("{}>{}", val, cur))) 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); assert_eq!(tasks.current_tasks().len(), 0);
let t2 = tasks.make_task("t2"); let t2 = tasks.make_task("t2");
assert_eq!(tasks.current_tasks().len(), 1); 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"); let t3 = tasks.make_task("t3");
assert_eq!(tasks.current_tasks().len(), 2); assert_eq!(tasks.current_tasks().len(), 2);
@ -424,12 +434,15 @@ fn test_depth() {
assert_eq!(tasks.current_tasks().len(), 0); assert_eq!(tasks.current_tasks().len(), 0);
let t4 = tasks.make_task("t4"); let t4 = tasks.make_task("t4");
assert_eq!(tasks.current_tasks().len(), 1); 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; tasks.depth = 2;
assert_eq!(tasks.current_tasks().len(), 1); assert_eq!(tasks.current_tasks().len(), 1);
tasks.depth = -1; tasks.depth = -1;
assert_eq!(tasks.current_tasks().len(), 1); assert_eq!(tasks.current_tasks().len(), 1);
tasks.move_to(t1); tasks.move_to(t1);
assert_eq!(tasks.relative_path(t4.unwrap()), "t2>t4");
assert_eq!(tasks.current_tasks().len(), 2); assert_eq!(tasks.current_tasks().len(), 2);
tasks.depth = 2; tasks.depth = 2;
assert_eq!(tasks.current_tasks().len(), 3); assert_eq!(tasks.current_tasks().len(), 3);
@ -465,6 +478,10 @@ fn test_depth() {
let zero = EventId::all_zeros(); let zero = EventId::all_zeros();
assert_eq!(tasks.get_task_path(Some(zero)), zero.to_string()); 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; use itertools::Itertools;
assert_eq!("test toast".split(' ').collect_vec().len(), 3); assert_eq!("test toast".split(' ').collect_vec().len(), 3);