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,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");
}

View File

@ -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<Item = TaskState> + '_ {
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<P>(&self, predicate: P) -> Option<String>
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::<Vec<String>>()
)),
"descriptions" => Some(format!("{:?}", self.descriptions().collect::<Vec<&String>>())),
"descriptions" => Some(format!(
"{:?}",
self.descriptions().collect::<Vec<&String>>()
)),
"desc" | "description" => self.descriptions().last().cloned(),
_ => {
warn!("Unknown task property {}", property);

View File

@ -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<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();
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
);
}