caldir-cli 0.7.1

CLI for interacting with your local caldir directory and syncing with external calendar providers
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use anyhow::{Context, Result};
use caldir_core::caldir::Caldir;
use caldir_core::calendar::Calendar;
use caldir_core::event::{Event, EventTime, Reminder};
use chrono::Duration;
use dialoguer::{Input, Select};
use owo_colors::OwoColorize;

#[allow(clippy::too_many_arguments)]
pub fn run(
    title: Option<String>,
    start: Option<String>,
    end: Option<String>,
    duration: Option<String>,
    location: Option<String>,
    calendar_slug: Option<String>,
    reminder_args: Vec<String>,
    no_reminders: bool,
    calendars: Vec<Calendar>,
) -> Result<()> {
    let interactive = title.is_none() || start.is_none();

    // --- Title ---
    let title = match title {
        Some(t) => t,
        None => Input::<String>::new()
            .with_prompt("  Title")
            .interact_text()?,
    };

    // --- Start ---
    let start_time = if let Some(s) = start {
        parse_datetime(&s)?
    } else {
        prompt_with_retry("  When?", parse_datetime)?
    };

    // --- Duration / End ---
    let is_allday = matches!(start_time, EventTime::Date(_));
    let default_hint = if is_allday { "1 day" } else { "1 hour" };

    let end_time = if let Some(end_input) = end {
        parse_datetime(&end_input)?
    } else if let Some(dur_input) = duration {
        apply_duration(&start_time, &dur_input)?
    } else if interactive {
        prompt_duration(&start_time, default_hint)?
    } else {
        default_end(&start_time)
    };

    // --- Location ---
    let location = if let Some(loc) = location {
        if loc.is_empty() { None } else { Some(loc) }
    } else if interactive {
        let loc: String = Input::new()
            .with_prompt("  Where? (skip)")
            .default(String::new())
            .show_default(false)
            .interact_text()?;
        if loc.is_empty() { None } else { Some(loc) }
    } else {
        None
    };

    // --- Reminders ---
    let reminders: Vec<Reminder> = if no_reminders {
        vec![]
    } else if !reminder_args.is_empty() {
        reminder_args
            .iter()
            .map(|r| parse_reminder(r))
            .collect::<Result<_>>()?
    } else {
        let caldir = Caldir::load()?;
        caldir
            .config()
            .parse_default_reminders()
            .map_err(|e| anyhow::anyhow!("{}", e))?
            .unwrap_or_default()
    };

    // --- Calendar ---
    let calendar = resolve_calendar(calendar_slug, &calendars, interactive)?;

    let event = Event::new(title, start_time, end_time, None, location, None, reminders);

    let path = calendar.create_event(&event)?;

    let display_path = if let Ok(home) = std::env::var("HOME") {
        if let Ok(relative) = path.strip_prefix(&home) {
            format!("~/{}", relative.display())
        } else {
            path.display().to_string()
        }
    } else {
        path.display().to_string()
    };

    if interactive {
        println!();
    }
    println!("{}", format!("  Created: {}", display_path).green());

    Ok(())
}

/// Prompt the user with retry on parse errors.
fn prompt_with_retry<F>(prompt: &str, parse: F) -> Result<EventTime>
where
    F: Fn(&str) -> Result<EventTime>,
{
    loop {
        let input: String = Input::new().with_prompt(prompt).interact_text()?;
        match parse(&input) {
            Ok(result) => return Ok(result),
            Err(e) => {
                eprintln!("  {}", e.to_string().red());
            }
        }
    }
}

/// Prompt for duration/end with retry on parse errors.
fn prompt_duration(start: &EventTime, default_hint: &str) -> Result<EventTime> {
    loop {
        let input: String = Input::new()
            .with_prompt(format!("  How long? ({})", default_hint))
            .default(String::new())
            .show_default(false)
            .interact_text()?;
        if input.is_empty() {
            return Ok(default_end(start));
        }
        match parse_end(&input, start) {
            Ok(result) => return Ok(result),
            Err(e) => {
                eprintln!("  {}", e.to_string().red());
            }
        }
    }
}

/// Expand common abbreviations that fuzzydate doesn't handle.
fn expand_abbreviations(input: &str) -> String {
    let abbrevs = [
        ("mon", "monday"),
        ("tue", "tuesday"),
        ("tues", "tuesday"),
        ("wed", "wednesday"),
        ("thu", "thursday"),
        ("thur", "thursday"),
        ("thurs", "thursday"),
        ("fri", "friday"),
        ("sat", "saturday"),
        ("sun", "sunday"),
        ("jan", "january"),
        ("feb", "february"),
        ("mar", "march"),
        ("apr", "april"),
        ("jun", "june"),
        ("jul", "july"),
        ("aug", "august"),
        ("sep", "september"),
        ("sept", "september"),
        ("oct", "october"),
        ("nov", "november"),
        ("dec", "december"),
    ];

    let mut result = String::new();
    let lower = input.to_lowercase();

    for (i, word) in lower.split_whitespace().enumerate() {
        if i > 0 {
            result.push(' ');
        }
        let expanded = abbrevs
            .iter()
            .find(|(abbr, _)| *abbr == word)
            .map(|(_, full)| *full)
            .unwrap_or(word);
        result.push_str(expanded);
    }

    result
}

