style: reformat code

This commit is contained in:
xeruf 2024-07-30 09:02:56 +03:00
parent b519d459db
commit f89615d8e4
3 changed files with 58 additions and 38 deletions

View File

@ -188,7 +188,7 @@ async fn main() {
println!(); println!();
let mut lines = stdin().lines(); let mut lines = stdin().lines();
loop { loop {
tasks.print_tasks(); or_print(tasks.print_tasks());
print!( print!(
"{}", "{}",
@ -196,7 +196,8 @@ async fn main() {
" {}{}) ", " {}{}) ",
tasks.get_task_path(tasks.get_position()), tasks.get_task_path(tasks.get_position()),
tasks.get_prompt_suffix() tasks.get_prompt_suffix()
).italic() )
.italic()
); );
stdout().flush().unwrap(); stdout().flush().unwrap();
match lines.next() { match lines.next() {

View File

@ -21,11 +21,9 @@ pub(crate) struct Task {
impl Task { impl Task {
pub(crate) fn new(event: Event) -> Task { pub(crate) fn new(event: Event) -> Task {
let (parents, tags) = event.tags.iter().partition_map(|tag| { let (parents, tags) = event.tags.iter().partition_map(|tag| match tag {
match tag {
Tag::Event { event_id, .. } => return Left(event_id), Tag::Event { event_id, .. } => return Left(event_id),
_ => Right(tag.clone()) _ => Right(tag.clone()),
}
}); });
Task { Task {
children: Default::default(), children: Default::default(),
@ -63,11 +61,7 @@ impl Task {
fn states(&self) -> impl Iterator<Item = TaskState> + '_ { fn states(&self) -> impl Iterator<Item = TaskState> + '_ {
self.props.iter().filter_map(|event| { self.props.iter().filter_map(|event| {
event.kind.try_into().ok().map(|s| TaskState { event.kind.try_into().ok().map(|s| TaskState {
name: if event.content.is_empty() { name: Some(event.content.clone()).filter(|c| !c.is_empty()),
None
} else {
Some(event.content.clone())
},
state: s, state: s,
time: event.created_at.clone(), time: event.created_at.clone(),
}) })
@ -130,7 +124,9 @@ impl Task {
} }
fn filter_tags<P>(&self, predicate: P) -> Option<String> fn filter_tags<P>(&self, predicate: P) -> Option<String>
where P: FnMut(&&Tag) -> bool{ where
P: FnMut(&&Tag) -> bool,
{
self.tags.as_ref().map(|tags| { self.tags.as_ref().map(|tags| {
tags.into_iter() tags.into_iter()
.filter(predicate) .filter(predicate)
@ -147,7 +143,10 @@ impl Task {
"state" => self.state().map(|s| s.to_string()), "state" => self.state().map(|s| s.to_string()),
"name" => Some(self.event.content.clone()), "name" => Some(self.event.content.clone()),
"time" => Some(format!("{}m", self.time_tracked().div(60))), "time" => Some(format!("{}m", self.time_tracked().div(60))),
"hashtags" => self.filter_tags(|tag| tag.single_letter_tag().is_some_and(|sltag| sltag.character == Alphabet::T)), "hashtags" => self.filter_tags(|tag| {
tag.single_letter_tag()
.is_some_and(|sltag| sltag.character == Alphabet::T)
}),
"tags" => self.filter_tags(|_| true), "tags" => self.filter_tags(|_| true),
"alltags" => Some(format!("{:?}", self.tags)), "alltags" => Some(format!("{:?}", self.tags)),
"props" => Some(format!( "props" => Some(format!(
@ -157,7 +156,10 @@ impl Task {
.map(|e| format!("{} kind {} '{}'", e.created_at, e.kind, e.content)) .map(|e| format!("{} kind {} '{}'", e.created_at, e.kind, e.content))
.collect::<Vec<String>>() .collect::<Vec<String>>()
)), )),
"descriptions" => Some(format!("{:?}", self.descriptions().collect::<Vec<&String>>())), "descriptions" => Some(format!(
"{:?}",
self.descriptions().collect::<Vec<&String>>()
)),
"desc" | "description" => self.descriptions().last().cloned(), "desc" | "description" => self.descriptions().last().cloned(),
_ => { _ => {
warn!("Unknown task property {}", property); warn!("Unknown task property {}", property);

View File

@ -2,8 +2,7 @@ use std::collections::{BTreeSet, HashMap};
use std::io::{Error, stdout, Write}; use std::io::{Error, stdout, Write};
use std::iter::once; use std::iter::once;
use chrono::{Datelike, DateTime, Local, LocalResult, MappedLocalTime, NaiveDate, TimeZone, Utc}; use chrono::{Local, TimeZone};
use chrono::format::DelayedFormat;
use chrono::LocalResult::Single; use chrono::LocalResult::Single;
use colored::Colorize; use colored::Colorize;
use itertools::Itertools; use itertools::Itertools;
@ -117,7 +116,8 @@ impl Tasks {
self.traverse_up_from(Some(id)) self.traverse_up_from(Some(id))
.take_while(|t| Some(t.event.id) != self.position), .take_while(|t| Some(t.event.id) != self.position),
false, false,
).unwrap_or(id.to_string()) )
.unwrap_or(id.to_string())
} }
// Helpers // Helpers
@ -208,7 +208,11 @@ impl Tasks {
match Local.timestamp_opt(state.time.as_i64(), 0) { match Local.timestamp_opt(state.time.as_i64(), 0) {
Single(time) => { Single(time) => {
let date = time.date_naive(); let date = time.date_naive();
let prefix = match Local::now().date_naive().signed_duration_since(date).num_days() { let prefix = match Local::now()
.date_naive()
.signed_duration_since(date)
.num_days()
{
0 => "".into(), 0 => "".into(),
1 => "yesterday ".into(), 1 => "yesterday ".into(),
2..=6 => date.format("%a ").to_string(), 2..=6 => date.format("%a ").to_string(),
@ -301,7 +305,13 @@ impl Tasks {
return match input.split_once(": ") { return match input.split_once(": ") {
None => EventBuilder::new(Kind::from(TASK_KIND), input, tags), None => EventBuilder::new(Kind::from(TASK_KIND), input, tags),
Some(s) => { Some(s) => {
tags.append(&mut s.1.split_ascii_whitespace().map(|t| Hashtag(t.to_string())).collect()); tags.append(
&mut s
.1
.split_ascii_whitespace()
.map(|t| Hashtag(t.to_string()))
.collect(),
);
EventBuilder::new(Kind::from(TASK_KIND), s.0, tags) EventBuilder::new(Kind::from(TASK_KIND), s.0, tags)
} }
}; };
@ -391,13 +401,15 @@ impl Tasks {
} }
} }
pub(crate) fn join_tasks<'a>(iter: impl Iterator<Item = &'a Task>, include_last_id: bool) -> Option<String> { pub(crate) fn join_tasks<'a>(
iter: impl Iterator<Item = &'a Task>,
include_last_id: bool,
) -> Option<String> {
let tasks: Vec<&Task> = iter.collect(); let tasks: Vec<&Task> = iter.collect();
tasks tasks
.iter() .iter()
.map(|t| t.get_title()) .map(|t| t.get_title())
.chain( .chain(if include_last_id {
if include_last_id {
tasks tasks
.last() .last()
.and_then(|t| t.parent_id()) .and_then(|t| t.parent_id())
@ -405,8 +417,7 @@ pub(crate) fn join_tasks<'a>(iter: impl Iterator<Item = &'a Task>, include_last_
.into_iter() .into_iter()
} else { } else {
None.into_iter() None.into_iter()
} })
)
.fold(None, |acc, val| { .fold(None, |acc, val| {
Some(acc.map_or_else(|| val.clone(), |cur| format!("{}>{}", val, cur))) Some(acc.map_or_else(|| val.clone(), |cur| format!("{}>{}", val, cur)))
}) })
@ -510,10 +521,16 @@ fn test_depth() {
assert_eq!(tasks.get_task_path(Some(zero)), zero.to_string()); assert_eq!(tasks.get_task_path(Some(zero)), zero.to_string());
tasks.move_to(Some(zero)); tasks.move_to(Some(zero));
let dangling = tasks.make_task("test"); let dangling = tasks.make_task("test");
assert_eq!(tasks.get_task_path(dangling), "0000000000000000000000000000000000000000000000000000000000000000>test"); assert_eq!(
tasks.get_task_path(dangling),
"0000000000000000000000000000000000000000000000000000000000000000>test"
);
assert_eq!(tasks.relative_path(dangling.unwrap()), "test"); assert_eq!(tasks.relative_path(dangling.unwrap()), "test");
use itertools::Itertools; use itertools::Itertools;
assert_eq!("test toast".split(' ').collect_vec().len(), 3); assert_eq!("test toast".split(' ').collect_vec().len(), 3);
assert_eq!("test toast".split_ascii_whitespace().collect_vec().len(), 2); assert_eq!(
"test toast".split_ascii_whitespace().collect_vec().len(),
2
);
} }