From 6932e1f257213d6ef73f12320911b122690c32af Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Wed, 7 Aug 2024 00:06:09 +0300 Subject: [PATCH] feat: hide Tasks attributes for feedback logs and make column interaction 1-indexed --- README.md | 2 +- src/main.rs | 24 ++++++------------------ src/tasks.rs | 42 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 63ab540..25a1717 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ when the application is terminated regularly. Dots can be repeated to move to parent tasks. -- `:[IND][COL]` - add property column COL at IND or end, if it already exists remove property column COL or IND +- `:[IND][COL]` - add property column COL at IND or end, if it already exists remove property column COL or IND (1-indexed) - `*[TIME]` - add timetracking with the specified offset - `>[TEXT]` - complete active task and move to parent, with optional state description - `<[TEXT]` - close active task and move to parent, with optional state description diff --git a/src/main.rs b/src/main.rs index afdc928..695d8bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -252,18 +252,14 @@ async fn main() { Some(':') => match iter.next().and_then(|s| s.to_digit(10)) { Some(digit) => { - let index = digit as usize; + let index = (digit as usize).saturating_sub(1); let remaining = iter.collect::().trim().to_string(); if remaining.is_empty() { - tasks.properties.remove(index); + tasks.remove_column(index); continue; } let value = input[2..].trim().to_string(); - if tasks.properties.get(index) == Some(&value) { - tasks.properties.remove(index); - } else { - tasks.properties.insert(index, value); - } + tasks.add_or_remove_property_column_at_index(value, index); } None => { if arg.is_empty() { @@ -284,15 +280,7 @@ async fn main() { - `subtasks` - how many direct subtasks are complete"); continue; } - let pos = tasks.properties.iter().position(|s| s == arg); - match pos { - None => { - tasks.properties.push(arg.to_string()); - } - Some(i) => { - tasks.properties.remove(i); - } - } + tasks.add_or_remove_property_column(arg); } }, @@ -363,7 +351,7 @@ async fn main() { } if let Ok(depth) = slice.parse::() { tasks.move_to(pos); - tasks.depth = depth; + tasks.set_depth(depth); } else { tasks.filter_or_create(slice).map(|id| tasks.move_to(Some(id))); } @@ -383,7 +371,7 @@ async fn main() { } if let Ok(depth) = slice.parse::() { tasks.move_to(pos); - tasks.depth = depth; + tasks.set_depth(depth); } else { let filtered = tasks .children_of(pos) diff --git a/src/tasks.rs b/src/tasks.rs index a8aa157..f5ccf42 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -23,11 +23,11 @@ pub(crate) struct Tasks { /// History of active tasks by PubKey history: HashMap>, /// The task properties currently visible - pub(crate) properties: Vec, + properties: Vec, /// Negative: Only Leaf nodes /// Zero: Only Active node /// Positive: Go down the respective level - pub(crate) depth: i8, + depth: i8, /// Currently active task position: Option, @@ -62,9 +62,7 @@ impl Tasks { sender, } } -} -impl Tasks { // Accessors #[inline] @@ -183,7 +181,7 @@ impl Tasks { .unwrap_or(String::new()) } - pub(crate) fn traverse_up_from(&self, id: Option) -> ParentIterator { + fn traverse_up_from(&self, id: Option) -> ParentIterator { ParentIterator { tasks: &self.tasks, current: id, @@ -593,6 +591,40 @@ impl Tasks { } } } + + // Properties + + pub(crate) fn set_depth(&mut self, depth: i8) { + self.depth = depth; + info!("Changed view depth to {depth}"); + } + + pub(crate) fn remove_column(&mut self, index: usize) { + let col = self.properties.remove(index); + info!("Removed property column \"{col}\""); + } + + pub(crate) fn add_or_remove_property_column(&mut self, property: &str) { + match self.properties.iter().position(|s| s == property) { + None => { + self.properties.push(property.to_string()); + info!("Added property column \"{property}\""); + } + Some(index) => { + self.properties.remove(index); + } + } + } + + pub(crate) fn add_or_remove_property_column_at_index(&mut self, property: String, index: usize) { + if self.properties.get(index) == Some(&property) { + self.properties.remove(index); + } else { + info!("Added property column \"{property}\" at position {}", index + 1); + self.properties.insert(index, property); + } + } + } /// Formats the given seconds according to the given format.