/// Parse a natural language date/time string into an EventTime.
/// If the input contains time tokens (am/pm, HH:MM, noon, midnight, "at"),
/// returns DateTimeZoned with the system timezone. Otherwise returns Date (all-day).
fn parse_datetime(input: &str) -> Result<EventTime> {
    let expanded = expand_abbreviations(input);
    let dt = fuzzydate::parse(&expanded)
        .map_err(|_| anyhow::anyhow!("Could not parse date/time: \"{}\"", input))?;

    if has_time_component(input) {
        let tzid = iana_time_zone::get_timezone().unwrap_or_else(|_| "UTC".to_string());
        Ok(EventTime::DateTimeZoned { datetime: dt, tzid })
    } else {
        Ok(EventTime::Date(dt.date()))
    }
}

/// Check if the user's input string contains time-related tokens.
fn has_time_component(input: &str) -> bool {
    let lower = input.to_lowercase();

    // Check for "noon" or "midnight"
    if lower.contains("noon") || lower.contains("midnight") {
        return true;
    }

    // Check for am/pm patterns like "6pm", "6 pm", "11am"
    let bytes = lower.as_bytes();
    for (i, &b) in bytes.iter().enumerate() {
        if (b == b'a' || b == b'p') && i + 1 < bytes.len() && bytes[i + 1] == b'm' {
            // Check that there's a digit before (possibly with space)
            if i > 0 && bytes[i - 1].is_ascii_digit() {
                return true;
            }
            if i > 1 && bytes[i - 1] == b' ' && bytes[i - 2].is_ascii_digit() {
                return true;
            }
        }
    }

    // Check for HH:MM pattern (digit(s):digit(s))
    for (i, &b) in bytes.iter().enumerate() {
        if b == b':' {
            let has_digit_before = i > 0 && bytes[i - 1].is_ascii_digit();
            let has_digit_after = i + 1 < bytes.len() && bytes[i + 1].is_ascii_digit();
            if has_digit_before && has_digit_after {
                return true;
            }
        }
    }

    // Check for "at" followed by a digit (e.g. "at 3", "at 15")
    if let Some(pos) = lower.find(" at ") {
        let after = &lower[pos + 4..];
        if after.starts_with(|c: char| c.is_ascii_digit()) {
            return true;
        }
    }
    // Also handle "at" at the start
    if let Some(after) = lower.strip_prefix("at ")
        && after.starts_with(|c: char| c.is_ascii_digit())
    {
        return true;
    }

    false
}

/// Parse an end input — tries duration first (humantime), then date/time (fuzzydate).
fn parse_end(input: &str, start: &EventTime) -> Result<EventTime> {
    // Try as duration first
    if let Ok(event_time) = try_apply_duration(start, input) {
        return Ok(event_time);
    }

    // Strip "until"/"to" prefix and parse as datetime
    let cleaned = input
        .strip_prefix("until ")
        .or_else(|| input.strip_prefix("to "))
        .unwrap_or(input);

    parse_datetime(cleaned)
}

/// Apply a duration string to a start time.
fn apply_duration(start: &EventTime, dur_input: &str) -> Result<EventTime> {
    try_apply_duration(start, dur_input)
        .with_context(|| format!("Could not parse duration: \"{}\"", dur_input))
}

fn try_apply_duration(start: &EventTime, dur_input: &str) -> Result<EventTime> {
    let std_dur = humantime::parse_duration(dur_input).map_err(|e| anyhow::anyhow!("{}", e))?;
    let chrono_dur = Duration::from_std(std_dur).context("Duration too large")?;

    match start {
        EventTime::Date(d) => Ok(EventTime::Date(*d + chrono_dur)),
        EventTime::DateTimeFloating(dt) => Ok(EventTime::DateTimeFloating(*dt + chrono_dur)),
        EventTime::DateTimeUtc(dt) => Ok(EventTime::DateTimeUtc(*dt + chrono_dur)),
        EventTime::DateTimeZoned { datetime, tzid } => Ok(EventTime::DateTimeZoned {
            datetime: *datetime + chrono_dur,
            tzid: tzid.clone(),
        }),
    }
}

