From f89615d8e4f7414e91d5fc26568f31aad72a37d6 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Tue, 30 Jul 2024 09:02:56 +0300 Subject: [PATCH] style: reformat code --- src/main.rs | 13 +++++++------ src/task.rs | 28 +++++++++++++------------- src/tasks.rs | 55 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 58 insertions(+), 38 deletions(-) diff --git a/src/main.rs b/src/main.rs index e95414a..7a1a703 100644 --- a/src/main.rs +++ b/src/main.rs @@ -188,15 +188,16 @@ async fn main() { println!(); let mut lines = stdin().lines(); loop { - tasks.print_tasks(); + or_print(tasks.print_tasks()); print!( "{}", format!( - " {}{}) ", - tasks.get_task_path(tasks.get_position()), - tasks.get_prompt_suffix() - ).italic() + " {}{}) ", + tasks.get_task_path(tasks.get_position()), + tasks.get_prompt_suffix() + ) + .italic() ); stdout().flush().unwrap(); match lines.next() { @@ -214,7 +215,7 @@ async fn main() { count += 1; } } - if count > 0 { + if count > 0 { info!("Received {count} updates"); } diff --git a/src/task.rs b/src/task.rs index b1e6872..ddf8872 100644 --- a/src/task.rs +++ b/src/task.rs @@ -21,11 +21,9 @@ pub(crate) struct Task { impl Task { pub(crate) fn new(event: Event) -> Task { - let (parents, tags) = event.tags.iter().partition_map(|tag| { - match tag { - Tag::Event { event_id, .. } => return Left(event_id), - _ => Right(tag.clone()) - } + let (parents, tags) = event.tags.iter().partition_map(|tag| match tag { + Tag::Event { event_id, .. } => return Left(event_id), + _ => Right(tag.clone()), }); Task { children: Default::default(), @@ -63,11 +61,7 @@ impl Task { fn states(&self) -> impl Iterator + '_ { self.props.iter().filter_map(|event| { event.kind.try_into().ok().map(|s| TaskState { - name: if event.content.is_empty() { - None - } else { - Some(event.content.clone()) - }, + name: Some(event.content.clone()).filter(|c| !c.is_empty()), state: s, time: event.created_at.clone(), }) @@ -130,7 +124,9 @@ impl Task { } fn filter_tags

(&self, predicate: P) -> Option - where P: FnMut(&&Tag) -> bool{ + where + P: FnMut(&&Tag) -> bool, + { self.tags.as_ref().map(|tags| { tags.into_iter() .filter(predicate) @@ -147,7 +143,10 @@ impl Task { "state" => self.state().map(|s| s.to_string()), "name" => Some(self.event.content.clone()), "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), "alltags" => Some(format!("{:?}", self.tags)), "props" => Some(format!( @@ -157,7 +156,10 @@ impl Task { .map(|e| format!("{} kind {} '{}'", e.created_at, e.kind, e.content)) .collect::>() )), - "descriptions" => Some(format!("{:?}", self.descriptions().collect::>())), + "descriptions" => Some(format!( + "{:?}", + self.descriptions().collect::>() + )), "desc" | "description" => self.descriptions().last().cloned(), _ => { warn!("Unknown task property {}", property); diff --git a/src/tasks.rs b/src/tasks.rs index 0a17829..dd54e30 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -2,8 +2,7 @@ use std::collections::{BTreeSet, HashMap}; use std::io::{Error, stdout, Write}; use std::iter::once; -use chrono::{Datelike, DateTime, Local, LocalResult, MappedLocalTime, NaiveDate, TimeZone, Utc}; -use chrono::format::DelayedFormat; +use chrono::{Local, TimeZone}; use chrono::LocalResult::Single; use colored::Colorize; use itertools::Itertools; @@ -117,7 +116,8 @@ impl Tasks { self.traverse_up_from(Some(id)) .take_while(|t| Some(t.event.id) != self.position), false, - ).unwrap_or(id.to_string()) + ) + .unwrap_or(id.to_string()) } // Helpers @@ -208,7 +208,11 @@ impl Tasks { match Local.timestamp_opt(state.time.as_i64(), 0) { Single(time) => { 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(), 1 => "yesterday ".into(), 2..=6 => date.format("%a ").to_string(), @@ -301,7 +305,13 @@ impl Tasks { return match input.split_once(": ") { None => EventBuilder::new(Kind::from(TASK_KIND), input, tags), 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) } }; @@ -391,22 +401,23 @@ impl Tasks { } } -pub(crate) fn join_tasks<'a>(iter: impl Iterator, include_last_id: bool) -> Option { +pub(crate) fn join_tasks<'a>( + iter: impl Iterator, + include_last_id: bool, +) -> Option { let tasks: Vec<&Task> = iter.collect(); tasks .iter() .map(|t| t.get_title()) - .chain( - if include_last_id { - tasks - .last() - .and_then(|t| t.parent_id()) - .map(|id| id.to_string()) - .into_iter() - } else { - None.into_iter() - } - ) + .chain(if include_last_id { + tasks + .last() + .and_then(|t| t.parent_id()) + .map(|id| id.to_string()) + .into_iter() + } else { + None.into_iter() + }) .fold(None, |acc, val| { 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()); tasks.move_to(Some(zero)); 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"); use itertools::Itertools; 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 + ); }