aimcal-cli 0.12.1

AIM - Analyze. Interact. Manage Your Time, with calendar support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// SPDX-FileCopyrightText: 2025-2026 Zexin Yuan <aim@yzx9.xyz>
//
// SPDX-License-Identifier: Apache-2.0

//! This module provides the tui command for the AIM CLI.
//! In general, they dont provide a comprehensive CLI command, but call TUI directly.

use std::error::Error;

use aimcal_core::{Aim, EventDraft, EventStatus, Id, Kind, Priority, TodoDraft, TodoStatus};
use clap::{ArgMatches, Command};

use crate::arg::{CommonArgs, EventArgs, EventOrTodoArgs, EventOrTodoStatus, TodoArgs};
use crate::cmd_event::{CmdEventEdit, CmdEventNew};
use crate::cmd_todo::{CmdTodoEdit, CmdTodoNew};
use crate::tui::{EventOrTodoDraft, draft_event_or_todo};
use crate::util::{OutputFormat, parse_datetime, parse_datetime_range};

#[derive(Debug, Clone)]
pub struct CmdNew {
    // fields
    pub kind: Option<Kind>,
    pub description: Option<String>,
    pub status: Option<EventOrTodoStatus>,
    pub summary: Option<String>,

    // fields (event specific)
    pub end: Option<String>,
    pub start: Option<String>,

    // fields (todo specific)
    pub due: Option<String>,
    pub percent_complete: Option<u8>,
    pub priority: Option<Priority>,

    // options
    pub output_format: OutputFormat,
}

impl CmdNew {
    pub const NAME: &str = "new";

    pub fn command() -> Command {
        let (args, event_args, todo_args) = args();
        Command::new(Self::NAME)
            .alias("add")
            .about("Add a new event or todo using TUI")
            // fields
            .arg(args.summary(true))
            .arg(args.description())
            .arg(args.status())
            // fields (event specific)
            .arg(event_args.start())
            .arg(event_args.end())
            // fields (todo specific)
            .arg(todo_args.due())
            .arg(todo_args.percent_complete())
            .arg(todo_args.priority())
            // options
            .arg(args.kind())
            .arg(CommonArgs::output_format())
    }

    pub fn from(matches: &ArgMatches) -> Self {
        Self {
            kind: EventOrTodoArgs::get_kind(matches),
            description: EventOrTodoArgs::get_description(matches),
            status: EventOrTodoArgs::get_status(matches),
            summary: EventOrTodoArgs::get_summary(matches),

            end: EventArgs::get_end(matches),
            start: EventArgs::get_start(matches),

            due: TodoArgs::get_due(matches),
            percent_complete: TodoArgs::get_percent_complete(matches),
            priority: TodoArgs::get_priority(matches),

            output_format: CommonArgs::get_output_format(matches),
        }
    }

    pub async fn run(self, aim: &mut Aim) -> Result<(), Box<dyn Error>> {
        // TODO: check is it a event / todo
        tracing::debug!(?self, "adding new item using TUI...");

        // infer kind
        let inferred_kind = self.infer_kind();
        match inferred_kind {
            Some(Kind::Event) => {
                if self.due.is_some() || self.percent_complete.is_some() || self.priority.is_some()
                {
                    return Err("Cannot provide todo-specific fields for an event".into());
                } else if let Some(EventOrTodoStatus::Todo(_)) = self.status {
                    return Err("Cannot provide todo-specific status for an event".into());
                }

                // if all required fields are provided, create directly
                if !CmdEventNew::need_tui(&self.summary, &self.start) {
                    tracing::info!("creating new event");
                    let draft = self.draft_event(aim)?;
                    return CmdEventNew::new_event(aim, draft, self.output_format).await;
                }
            }
            Some(Kind::Todo) => {
                if self.start.is_some() || self.end.is_some() {
                    return Err("Cannot provide event-specific fields for a todo".into());
                } else if let Some(EventOrTodoStatus::Event(_)) = self.status {
                    return Err("Cannot provide event-specific status for a todo".into());
                }

                // if all required fields are provided, create directly
                if !CmdTodoNew::need_tui(&self.summary) {
                    tracing::info!("creating new todo");
                    let draft = self.draft_todo(aim)?;
                    return CmdTodoNew::new_todo(aim, draft, self.output_format).await;
                }
            }
            None => { /* do nothing */ }
        }

        self.run_tui(aim, inferred_kind).await
    }

