forked from janek/mostr
style: reorder tasks methods logically
This commit is contained in:
parent
81f226fa43
commit
ed5e4d97d7
150
src/tasks.rs
150
src/tasks.rs
|
@ -39,6 +39,8 @@ impl Tasks {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tasks {
|
impl Tasks {
|
||||||
|
// Accessors
|
||||||
|
|
||||||
pub(crate) fn get_position(&self) -> Option<EventId> {
|
pub(crate) fn get_position(&self) -> Option<EventId> {
|
||||||
self.position
|
self.position
|
||||||
}
|
}
|
||||||
|
@ -54,17 +56,34 @@ impl Tasks {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_filter(&mut self, view: Vec<EventId>) {
|
// Parents
|
||||||
self.view = view
|
|
||||||
|
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)
|
self.resolve_tasks_rec(iter, self.depth)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_tasks_rec<'a>(
|
fn resolve_tasks_rec<'a>(
|
||||||
&self,
|
&self,
|
||||||
iter: impl IntoIterator<Item=&'a EventId>,
|
iter: impl IntoIterator<Item = &'a EventId>,
|
||||||
depth: i8,
|
depth: i8,
|
||||||
) -> Vec<&Task> {
|
) -> Vec<&Task> {
|
||||||
iter.into_iter()
|
iter.into_iter()
|
||||||
|
@ -93,6 +112,14 @@ impl Tasks {
|
||||||
.collect()
|
.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> {
|
pub(crate) fn current_tasks(&self) -> Vec<&Task> {
|
||||||
if self.depth == 0 {
|
if self.depth == 0 {
|
||||||
return self
|
return self
|
||||||
|
@ -154,63 +181,10 @@ impl Tasks {
|
||||||
println!();
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn make_task(&mut self, input: &str) -> Option<EventId> {
|
// Movement and Selection
|
||||||
self.sender.submit(self.build_task(input)).map(|e| {
|
|
||||||
let id = e.id;
|
|
||||||
self.add_task(e);
|
|
||||||
id
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn build_task(&self, input: &str) -> EventBuilder {
|
pub(crate) fn set_filter(&mut self, view: Vec<EventId>) {
|
||||||
let mut tags: Vec<Tag> = Vec::new();
|
self.view = view
|
||||||
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 move_up(&mut self) {
|
pub(crate) fn move_up(&mut self) {
|
||||||
|
@ -243,23 +217,59 @@ impl Tasks {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn parent(&self, id: Option<EventId>) -> Option<EventId> {
|
// Updates
|
||||||
id.and_then(|id| self.tasks.get(&id))
|
|
||||||
.and_then(|t| t.parent_id())
|
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 {
|
pub(crate) fn make_task(&mut self, input: &str) -> Option<EventId> {
|
||||||
join_tasks(self.traverse_up_from(id))
|
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 {
|
pub(crate) fn add(&mut self, event: Event) {
|
||||||
ParentIterator {
|
if event.kind.as_u64() == 1621 {
|
||||||
tasks: &self.tasks,
|
self.add_task(event)
|
||||||
current: id,
|
} else {
|
||||||
prev: None,
|
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>
|
pub(crate) fn update_state_for<F>(&mut self, id: &EventId, comment: &str, f: F) -> Option<Event>
|
||||||
where
|
where
|
||||||
F: FnOnce(&Task) -> Option<State>,
|
F: FnOnce(&Task) -> Option<State>,
|
||||||
|
|
Loading…
Reference in New Issue