Skip to main content

things3_cloud/commands/
someday.rs

1use crate::app::Cli;
2use crate::commands::{Command, DetailedArgs};
3use crate::common::{
4    colored, fmt_project_with_note, fmt_task_line, fmt_task_with_note, BOLD, CYAN, DIM, ICONS,
5};
6use anyhow::Result;
7use clap::Args;
8use std::io::Write;
9
10#[derive(Debug, Default, Args)]
11pub struct SomedayArgs {
12    #[command(flatten)]
13    pub detailed: DetailedArgs,
14}
15
16impl Command for SomedayArgs {
17    fn run_with_ctx(
18        &self,
19        cli: &Cli,
20        out: &mut dyn Write,
21        ctx: &mut dyn crate::cmd_ctx::CmdCtx,
22    ) -> Result<()> {
23        let store = cli.load_store()?;
24        let today = ctx.today();
25        let items = store.someday();
26
27        if items.is_empty() {
28            writeln!(
29                out,
30                "{}",
31                colored("Someday is empty.", &[DIM], cli.no_color)
32            )?;
33            return Ok(());
34        }
35
36        writeln!(
37            out,
38            "{}",
39            colored(
40                &format!("{} Someday  ({} items)", ICONS.task_someday, items.len()),
41                &[BOLD, CYAN],
42                cli.no_color,
43            )
44        )?;
45        writeln!(out)?;
46
47        let id_prefix_len =
48            store.unique_prefix_length(&items.iter().map(|i| i.uuid.clone()).collect::<Vec<_>>());
49        let projects = items
50            .iter()
51            .filter(|i| i.is_project())
52            .cloned()
53            .collect::<Vec<_>>();
54        let tasks = items
55            .iter()
56            .filter(|i| !i.is_project())
57            .cloned()
58            .collect::<Vec<_>>();
59
60        for item in projects {
61            writeln!(
62                out,
63                "{}",
64                fmt_project_with_note(
65                    &item,
66                    &store,
67                    "  ",
68                    Some(id_prefix_len),
69                    true,
70                    false,
71                    self.detailed.detailed,
72                    &today,
73                    cli.no_color,
74                )
75            )?;
76        }
77
78        if !tasks.is_empty()
79            && !store
80                .someday()
81                .iter()
82                .filter(|i| i.is_project())
83                .collect::<Vec<_>>()
84                .is_empty()
85        {
86            writeln!(out)?;
87        }
88
89        for item in tasks {
90            let line = fmt_task_line(
91                &item,
92                &store,
93                false,
94                false,
95                false,
96                Some(id_prefix_len),
97                &today,
98                cli.no_color,
99            );
100            writeln!(
101                out,
102                "{}",
103                fmt_task_with_note(
104                    line,
105                    &item,
106                    "  ",
107                    Some(id_prefix_len),
108                    self.detailed.detailed,
109                    cli.no_color,
110                )
111            )?;
112        }
113        Ok(())
114    }
115}