    async fn run_tui(
        self,
        aim: &mut Aim,
        inferred_kind: Option<Kind>,
    ) -> Result<(), Box<dyn Error>> {
        tracing::info!("adding new item using TUI");
        let mut event_draft = aim.default_event_draft();
        let mut todo_draft = aim
            .default_todo_draft()
            .map_err(|e| format!("Failed to create default todo draft: {e}"))?;
        let now = aim.now();

        // fields
        //
        if let Some(desc) = self.description {
            event_draft.description = Some(desc.clone());
            todo_draft.description = Some(desc);
        }

        if let Some(summary) = self.summary {
            event_draft.summary.clone_from(&summary);
            todo_draft.summary.clone_from(&summary);
        }

        match self.status {
            Some(EventOrTodoStatus::Event(s)) => event_draft.status = s,
            Some(EventOrTodoStatus::Todo(s)) => todo_draft.status = s,
            None => { /* do nothing */ }
        }

        // fields (event specific)
        //
        match (self.start, self.end) {
            (Some(start), Some(end)) => {
                let (s, e) = parse_datetime_range(&now, &start, &end)?;
                event_draft.start = s;
                event_draft.end = e;
            }
            (Some(start), None) => event_draft.start = parse_datetime(&aim.now(), &start)?,
            (None, Some(end)) => event_draft.end = parse_datetime(&aim.now(), &end)?,
            (None, None) => { /* do nothing */ }
        }

        // fields (todo specific)
        //
        if let Some(due) = self.due {
            todo_draft.due = parse_datetime(&now, &due)?;
        }

        if let Some(pc) = self.percent_complete {
            todo_draft.percent_complete = Some(pc);
        }

        if let Some(priority) = self.priority {
            todo_draft.priority = Some(priority);
        }

        // launch TUI
        let draft = draft_event_or_todo(aim, inferred_kind, event_draft, todo_draft)?;
        match draft {
            Some(EventOrTodoDraft::Event(draft)) => {
                tracing::info!("creating new event");
                CmdEventNew::new_event(aim, draft, self.output_format).await
            }
            Some(EventOrTodoDraft::Todo(draft)) => {
                tracing::info!("creating new todo");
                CmdTodoNew::new_todo(aim, draft, self.output_format).await
            }
            None => {
                tracing::info!("user cancel the drafting");
                Ok(())
            }
        }
    }

    /// Infer the kind of the new item based on the provided fields.
    fn infer_kind(&self) -> Option<Kind> {
        if let Some(kind) = self.kind {
            Some(kind)
        } else if let Some(status) = self.status {
            // as status is mutually exclusive, we can infer the kind directly
            match status {
                EventOrTodoStatus::Event(_) => Some(Kind::Event),
                EventOrTodoStatus::Todo(_) => Some(Kind::Todo),
            }
        } else {
            let maybe_event = self.start.is_some() || self.end.is_some();
            let maybe_todo =
                self.due.is_some() || self.percent_complete.is_some() || self.priority.is_some();

            match (maybe_event, maybe_todo) {
                (true, false) => Some(Kind::Event),
                (false, true) => Some(Kind::Todo),
                (true, true) => {
                    tracing::info!(
                        "both event-specific and todo-specific fields are provided, cannot infer the kind"
                    );
                    None
                }
                (false, false) => None,
            }
        }
    }

    fn draft_event(&self, aim: &mut Aim) -> Result<EventDraft, Box<dyn Error>> {
        let mut draft = aim.default_event_draft();
        let now = aim.now();

        // fields
        //
        if let Some(desc) = &self.description {
            draft.description = Some(desc.clone());
        }

        if let Some(summary) = &self.summary {
            draft.summary.clone_from(summary);
        }

        if let Some(EventOrTodoStatus::Event(s)) = self.status {
            draft.status = s;
        }

        // fields (event specific)
        //
        match (&self.start, &self.end) {
            (Some(start), Some(end)) => {
                let (s, e) = parse_datetime_range(&now, start, end)?;
                draft.start = s;
                draft.end = e;
            }
            (Some(start), None) => draft.start = parse_datetime(&aim.now(), start)?,
            (None, Some(end)) => draft.end = parse_datetime(&aim.now(), end)?,
            (None, None) => { /* do nothing */ }
        }

        Ok(draft)
    }

    fn draft_todo(&self, aim: &mut Aim) -> Result<TodoDraft, Box<dyn Error>> {
        let mut draft = aim
            .default_todo_draft()
            .map_err(|e| format!("Failed to create default todo draft: {e}"))?;
        let now = aim.now();

        // fields
        //
        if let Some(desc) = &self.description {
            draft.description = Some(desc.clone());
        }

        if let Some(summary) = &self.summary {
            draft.summary.clone_from(summary);
        }

        if let Some(EventOrTodoStatus::Todo(s)) = self.status {
            draft.status = s;
        }

        // fields (todo specific)
        //
        if let Some(due) = &self.due {
            draft.due = parse_datetime(&now, due)?;
        }

        if let Some(pc) = self.percent_complete {
            draft.percent_complete = Some(pc);
        }

        if let Some(priority) = self.priority {
            draft.priority = Some(priority);
        }

        Ok(draft)
    }
}

#[derive(Debug, Clone)]
pub struct CmdEdit {
    pub id: Id,

    // fields
    pub description: Option<String>,
    pub status: Option<EventOrTodoStatus>,
    pub summary: Option<String>,

    // fields (event specific)
    pub end: Option<String>,
    pub start: Option<String>,

