Skip to main content

things3_cloud/commands/
logbook.rs

1use std::{io::Write, sync::Arc};
2
3use anyhow::Result;
4use clap::Args;
5use iocraft::prelude::*;
6
7use crate::{
8    app::Cli,
9    commands::{Command, DetailedArgs},
10    common::parse_day,
11    ui::{render_element_to_string, views::logbook::LogbookView},
12};
13
14#[derive(Debug, Default, Args)]
15#[command(about = "Show the Logbook")]
16pub struct LogbookArgs {
17    #[command(flatten)]
18    pub detailed: DetailedArgs,
19    #[arg(
20        long = "from",
21        help = "Show items completed on/after this date (YYYY-MM-DD)"
22    )]
23    pub from_date: Option<String>,
24    #[arg(
25        long = "to",
26        help = "Show items completed on/before this date (YYYY-MM-DD)"
27    )]
28    pub to_date: Option<String>,
29}
30
31impl Command for LogbookArgs {
32    fn run_with_ctx(
33        &self,
34        cli: &Cli,
35        out: &mut dyn Write,
36        ctx: &mut dyn crate::cmd_ctx::CmdCtx,
37    ) -> Result<()> {
38        let store = Arc::new(cli.load_store()?);
39        let today = ctx.today();
40
41        let from_day =
42            parse_day(self.from_date.as_deref(), "--from").map_err(anyhow::Error::msg)?;
43        let to_day = parse_day(self.to_date.as_deref(), "--to").map_err(anyhow::Error::msg)?;
44
45        if let (Some(from), Some(to)) = (from_day, to_day)
46            && from > to
47        {
48            eprintln!("--from date must be before or equal to --to date");
49            return Ok(());
50        }
51
52        let tasks = store.logbook(from_day, to_day);
53        let mut ui = element! {
54            ContextProvider(value: Context::owned(store.clone())) {
55                ContextProvider(value: Context::owned(today)) {
56                    LogbookView(
57                        items: &tasks,
58                        detailed: self.detailed.detailed,
59                    )
60                }
61            }
62        };
63        let rendered = render_element_to_string(&mut ui, cli.no_color);
64        writeln!(out, "{}", rendered)?;
65
66        Ok(())
67    }
68}