From f8f0cf75700b79e8b0f4934b99bf9d7e42002ad2 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Tue, 30 Jul 2024 08:23:32 +0300 Subject: [PATCH] feat(tasks): print info on currently selected task --- src/task.rs | 4 ++-- src/tasks.rs | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/task.rs b/src/task.rs index 18be881..b1e6872 100644 --- a/src/task.rs +++ b/src/task.rs @@ -50,7 +50,7 @@ impl Task { .unwrap_or_else(|| self.get_id().to_string()) } - fn descriptions(&self) -> impl Iterator + '_ { + pub(crate) fn descriptions(&self) -> impl Iterator + '_ { self.props.iter().filter_map(|event| { if event.kind == Kind::TextNote { Some(&event.content) @@ -170,7 +170,7 @@ impl Task { pub(crate) struct TaskState { state: State, name: Option, - time: Timestamp, + pub(crate) time: Timestamp, } impl TaskState { pub(crate) fn get_label(&self) -> String { diff --git a/src/tasks.rs b/src/tasks.rs index 84dc103..cece9e7 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -1,6 +1,8 @@ use std::collections::{BTreeSet, HashMap}; +use std::io::{Error, stdout, Write}; use std::iter::once; +use itertools::Itertools; use log::{debug, error, info, trace, warn}; use nostr_sdk::{Event, EventBuilder, EventId, Keys, Kind, Tag}; use nostr_sdk::Tag::Hashtag; @@ -159,13 +161,13 @@ impl Tasks { } } + fn current_task(&self) -> Option<&Task> { + self.position.and_then(|id| self.tasks.get(&id)) + } + pub(crate) fn current_tasks(&self) -> Vec<&Task> { if self.depth == 0 { - return self - .position - .and_then(|id| self.tasks.get(&id)) - .into_iter() - .collect(); + return self.current_task().into_iter().collect(); } let res: Vec<&Task> = self.resolve_tasks(self.view.iter()); if res.len() > 0 { @@ -191,10 +193,24 @@ impl Tasks { .collect() } - pub(crate) fn print_tasks(&self) { - println!("{}", self.properties.join("\t")); + pub(crate) fn print_tasks(&self) -> Result<(), Error> { + let mut lock = stdout().lock(); + if let Some(t) = self.current_task() { + if let Some(state) = t.state() { + writeln!( + lock, + "{} since {} (total time {}m)", + state.get_label(), + state.time.to_human_datetime(), + t.time_tracked() / 60 + )?; + } + writeln!(lock, "{}", t.descriptions().join("\n"))?; + } + writeln!(lock, "{}", self.properties.join("\t"))?; // TODO proper columns for task in self.current_tasks() { - println!( + writeln!( + lock, "{}", self.properties .iter() @@ -209,9 +225,10 @@ impl Tasks { }) .collect::>() .join(" \t") - ); + )?; } - println!(); + writeln!(lock)?; + Ok(()) } // Movement and Selection @@ -231,11 +248,7 @@ impl Tasks { } pub(crate) fn move_up(&mut self) { - self.move_to( - self.position - .and_then(|id| self.tasks.get(&id)) - .and_then(|t| t.parent_id()), - ) + self.move_to(self.current_task().and_then(|t| t.parent_id())) } pub(crate) fn move_to(&mut self, id: Option) {