    // fields (todo specific)
    pub due: Option<String>,
    pub percent_complete: Option<u8>,
    pub priority: Option<Priority>,

    // options
    pub output_format: OutputFormat,
}

impl CmdEdit {
    pub const NAME: &str = "edit";

    pub fn command() -> Command {
        let (args, event_args, todo_args) = args();
        Command::new(Self::NAME)
            .about("Edit a event or todo using TUI")
            .arg(args.id())
            // fields
            .arg(args.summary(false))
            .arg(args.description())
            .arg(args.status())
            // fields (event specific)
            .arg(event_args.start())
            .arg(event_args.end())
            // fields (todo specific)
            .arg(todo_args.due())
            .arg(todo_args.percent_complete())
            .arg(todo_args.priority())
            // options
            .arg(CommonArgs::output_format())
    }

    pub fn from(matches: &ArgMatches) -> Self {
        Self {
            id: EventOrTodoArgs::get_id(matches),

            description: EventOrTodoArgs::get_description(matches),
            status: EventOrTodoArgs::get_status(matches),
            summary: EventOrTodoArgs::get_summary(matches),

            end: EventArgs::get_end(matches),
            start: EventArgs::get_start(matches),

            due: TodoArgs::get_due(matches),
            percent_complete: TodoArgs::get_percent_complete(matches),
            priority: TodoArgs::get_priority(matches),

            output_format: CommonArgs::get_output_format(matches),
        }
    }

    pub async fn run(self, aim: &mut Aim) -> Result<(), Box<dyn Error>> {
        tracing::debug!(?self, "editing item using TUI...");

        let kind = aim.get_kind(&self.id).await?;

        // Check if any fields are provided to determine if we should use TUI mode
        let use_tui = match kind {
            Kind::Event => {
                tracing::debug!("item with id {} is an event", self.id.as_uid());
                if self.due.is_some() || self.percent_complete.is_some() || self.priority.is_some()
                {
                    return Err("Cannot provide todo-specific fields for an event".into());
                } else if let Some(EventOrTodoStatus::Todo(_)) = self.status {
                    return Err("Cannot provide todo-specific status for an event".into());
                }

                self.description.is_none()
                    && self.end.is_none()
                    && self.start.is_none()
                    && self.summary.is_none()
            }
            Kind::Todo => {
                tracing::debug!("item with id {} is a todo", self.id.as_uid());
                if self.start.is_some() || self.end.is_some() {
                    return Err("Cannot provide event-specific fields for a todo".into());
                } else if let Some(EventOrTodoStatus::Event(_)) = self.status {
                    return Err("Cannot provide event-specific status for a todo".into());
                }

                self.description.is_none()
                    && self.due.is_none()
                    && self.percent_complete.is_none()
                    && self.priority.is_none()
                    && self.summary.is_none()
            }
        };

        if use_tui {
            self.run_tui(aim, kind).await
        } else {
            self.run_direct(aim, kind).await
        }
    }

    /// Use TUI mode when no fields are provided
    async fn run_tui(self, aim: &mut Aim, kind: Kind) -> Result<(), Box<dyn Error>> {
        match kind {
            Kind::Event => {
                tracing::info!("editing event using TUI");
                CmdEventEdit::new_tui(self.id, self.output_format)
                    .run(aim)
                    .await
            }
            Kind::Todo => {
                tracing::info!("editing todo using TUI");
                CmdTodoEdit::new_tui(self.id, self.output_format)
                    .run(aim)
                    .await
            }
        }
    }

    /// Use direct edit mode when fields are provided
    async fn run_direct(self, aim: &mut Aim, kind: Kind) -> Result<(), Box<dyn Error>> {
        match kind {
            Kind::Event => {
                tracing::info!("editing event with provided fields");
                CmdEventEdit {
                    id: self.id,
                    description: self.description,
                    end: self.end,
                    start: self.start,
                    status: self.status.map(|s| match s {
                        EventOrTodoStatus::Event(status) => status,
                        EventOrTodoStatus::Todo(_) => EventStatus::default(),
                    }),
                    summary: self.summary,

                    output_format: self.output_format,
                }
                .run(aim)
                .await
            }
            Kind::Todo => {
                tracing::info!("editing todo with provided fields");
                CmdTodoEdit {
                    id: self.id,
                    description: self.description,
                    due: self.due,
                    percent_complete: self.percent_complete,
                    priority: self.priority,
                    status: self.status.map(|s| match s {
                        EventOrTodoStatus::Todo(status) => status,
                        EventOrTodoStatus::Event(_) => TodoStatus::default(),
                    }),
                    summary: self.summary,

                    output_format: self.output_format,
                }
                .run(aim)
                .await
            }
        }
    }
}

const fn args() -> (EventOrTodoArgs, EventArgs, TodoArgs) {
    (
        EventOrTodoArgs::new(None),
        EventArgs::new(false),
        TodoArgs::new(false),
    )
}