refactor: provide position ref and cloned

This commit is contained in:
xeruf 2024-08-19 16:36:06 +03:00
parent 629db66018
commit 5bc3509930
2 changed files with 61 additions and 45 deletions

View File

@ -377,7 +377,7 @@ async fn main() {
let author = arg.and_then(|a| PublicKey::from_str(a).ok()).unwrap_or_else(|| keys.public_key()); let author = arg.and_then(|a| PublicKey::from_str(a).ok()).unwrap_or_else(|| keys.public_key());
info!("Filtering for events by {author}"); info!("Filtering for events by {author}");
tasks.set_filter( tasks.set_filter(
tasks.filtered_tasks(tasks.get_position()) tasks.filtered_tasks(tasks.get_position_ref())
.filter(|t| t.event.pubkey == author) .filter(|t| t.event.pubkey == author)
.map(|t| t.event.id) .map(|t| t.event.id)
.collect() .collect()
@ -477,42 +477,46 @@ async fn main() {
Some('.') => { Some('.') => {
let mut dots = 1; let mut dots = 1;
let mut pos = tasks.get_position(); let mut pos = tasks.get_position_ref();
for _ in iter.take_while(|c| c == &'.') { for _ in iter.take_while(|c| c == &'.') {
dots += 1; dots += 1;
pos = tasks.get_parent(pos).cloned(); pos = tasks.get_parent(pos);
} }
let slice = input[dots..].trim(); let slice = input[dots..].trim();
if pos != tasks.get_position() || slice.is_empty() {
tasks.move_to(pos);
}
if slice.is_empty() { if slice.is_empty() {
tasks.move_to(pos.cloned());
if dots > 1 { if dots > 1 {
info!("Moving up {} tasks", dots - 1) info!("Moving up {} tasks", dots - 1)
} }
} else if let Ok(depth) = slice.parse::<i8>() { } else if let Ok(depth) = slice.parse::<i8>() {
if pos != tasks.get_position_ref() {
tasks.move_to(pos.cloned());
}
tasks.set_depth(depth); tasks.set_depth(depth);
} else { } else {
tasks.filter_or_create(slice).map(|id| tasks.move_to(Some(id))); tasks.filter_or_create(pos.cloned().as_ref(), slice).map(|id| tasks.move_to(Some(id)));
} }
} }
Some('/') => { Some('/') => {
let mut dots = 1; let mut dots = 1;
let mut pos = tasks.get_position(); let mut pos = tasks.get_position_ref();
for _ in iter.take_while(|c| c == &'/') { for _ in iter.take_while(|c| c == &'/') {
dots += 1; dots += 1;
pos = tasks.get_parent(pos).cloned(); pos = tasks.get_parent(pos);
} }
let slice = input[dots..].trim(); let slice = input[dots..].trim();
if slice.is_empty() { if slice.is_empty() {
tasks.move_to(pos); tasks.move_to(pos.cloned());
if dots > 1 { if dots > 1 {
info!("Moving up {} tasks", dots - 1) info!("Moving up {} tasks", dots - 1)
} }
} else if let Ok(depth) = slice.parse::<i8>() { } else if let Ok(depth) = slice.parse::<i8>() {
if pos != tasks.get_position_ref() {
tasks.move_to(pos.cloned());
}
tasks.set_depth(depth); tasks.set_depth(depth);
} else { } else {
let mut transform: Box<dyn Fn(&str) -> String> = Box::new(|s: &str| s.to_string()); let mut transform: Box<dyn Fn(&str) -> String> = Box::new(|s: &str| s.to_string());
@ -532,7 +536,7 @@ async fn main() {
if filtered.len() == 1 { if filtered.len() == 1 {
tasks.move_to(filtered.into_iter().nth(0)); tasks.move_to(filtered.into_iter().nth(0));
} else { } else {
tasks.move_to(pos); tasks.move_to(pos.cloned());
tasks.set_filter(filtered); tasks.set_filter(filtered);
} }
} }
@ -559,7 +563,7 @@ async fn main() {
} }
continue; continue;
} else { } else {
tasks.filter_or_create(&input); tasks.filter_or_create(tasks.get_position().as_ref(), &input);
} }
} }
or_print(tasks.print_tasks()); or_print(tasks.print_tasks());

View File

@ -151,10 +151,13 @@ impl Tasks {
pub(crate) fn len(&self) -> usize { self.tasks.len() } pub(crate) fn len(&self) -> usize { self.tasks.len() }
pub(crate) fn get_position(&self) -> Option<EventId> { pub(crate) fn get_position(&self) -> Option<EventId> {
self.get_position_ref().cloned()
}
pub(crate) fn get_position_ref(&self) -> Option<&EventId> {
self.history_until(Timestamp::from(Timestamp::now() + Self::MAX_OFFSET)) self.history_until(Timestamp::from(Timestamp::now() + Self::MAX_OFFSET))
.last() .last()
.and_then(|e| referenced_events(e)) .and_then(|e| referenced_events(e))
.cloned()
} }
/// Ids of all subtasks recursively found for id, including itself /// Ids of all subtasks recursively found for id, including itself
@ -174,7 +177,7 @@ impl Tasks {
/// Dynamic time tracking overview for current task or current user. /// Dynamic time tracking overview for current task or current user.
pub(crate) fn times_tracked(&self) -> (String, Box<dyn DoubleEndedIterator<Item=String>>) { pub(crate) fn times_tracked(&self) -> (String, Box<dyn DoubleEndedIterator<Item=String>>) {
match self.get_position() { match self.get_position_ref() {
None => { None => {
if let Some(set) = self.history.get(&self.sender.pubkey()) { if let Some(set) = self.history.get(&self.sender.pubkey()) {
let mut full = Vec::with_capacity(set.len()); let mut full = Vec::with_capacity(set.len());
@ -193,7 +196,7 @@ impl Tasks {
} }
} }
Some(id) => { Some(id) => {
let ids = vec![&id]; let ids = vec![id];
let history = let history =
self.history.iter().flat_map(|(key, set)| { self.history.iter().flat_map(|(key, set)| {
let mut vec = Vec::with_capacity(set.len() / 2); let mut vec = Vec::with_capacity(set.len() / 2);
@ -260,8 +263,8 @@ impl Tasks {
// Parents // Parents
pub(crate) fn get_parent(&self, id: Option<EventId>) -> Option<&EventId> { pub(crate) fn get_parent(&self, id: Option<&EventId>) -> Option<&EventId> {
id.and_then(|id| self.get_by_id(&id)) id.and_then(|id| self.get_by_id(id))
.and_then(|t| t.parent_id()) .and_then(|t| t.parent_id())
} }
@ -292,7 +295,7 @@ impl Tasks {
fn relative_path(&self, id: EventId) -> String { fn relative_path(&self, id: EventId) -> String {
join_tasks( join_tasks(
self.traverse_up_from(Some(id)) self.traverse_up_from(Some(id))
.take_while(|t| Some(t.event.id) != self.get_position()), .take_while(|t| Some(&t.event.id) != self.get_position_ref()),
false, false,
).unwrap_or(id.to_string()) ).unwrap_or(id.to_string())
} }
@ -349,20 +352,20 @@ impl Tasks {
#[inline] #[inline]
pub(crate) fn get_current_task(&self) -> Option<&Task> { pub(crate) fn get_current_task(&self) -> Option<&Task> {
self.get_position().and_then(|id| self.get_by_id(&id)) self.get_position_ref().and_then(|id| self.get_by_id(id))
} }
pub(crate) fn children_of(&self, id: Option<EventId>) -> impl Iterator<Item=&EventId> + '_ { pub(crate) fn children_of<'a>(&'a self, id: Option<&'a EventId>) -> impl Iterator<Item=&EventId> + 'a {
self.tasks self.tasks
.values() .values()
.filter(move |t| t.parent_id() == id.as_ref()) .filter(move |t| t.parent_id() == id)
.map(|t| t.get_id()) .map(|t| t.get_id())
} }
pub(crate) fn filtered_tasks(&self, position: Option<EventId>) -> impl Iterator<Item=&Task> { pub(crate) fn filtered_tasks<'a>(&'a self, position: Option<&'a EventId>) -> impl Iterator<Item=&Task> + 'a {
// TODO use ChildIterator // TODO use ChildIterator
self.resolve_tasks(self.children_of(position)) self.resolve_tasks(self.children_of(position))
.filter(|t| { .filter(move |t| {
// TODO apply filters in transit // TODO apply filters in transit
self.state.matches(t) && self.state.matches(t) &&
t.tags.as_ref().map_or(true, |tags| { t.tags.as_ref().map_or(true, |tags| {
@ -383,7 +386,7 @@ impl Tasks {
if self.view.len() > 0 { if self.view.len() > 0 {
return self.resolve_tasks(self.view.iter()).collect(); return self.resolve_tasks(self.view.iter()).collect();
} }
self.filtered_tasks(self.get_position()).collect() self.filtered_tasks(self.get_position_ref()).collect()
} }
pub(crate) fn print_tasks(&self) -> Result<(), Error> { pub(crate) fn print_tasks(&self) -> Result<(), Error> {
@ -430,7 +433,7 @@ impl Tasks {
.map(|p| self.get_property(task, p.as_str())) .map(|p| self.get_property(task, p.as_str()))
.join(" \t") .join(" \t")
)?; )?;
if self.depth < 2 || task.parent_id() == self.get_position().as_ref() { if self.depth < 2 || task.parent_id() == self.get_position_ref() {
total_time += self.total_time_tracked(task.event.id) total_time += self.total_time_tracked(task.event.id)
} }
} }
@ -547,14 +550,14 @@ impl Tasks {
} }
/// Returns ids of tasks starting with the given string. /// Returns ids of tasks starting with the given string.
pub(crate) fn get_filtered(&self, arg: &str) -> Vec<EventId> { pub(crate) fn get_filtered(&self, position: Option<&EventId>, arg: &str) -> Vec<EventId> {
if let Ok(id) = EventId::parse(arg) { if let Ok(id) = EventId::parse(arg) {
return vec![id]; return vec![id];
} }
let mut filtered: Vec<EventId> = Vec::with_capacity(32); let mut filtered: Vec<EventId> = Vec::with_capacity(32);
let lowercase_arg = arg.to_ascii_lowercase(); let lowercase_arg = arg.to_ascii_lowercase();
let mut filtered_more: Vec<EventId> = Vec::with_capacity(32); let mut filtered_more: Vec<EventId> = Vec::with_capacity(32);
for task in self.filtered_tasks(self.get_position()) { for task in self.filtered_tasks(position) {
let lowercase = task.event.content.to_ascii_lowercase(); let lowercase = task.event.content.to_ascii_lowercase();
if lowercase == lowercase_arg { if lowercase == lowercase_arg {
return vec![task.event.id]; return vec![task.event.id];
@ -572,8 +575,8 @@ impl Tasks {
/// Finds out what to do with the given string. /// Finds out what to do with the given string.
/// Returns an EventId if a new Task was created. /// Returns an EventId if a new Task was created.
pub(crate) fn filter_or_create(&mut self, arg: &str) -> Option<EventId> { pub(crate) fn filter_or_create(&mut self, position: Option<&EventId>, arg: &str) -> Option<EventId> {
let filtered = self.get_filtered(arg); let filtered = self.get_filtered(position, arg);
match filtered.len() { match filtered.len() {
0 => { 0 => {
// No match, new task // No match, new task
@ -592,6 +595,7 @@ impl Tasks {
} }
_ => { _ => {
// Multiple match, filter // Multiple match, filter
self.move_to(position.cloned());
self.set_filter(filtered); self.set_filter(filtered);
None None
} }
@ -607,16 +611,16 @@ impl Tasks {
const MAX_OFFSET: u64 = 9; const MAX_OFFSET: u64 = 9;
pub(crate) fn move_to(&mut self, id: Option<EventId>) { pub(crate) fn move_to(&mut self, target: Option<EventId>) {
self.view.clear(); self.view.clear();
let pos = self.get_position(); let pos = self.get_position_ref();
if id == pos { if target.as_ref() == pos {
debug!("Flushing Tasks because of move in place"); debug!("Flushing Tasks because of move in place");
self.flush(); self.flush();
return; return;
} }
if !id.and_then(|id| self.tasks.get(&id)).is_some_and(|t| t.parent_id() == pos.as_ref()) { if !target.and_then(|id| self.tasks.get(&id)).is_some_and(|t| t.parent_id() == pos) {
debug!("Flushing Tasks because of move beyond child"); debug!("Flushing Tasks because of move beyond child");
self.flush(); self.flush();
} }
@ -627,7 +631,7 @@ impl Tasks {
warn!("Whoa you are moving around quickly! Give me a few seconds to process.") warn!("Whoa you are moving around quickly! Give me a few seconds to process.")
} }
self.submit( self.submit(
build_tracking(id) build_tracking(target)
.custom_created_at(Timestamp::from(now.as_u64() + offset)) .custom_created_at(Timestamp::from(now.as_u64() + offset))
); );
} }
@ -653,7 +657,7 @@ impl Tasks {
} }
pub(crate) fn parent_tag(&self) -> Option<Tag> { pub(crate) fn parent_tag(&self) -> Option<Tag> {
self.get_position().map(|p| self.make_event_tag_from_id(p, MARKER_PARENT)) self.get_position_ref().map(|p| self.make_event_tag_from_id(*p, MARKER_PARENT))
} }
pub(crate) fn position_tags(&self) -> Vec<Tag> { pub(crate) fn position_tags(&self) -> Vec<Tag> {
@ -806,15 +810,15 @@ impl Tasks {
self.submit(prop) self.submit(prop)
} }
pub(crate) fn update_state(&mut self, comment: &str, state: State) { pub(crate) fn update_state(&mut self, comment: &str, state: State) -> Option<EventId> {
self.get_position() let id = self.get_position_ref()?;
.map(|id| self.set_state_for(id, comment, state)); Some(self.set_state_for(id.clone(), comment, state))
} }
pub(crate) fn make_note(&mut self, note: &str) { pub(crate) fn make_note(&mut self, note: &str) {
if let Some(id) = self.get_position() { if let Some(id) = self.get_position_ref() {
if self.get_by_id(&id).is_some_and(|t| t.is_task()) { if self.get_by_id(id).is_some_and(|t| t.is_task()) {
let prop = build_prop(Kind::TextNote, note.trim(), id); let prop = build_prop(Kind::TextNote, note.trim(), id.clone());
self.submit(prop); self.submit(prop);
return; return;
} }
@ -1069,11 +1073,19 @@ mod tasks_test {
}) })
} }
macro_rules! assert_position {
($left:expr, $right:expr $(,)?) => {
assert_eq!($left.get_position_ref(), Some(&$right))
//assert_eq!($left, $right)
};
}
#[test] #[test]
fn test_procedures() { fn test_procedures() {
let mut tasks = stub_tasks(); let mut tasks = stub_tasks();
let id = tasks.make_task_and_enter("proc: tags", State::Procedure); let id = tasks.make_task_and_enter("proc: tags", State::Procedure);
assert_eq!(tasks.get_position(), Some(id)); assert_position!(tasks, id);
assert_eq!(tasks.get_own_history().unwrap().len(), 1); assert_eq!(tasks.get_own_history().unwrap().len(), 1);
let side = tasks.submit(build_task("side", vec![tasks.make_event_tag(&tasks.get_current_task().unwrap().event, MARKER_DEPENDS)], None)); let side = tasks.submit(build_task("side", vec![tasks.make_event_tag(&tasks.get_current_task().unwrap().event, MARKER_DEPENDS)], None));
assert_eq!(tasks.get_current_task().unwrap().children, HashSet::<EventId>::new()); assert_eq!(tasks.get_current_task().unwrap().children, HashSet::<EventId>::new());
@ -1130,7 +1142,7 @@ mod tasks_test {
assert_eq!(tasks.visible_tasks().len(), 0); assert_eq!(tasks.visible_tasks().len(), 0);
tasks.move_to(Some(t1)); tasks.move_to(Some(t1));
assert_eq!(tasks.get_position(), Some(t1)); assert_position!(tasks, t1);
tasks.depth = 2; tasks.depth = 2;
assert_eq!(tasks.visible_tasks().len(), 0); assert_eq!(tasks.visible_tasks().len(), 0);
let t2 = tasks.make_task("t2"); let t2 = tasks.make_task("t2");
@ -1141,7 +1153,7 @@ mod tasks_test {
assert_eq!(tasks.visible_tasks().len(), 2); assert_eq!(tasks.visible_tasks().len(), 2);
tasks.move_to(Some(t2)); tasks.move_to(Some(t2));
assert_eq!(tasks.get_position(), Some(t2)); assert_position!(tasks, t2);
assert_eq!(tasks.visible_tasks().len(), 0); assert_eq!(tasks.visible_tasks().len(), 0);
let t4 = tasks.make_task("t4"); let t4 = tasks.make_task("t4");
assert_eq!(tasks.visible_tasks().len(), 1); assert_eq!(tasks.visible_tasks().len(), 1);
@ -1153,8 +1165,8 @@ mod tasks_test {
assert_eq!(tasks.visible_tasks().len(), 1); assert_eq!(tasks.visible_tasks().len(), 1);
tasks.move_to(Some(t1)); tasks.move_to(Some(t1));
assert_position!(tasks, t1);
assert_eq!(tasks.get_own_history().unwrap().len(), 3); assert_eq!(tasks.get_own_history().unwrap().len(), 3);
assert_eq!(tasks.get_position(), Some(t1));
assert_eq!(tasks.relative_path(t4), "t2>t4"); assert_eq!(tasks.relative_path(t4), "t2>t4");
assert_eq!(tasks.visible_tasks().len(), 2); assert_eq!(tasks.visible_tasks().len(), 2);
tasks.depth = 2; tasks.depth = 2;