/// Default end time: +1 hour for timed events, +1 day for all-day events.
fn default_end(start: &EventTime) -> EventTime {
    match start {
        EventTime::Date(d) => EventTime::Date(*d + Duration::days(1)),
        EventTime::DateTimeFloating(dt) => EventTime::DateTimeFloating(*dt + Duration::hours(1)),
        EventTime::DateTimeUtc(dt) => EventTime::DateTimeUtc(*dt + Duration::hours(1)),
        EventTime::DateTimeZoned { datetime, tzid } => EventTime::DateTimeZoned {
            datetime: *datetime + Duration::hours(1),
            tzid: tzid.clone(),
        },
    }
}

/// Parse a reminder string like "10m", "1h", "2 days" into a Reminder.
fn parse_reminder(input: &str) -> Result<Reminder> {
    Reminder::from_duration_str(input).map_err(|e| anyhow::anyhow!("{}", e))
}

/// Resolve which calendar to use.
fn resolve_calendar(
    slug: Option<String>,
    calendars: &[Calendar],
    interactive: bool,
) -> Result<&Calendar> {
    if let Some(slug) = slug {
        return calendars.iter().find(|c| c.slug == slug).ok_or_else(|| {
            let available: Vec<_> = calendars.iter().map(|c| c.slug.as_str()).collect();
            anyhow::anyhow!(
                "Calendar '{}' not found. Available: {}",
                slug,
                available.join(", ")
            )
        });
    }

    // If only one calendar, use it
    if calendars.len() == 1 {
        return Ok(&calendars[0]);
    }

    // Try the default calendar
    let caldir = Caldir::load()?;
    if let Some(default) = caldir.default_calendar()
        && let Some(cal) = calendars.iter().find(|c| c.slug == default.slug)
    {
        return Ok(cal);
    }

    // Multiple calendars, no default — ask if interactive
    if interactive {
        let items: Vec<&str> = calendars.iter().map(|c| c.slug.as_str()).collect();
        let selection = Select::new()
            .with_prompt("  Calendar")
            .items(&items)
            .default(0)
            .interact()?;
        Ok(&calendars[selection])
    } else {
        // Non-interactive with multiple calendars and no default
        let available: Vec<_> = calendars.iter().map(|c| c.slug.as_str()).collect();
        anyhow::bail!(
            "Multiple calendars found ({}). Use --calendar to specify one.",
            available.join(", ")
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{Datelike, NaiveDate};

    // --- has_time_component ---

    #[test]
    fn time_component_am_pm() {
        assert!(has_time_component("tomorrow 6pm"));
        assert!(has_time_component("friday 11am"));
        assert!(has_time_component("sat 3 pm"));
        assert!(has_time_component("9AM"));
    }

    #[test]
    fn time_component_colon() {
        assert!(has_time_component("tomorrow 15:00"));
        assert!(has_time_component("march 20 9:30"));
    }

    #[test]
    fn time_component_keywords() {
        assert!(has_time_component("tomorrow noon"));
        assert!(has_time_component("friday midnight"));
    }

    #[test]
    fn time_component_at_digit() {
        assert!(has_time_component("tomorrow at 3"));
        assert!(has_time_component("friday at 15"));
        assert!(has_time_component("at 9"));
    }

    #[test]
    fn no_time_component() {
        assert!(!has_time_component("tomorrow"));
        assert!(!has_time_component("march 20"));
        assert!(!has_time_component("next friday"));
        assert!(!has_time_component("saturday"));
    }

    #[test]
    fn no_false_positive_am_in_words() {
        // "am" inside words like "amsterdam" shouldn't match
        assert!(!has_time_component("december"));
        assert!(!has_time_component("camp"));
    }

    // --- expand_abbreviations ---

    #[test]
    fn expand_day_abbreviations() {
        assert_eq!(expand_abbreviations("sat 3pm"), "saturday 3pm");
        assert_eq!(expand_abbreviations("fri 9am"), "friday 9am");
        assert_eq!(expand_abbreviations("mon"), "monday");
        assert_eq!(expand_abbreviations("thu noon"), "thursday noon");
        assert_eq!(expand_abbreviations("tues 10am"), "tuesday 10am");
    }

    #[test]
    fn expand_month_abbreviations() {
        assert_eq!(expand_abbreviations("jan 20"), "january 20");
        assert_eq!(expand_abbreviations("sep 5 3pm"), "september 5 3pm");
        assert_eq!(expand_abbreviations("sept 5"), "september 5");
    }

    #[test]
    fn expand_preserves_non_abbreviations() {
        assert_eq!(expand_abbreviations("tomorrow 6pm"), "tomorrow 6pm");
        assert_eq!(expand_abbreviations("next friday"), "next friday");
    }

    // --- parse_datetime ---

    #[test]
    fn parse_datetime_timed_returns_zoned() {
        let result = parse_datetime("tomorrow 3pm").unwrap();
        assert!(matches!(result, EventTime::DateTimeZoned { .. }));
    }

    #[test]
    fn parse_datetime_date_only_returns_date() {
        let result = parse_datetime("tomorrow").unwrap();
        assert!(matches!(result, EventTime::Date(_)));
    }

    #[test]
    fn parse_datetime_abbreviation_works() {
        let result = parse_datetime("sat 3pm").unwrap();
        assert!(matches!(result, EventTime::DateTimeZoned { .. }));
    }

    #[test]
    fn parse_datetime_absolute_date() {
        let result = parse_datetime("march 20").unwrap();
        assert!(matches!(result, EventTime::Date(_)));
        if let EventTime::Date(d) = result {
            assert_eq!(d.month(), 3);
            assert_eq!(d.day(), 20);
        }
    }

    #[test]
    fn parse_datetime_invalid_input() {
        assert!(parse_datetime("not a date at all xyz").is_err());
    }

    // --- default_end ---

    #[test]
    fn default_end_allday_adds_one_day() {
        let start = EventTime::Date(NaiveDate::from_ymd_opt(2026, 3, 20).unwrap());
        let end = default_end(&start);
        assert_eq!(
            end,
            EventTime::Date(NaiveDate::from_ymd_opt(2026, 3, 21).unwrap())
        );
    }

    #[test]
    fn default_end_timed_adds_one_hour() {
        let start = EventTime::DateTimeFloating(
            NaiveDate::from_ymd_opt(2026, 3, 20)
                .unwrap()
                .and_hms_opt(15, 0, 0)
                .unwrap(),
        );
        let end = default_end(&start);
        assert_eq!(
            end,
            EventTime::DateTimeFloating(
                NaiveDate::from_ymd_opt(2026, 3, 20)
                    .unwrap()
                    .and_hms_opt(16, 0, 0)
                    .unwrap()
            )
        );
    }

    // --- try_apply_duration ---

    #[test]
    fn apply_duration_minutes() {
        let start = EventTime::DateTimeFloating(
            NaiveDate::from_ymd_opt(2026, 3, 20)
                .unwrap()
                .and_hms_opt(15, 0, 0)
                .unwrap(),
        );
        let end = try_apply_duration(&start, "30m").unwrap();
        assert_eq!(
            end,
            EventTime::DateTimeFloating(
                NaiveDate::from_ymd_opt(2026, 3, 20)
                    .unwrap()
                    .and_hms_opt(15, 30, 0)
                    .unwrap()
            )
        );
    }

    #[test]
    fn apply_duration_hours() {
        let start = EventTime::DateTimeFloating(
            NaiveDate::from_ymd_opt(2026, 3, 20)
                .unwrap()
                .and_hms_opt(14, 0, 0)
                .unwrap(),
        );
        let end = try_apply_duration(&start, "2hours").unwrap();
        assert_eq!(
            end,
            EventTime::DateTimeFloating(
                NaiveDate::from_ymd_opt(2026, 3, 20)
                    .unwrap()
                    .and_hms_opt(16, 0, 0)
                    .unwrap()
            )
        );
    }

    #[test]
    fn apply_duration_to_allday() {
        let start = EventTime::Date(NaiveDate::from_ymd_opt(2026, 3, 20).unwrap());
        let end = try_apply_duration(&start, "3days").unwrap();
        assert_eq!(
            end,
            EventTime::Date(NaiveDate::from_ymd_opt(2026, 3, 23).unwrap())
        );
    }

    // --- parse_end ---

    #[test]
    fn parse_end_duration_string() {
        let start = EventTime::DateTimeFloating(
            NaiveDate::from_ymd_opt(2026, 3, 20)
                .unwrap()
                .and_hms_opt(15, 0, 0)
                .unwrap(),
        );
        let end = parse_end("45m", &start).unwrap();
        assert_eq!(
            end,
            EventTime::DateTimeFloating(
                NaiveDate::from_ymd_opt(2026, 3, 20)
                    .unwrap()
                    .and_hms_opt(15, 45, 0)
                    .unwrap()
            )
        );
    }

    #[test]
    fn parse_end_until_datetime() {
        let start = EventTime::DateTimeFloating(
            NaiveDate::from_ymd_opt(2026, 3, 20)
                .unwrap()
                .and_hms_opt(15, 0, 0)
                .unwrap(),
        );
        let end = parse_end("until tomorrow 5pm", &start).unwrap();
        assert!(matches!(end, EventTime::DateTimeZoned { .. }));
    }
}