From c836b0e57bd77c121caa86d64522d6fad7e22f91 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Sun, 11 May 2025 16:55:14 +0200 Subject: [PATCH] enhance(tasks): make state update movement more intuitive --- src/main.rs | 6 ++---- src/tasks.rs | 17 +++++++++++++---- src/tasks/tests.rs | 6 ++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index f774f3f..6dda5bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -460,13 +460,11 @@ async fn main() -> Result<()> { } Some('>') => { - tasks.update_state(arg_default, State::Done); - if tasks.custom_time.is_none() { tasks.move_up(); } + tasks.update_state_and_up(arg_default, State::Done); } Some('<') => { - tasks.update_state(arg_default, State::Closed); - if tasks.custom_time.is_none() { tasks.move_up(); } + tasks.update_state_and_up(arg_default, State::Closed); } Some('&') => diff --git a/src/tasks.rs b/src/tasks.rs index dc6e62c..b556860 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -243,11 +243,15 @@ impl TasksRelay { pub(crate) fn get_position(&self) -> Option { self.position } + + pub(crate) fn calculate_position(&self, time: Option) -> Option { + self.get_position_at(time.unwrap_or(now())).1 + } // Update the current position // Returns whether the position changed pub(super) fn update_position(&mut self) -> bool { - let new_position = self.get_position_at(now()).1; + let new_position = self.calculate_position(None); if new_position != self.position { self.view.clear(); self.position = new_position; @@ -1412,9 +1416,14 @@ impl TasksRelay { self.submit(prop) } - pub(crate) fn update_state(&mut self, comment: &str, state: State) -> Option { - let id = self.get_position()?; - Some(self.set_state_for(id, comment, state)) + /// Update state of current task (if one is selected) and move out of it + pub(crate) fn update_state_and_up(&mut self, comment: &str, state: State) { + if let Some(id) = self.get_position() { + self.set_state_for(id, comment, state); + if self.calculate_position(self.custom_time) == Some(id) { + self.move_up(); + } + } } /// Creates a note or activity, depending on whether the parent is a task. diff --git a/src/tasks/tests.rs b/src/tasks/tests.rs index 6ed0f34..78d8c60 100644 --- a/src/tasks/tests.rs +++ b/src/tasks/tests.rs @@ -68,17 +68,19 @@ fn test_recursive_closing() { tasks.custom_time = Some(Timestamp::zero()); let parent = tasks.make_task_unwrapped("parent #tag1"); tasks.move_to(Some(parent)); + tasks.update_position(); let sub = tasks.make_task_unwrapped("sub #oi # tag2"); assert_eq!( tasks.all_hashtags(), ["oi", "tag1", "tag2"].into_iter().map(Hashtag::from).collect() ); + tasks.update_position(); tasks.make_note("note with #tag3 # yeah"); let all_tags = ["oi", "tag1", "tag2", "tag3", "yeah"].into_iter().map(Hashtag::from).collect(); assert_eq!(tasks.all_hashtags(), all_tags); tasks.custom_time = Some(Timestamp::now()); - tasks.update_state("Finished #YeaH # oi", State::Done); + tasks.update_state_and_up("Finished #YeaH # oi", State::Done); assert_eq!( tasks.get_by_id(&parent).unwrap().list_hashtags().collect_vec(), ["YeaH", "oi", "tag3", "yeah", "tag1"].map(Hashtag::from) @@ -86,7 +88,7 @@ fn test_recursive_closing() { assert_eq!(tasks.all_hashtags(), all_tags); tasks.custom_time = Some(now()); - tasks.update_state("Closing Down", State::Closed); + tasks.update_state_and_up("Closing Down", State::Closed); assert_eq!(tasks.get_by_id(&sub).unwrap().pure_state(), State::Closed); assert_eq!(tasks.get_by_id(&parent).unwrap().pure_state(), State::Closed); assert_eq!(tasks.nonclosed_tasks().next(), None);