Skip to main content

things3_cloud/commands/
anytime.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, detailed_json_conflict, write_json},
10    ui::{
11        render_element_to_string,
12        views::{anytime::AnytimeView, json::common::build_tasks_json},
13    },
14};
15
16#[derive(Debug, Default, Args)]
17pub struct AnytimeArgs {
18    #[command(flatten)]
19    pub detailed: DetailedArgs,
20}
21
22impl Command for AnytimeArgs {
23    fn run_with_ctx(
24        &self,
25        cli: &Cli,
26        out: &mut dyn Write,
27        ctx: &mut dyn crate::cmd_ctx::CmdCtx,
28    ) -> Result<()> {
29        let store = Arc::new(cli.load_store()?);
30        let today = ctx.today();
31        let tasks = store.anytime(&today);
32
33        let json = cli.json;
34        if json {
35            if detailed_json_conflict(json, self.detailed.detailed) {
36                return Ok(());
37            }
38            write_json(out, &build_tasks_json(&tasks, &store, &today))?;
39            return Ok(());
40        }
41
42        let mut ui = element! {
43            ContextProvider(value: Context::owned(store.clone())) {
44                ContextProvider(value: Context::owned(today)) {
45                    AnytimeView(
46                        items: &tasks,
47                        detailed: self.detailed.detailed,
48                    )
49                }
50            }
51        };
52
53        let rendered = render_element_to_string(&mut ui, cli.no_color);
54        writeln!(out, "{}", rendered)?;
55        Ok(())
56    }
57}