2024-07-25 22:40:35 +03:00
|
|
|
use std::collections::{BTreeSet, HashMap};
|
2024-07-30 08:23:32 +03:00
|
|
|
use std::io::{Error, stdout, Write};
|
2024-08-08 00:18:34 +03:00
|
|
|
use std::iter::{once, Sum};
|
2024-07-31 20:05:52 +03:00
|
|
|
use std::ops::{Div, Rem};
|
2024-08-07 15:03:29 +03:00
|
|
|
use std::sync::mpsc::Sender;
|
2024-08-08 00:18:34 +03:00
|
|
|
use std::time::Duration;
|
|
|
|
use chrono::{DateTime, Local, TimeZone};
|
2024-07-30 09:00:39 +03:00
|
|
|
use chrono::LocalResult::Single;
|
|
|
|
use colored::Colorize;
|
2024-07-30 08:23:32 +03:00
|
|
|
use itertools::Itertools;
|
2024-07-29 21:06:23 +03:00
|
|
|
use log::{debug, error, info, trace, warn};
|
2024-08-07 15:03:29 +03:00
|
|
|
use nostr_sdk::{Event, EventBuilder, EventId, Keys, Kind, PublicKey, Tag, TagStandard, Timestamp, Url};
|
2024-08-06 11:34:18 +03:00
|
|
|
use TagStandard::Hashtag;
|
2024-07-19 21:06:03 +03:00
|
|
|
|
2024-08-07 15:03:29 +03:00
|
|
|
use crate::{Events, EventSender};
|
2024-08-02 14:43:39 +03:00
|
|
|
use crate::kinds::*;
|
2024-08-06 22:11:34 +03:00
|
|
|
use crate::task::{State, Task, TaskState};
|
2024-07-19 21:06:03 +03:00
|
|
|
|
2024-07-19 01:15:11 +03:00
|
|
|
type TaskMap = HashMap<EventId, Task>;
|
2024-07-26 21:45:29 +03:00
|
|
|
#[derive(Debug, Clone)]
|
2024-07-19 01:15:11 +03:00
|
|
|
pub(crate) struct Tasks {
|
2024-07-19 09:35:03 +03:00
|
|
|
/// The Tasks
|
2024-07-24 21:11:36 +03:00
|
|
|
tasks: TaskMap,
|
2024-07-31 20:05:52 +03:00
|
|
|
/// History of active tasks by PubKey
|
|
|
|
history: HashMap<PublicKey, BTreeSet<Event>>,
|
2024-07-19 09:35:03 +03:00
|
|
|
/// The task properties currently visible
|
2024-08-07 00:06:09 +03:00
|
|
|
properties: Vec<String>,
|
2024-07-25 00:26:29 +03:00
|
|
|
/// Negative: Only Leaf nodes
|
|
|
|
/// Zero: Only Active node
|
|
|
|
/// Positive: Go down the respective level
|
2024-08-07 00:06:09 +03:00
|
|
|
depth: i8,
|
2024-07-24 21:11:36 +03:00
|
|
|
|
2024-07-25 22:40:35 +03:00
|
|
|
/// Currently active task
|
2024-07-19 01:15:11 +03:00
|
|
|
position: Option<EventId>,
|
2024-07-25 22:40:35 +03:00
|
|
|
/// Currently active tags
|
|
|
|
tags: BTreeSet<Tag>,
|
2024-07-26 21:45:29 +03:00
|
|
|
/// Current active state
|
|
|
|
state: Option<String>,
|
2024-07-19 09:35:03 +03:00
|
|
|
/// A filtered view of the current tasks
|
|
|
|
view: Vec<EventId>,
|
2024-07-24 21:11:36 +03:00
|
|
|
|
2024-07-25 10:55:29 +03:00
|
|
|
sender: EventSender,
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
|
|
|
|
2024-07-24 16:03:34 +03:00
|
|
|
impl Tasks {
|
2024-08-07 15:03:29 +03:00
|
|
|
pub(crate) fn from(url: Option<Url>, tx: &Sender<(Url, Events)>, keys: &Keys) -> Self {
|
|
|
|
Self::with_sender(EventSender {
|
|
|
|
url,
|
|
|
|
tx: tx.clone(),
|
|
|
|
keys: keys.clone(),
|
|
|
|
queue: Default::default(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn with_sender(sender: EventSender) -> Self {
|
2024-07-19 01:15:11 +03:00
|
|
|
Tasks {
|
|
|
|
tasks: Default::default(),
|
2024-07-31 20:05:52 +03:00
|
|
|
history: Default::default(),
|
2024-07-26 21:50:55 +03:00
|
|
|
properties: vec![
|
|
|
|
"state".into(),
|
2024-07-30 17:11:43 +03:00
|
|
|
"progress".into(),
|
2024-07-28 11:37:36 +03:00
|
|
|
"rtime".into(),
|
2024-07-30 09:37:12 +03:00
|
|
|
"hashtags".into(),
|
2024-07-26 21:50:55 +03:00
|
|
|
"rpath".into(),
|
|
|
|
"desc".into(),
|
|
|
|
],
|
2024-08-07 15:04:18 +03:00
|
|
|
position: None, // TODO persist position
|
2024-07-19 09:35:03 +03:00
|
|
|
view: Default::default(),
|
2024-07-25 22:40:35 +03:00
|
|
|
tags: Default::default(),
|
2024-08-01 14:07:40 +03:00
|
|
|
state: None,
|
2024-07-25 00:26:29 +03:00
|
|
|
depth: 1,
|
2024-07-25 10:55:29 +03:00
|
|
|
sender,
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:10:01 +03:00
|
|
|
// Accessors
|
|
|
|
|
2024-07-30 17:13:29 +03:00
|
|
|
#[inline]
|
2024-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn get_by_id(&self, id: &EventId) -> Option<&Task> { self.tasks.get(id) }
|
2024-07-26 21:45:29 +03:00
|
|
|
|
2024-07-30 17:13:29 +03:00
|
|
|
#[inline]
|
2024-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn get_position(&self) -> Option<EventId> { self.position }
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn len(&self) -> usize { self.tasks.len() }
|
2024-07-19 21:06:03 +03:00
|
|
|
|
2024-07-31 20:05:52 +03:00
|
|
|
/// Ids of all subtasks found for id, including itself
|
|
|
|
fn get_subtasks(&self, id: EventId) -> Vec<EventId> {
|
|
|
|
let mut children = Vec::with_capacity(32);
|
|
|
|
let mut index = 0;
|
|
|
|
|
|
|
|
children.push(id);
|
|
|
|
while index < children.len() {
|
|
|
|
self.tasks.get(&children[index]).map(|t| {
|
|
|
|
children.reserve(t.children.len());
|
|
|
|
for child in t.children.iter() {
|
|
|
|
children.push(child.clone());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
children
|
|
|
|
}
|
|
|
|
|
2024-08-08 00:18:34 +03:00
|
|
|
/// Total time in seconds tracked on this task by the current user.
|
2024-08-06 11:34:18 +03:00
|
|
|
pub(crate) fn time_tracked(&self, id: EventId) -> u64 {
|
2024-08-08 00:18:34 +03:00
|
|
|
TimesTracked::from(self.history.get(&self.sender.pubkey()).into_iter().flatten(), &vec![id]).sum::<Duration>().as_secs()
|
2024-08-06 11:34:18 +03:00
|
|
|
}
|
|
|
|
|
2024-08-08 00:18:34 +03:00
|
|
|
/// Total time in seconds tracked on this task and its subtasks by all users.
|
2024-08-06 11:34:18 +03:00
|
|
|
fn total_time_tracked(&self, id: EventId) -> u64 {
|
|
|
|
let mut total = 0;
|
|
|
|
|
|
|
|
let children = self.get_subtasks(id);
|
|
|
|
for user in self.history.values() {
|
2024-08-08 00:18:34 +03:00
|
|
|
total += TimesTracked::from(user, &children).into_iter().sum::<Duration>().as_secs();
|
2024-07-31 20:05:52 +03:00
|
|
|
}
|
|
|
|
total
|
|
|
|
}
|
|
|
|
|
2024-07-30 17:11:43 +03:00
|
|
|
fn total_progress(&self, id: &EventId) -> Option<f32> {
|
2024-07-30 17:13:29 +03:00
|
|
|
self.get_by_id(id).and_then(|t| match t.pure_state() {
|
2024-07-30 17:11:43 +03:00
|
|
|
State::Closed => None,
|
|
|
|
State::Done => Some(1.0),
|
|
|
|
_ => {
|
2024-08-01 20:40:55 +03:00
|
|
|
let mut sum = 0f32;
|
|
|
|
let mut count = 0;
|
|
|
|
for prog in t.children.iter().filter_map(|e| self.total_progress(e)) {
|
|
|
|
sum += prog;
|
|
|
|
count += 1;
|
|
|
|
}
|
2024-07-30 17:11:43 +03:00
|
|
|
Some(
|
2024-08-01 20:40:55 +03:00
|
|
|
if count > 0 {
|
|
|
|
sum / (count as f32)
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
2024-07-30 17:11:43 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:10:01 +03:00
|
|
|
// Parents
|
|
|
|
|
2024-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn get_parent(&self, id: Option<EventId>) -> Option<&EventId> {
|
2024-07-30 17:13:29 +03:00
|
|
|
id.and_then(|id| self.get_by_id(&id))
|
2024-07-25 22:10:01 +03:00
|
|
|
.and_then(|t| t.parent_id())
|
|
|
|
}
|
|
|
|
|
2024-07-26 21:45:29 +03:00
|
|
|
pub(crate) fn get_prompt_suffix(&self) -> String {
|
|
|
|
self.tags
|
|
|
|
.iter()
|
|
|
|
.map(|t| format!(" #{}", t.content().unwrap()))
|
|
|
|
.chain(self.state.as_ref().map(|s| format!(" ?{s}")).into_iter())
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn get_task_path(&self, id: Option<EventId>) -> String {
|
2024-07-29 21:27:50 +03:00
|
|
|
join_tasks(self.traverse_up_from(id), true)
|
2024-07-29 13:32:47 +03:00
|
|
|
.filter(|s| !s.is_empty())
|
|
|
|
.or_else(|| id.map(|id| id.to_string()))
|
|
|
|
.unwrap_or(String::new())
|
2024-07-25 22:10:01 +03:00
|
|
|
}
|
|
|
|
|
2024-08-07 00:06:09 +03:00
|
|
|
fn traverse_up_from(&self, id: Option<EventId>) -> ParentIterator {
|
2024-07-25 22:10:01 +03:00
|
|
|
ParentIterator {
|
|
|
|
tasks: &self.tasks,
|
|
|
|
current: id,
|
|
|
|
prev: None,
|
|
|
|
}
|
2024-07-19 09:35:03 +03:00
|
|
|
}
|
2024-07-25 10:55:29 +03:00
|
|
|
|
2024-07-29 21:27:50 +03:00
|
|
|
fn relative_path(&self, id: EventId) -> String {
|
|
|
|
join_tasks(
|
|
|
|
self.traverse_up_from(Some(id))
|
|
|
|
.take_while(|t| Some(t.event.id) != self.position),
|
|
|
|
false,
|
2024-07-31 20:08:33 +03:00
|
|
|
).unwrap_or(id.to_string())
|
2024-07-29 21:27:50 +03:00
|
|
|
}
|
|
|
|
|
2024-07-25 22:10:01 +03:00
|
|
|
// Helpers
|
|
|
|
|
2024-07-31 20:08:33 +03:00
|
|
|
fn resolve_tasks<'a>(&self, iter: impl IntoIterator<Item=&'a EventId>) -> Vec<&Task> {
|
2024-07-25 00:26:29 +03:00
|
|
|
self.resolve_tasks_rec(iter, self.depth)
|
|
|
|
}
|
|
|
|
|
2024-07-25 10:55:29 +03:00
|
|
|
fn resolve_tasks_rec<'a>(
|
|
|
|
&self,
|
2024-07-31 20:08:33 +03:00
|
|
|
iter: impl IntoIterator<Item=&'a EventId>,
|
2024-07-25 10:55:29 +03:00
|
|
|
depth: i8,
|
|
|
|
) -> Vec<&Task> {
|
|
|
|
iter.into_iter()
|
2024-07-30 17:13:29 +03:00
|
|
|
.filter_map(|id| self.get_by_id(&id))
|
2024-07-25 10:55:29 +03:00
|
|
|
.flat_map(|task| {
|
|
|
|
let new_depth = depth - 1;
|
|
|
|
if new_depth < 0 {
|
|
|
|
let tasks = self
|
|
|
|
.resolve_tasks_rec(task.children.iter(), new_depth)
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<&Task>>();
|
|
|
|
if tasks.is_empty() {
|
|
|
|
vec![task]
|
|
|
|
} else {
|
|
|
|
tasks
|
|
|
|
}
|
|
|
|
} else if new_depth > 0 {
|
|
|
|
self.resolve_tasks_rec(task.children.iter(), new_depth)
|
|
|
|
.into_iter()
|
|
|
|
.chain(once(task))
|
|
|
|
.collect()
|
2024-07-25 00:26:29 +03:00
|
|
|
} else {
|
2024-07-25 10:55:29 +03:00
|
|
|
vec![task]
|
2024-07-25 00:26:29 +03:00
|
|
|
}
|
2024-07-25 10:55:29 +03:00
|
|
|
})
|
|
|
|
.collect()
|
2024-07-24 21:11:36 +03:00
|
|
|
}
|
2024-07-25 00:26:29 +03:00
|
|
|
|
2024-07-25 22:10:01 +03:00
|
|
|
pub(crate) fn referenced_tasks<F: Fn(&mut Task)>(&mut self, event: &Event, f: F) {
|
|
|
|
for tag in event.tags.iter() {
|
2024-08-06 11:34:18 +03:00
|
|
|
if let Some(TagStandard::Event { event_id, .. }) = tag.as_standardized() {
|
2024-07-25 22:10:01 +03:00
|
|
|
self.tasks.get_mut(event_id).map(|t| f(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 17:13:29 +03:00
|
|
|
#[inline]
|
2024-08-08 00:18:34 +03:00
|
|
|
pub(crate) fn get_current_task(&self) -> Option<&Task> {
|
2024-07-30 17:13:29 +03:00
|
|
|
self.position.and_then(|id| self.get_by_id(&id))
|
2024-07-30 08:23:32 +03:00
|
|
|
}
|
2024-08-06 11:34:18 +03:00
|
|
|
|
2024-08-01 21:40:15 +03:00
|
|
|
pub(crate) fn children_of(&self, id: Option<EventId>) -> impl IntoIterator<Item=&EventId> + '_ {
|
|
|
|
self.tasks
|
|
|
|
.values()
|
|
|
|
.filter(move |t| t.parent_id() == id.as_ref())
|
|
|
|
.map(|t| t.get_id())
|
|
|
|
}
|
2024-07-30 08:23:32 +03:00
|
|
|
|
2024-07-19 09:35:03 +03:00
|
|
|
pub(crate) fn current_tasks(&self) -> Vec<&Task> {
|
2024-07-25 00:26:29 +03:00
|
|
|
if self.depth == 0 {
|
2024-08-08 00:18:34 +03:00
|
|
|
return self.get_current_task().into_iter().collect();
|
2024-07-25 00:26:29 +03:00
|
|
|
}
|
2024-07-24 21:11:36 +03:00
|
|
|
let res: Vec<&Task> = self.resolve_tasks(self.view.iter());
|
2024-07-19 09:35:03 +03:00
|
|
|
if res.len() > 0 {
|
2024-07-26 21:45:29 +03:00
|
|
|
// Currently ignores filter when it matches nothing
|
2024-07-19 09:35:03 +03:00
|
|
|
return res;
|
|
|
|
}
|
2024-08-01 21:40:15 +03:00
|
|
|
self.resolve_tasks(self.children_of(self.position)).into_iter()
|
2024-07-31 20:08:33 +03:00
|
|
|
.filter(|t| {
|
2024-08-01 21:40:15 +03:00
|
|
|
// TODO apply filters in transit
|
2024-08-01 14:07:40 +03:00
|
|
|
let state = t.pure_state();
|
|
|
|
self.state.as_ref().map_or_else(|| {
|
|
|
|
state == State::Open || (
|
|
|
|
state == State::Done &&
|
|
|
|
t.parent_id() != None
|
|
|
|
)
|
|
|
|
}, |filter| {
|
|
|
|
t.state().is_some_and(|t| t.matches_label(filter))
|
2024-07-31 20:08:33 +03:00
|
|
|
}) && (self.tags.is_empty()
|
|
|
|
|| t.tags.as_ref().map_or(false, |tags| {
|
2024-07-26 21:45:29 +03:00
|
|
|
let mut iter = tags.iter();
|
|
|
|
self.tags.iter().all(|tag| iter.any(|t| t == tag))
|
|
|
|
}))
|
2024-07-31 20:08:33 +03:00
|
|
|
})
|
|
|
|
.collect()
|
2024-07-19 09:35:03 +03:00
|
|
|
}
|
2024-07-19 01:15:11 +03:00
|
|
|
|
2024-07-30 08:23:32 +03:00
|
|
|
pub(crate) fn print_tasks(&self) -> Result<(), Error> {
|
|
|
|
let mut lock = stdout().lock();
|
2024-08-08 00:18:34 +03:00
|
|
|
if let Some(t) = self.get_current_task() {
|
2024-08-01 14:07:40 +03:00
|
|
|
let state = t.state_or_default();
|
|
|
|
writeln!(
|
|
|
|
lock,
|
|
|
|
"{} since {} (total tracked time {}m)",
|
2024-08-07 15:04:18 +03:00
|
|
|
// TODO tracking since, scheduled/planned for
|
2024-08-01 14:07:40 +03:00
|
|
|
state.get_label(),
|
2024-08-06 11:34:18 +03:00
|
|
|
match Local.timestamp_opt(state.time.as_u64() as i64, 0) {
|
2024-08-01 14:07:40 +03:00
|
|
|
Single(time) => {
|
|
|
|
let date = time.date_naive();
|
|
|
|
let prefix = match Local::now()
|
|
|
|
.date_naive()
|
|
|
|
.signed_duration_since(date)
|
|
|
|
.num_days()
|
|
|
|
{
|
|
|
|
0 => "".into(),
|
|
|
|
1 => "yesterday ".into(),
|
|
|
|
2..=6 => date.format("%a ").to_string(),
|
|
|
|
_ => date.format("%y-%m-%d ").to_string(),
|
|
|
|
};
|
|
|
|
format!("{}{}", prefix, time.format("%H:%M"))
|
|
|
|
}
|
|
|
|
_ => state.time.to_human_datetime(),
|
|
|
|
},
|
2024-08-06 11:34:18 +03:00
|
|
|
self.time_tracked(*t.get_id()) / 60
|
2024-08-01 14:07:40 +03:00
|
|
|
)?;
|
2024-07-30 08:23:32 +03:00
|
|
|
writeln!(lock, "{}", t.descriptions().join("\n"))?;
|
|
|
|
}
|
2024-08-07 15:04:18 +03:00
|
|
|
// TODO proper column alignment
|
2024-07-30 09:00:39 +03:00
|
|
|
writeln!(lock, "{}", self.properties.join("\t").bold())?;
|
2024-07-19 09:35:03 +03:00
|
|
|
for task in self.current_tasks() {
|
2024-07-30 08:23:32 +03:00
|
|
|
writeln!(
|
|
|
|
lock,
|
2024-07-19 09:35:03 +03:00
|
|
|
"{}",
|
|
|
|
self.properties
|
|
|
|
.iter()
|
|
|
|
.map(|p| match p.as_str() {
|
2024-07-30 17:11:43 +03:00
|
|
|
"subtasks" => {
|
|
|
|
let mut total = 0;
|
|
|
|
let mut done = 0;
|
|
|
|
for subtask in task.children.iter().filter_map(|id| self.get_by_id(id))
|
|
|
|
{
|
|
|
|
let state = subtask.pure_state();
|
|
|
|
total += &(state != State::Closed).into();
|
|
|
|
done += &(state == State::Done).into();
|
|
|
|
}
|
|
|
|
if total > 0 {
|
|
|
|
format!("{done}/{total}")
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"progress" => self
|
2024-07-31 20:06:16 +03:00
|
|
|
.total_progress(task.get_id())
|
2024-08-01 20:40:55 +03:00
|
|
|
.filter(|_| task.children.len() > 0)
|
2024-07-31 20:06:16 +03:00
|
|
|
.map_or(String::new(), |p| format!("{:2.0}%", p * 100.0)),
|
2024-07-26 21:45:29 +03:00
|
|
|
"path" => self.get_task_path(Some(task.event.id)),
|
2024-07-29 21:27:50 +03:00
|
|
|
"rpath" => self.relative_path(task.event.id),
|
2024-08-07 15:04:57 +03:00
|
|
|
// TODO format strings as config
|
2024-08-06 11:34:18 +03:00
|
|
|
"time" => display_time("MMMm", self.time_tracked(*task.get_id())),
|
2024-08-07 15:04:57 +03:00
|
|
|
"rtime" => display_time("HH:MM", self.total_time_tracked(*task.get_id())),
|
2024-07-19 09:35:03 +03:00
|
|
|
prop => task.get(prop).unwrap_or(String::new()),
|
|
|
|
})
|
|
|
|
.collect::<Vec<String>>()
|
2024-07-29 16:20:43 +03:00
|
|
|
.join(" \t")
|
2024-07-30 08:23:32 +03:00
|
|
|
)?;
|
2024-07-19 09:35:03 +03:00
|
|
|
}
|
2024-07-30 08:23:32 +03:00
|
|
|
Ok(())
|
2024-07-19 09:35:03 +03:00
|
|
|
}
|
2024-07-19 21:06:03 +03:00
|
|
|
|
2024-07-25 22:10:01 +03:00
|
|
|
// Movement and Selection
|
|
|
|
|
|
|
|
pub(crate) fn set_filter(&mut self, view: Vec<EventId>) {
|
2024-07-26 21:45:29 +03:00
|
|
|
self.view = view;
|
2024-07-25 22:10:01 +03:00
|
|
|
}
|
|
|
|
|
2024-08-08 00:18:34 +03:00
|
|
|
pub(crate) fn clear_filter(&mut self) {
|
|
|
|
self.view.clear();
|
|
|
|
self.tags.clear();
|
|
|
|
info!("Removed all filters");
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:40:35 +03:00
|
|
|
pub(crate) fn add_tag(&mut self, tag: String) {
|
|
|
|
self.view.clear();
|
2024-08-08 00:18:34 +03:00
|
|
|
info!("Added tag filter for #{tag}");
|
2024-08-06 11:34:18 +03:00
|
|
|
self.tags.insert(Hashtag(tag).into());
|
2024-07-25 22:40:35 +03:00
|
|
|
}
|
|
|
|
|
2024-08-08 00:18:34 +03:00
|
|
|
pub(crate) fn remove_tag(&mut self, tag: &str) {
|
2024-08-01 20:12:04 +03:00
|
|
|
self.view.clear();
|
2024-08-08 00:18:34 +03:00
|
|
|
let len = self.tags.len();
|
|
|
|
self.tags.retain(|t| !t.content().is_some_and(|value| value.to_string().starts_with(tag)));
|
|
|
|
if self.tags.len() < len {
|
|
|
|
info!("Removed tag filters starting with {tag}");
|
|
|
|
} else {
|
|
|
|
info!("Found no tag filters starting with {tag} to remove");
|
|
|
|
}
|
2024-08-01 20:12:04 +03:00
|
|
|
}
|
|
|
|
|
2024-07-26 21:45:29 +03:00
|
|
|
pub(crate) fn set_state_filter(&mut self, state: Option<String>) {
|
|
|
|
self.view.clear();
|
2024-08-08 00:18:34 +03:00
|
|
|
info!("Filtering for {}", state.as_ref().map_or("open tasks".to_string(), |s| format!("state {s}")));
|
2024-07-26 21:45:29 +03:00
|
|
|
self.state = state;
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:10:01 +03:00
|
|
|
pub(crate) fn move_up(&mut self) {
|
2024-08-08 00:18:34 +03:00
|
|
|
self.move_to(self.get_current_task().and_then(|t| t.parent_id()).cloned());
|
2024-08-01 14:07:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn flush(&self) {
|
|
|
|
self.sender.flush();
|
2024-07-24 16:03:34 +03:00
|
|
|
}
|
2024-08-06 11:34:18 +03:00
|
|
|
|
2024-08-01 21:40:15 +03:00
|
|
|
/// Returns ids of tasks matching the filter.
|
|
|
|
pub(crate) fn get_filtered(&self, arg: &str) -> Vec<EventId> {
|
2024-08-01 21:11:33 +03:00
|
|
|
if let Ok(id) = EventId::parse(arg) {
|
2024-08-01 21:40:15 +03:00
|
|
|
return vec![id];
|
2024-08-01 21:11:33 +03:00
|
|
|
}
|
|
|
|
let tasks = self.current_tasks();
|
|
|
|
let mut filtered: Vec<EventId> = Vec::with_capacity(tasks.len());
|
|
|
|
let lowercase_arg = arg.to_ascii_lowercase();
|
|
|
|
let mut filtered_more: Vec<EventId> = Vec::with_capacity(tasks.len());
|
|
|
|
for task in tasks {
|
|
|
|
let lowercase = task.event.content.to_ascii_lowercase();
|
|
|
|
if lowercase == lowercase_arg {
|
2024-08-06 11:34:18 +03:00
|
|
|
return vec![task.event.id];
|
2024-08-01 21:11:33 +03:00
|
|
|
} else if task.event.content.starts_with(arg) {
|
|
|
|
filtered.push(task.event.id)
|
|
|
|
} else if lowercase.starts_with(&lowercase_arg) {
|
|
|
|
filtered_more.push(task.event.id)
|
|
|
|
}
|
|
|
|
}
|
2024-08-01 21:40:15 +03:00
|
|
|
if filtered.len() == 0 {
|
2024-08-06 11:34:18 +03:00
|
|
|
return filtered_more;
|
2024-08-01 21:11:33 +03:00
|
|
|
}
|
2024-08-06 11:34:18 +03:00
|
|
|
return filtered;
|
2024-08-01 21:40:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Finds out what to do with the given string.
|
|
|
|
/// Returns an EventId if a new Task was created.
|
|
|
|
pub(crate) fn filter_or_create(&mut self, arg: &str) -> Option<EventId> {
|
|
|
|
let filtered = self.get_filtered(arg);
|
2024-08-01 21:11:33 +03:00
|
|
|
match filtered.len() {
|
|
|
|
0 => {
|
|
|
|
// No match, new task
|
|
|
|
self.view.clear();
|
|
|
|
Some(self.make_task(arg))
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
// One match, activate
|
|
|
|
self.move_to(filtered.into_iter().nth(0));
|
|
|
|
None
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Multiple match, filter
|
|
|
|
self.set_filter(filtered);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-25 22:10:01 +03:00
|
|
|
pub(crate) fn move_to(&mut self, id: Option<EventId>) {
|
|
|
|
self.view.clear();
|
|
|
|
if id == self.position {
|
2024-08-01 20:00:45 +03:00
|
|
|
debug!("Flushing Tasks because of move in place");
|
2024-08-01 14:07:40 +03:00
|
|
|
self.flush();
|
2024-07-25 22:10:01 +03:00
|
|
|
return;
|
|
|
|
}
|
2024-08-02 14:43:39 +03:00
|
|
|
self.submit(build_tracking(id));
|
2024-08-01 14:07:40 +03:00
|
|
|
if !id.and_then(|id| self.tasks.get(&id)).is_some_and(|t| t.parent_id() == self.position.as_ref()) {
|
2024-08-06 11:34:18 +03:00
|
|
|
debug!("Flushing Tasks because of move beyond child");
|
2024-08-01 14:07:40 +03:00
|
|
|
self.flush();
|
|
|
|
}
|
2024-08-01 20:00:45 +03:00
|
|
|
self.position = id;
|
2024-07-25 22:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates
|
|
|
|
|
2024-07-29 16:20:43 +03:00
|
|
|
/// Expects sanitized input
|
2024-08-02 14:31:00 +03:00
|
|
|
pub(crate) fn parse_task(&self, input: &str) -> EventBuilder {
|
2024-07-25 22:40:35 +03:00
|
|
|
let mut tags: Vec<Tag> = self.tags.iter().cloned().collect();
|
2024-07-19 01:15:11 +03:00
|
|
|
self.position.inspect(|p| tags.push(Tag::event(*p)));
|
2024-08-02 14:31:00 +03:00
|
|
|
match input.split_once(": ") {
|
|
|
|
None => build_task(input, tags),
|
2024-07-19 01:15:11 +03:00
|
|
|
Some(s) => {
|
2024-07-30 09:02:56 +03:00
|
|
|
tags.append(
|
|
|
|
&mut s
|
|
|
|
.1
|
|
|
|
.split_ascii_whitespace()
|
2024-08-06 11:34:18 +03:00
|
|
|
.map(|t| Hashtag(t.to_string()).into())
|
2024-07-30 09:02:56 +03:00
|
|
|
.collect(),
|
|
|
|
);
|
2024-08-02 14:31:00 +03:00
|
|
|
build_task(s.0, tags)
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
2024-08-02 14:31:00 +03:00
|
|
|
}
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
|
|
|
|
2024-07-29 16:20:43 +03:00
|
|
|
/// Sanitizes input
|
2024-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn make_task(&mut self, input: &str) -> EventId {
|
2024-08-07 23:59:05 +03:00
|
|
|
let id = self.submit(self.parse_task(input.trim()));
|
|
|
|
self.state.clone().inspect(|s| self.set_state_for_with(id, s));
|
|
|
|
id
|
2024-08-01 14:07:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn build_prop(
|
|
|
|
&mut self,
|
|
|
|
kind: Kind,
|
|
|
|
comment: &str,
|
|
|
|
id: EventId,
|
|
|
|
) -> EventBuilder {
|
|
|
|
EventBuilder::new(
|
|
|
|
kind,
|
|
|
|
comment,
|
|
|
|
vec![Tag::event(id)],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-08-06 17:52:20 +03:00
|
|
|
fn get_task_title(&self, id: &EventId) -> String {
|
|
|
|
self.tasks.get(id).map_or(id.to_string(), |t| t.get_title())
|
|
|
|
}
|
|
|
|
|
2024-08-02 20:40:42 +03:00
|
|
|
pub(crate) fn track_at(&mut self, time: Timestamp) -> EventId {
|
|
|
|
info!("Tracking \"{:?}\" from {}", self.position.map(|id| self.get_task_title(&id)), time.to_human_datetime());
|
2024-08-06 17:52:20 +03:00
|
|
|
let pos = self.get_position();
|
|
|
|
let tracking = build_tracking(pos);
|
|
|
|
self.get_own_history().map(|events| {
|
|
|
|
if let Some(event) = events.pop_last() {
|
|
|
|
if event.kind.as_u16() == TRACKING_KIND &&
|
|
|
|
(pos == None && event.tags.is_empty()) ||
|
|
|
|
event.tags.iter().all(|t| t.content().map(|str| str.to_string()) == pos.map(|id| id.to_string())) {
|
|
|
|
// Replace last for easier calculation
|
|
|
|
} else {
|
|
|
|
events.insert(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
self.submit(tracking.custom_created_at(time))
|
2024-08-02 20:40:42 +03:00
|
|
|
}
|
|
|
|
|
2024-08-01 14:07:40 +03:00
|
|
|
fn submit(&mut self, builder: EventBuilder) -> EventId {
|
2024-08-01 20:00:45 +03:00
|
|
|
let event = self.sender.submit(builder).unwrap();
|
2024-08-01 14:07:40 +03:00
|
|
|
let id = event.id;
|
|
|
|
self.add(event);
|
|
|
|
id
|
2024-07-19 16:49:23 +03:00
|
|
|
}
|
|
|
|
|
2024-07-19 21:04:21 +03:00
|
|
|
pub(crate) fn add(&mut self, event: Event) {
|
2024-08-06 11:34:18 +03:00
|
|
|
match event.kind.as_u16() {
|
2024-07-31 20:05:52 +03:00
|
|
|
TASK_KIND => self.add_task(event),
|
|
|
|
TRACKING_KIND =>
|
|
|
|
match self.history.get_mut(&event.pubkey) {
|
|
|
|
Some(c) => { c.insert(event); }
|
|
|
|
None => { self.history.insert(event.pubkey, BTreeSet::from([event])); }
|
|
|
|
},
|
|
|
|
_ => self.add_prop(&event),
|
2024-07-19 21:04:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-19 16:49:23 +03:00
|
|
|
pub(crate) fn add_task(&mut self, event: Event) {
|
2024-07-25 10:55:29 +03:00
|
|
|
self.referenced_tasks(&event, |t| {
|
|
|
|
t.children.insert(event.id);
|
|
|
|
});
|
2024-07-24 16:03:34 +03:00
|
|
|
if self.tasks.contains_key(&event.id) {
|
2024-08-06 17:52:20 +03:00
|
|
|
warn!("Did not insert duplicate event {}", event.id);
|
2024-07-24 16:03:34 +03:00
|
|
|
} else {
|
|
|
|
self.tasks.insert(event.id, Task::new(event));
|
|
|
|
}
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
2024-07-25 10:55:29 +03:00
|
|
|
|
2024-07-31 20:05:52 +03:00
|
|
|
fn add_prop(&mut self, event: &Event) {
|
2024-07-25 10:55:29 +03:00
|
|
|
self.referenced_tasks(&event, |t| {
|
|
|
|
t.props.insert(event.clone());
|
|
|
|
});
|
2024-07-19 21:04:21 +03:00
|
|
|
}
|
2024-07-19 01:15:11 +03:00
|
|
|
|
2024-08-06 17:52:20 +03:00
|
|
|
fn get_own_history(&mut self) -> Option<&mut BTreeSet<Event>> {
|
|
|
|
self.history.get_mut(&self.sender.pubkey())
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn undo(&mut self) {
|
2024-08-06 22:11:34 +03:00
|
|
|
let mut count = 0;
|
2024-08-01 14:07:40 +03:00
|
|
|
self.sender.clear().into_iter().rev().for_each(|event| {
|
2024-08-06 22:11:34 +03:00
|
|
|
count += 1;
|
2024-08-01 14:07:40 +03:00
|
|
|
self.remove(&event)
|
|
|
|
});
|
2024-08-06 22:11:34 +03:00
|
|
|
info!("Reverted last {count} actions!")
|
2024-07-26 21:45:29 +03:00
|
|
|
}
|
|
|
|
|
2024-08-01 14:07:40 +03:00
|
|
|
fn remove(&mut self, event: &Event) {
|
2024-08-01 20:00:45 +03:00
|
|
|
if let Some(pos) = self.position {
|
|
|
|
if pos == event.id {
|
|
|
|
self.move_up()
|
|
|
|
}
|
|
|
|
}
|
2024-08-01 14:07:40 +03:00
|
|
|
self.tasks.remove(&event.id);
|
2024-08-06 17:52:20 +03:00
|
|
|
self.get_own_history().map(|t| t.remove(event));
|
2024-08-01 14:07:40 +03:00
|
|
|
self.referenced_tasks(event, |t| { t.props.remove(event); });
|
|
|
|
}
|
|
|
|
|
2024-08-07 23:59:05 +03:00
|
|
|
pub(crate) fn set_state_for_with(&mut self, id: EventId, comment: &str) {
|
|
|
|
self.set_state_for(id, comment, match comment {
|
|
|
|
"Closed" => State::Closed,
|
|
|
|
"Done" => State::Done,
|
|
|
|
_ => State::Open,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:07:40 +03:00
|
|
|
pub(crate) fn set_state_for(&mut self, id: EventId, comment: &str, state: State) -> EventId {
|
|
|
|
let prop = self.build_prop(
|
|
|
|
state.into(),
|
|
|
|
comment,
|
|
|
|
id,
|
|
|
|
);
|
2024-08-06 22:11:34 +03:00
|
|
|
info!("Task status {} set for \"{}\"", TaskState::get_label_for(&state, comment), self.get_task_title(&id));
|
2024-08-01 14:07:40 +03:00
|
|
|
self.submit(prop)
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
|
|
|
|
2024-08-02 14:31:00 +03:00
|
|
|
pub(crate) fn update_state(&mut self, comment: &str, state: State) {
|
2024-07-25 10:50:53 +03:00
|
|
|
self.position
|
2024-08-01 14:07:40 +03:00
|
|
|
.map(|id| self.set_state_for(id, comment, state));
|
2024-07-25 10:50:53 +03:00
|
|
|
}
|
|
|
|
|
2024-08-02 14:43:39 +03:00
|
|
|
pub(crate) fn make_note(&mut self, note: &str) {
|
2024-07-25 10:50:53 +03:00
|
|
|
match self.position {
|
2024-08-06 22:11:34 +03:00
|
|
|
None => warn!("Cannot add note \"{}\" without active task", note),
|
2024-07-25 10:50:53 +03:00
|
|
|
Some(id) => {
|
2024-08-01 19:18:46 +03:00
|
|
|
let prop = self.build_prop(Kind::TextNote, note, id);
|
|
|
|
self.submit(prop);
|
2024-07-25 10:50:53 +03:00
|
|
|
}
|
|
|
|
}
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
2024-08-08 00:18:34 +03:00
|
|
|
|
2024-08-07 00:06:09 +03:00
|
|
|
// Properties
|
|
|
|
|
|
|
|
pub(crate) fn set_depth(&mut self, depth: i8) {
|
|
|
|
self.depth = depth;
|
|
|
|
info!("Changed view depth to {depth}");
|
|
|
|
}
|
2024-08-08 00:18:34 +03:00
|
|
|
|
2024-08-07 00:06:09 +03:00
|
|
|
pub(crate) fn remove_column(&mut self, index: usize) {
|
|
|
|
let col = self.properties.remove(index);
|
|
|
|
info!("Removed property column \"{col}\"");
|
|
|
|
}
|
2024-08-08 00:18:34 +03:00
|
|
|
|
2024-08-07 00:06:09 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
|
|
|
|
2024-08-02 11:13:36 +03:00
|
|
|
/// Formats the given seconds according to the given format.
|
|
|
|
/// MMM - minutes
|
|
|
|
/// MM - minutes of the hour
|
|
|
|
/// HH - hours
|
|
|
|
/// Returns an empty string if under a minute.
|
2024-07-31 20:05:52 +03:00
|
|
|
fn display_time(format: &str, secs: u64) -> String {
|
|
|
|
Some(secs / 60)
|
|
|
|
.filter(|t| t > &0)
|
|
|
|
.map_or(String::new(), |mins| format
|
2024-08-02 11:13:36 +03:00
|
|
|
.replace("MMM", &format!("{:3}", mins))
|
2024-07-31 20:05:52 +03:00
|
|
|
.replace("HH", &format!("{:02}", mins.div(60)))
|
2024-08-06 11:34:18 +03:00
|
|
|
.replace("MM", &format!("{:02}", mins.rem(60))),
|
2024-07-31 20:05:52 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-07-30 09:02:56 +03:00
|
|
|
pub(crate) fn join_tasks<'a>(
|
2024-07-31 20:05:52 +03:00
|
|
|
iter: impl Iterator<Item=&'a Task>,
|
2024-07-30 09:02:56 +03:00
|
|
|
include_last_id: bool,
|
|
|
|
) -> Option<String> {
|
2024-07-29 13:32:47 +03:00
|
|
|
let tasks: Vec<&Task> = iter.collect();
|
|
|
|
tasks
|
|
|
|
.iter()
|
|
|
|
.map(|t| t.get_title())
|
2024-07-30 09:02:56 +03:00
|
|
|
.chain(if include_last_id {
|
|
|
|
tasks
|
|
|
|
.last()
|
|
|
|
.and_then(|t| t.parent_id())
|
|
|
|
.map(|id| id.to_string())
|
|
|
|
.into_iter()
|
|
|
|
} else {
|
|
|
|
None.into_iter()
|
|
|
|
})
|
2024-07-25 10:55:29 +03:00
|
|
|
.fold(None, |acc, val| {
|
|
|
|
Some(acc.map_or_else(|| val.clone(), |cur| format!("{}>{}", val, cur)))
|
|
|
|
})
|
2024-07-25 00:52:03 +03:00
|
|
|
}
|
|
|
|
|
2024-08-08 00:18:34 +03:00
|
|
|
|
|
|
|
struct TimesTracked<'a> {
|
|
|
|
events: Box<dyn Iterator<Item=&'a Event> + 'a>,
|
|
|
|
ids: &'a Vec<EventId>,
|
|
|
|
}
|
|
|
|
impl TimesTracked<'_> {
|
|
|
|
fn from<'b>(events: impl IntoIterator<Item=&'b Event> + 'b, ids: &'b Vec<EventId>) -> TimesTracked<'b> {
|
|
|
|
TimesTracked {
|
|
|
|
events: Box::new(events.into_iter()),
|
|
|
|
ids,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for TimesTracked<'_> {
|
|
|
|
type Item = Duration;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let mut start: Option<u64> = None;
|
|
|
|
while let Some(event) = self.events.next() {
|
|
|
|
match event.tags.first().and_then(|tag| tag.as_standardized()) {
|
|
|
|
Some(TagStandard::Event {
|
|
|
|
event_id,
|
|
|
|
..
|
|
|
|
}) if self.ids.contains(event_id) => {
|
|
|
|
start = start.or(Some(event.created_at.as_u64()))
|
|
|
|
}
|
|
|
|
_ => if let Some(stamp) = start {
|
|
|
|
return Some(Duration::from_secs(event.created_at.as_u64() - stamp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return start.map(|stamp| Duration::from_secs(Timestamp::now().as_u64() - stamp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-19 01:15:11 +03:00
|
|
|
struct ParentIterator<'a> {
|
|
|
|
tasks: &'a TaskMap,
|
|
|
|
current: Option<EventId>,
|
2024-07-19 16:49:23 +03:00
|
|
|
/// Inexpensive helper to assert correctness
|
|
|
|
prev: Option<EventId>,
|
2024-07-19 01:15:11 +03:00
|
|
|
}
|
|
|
|
impl<'a> Iterator for ParentIterator<'a> {
|
|
|
|
type Item = &'a Task;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.current.and_then(|id| self.tasks.get(&id)).map(|t| {
|
2024-07-19 16:49:23 +03:00
|
|
|
self.prev.map(|id| assert!(t.children.contains(&id)));
|
|
|
|
self.prev = self.current;
|
2024-08-01 14:07:40 +03:00
|
|
|
self.current = t.parent_id().cloned();
|
2024-07-19 01:15:11 +03:00
|
|
|
t
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 00:26:29 +03:00
|
|
|
|
2024-08-06 17:52:20 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tasks_test {
|
|
|
|
use super::*;
|
2024-08-06 11:34:18 +03:00
|
|
|
|
2024-08-06 17:52:20 +03:00
|
|
|
fn stub_tasks() -> Tasks {
|
|
|
|
use std::sync::mpsc;
|
|
|
|
use nostr_sdk::Keys;
|
2024-08-02 14:31:28 +03:00
|
|
|
|
2024-08-06 17:52:20 +03:00
|
|
|
let (tx, _rx) = mpsc::channel();
|
2024-08-07 15:03:29 +03:00
|
|
|
Tasks::with_sender(EventSender {
|
|
|
|
url: None,
|
2024-08-06 17:52:20 +03:00
|
|
|
tx,
|
|
|
|
keys: Keys::generate(),
|
|
|
|
queue: Default::default(),
|
|
|
|
})
|
|
|
|
}
|
2024-07-29 13:32:47 +03:00
|
|
|
|
2024-08-06 17:52:20 +03:00
|
|
|
#[test]
|
|
|
|
fn test_tracking() {
|
|
|
|
let mut tasks = stub_tasks();
|
|
|
|
|
|
|
|
//let task = tasks.make_task("task");
|
|
|
|
tasks.track_at(Timestamp::from(0));
|
|
|
|
assert_eq!(tasks.history.len(), 1);
|
|
|
|
let zero = EventId::all_zeros();
|
|
|
|
|
|
|
|
tasks.move_to(Some(zero));
|
|
|
|
let now: Timestamp = Timestamp::now() - 2u64;
|
|
|
|
tasks.track_at(Timestamp::from(1));
|
|
|
|
assert!(tasks.time_tracked(zero) > now.as_u64());
|
|
|
|
|
|
|
|
tasks.move_to(None);
|
|
|
|
tasks.track_at(Timestamp::from(2));
|
|
|
|
assert_eq!(tasks.get_own_history().unwrap().len(), 3);
|
|
|
|
assert_eq!(tasks.time_tracked(zero), 1);
|
2024-08-08 00:18:34 +03:00
|
|
|
|
2024-08-07 15:04:18 +03:00
|
|
|
// TODO test received events
|
2024-08-06 17:52:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_depth() {
|
|
|
|
let mut tasks = stub_tasks();
|
|
|
|
|
|
|
|
let t1 = tasks.make_task("t1");
|
|
|
|
let task1 = tasks.get_by_id(&t1).unwrap();
|
|
|
|
assert_eq!(tasks.depth, 1);
|
|
|
|
assert_eq!(task1.pure_state(), State::Open);
|
|
|
|
debug!("{:?}", tasks);
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
tasks.depth = 0;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 0);
|
|
|
|
|
|
|
|
tasks.move_to(Some(t1));
|
|
|
|
tasks.depth = 2;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 0);
|
|
|
|
let t2 = tasks.make_task("t2");
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
assert_eq!(tasks.get_task_path(Some(t2)), "t1>t2");
|
|
|
|
assert_eq!(tasks.relative_path(t2), "t2");
|
|
|
|
let t3 = tasks.make_task("t3");
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 2);
|
|
|
|
|
|
|
|
tasks.move_to(Some(t2));
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 0);
|
|
|
|
let t4 = tasks.make_task("t4");
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
assert_eq!(tasks.get_task_path(Some(t4)), "t1>t2>t4");
|
|
|
|
assert_eq!(tasks.relative_path(t4), "t4");
|
|
|
|
tasks.depth = 2;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
tasks.depth = -1;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
|
|
|
|
tasks.move_to(Some(t1));
|
|
|
|
assert_eq!(tasks.relative_path(t4), "t2>t4");
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 2);
|
|
|
|
tasks.depth = 2;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 3);
|
|
|
|
tasks.set_filter(vec![t2]);
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 2);
|
|
|
|
tasks.depth = 1;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
tasks.depth = -1;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
tasks.set_filter(vec![t2, t3]);
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 2);
|
|
|
|
tasks.depth = 2;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 3);
|
|
|
|
tasks.depth = 1;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 2);
|
|
|
|
|
|
|
|
tasks.move_to(None);
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 1);
|
|
|
|
tasks.depth = 2;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 3);
|
|
|
|
tasks.depth = 3;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 4);
|
|
|
|
tasks.depth = 9;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 4);
|
|
|
|
tasks.depth = -1;
|
|
|
|
assert_eq!(tasks.current_tasks().len(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_task_title_fallback_to_id() {
|
|
|
|
let mut tasks = stub_tasks();
|
|
|
|
|
|
|
|
let empty = tasks.make_task("");
|
|
|
|
let empty_task = tasks.get_by_id(&empty).unwrap();
|
|
|
|
let empty_id = empty_task.event.id.to_string();
|
|
|
|
assert_eq!(empty_task.get_title(), empty_id);
|
|
|
|
assert_eq!(tasks.get_task_path(Some(empty)), empty_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unknown_task() {
|
|
|
|
let mut tasks = stub_tasks();
|
|
|
|
|
|
|
|
let zero = EventId::all_zeros();
|
|
|
|
assert_eq!(tasks.get_task_path(Some(zero)), zero.to_string());
|
|
|
|
tasks.move_to(Some(zero));
|
|
|
|
let dangling = tasks.make_task("test");
|
|
|
|
assert_eq!(
|
|
|
|
tasks.get_task_path(Some(dangling)),
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000>test"
|
|
|
|
);
|
|
|
|
assert_eq!(tasks.relative_path(dangling), "test");
|
2024-08-07 15:04:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
fn test_itertools() {
|
2024-08-06 17:52:20 +03:00
|
|
|
use itertools::Itertools;
|
2024-08-07 15:04:18 +03:00
|
|
|
assert_eq!(
|
|
|
|
"test toast".split(' ').collect_vec().len(),
|
|
|
|
3
|
|
|
|
);
|
2024-08-06 17:52:20 +03:00
|
|
|
assert_eq!(
|
|
|
|
"test toast".split_ascii_whitespace().collect_vec().len(),
|
|
|
|
2
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|