2022-08-20 04:01:43 +00:00
|
|
|
use crate::error;
|
|
|
|
use crate::state;
|
2022-08-25 06:40:56 +00:00
|
|
|
use crate::graph;
|
2022-08-20 04:01:43 +00:00
|
|
|
use crate::colour;
|
|
|
|
|
2022-08-20 23:58:05 +00:00
|
|
|
use std::io;
|
2022-08-20 04:01:43 +00:00
|
|
|
use std::fs;
|
2022-08-20 23:58:05 +00:00
|
|
|
use std::fmt;
|
2022-08-23 23:56:23 +00:00
|
|
|
use std::ops;
|
2022-08-20 04:01:43 +00:00
|
|
|
use std::mem;
|
2022-08-29 07:34:22 +00:00
|
|
|
use std::cmp;
|
2022-08-20 04:01:43 +00:00
|
|
|
use std::path;
|
|
|
|
use std::io::{Write, Seek};
|
2022-08-25 06:40:56 +00:00
|
|
|
use std::collections::{HashSet, HashMap};
|
2022-08-24 12:09:52 +00:00
|
|
|
use chrono::SubsecRound;
|
2022-08-20 04:01:43 +00:00
|
|
|
|
|
|
|
pub type Id = u64;
|
|
|
|
|
|
|
|
pub struct Task {
|
2022-08-25 10:44:10 +00:00
|
|
|
pub path : path::PathBuf,
|
2022-08-20 04:01:43 +00:00
|
|
|
file : fs::File,
|
|
|
|
pub data : InternalTask,
|
|
|
|
}
|
|
|
|
|
2022-08-21 02:34:34 +00:00
|
|
|
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum, serde::Serialize, serde::Deserialize)]
|
2022-08-20 04:01:43 +00:00
|
|
|
pub enum Priority {
|
|
|
|
#[default]
|
|
|
|
Low,
|
|
|
|
Medium,
|
|
|
|
High,
|
|
|
|
}
|
|
|
|
|
2022-08-20 23:58:05 +00:00
|
|
|
impl fmt::Display for Priority {
|
|
|
|
fn fmt(&self, f : &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
use Priority::*;
|
|
|
|
let priority = match self {
|
|
|
|
Low => "low",
|
|
|
|
Medium => "medium",
|
|
|
|
High => "high",
|
|
|
|
};
|
|
|
|
write!(f, "{}", priority)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-22 10:27:17 +00:00
|
|
|
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
2022-08-20 04:01:43 +00:00
|
|
|
pub struct TimeEntry {
|
2022-08-23 23:56:23 +00:00
|
|
|
pub logged_date : chrono::NaiveDate,
|
2022-08-30 08:23:23 +00:00
|
|
|
pub message : Option<String>,
|
2022-08-23 23:56:23 +00:00
|
|
|
pub duration : Duration,
|
2022-08-22 10:27:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 23:56:23 +00:00
|
|
|
// Needs to preserve representation invariant of minutes < 60
|
2022-08-29 07:34:22 +00:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
|
2022-08-23 23:56:23 +00:00
|
|
|
pub struct Duration {
|
|
|
|
hours : u16,
|
|
|
|
minutes : u16,
|
2022-08-22 10:37:50 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
|
2022-08-20 23:58:05 +00:00
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
2022-08-20 04:01:43 +00:00
|
|
|
pub struct InternalTask {
|
|
|
|
pub id : Id,
|
|
|
|
pub name : String,
|
|
|
|
pub tags : HashSet<String>,
|
|
|
|
pub dependencies : HashSet<Id>,
|
|
|
|
pub priority : Priority,
|
2022-08-24 12:09:52 +00:00
|
|
|
pub due : Option<chrono::NaiveDateTime>,
|
2022-08-20 04:01:43 +00:00
|
|
|
pub created : chrono::NaiveDateTime,
|
2022-08-24 22:30:52 +00:00
|
|
|
pub completed : Option<chrono::NaiveDateTime>,
|
2022-08-23 00:12:48 +00:00
|
|
|
pub info : Option<String>,
|
2022-08-22 10:27:17 +00:00
|
|
|
pub time_entries : Vec<TimeEntry>,
|
2022-08-20 04:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Task {
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Creates a new task from the input data.
|
2022-08-24 12:09:52 +00:00
|
|
|
pub fn new(name : String, info : Option<String>, tags : Vec<String>, dependencies : Vec<Id>, priority : Option<Priority>, due : Option<chrono::NaiveDateTime>, vault_folder : &path::Path, state : &mut state::State) -> Result<Self, error::Error> {
|
2022-08-20 04:01:43 +00:00
|
|
|
|
2022-08-21 06:43:42 +00:00
|
|
|
if name.chars().all(|c| c.is_numeric()) {
|
|
|
|
return Err(error::Error::Generic(String::from("Name must not be purely numeric")));
|
|
|
|
};
|
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
let id = state.data.next_id;
|
|
|
|
state.data.next_id += 1;
|
|
|
|
|
|
|
|
let path = vault_folder.join("notes").join(&format!("{}.toml", id));
|
|
|
|
|
|
|
|
let mut file = fs::File::options()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(&path)?;
|
|
|
|
|
2022-08-25 06:40:56 +00:00
|
|
|
// Adding to dependency graph appropriately.
|
|
|
|
state.data.deps.insert_node(id);
|
|
|
|
if !dependencies.is_empty() {
|
|
|
|
for dependency in &dependencies {
|
|
|
|
if state.data.deps.contains_node(*dependency) {
|
|
|
|
state.data.deps.insert_edge(id, *dependency)?;
|
|
|
|
}
|
|
|
|
else {
|
2022-08-29 09:38:21 +00:00
|
|
|
return Err(error::Error::Generic(format!("No task with an ID of {} exists", colour::text::id(*dependency))));
|
2022-08-25 06:40:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
let data = InternalTask {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
info,
|
|
|
|
tags : tags.into_iter().collect(),
|
|
|
|
dependencies : dependencies.into_iter().collect(),
|
|
|
|
priority : priority.unwrap_or_default(),
|
2022-08-24 12:09:52 +00:00
|
|
|
due,
|
2022-08-20 04:01:43 +00:00
|
|
|
time_entries : Vec::new(),
|
2022-08-24 12:09:52 +00:00
|
|
|
created : chrono::Local::now().naive_local(),
|
2022-08-24 22:30:52 +00:00
|
|
|
completed : None,
|
2022-08-20 04:01:43 +00:00
|
|
|
};
|
|
|
|
|
2022-08-22 10:27:17 +00:00
|
|
|
let file_contents = toml::to_string(&data)?;
|
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
file.set_len(0)?;
|
|
|
|
file.seek(io::SeekFrom::Start(0))?;
|
2022-08-22 10:27:17 +00:00
|
|
|
file.write_all(file_contents.as_bytes())?;
|
2022-08-20 04:01:43 +00:00
|
|
|
|
2022-08-25 00:44:22 +00:00
|
|
|
state.data.index.insert(data.name.clone(), id);
|
2022-08-21 06:43:42 +00:00
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
Ok(Task {
|
|
|
|
path,
|
|
|
|
file,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Loads a task directly from its path, for use with the temporary edit file.
|
2022-08-21 00:59:09 +00:00
|
|
|
pub fn load_direct(path : path::PathBuf, read_only : bool) -> Result<Self, error::Error> {
|
2022-08-20 04:01:43 +00:00
|
|
|
let file_contents = fs::read_to_string(&path)?;
|
2022-08-21 00:59:09 +00:00
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
let file = if read_only {
|
|
|
|
fs::File::open(&path)?
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fs::File::options()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(&path)?
|
|
|
|
};
|
|
|
|
|
|
|
|
let data = toml::from_str(&file_contents)?;
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
path,
|
|
|
|
file,
|
|
|
|
data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Loads a task in to memory.
|
2022-08-21 06:43:42 +00:00
|
|
|
pub fn load(id : Id, vault_folder : &path::Path, read_only : bool) -> Result<Self, error::Error> {
|
|
|
|
let path = Task::check_exists(id, vault_folder)?;
|
2022-08-21 00:59:09 +00:00
|
|
|
|
|
|
|
Task::load_direct(path, read_only)
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Get an iterator over the IDs of tasks in a vault.
|
2022-08-25 06:40:56 +00:00
|
|
|
fn id_iter(vault_folder : &path::Path) -> impl Iterator<Item = u64> {
|
|
|
|
fs::read_dir(vault_folder.join("notes"))
|
|
|
|
.unwrap()
|
|
|
|
.map(|entry| entry.unwrap().path())
|
|
|
|
.filter(|p| p.is_file())
|
|
|
|
.map(|p| p.file_stem().unwrap().to_str().unwrap().to_string())
|
|
|
|
.filter_map(|n| n.parse::<Id>().ok())
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Load all tasks of a vault into a `Vec`.
|
2022-08-21 06:43:42 +00:00
|
|
|
pub fn load_all(vault_folder : &path::Path, read_only : bool) -> Result<Vec<Self>, error::Error> {
|
2022-08-25 06:40:56 +00:00
|
|
|
let ids = Task::id_iter(vault_folder);
|
|
|
|
|
|
|
|
let mut tasks = Vec::new();
|
2022-08-21 06:43:42 +00:00
|
|
|
for id in ids {
|
|
|
|
tasks.push(Task::load(id, vault_folder, read_only)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tasks)
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Load all tasks of a vault into a `HashMap`.
|
2022-08-25 06:40:56 +00:00
|
|
|
pub fn load_all_as_map(vault_folder : &path::Path, read_only : bool) -> Result<HashMap<Id, Self>, error::Error> {
|
|
|
|
let ids = Task::id_iter(vault_folder);
|
|
|
|
|
|
|
|
let mut tasks = HashMap::new();
|
|
|
|
for id in ids {
|
|
|
|
tasks.insert(id, Task::load(id, vault_folder, read_only)?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(tasks)
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Checks that a task with the prodided ID exists in the provided vault_folder. Returns the
|
|
|
|
/// path of that task.
|
2022-08-20 04:01:43 +00:00
|
|
|
pub fn check_exists(id : Id, vault_folder : &path::Path) -> Result<path::PathBuf, error::Error> {
|
|
|
|
let path = vault_folder.join("notes").join(format!("{}.toml", id));
|
|
|
|
if path.exists() && path.is_file() {
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
else {
|
2022-08-29 09:38:21 +00:00
|
|
|
Err(error::Error::Generic(format!("No task with the ID {} exists", colour::text::id(id))))
|
2022-08-20 04:01:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Saves the in memory task data to the corresponding file.
|
2022-08-20 04:01:43 +00:00
|
|
|
pub fn save(self) -> Result<(), error::Error> {
|
|
|
|
let Self {
|
2022-08-21 06:43:42 +00:00
|
|
|
path : _,
|
2022-08-20 04:01:43 +00:00
|
|
|
mut file,
|
|
|
|
data,
|
|
|
|
} = self;
|
|
|
|
|
2022-08-22 10:27:17 +00:00
|
|
|
let file_contents = toml::to_string(&data)?;
|
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
file.set_len(0)?;
|
|
|
|
file.seek(io::SeekFrom::Start(0))?;
|
2022-08-22 10:27:17 +00:00
|
|
|
file.write_all(file_contents.as_bytes())?;
|
2022-08-20 04:01:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Deletes the task.
|
2022-08-20 04:01:43 +00:00
|
|
|
pub fn delete(self) -> Result<(), error::Error> {
|
|
|
|
let Self {
|
|
|
|
path,
|
|
|
|
file,
|
2022-08-21 06:43:42 +00:00
|
|
|
data : _,
|
2022-08-20 04:01:43 +00:00
|
|
|
} = self;
|
|
|
|
|
|
|
|
mem::drop(file);
|
2022-08-25 06:40:56 +00:00
|
|
|
trash::delete(&path)?;
|
2022-08-20 04:01:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
/// Displays a task to the terminal.
|
2022-08-25 06:40:56 +00:00
|
|
|
pub fn display(&self, vault_folder : &path::Path, state : &state::State) -> Result<(), error::Error> {
|
2022-08-22 10:27:17 +00:00
|
|
|
|
2022-08-20 23:58:05 +00:00
|
|
|
fn line(len : usize) {
|
|
|
|
for _ in 0..len {
|
|
|
|
print!("-");
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:27:17 +00:00
|
|
|
let (heading, heading_length) = {
|
|
|
|
|
|
|
|
(
|
2022-08-29 09:38:21 +00:00
|
|
|
format!("[{}] {} {}", if self.data.completed.is_some() {"X"} else {" "}, colour::text::id(self.data.id), colour::text::task(&self.data.name)),
|
2022-08-25 06:40:56 +00:00
|
|
|
5 + self.data.name.chars().count() + self.data.id.to_string().chars().count()
|
2022-08-22 10:27:17 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2022-08-20 23:58:05 +00:00
|
|
|
println!("{}", heading);
|
2022-08-22 10:27:17 +00:00
|
|
|
line(heading_length);
|
2022-08-21 01:09:22 +00:00
|
|
|
|
2022-08-29 09:38:21 +00:00
|
|
|
println!("Priority: {}", colour::text::priority(&self.data.priority));
|
2022-08-22 10:27:17 +00:00
|
|
|
println!("Tags: [{}]", format_hash_set(&self.data.tags)?);
|
2022-08-24 12:09:52 +00:00
|
|
|
println!("Created: {}", self.data.created.round_subsecs(0));
|
|
|
|
|
|
|
|
if let Some(due) = self.data.due {
|
2022-08-29 09:38:21 +00:00
|
|
|
let due = colour::text::due_date(&due, self.data.completed.is_none(), true);
|
2022-08-24 12:09:52 +00:00
|
|
|
println!("Due: {}", due);
|
|
|
|
}
|
2022-08-20 23:58:05 +00:00
|
|
|
|
2022-08-21 02:17:23 +00:00
|
|
|
if let Some(mut info) = self.data.info.clone() {
|
|
|
|
let mut max_line_width = 0;
|
2022-08-20 23:58:05 +00:00
|
|
|
println!("Info:");
|
2022-08-21 02:17:23 +00:00
|
|
|
|
2022-08-21 06:43:42 +00:00
|
|
|
while info.ends_with('\n') {
|
2022-08-21 02:17:23 +00:00
|
|
|
info.pop();
|
|
|
|
}
|
|
|
|
|
2022-08-21 06:43:42 +00:00
|
|
|
let info_lines : Vec<&str> = info.split('\n').collect();
|
2022-08-21 01:48:30 +00:00
|
|
|
for line in info_lines {
|
|
|
|
max_line_width = usize::max(max_line_width, line.chars().count() + 4);
|
|
|
|
println!(" {}", line);
|
|
|
|
}
|
2022-08-21 02:17:23 +00:00
|
|
|
}
|
2022-08-22 10:27:17 +00:00
|
|
|
|
|
|
|
if !self.data.time_entries.is_empty() {
|
|
|
|
|
|
|
|
let mut entries = self.data.time_entries.clone();
|
|
|
|
// Sort entries by date.
|
|
|
|
entries.sort_by(|e1, e2| e1.logged_date.cmp(&e2.logged_date));
|
|
|
|
|
2022-08-24 23:26:33 +00:00
|
|
|
let mut total = Duration::zero();
|
|
|
|
let mut lines = Vec::with_capacity(entries.len());
|
2022-08-22 10:27:17 +00:00
|
|
|
for entry in &entries {
|
2022-08-30 08:23:23 +00:00
|
|
|
lines.push(format!(
|
|
|
|
" {} [{}] {}",
|
|
|
|
entry.duration,
|
|
|
|
entry.logged_date,
|
|
|
|
entry.message.as_ref().unwrap_or(&String::new())
|
|
|
|
));
|
2022-08-24 23:26:33 +00:00
|
|
|
total = total + entry.duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
println!("Time Entries (totaling {}):", total);
|
|
|
|
for line in lines {
|
|
|
|
println!("{}", line);
|
2022-08-22 10:27:17 +00:00
|
|
|
}
|
2022-08-20 23:58:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 06:40:56 +00:00
|
|
|
if !self.data.dependencies.is_empty() {
|
|
|
|
let tasks = Task::load_all_as_map(vault_folder, true)?;
|
|
|
|
|
|
|
|
println!("Dependencies:");
|
|
|
|
dependency_tree(self.data.id, &String::new(), true, &state.data.deps, &tasks);
|
|
|
|
}
|
2022-08-21 01:48:30 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-08-20 23:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_hash_set<T : fmt::Display>(set : &HashSet<T>) -> Result<String, error::Error> {
|
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
for value in set.iter() {
|
|
|
|
fmt::write(&mut output, format_args!("{}, ", value))?;
|
|
|
|
}
|
|
|
|
|
2022-08-21 06:43:42 +00:00
|
|
|
// Remove the trailing comma and space.
|
|
|
|
if !output.is_empty() {
|
2022-08-20 23:58:05 +00:00
|
|
|
output.pop();
|
|
|
|
output.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(output)
|
|
|
|
}
|
|
|
|
|
2022-08-25 06:40:56 +00:00
|
|
|
fn dependency_tree(start : Id, prefix : &String, is_last_item : bool, graph : &graph::Graph, tasks : &HashMap<Id, Task>) {
|
|
|
|
let next = graph.edges.get(&start).unwrap();
|
|
|
|
|
|
|
|
{
|
|
|
|
let task = tasks.get(&start).unwrap();
|
|
|
|
|
|
|
|
let name = if task.data.completed.is_some() {
|
2022-08-29 09:38:21 +00:00
|
|
|
colour::text::greyed_out(&task.data.name)
|
2022-08-25 06:40:56 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-08-29 09:38:21 +00:00
|
|
|
colour::text::task(&task.data.name)
|
2022-08-25 06:40:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if is_last_item {
|
2022-08-29 09:38:21 +00:00
|
|
|
println!("{}└──{} (ID: {})", prefix, name, colour::text::id(start))
|
2022-08-25 06:40:56 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-08-29 09:38:21 +00:00
|
|
|
println!("{}├──{} (ID: {})", prefix, name, colour::text::id(start))
|
2022-08-25 06:40:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let count = next.len();
|
|
|
|
|
|
|
|
for (i, node) in next.iter().enumerate() {
|
|
|
|
let new_is_last_item = i == count - 1;
|
|
|
|
|
|
|
|
let new_prefix = if is_last_item {
|
|
|
|
format!("{} ", prefix)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
format!("{}│ ", prefix)
|
|
|
|
};
|
|
|
|
|
|
|
|
dependency_tree(*node, &new_prefix, new_is_last_item, graph, tasks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:09:52 +00:00
|
|
|
|
2022-08-29 07:34:22 +00:00
|
|
|
fn compare_due_dates<T : Ord>(first : &Option<T>, second : &Option<T>) -> cmp::Ordering {
|
|
|
|
match (first, second) {
|
|
|
|
(None, None) => cmp::Ordering::Equal,
|
|
|
|
(Some(_), None) => cmp::Ordering::Less,
|
|
|
|
(None, Some(_)) => cmp::Ordering::Greater,
|
|
|
|
(Some(first), Some(second)) => first.cmp(second),
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 10:44:10 +00:00
|
|
|
|
2022-08-29 07:34:22 +00:00
|
|
|
pub fn list(mut options : super::ListOptions, vault_folder : &path::Path, state : &state::State) -> Result<(), error::Error> {
|
2022-08-25 10:44:10 +00:00
|
|
|
|
|
|
|
|
2022-08-20 23:58:05 +00:00
|
|
|
let mut table = comfy_table::Table::new();
|
|
|
|
table
|
|
|
|
.load_preset(comfy_table::presets::UTF8_FULL)
|
2022-08-21 05:31:27 +00:00
|
|
|
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS)
|
|
|
|
.set_content_arrangement(comfy_table::ContentArrangement::Dynamic);
|
2022-08-20 23:58:05 +00:00
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
|
|
|
|
let mut tasks : Box<dyn Iterator<Item = Task>> = Box::new(Task::load_all(vault_folder, true)?.into_iter());
|
|
|
|
|
2022-08-29 07:34:22 +00:00
|
|
|
// Filter the tasks.
|
|
|
|
if let Some(date) = options.created_before {
|
|
|
|
tasks = Box::new(tasks.filter(move |t| t.data.created.date() <= date));
|
2022-08-25 10:44:10 +00:00
|
|
|
}
|
2022-08-29 07:34:22 +00:00
|
|
|
if let Some(date) = options.created_after {
|
|
|
|
tasks = Box::new(tasks.filter(move |t| t.data.created.date() >= date));
|
2022-08-25 10:44:10 +00:00
|
|
|
}
|
2022-08-29 07:34:22 +00:00
|
|
|
|
|
|
|
if let Some(date) = options.due_before {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks = Box::new(tasks.filter(move |t| {
|
2022-08-29 07:34:22 +00:00
|
|
|
match compare_due_dates(&t.data.due.map(|d| d.date()), &Some(date)) {
|
|
|
|
cmp::Ordering::Less | cmp::Ordering::Equal => true,
|
|
|
|
cmp::Ordering::Greater => false,
|
2022-08-25 10:44:10 +00:00
|
|
|
}
|
2022-08-29 07:34:22 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(date) = options.due_after {
|
|
|
|
tasks = Box::new(tasks.filter(move |t| {
|
|
|
|
match compare_due_dates(&t.data.due.map(|d| d.date()), &Some(date)) {
|
|
|
|
cmp::Ordering::Greater | cmp::Ordering::Equal => true,
|
|
|
|
cmp::Ordering::Less => false,
|
2022-08-25 10:44:10 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2022-08-29 07:34:22 +00:00
|
|
|
|
2022-08-25 10:55:28 +00:00
|
|
|
if !options.include_completed {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks = Box::new(tasks.filter(|t| t.data.completed.is_none()));
|
|
|
|
}
|
|
|
|
|
2022-08-29 07:34:22 +00:00
|
|
|
if !options.tag.is_empty() {
|
|
|
|
let specified_tags : HashSet<_> = options.tag.iter().collect();
|
|
|
|
|
|
|
|
tasks = Box::new(tasks.filter(move |t| {
|
|
|
|
let task_tags : HashSet<_> = t.data.tags.iter().collect();
|
|
|
|
|
|
|
|
// Non empty intersection of tags means the task should be displayed
|
|
|
|
specified_tags.intersection(&task_tags).next().is_some()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.no_dependencies {
|
|
|
|
tasks = Box::new(tasks.filter(move |t| {
|
|
|
|
t.data.dependencies.is_empty()
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.no_dependents {
|
|
|
|
let tasks_with_dependents = state.data.deps.get_tasks_with_dependents();
|
|
|
|
|
|
|
|
tasks = Box::new(tasks.filter(move |t| {
|
|
|
|
!tasks_with_dependents.contains(&t.data.id)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-08-25 10:44:10 +00:00
|
|
|
let mut tasks : Vec<Task> = tasks.collect();
|
|
|
|
|
|
|
|
|
2022-08-29 07:34:22 +00:00
|
|
|
// Sort the tasks.
|
|
|
|
use super::{OrderBy, Order};
|
|
|
|
match options.order_by {
|
|
|
|
OrderBy::Id => {
|
|
|
|
match options.order {
|
|
|
|
Order::Asc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t1.data.id.cmp(&t2.data.id));
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
Order::Desc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t2.data.id.cmp(&t1.data.id));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
OrderBy::Name => {
|
|
|
|
match options.order {
|
|
|
|
Order::Asc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t1.data.name.cmp(&t2.data.name));
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
Order::Desc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t2.data.name.cmp(&t1.data.name));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
OrderBy::Due => {
|
|
|
|
match options.order {
|
|
|
|
Order::Asc => {
|
|
|
|
tasks.sort_by(|t1, t2| compare_due_dates(&t1.data.due, &t2.data.due));
|
2022-08-25 10:44:10 +00:00
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
Order::Desc => {
|
|
|
|
tasks.sort_by(|t1, t2| compare_due_dates(&t2.data.due, &t1.data.due));
|
2022-08-25 10:44:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
OrderBy::Priority => {
|
|
|
|
match options.order {
|
|
|
|
Order::Asc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t1.data.priority.cmp(&t2.data.priority));
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
Order::Desc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t2.data.priority.cmp(&t1.data.priority));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
OrderBy::Created => {
|
|
|
|
match options.order {
|
|
|
|
Order::Asc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t1.data.created.cmp(&t2.data.created));
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
Order::Desc => {
|
2022-08-25 10:44:10 +00:00
|
|
|
tasks.sort_by(|t1, t2| t2.data.created.cmp(&t1.data.created));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2022-08-29 07:34:22 +00:00
|
|
|
OrderBy::Tracked => {
|
|
|
|
match options.order {
|
|
|
|
Order::Asc => {
|
|
|
|
tasks.sort_by(|t1, t2| TimeEntry::total(&t1.data.time_entries).cmp(&TimeEntry::total(&t2.data.time_entries)));
|
|
|
|
},
|
|
|
|
Order::Desc => {
|
|
|
|
tasks.sort_by(|t1, t2| TimeEntry::total(&t2.data.time_entries).cmp(&TimeEntry::total(&t1.data.time_entries)));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 10:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Include the required columns
|
2022-08-29 07:34:22 +00:00
|
|
|
let mut headers = vec!["Id", "Name"];
|
2022-08-25 10:44:10 +00:00
|
|
|
|
2022-08-29 07:34:22 +00:00
|
|
|
// Remove duplicate columns.
|
|
|
|
{
|
|
|
|
let mut columns = HashSet::new();
|
|
|
|
|
|
|
|
options.column = options.column
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| {
|
|
|
|
if columns.contains(c) {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
columns.insert(c.clone());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
|
|
|
use super::Column;
|
|
|
|
for column in &options.column {
|
|
|
|
match column {
|
|
|
|
Column::Tracked => {
|
|
|
|
headers.push("Tracked");
|
|
|
|
},
|
|
|
|
Column::Due => {
|
|
|
|
headers.push("Due");
|
|
|
|
},
|
|
|
|
Column::Tags => {
|
|
|
|
headers.push("Tags");
|
|
|
|
},
|
|
|
|
Column::Priority => {
|
|
|
|
headers.push("Priority");
|
|
|
|
},
|
|
|
|
Column::Status => {
|
|
|
|
headers.push("Status");
|
|
|
|
},
|
|
|
|
Column::Created => {
|
|
|
|
headers.push("Created");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 10:44:10 +00:00
|
|
|
|
|
|
|
table.set_header(headers);
|
2022-08-20 23:58:05 +00:00
|
|
|
|
2022-08-21 02:34:34 +00:00
|
|
|
for task in tasks {
|
2022-08-22 10:37:50 +00:00
|
|
|
|
2022-08-29 09:38:21 +00:00
|
|
|
use comfy_table::Cell;
|
|
|
|
let mut row = vec![Cell::from(task.data.id), Cell::from(task.data.name)];
|
2022-08-22 10:37:50 +00:00
|
|
|
|
2022-08-29 07:34:22 +00:00
|
|
|
for column in &options.column {
|
|
|
|
match column {
|
|
|
|
Column::Tracked => {
|
|
|
|
let duration = TimeEntry::total(&task.data.time_entries);
|
|
|
|
row.push(
|
2022-08-29 09:38:21 +00:00
|
|
|
Cell::from(if duration == Duration::zero() { String::new() } else { duration.to_string() })
|
2022-08-29 07:34:22 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
Column::Due => {
|
|
|
|
row.push(match task.data.due {
|
2022-08-29 09:38:21 +00:00
|
|
|
Some(due) => colour::cell::due_date(&due, task.data.completed.is_none(), true),
|
|
|
|
None => Cell::from(String::new())
|
2022-08-29 07:34:22 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
Column::Tags => {
|
2022-08-29 09:38:21 +00:00
|
|
|
row.push(Cell::new(format_hash_set(&task.data.tags)?));
|
2022-08-29 07:34:22 +00:00
|
|
|
},
|
|
|
|
Column::Priority => {
|
2022-08-29 09:38:21 +00:00
|
|
|
row.push(colour::cell::priority(&task.data.priority));
|
2022-08-29 07:34:22 +00:00
|
|
|
},
|
|
|
|
Column::Status => {
|
|
|
|
row.push(
|
2022-08-29 09:38:21 +00:00
|
|
|
Cell::new(if task.data.completed.is_some() {
|
2022-08-29 07:34:22 +00:00
|
|
|
String::from("complete")
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
String::from("incomplete")
|
2022-08-29 09:38:21 +00:00
|
|
|
})
|
2022-08-29 07:34:22 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
Column::Created => {
|
2022-08-29 09:38:21 +00:00
|
|
|
row.push(Cell::new(task.data.created.round_subsecs(0).to_string()));
|
2022-08-29 07:34:22 +00:00
|
|
|
},
|
|
|
|
}
|
2022-08-25 10:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table.add_row(row);
|
2022-08-20 23:58:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("{}", table);
|
|
|
|
|
|
|
|
Ok(())
|
2022-08-20 04:01:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 23:56:23 +00:00
|
|
|
impl ops::Add for Duration {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, other : Self) -> Self::Output {
|
|
|
|
|
|
|
|
Self {
|
|
|
|
hours : self.hours + other.hours + (self.minutes + other.minutes) / 60,
|
|
|
|
minutes : (self.minutes + other.minutes) % 60,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Duration {
|
|
|
|
pub fn zero() -> Self {
|
|
|
|
Self {
|
|
|
|
minutes : 0,
|
|
|
|
hours : 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl ops::Div<usize> for Duration {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn div(self, divisor : usize) -> Self::Output {
|
|
|
|
let total_mins = f64::from(self.hours * 60 + self.minutes);
|
2022-08-24 22:30:52 +00:00
|
|
|
let divided_mins = total_mins / (divisor as f64);
|
2022-08-23 23:56:23 +00:00
|
|
|
let divided_mins = divided_mins.round() as u16;
|
|
|
|
|
|
|
|
Self {
|
|
|
|
hours : divided_mins / 60,
|
|
|
|
minutes : divided_mins % 60,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Duration {
|
|
|
|
fn fmt(&self, f : &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}:{:0>2}", self.hours, self.minutes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TimeEntry {
|
|
|
|
/// Adds up the times from a collection of time entries.
|
2022-08-25 06:40:56 +00:00
|
|
|
fn total(entries : &[TimeEntry]) -> Duration {
|
2022-08-23 23:56:23 +00:00
|
|
|
entries
|
2022-08-25 06:40:56 +00:00
|
|
|
.iter()
|
2022-08-23 23:56:23 +00:00
|
|
|
.map(|e| e.duration)
|
|
|
|
.fold(Duration::zero(), |a, d| a + d)
|
|
|
|
}
|
|
|
|
|
2022-08-30 08:23:23 +00:00
|
|
|
/// Creates a new TimeEntry, correctly validating and setting defaults.
|
|
|
|
pub fn new(hours : u16, minutes : u16, date : Option<chrono::NaiveDate>, message : Option<String>) -> Self {
|
2022-08-23 23:56:23 +00:00
|
|
|
|
|
|
|
let (hours, minutes) = {
|
|
|
|
(hours + minutes / 60, minutes % 60)
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
2022-08-30 08:23:23 +00:00
|
|
|
logged_date : date.unwrap_or(chrono::Utc::now().naive_local().date()),
|
|
|
|
message,
|
2022-08-23 23:56:23 +00:00
|
|
|
duration : Duration {
|
|
|
|
hours,
|
|
|
|
minutes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 04:01:43 +00:00
|
|
|
|