forked from janek/mostr
1
0
Fork 0

style: reorder tasks methods logically

This commit is contained in:
xeruf 2024-07-25 22:10:01 +03:00
parent 81f226fa43
commit ed5e4d97d7
1 changed files with 80 additions and 70 deletions

View File

@ -39,6 +39,8 @@ impl Tasks {
}
impl Tasks {
// Accessors
pub(crate) fn get_position(&self) -> Option<EventId> {
self.position
}
@ -54,17 +56,34 @@ impl Tasks {
})
}
pub(crate) fn set_filter(&mut self, view: Vec<EventId>) {
self.view = view
// Parents
pub(crate) fn parent(&self, id: Option<EventId>) -> Option<EventId> {
id.and_then(|id| self.tasks.get(&id))
.and_then(|t| t.parent_id())
}
fn resolve_tasks<'a>(&self, iter: impl IntoIterator<Item=&'a EventId>) -> Vec<&Task> {
pub(crate) fn taskpath(&self, id: Option<EventId>) -> String {
join_tasks(self.traverse_up_from(id))
}
pub(crate) fn traverse_up_from(&self, id: Option<EventId>) -> ParentIterator {
ParentIterator {
tasks: &self.tasks,
current: id,
prev: None,
}
}
// Helpers
fn resolve_tasks<'a>(&self, iter: impl IntoIterator<Item = &'a EventId>) -> Vec<&Task> {
self.resolve_tasks_rec(iter, self.depth)
}
fn resolve_tasks_rec<'a>(
&self,
iter: impl IntoIterator<Item=&'a EventId>,
iter: impl IntoIterator<Item = &'a EventId>,
depth: i8,
) -> Vec<&Task> {
iter.into_iter()
@ -93,6 +112,14 @@ impl Tasks {
.collect()
}
pub(crate) fn referenced_tasks<F: Fn(&mut Task)>(&mut self, event: &Event, f: F) {
for tag in event.tags.iter() {
if let Tag::Event { event_id, .. } = tag {
self.tasks.get_mut(event_id).map(|t| f(t));
}
}
}
pub(crate) fn current_tasks(&self) -> Vec<&Task> {
if self.depth == 0 {
return self
@ -154,63 +181,10 @@ impl Tasks {
println!();
}
pub(crate) fn make_task(&mut self, input: &str) -> Option<EventId> {
self.sender.submit(self.build_task(input)).map(|e| {
let id = e.id;
self.add_task(e);
id
})
}
// Movement and Selection
pub(crate) fn build_task(&self, input: &str) -> EventBuilder {
let mut tags: Vec<Tag> = Vec::new();
self.position.inspect(|p| tags.push(Tag::event(*p)));
return match input.split_once(": ") {
None => EventBuilder::new(Kind::from(TASK_KIND), input, tags),
Some(s) => {
tags.append(
&mut s
.1
.split(" ")
.map(|t| Tag::Hashtag(t.to_string()))
.collect(),
);
EventBuilder::new(Kind::from(TASK_KIND), s.0, tags)
}
};
}
pub(crate) fn referenced_tasks<F: Fn(&mut Task)>(&mut self, event: &Event, f: F) {
for tag in event.tags.iter() {
if let Tag::Event { event_id, .. } = tag {
self.tasks.get_mut(event_id).map(|t| f(t));
}
}
}
pub(crate) fn add(&mut self, event: Event) {
if event.kind.as_u64() == 1621 {
self.add_task(event)
} else {
self.add_prop(&event)
}
}
pub(crate) fn add_task(&mut self, event: Event) {
self.referenced_tasks(&event, |t| {
t.children.insert(event.id);
});
if self.tasks.contains_key(&event.id) {
//eprintln!("Did not insert duplicate event {}", event.id);
} else {
self.tasks.insert(event.id, Task::new(event));
}
}
pub(crate) fn add_prop(&mut self, event: &Event) {
self.referenced_tasks(&event, |t| {
t.props.insert(event.clone());
});
pub(crate) fn set_filter(&mut self, view: Vec<EventId>) {
self.view = view
}
pub(crate) fn move_up(&mut self) {
@ -243,23 +217,59 @@ impl Tasks {
});
}
pub(crate) fn parent(&self, id: Option<EventId>) -> Option<EventId> {
id.and_then(|id| self.tasks.get(&id))
.and_then(|t| t.parent_id())
// Updates
pub(crate) fn build_task(&self, input: &str) -> EventBuilder {
let mut tags: Vec<Tag> = Vec::new();
self.position.inspect(|p| tags.push(Tag::event(*p)));
return match input.split_once(": ") {
None => EventBuilder::new(Kind::from(TASK_KIND), input, tags),
Some(s) => {
tags.append(
&mut s
.1
.split(" ")
.map(|t| Tag::Hashtag(t.to_string()))
.collect(),
);
EventBuilder::new(Kind::from(TASK_KIND), s.0, tags)
}
};
}
pub(crate) fn taskpath(&self, id: Option<EventId>) -> String {
join_tasks(self.traverse_up_from(id))
pub(crate) fn make_task(&mut self, input: &str) -> Option<EventId> {
self.sender.submit(self.build_task(input)).map(|e| {
let id = e.id;
self.add_task(e);
id
})
}
pub(crate) fn traverse_up_from(&self, id: Option<EventId>) -> ParentIterator {
ParentIterator {
tasks: &self.tasks,
current: id,
prev: None,
pub(crate) fn add(&mut self, event: Event) {
if event.kind.as_u64() == 1621 {
self.add_task(event)
} else {
self.add_prop(&event)
}
}
pub(crate) fn add_task(&mut self, event: Event) {
self.referenced_tasks(&event, |t| {
t.children.insert(event.id);
});
if self.tasks.contains_key(&event.id) {
//eprintln!("Did not insert duplicate event {}", event.id);
} else {
self.tasks.insert(event.id, Task::new(event));
}
}
pub(crate) fn add_prop(&mut self, event: &Event) {
self.referenced_tasks(&event, |t| {
t.props.insert(event.clone());
});
}
pub(crate) fn update_state_for<F>(&mut self, id: &EventId, comment: &str, f: F) -> Option<Event>
where
F: FnOnce(&Task) -> Option<State>,