cfait 1.0.1

Powerful, fast and elegant task / TODO manager. (GUI & TUI, CalDAV & local)
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
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Binary entry point for the TUI application (supports non-interactive CLI subcommands).
//
// This file implements a small CLI dispatcher on top of the existing TUI entry
// point so that users can run quick commands non-interactively (add, list,
// search, toggle, delete, import, export, sync, daemon). The interactive TUI
// still runs when no command is provided.
//
// Note: This file intentionally mirrors the project's existing controller/store
// APIs and uses the app context for config/data paths.

use anyhow::Result;
use cfait::context::{AppContext, StandardContext};
use cfait::model::Task;
use cfait::storage::LocalStorage;
use cfait::store::{FilterOptions, TaskStore};
use chrono::Utc;
use std::collections::HashSet;
use std::env;
use std::path::PathBuf;
use std::sync::Arc;

// Helper to quickly build the store from local files and cache for CLI reads
async fn build_store_cli(ctx: &Arc<dyn AppContext>) -> TaskStore {
    let mut store = TaskStore::new(ctx.clone());

    if let Ok(locals) = cfait::storage::LocalCalendarRegistry::load(ctx.as_ref()) {
        for loc in locals {
            if let Ok(mut tasks) =
                cfait::storage::LocalStorage::load_for_href(ctx.as_ref(), &loc.href)
            {
                cfait::journal::Journal::apply_to_tasks(ctx.as_ref(), &mut tasks, &loc.href);
                store.insert(loc.href, tasks);
            }
        }
    }

    if let Ok(cals) = cfait::cache::Cache::load_calendars(ctx.as_ref()) {
        for cal in cals {
            if cal.href.starts_with("local://") {
                continue;
            }
            if let Ok((mut tasks, _)) = cfait::cache::Cache::load(ctx.as_ref(), &cal.href) {
                cfait::journal::Journal::apply_to_tasks(ctx.as_ref(), &mut tasks, &cal.href);
                store.insert(cal.href, tasks);
            }
        }
    }

    store
}

// Helper to resolve short partial UIDs back to a full UID
fn resolve_uid(store: &TaskStore, partial: &str) -> Option<String> {
    let mut matches: Vec<String> = Vec::new();
    for map in store.calendars.values() {
        for uid in map.keys() {
            if uid.starts_with(partial) {
                matches.push(uid.clone());
            }
        }
    }

    match matches.len() {
        1 => Some(matches.into_iter().next().unwrap()),
        0 => {
            eprintln!("Error: No task matches UID '{}'", partial);
            None
        }
        _ => {
            eprintln!("Error: Ambiguous UID '{}'. Matches:", partial);
            for m in matches {
                if let Some(t) = store.get_task_ref(&m) {
                    let short = &m[..std::cmp::min(8, m.len())];
                    eprintln!("  {} - {}", short, t.summary);
                }
            }
            None
        }
    }
}

// Best-effort sync helper that can be called without passing a pre-loaded config
// Delegate to `sync_background` so the shared helper is used and keeps logic centralized.
async fn maybe_sync(ctx: Arc<dyn AppContext>) -> Result<(), String> {
    if let Ok(config) = cfait::config::Config::load_with_credentials(ctx.as_ref()) {
        // Reuse the existing background sync helper which already implements a
        // timeout and client fallback. This ensures `sync_background` is referenced.
        sync_background(ctx, config).await
    } else {
        Ok(())
    }
}

// Background sync trigger so the CLI stays incredibly fast (keeps old helper for callers that prefer to pass config)
async fn sync_background(
    ctx: Arc<dyn AppContext>,
    config: cfait::config::Config,
) -> Result<(), String> {
    if config.url.is_empty() {
        return Ok(());
    }

    let ctx_clone = ctx.clone();
    let cfg_clone = config.clone();
    let sync_future = async move {
        // Pass an Arc<dyn AppContext> (clone) to the client helper which expects an Arc.
        if let Ok((client, _, _, _, _)) = cfait::client::RustyClient::connect_with_fallback(
            ctx_clone.clone(),
            cfg_clone,
            Some("CLI"),
        )
        .await
        {
            client.sync_journal().await?;
        }
        Ok(())
    };

    // Give it up to 10 seconds to sync, otherwise gracefully detach
    match tokio::time::timeout(std::time::Duration::from_secs(10), sync_future).await {
        Ok(res) => res,
        Err(_) => Err("Sync timed out (changes are safely queued for next sync)".to_string()),
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let mut args: Vec<String> = env::args().collect();
    let binary_name = args.first().cloned().unwrap_or_else(|| "cfait".to_string());

    // Parse for --root argument before creating the context
    let mut override_root: Option<PathBuf> = None;
    if let Some(pos) = args.iter().position(|arg| arg == "--root" || arg == "-r")
        && pos + 1 < args.len()
    {
        override_root = Some(PathBuf::from(args[pos + 1].clone()));
        // Remove the flag and its value so they don't interfere with other parsing
        args.remove(pos);
        args.remove(pos);
    }

    let ctx: Arc<dyn AppContext> = Arc::new(StandardContext::new(override_root));
    cfait::config::init_locale(ctx.as_ref());

    let command = args.get(1).map(|s| s.as_str()).unwrap_or("");

    // If command is empty, we are launching the interactive TUI.
    // It is ONLY safe to use stderr if we are NOT in the interactive TUI.
    let is_interactive_tui = command.is_empty();
    cfait::system::init_logging(ctx.as_ref(), !is_interactive_tui);
    cfait::system::init_keyring(); // <-- ADD THIS LINE

    if command.starts_with('-') || command == "help" {
        cfait::cli::print_help(&binary_name);
        return Ok(());
    }

    match command {
        "import" => {
            if args.len() < 3 {
                eprintln!("Error: Missing file path");
                eprintln!("Usage: cfait import <file.ics> [--collection <id>]");
                std::process::exit(1);
            }
            let file_path = &args[2];
            let collection_id = if args.len() > 4 && args[3] == "--collection" {
                Some(args[4].clone())
            } else {
                None
            };
            let ics_content = std::fs::read_to_string(file_path).unwrap_or_else(|e| {
                eprintln!("Error reading file '{}': {}", file_path, e);
                std::process::exit(1);
            });
            let href = if let Some(col_id) = collection_id {
                if col_id == "default" {
                    "local://default".to_string()
                } else {
                    format!("local://{}", col_id)
                }
            } else {
                "local://default".to_string()
            };

            match LocalStorage::import_from_ics(ctx.as_ref(), &href, &ics_content) {
                Ok(count) => println!(
                    "Successfully imported {} task(s) to collection '{}'",
                    count, href
                ),
                Err(e) => {
                    eprintln!("Error importing tasks: {}", e);
                    std::process::exit(1);
                }
            }
            return Ok(());
        }
        "export" => {
            let collection_id = if args.len() > 3 && args[2] == "--collection" {
                Some(args[3].clone())
            } else {
                None
            };
            let tasks = if let Some(col_id) = collection_id {
                let href = if col_id == "default" {
                    "local://default".to_string()
                } else {
                    format!("local://{}", col_id)
                };
                LocalStorage::load_for_href(ctx.as_ref(), &href)?
            } else {
                LocalStorage::load_for_href(ctx.as_ref(), cfait::storage::LOCAL_CALENDAR_HREF)?
            };
            println!("{}", LocalStorage::to_ics_string(&tasks));
            return Ok(());
        }
        "sync" => {
            let config = cfait::config::Config::load_with_credentials(ctx.as_ref()).unwrap_or_default();
            if config.url.is_empty() {
                println!("Offline mode configured; nothing to sync.");
                return Ok(());
            }
            println!("Syncing with {}...", config.url);
            match cfait::client::RustyClient::connect_with_fallback(
                ctx.clone(),
                config,
                Some("CLI-Sync"),
            )
            .await
            {
                Ok(_) => println!("Sync completed successfully."),
                Err(e) => {
                    eprintln!("Sync failed: {}", e);
                    std::process::exit(1);
                }
            }
            return Ok(());
        }
        "daemon" => {
            println!("Starting Cfait background daemon...");
            loop {
                let config = cfait::config::Config::load_with_credentials(ctx.as_ref()).unwrap_or_default();
                let interval = config.auto_refresh_interval_mins;
                if interval == 0 {
                    println!("Auto-refresh is disabled in config. Daemon exiting.");
                    return Ok(());
                }
                if config.url.is_empty() {
                    println!("Offline mode configured. Daemon sleeping...");
                } else {
                    #[cfg(not(target_os = "android"))]
                    match cfait::storage::DaemonLock::try_acquire_exclusive(ctx.as_ref()) {
                        Ok(Some(_lock)) => {
                            println!("Daemon: Syncing with {}...", config.url);
                            let _ = cfait::client::RustyClient::connect_with_fallback(
                                ctx.clone(),
                                config,
                                Some("CLI-Daemon"),
                            )
                            .await;
                        }
                        Ok(None) => {}
                        Err(e) => eprintln!("Daemon: Failed to check instance lock: {}", e),
                    }
                }
                tokio::time::sleep(tokio::time::Duration::from_secs(interval as u64 * 60)).await;
            }
        }
        "add" | "create" => {
            let input = args[2..].join(" ");
            if input.trim().is_empty() {
                eprintln!("Error: Task description cannot be empty.");
                std::process::exit(1);
            }

            let mut config = cfait::config::Config::load_with_credentials(ctx.as_ref()).unwrap_or_default();
            let def_time =
                chrono::NaiveTime::parse_from_str(&config.default_reminder_time, "%H:%M").ok();

            // Allow ad-hoc alias definition via CLI
            let (clean_input, new_aliases) = cfait::model::extract_inline_aliases(&input);
            if !new_aliases.is_empty() {
                for (k, v) in &new_aliases {
                    let _ = cfait::model::validate_alias_integrity(k, v, &config.tag_aliases);
                    config.tag_aliases.insert(k.clone(), v.clone());
                }
                let _ = config.save_with_credentials(ctx.as_ref());
            }

            let mut task = Task::new(&clean_input, &config.tag_aliases, def_time);

            let mut target_href = config
                .default_calendar
                .clone()
                .unwrap_or_else(|| cfait::storage::LOCAL_CALENDAR_HREF.to_string());

            let mut all_cals = Vec::new();
            if let Ok(locals) = cfait::storage::LocalCalendarRegistry::load(ctx.as_ref()) {
                all_cals.extend(locals);
            }
            if let Ok(remotes) = cfait::cache::Cache::load_calendars(ctx.as_ref()) {
                all_cals.extend(remotes);
            }

            let mut matched = false;
            if let Some(found) = all_cals.iter().find(|c| {
                c.name == target_href
                    || c.href == target_href
                    || c.href.ends_with(&format!("/{}/", target_href))
                    || c.href.ends_with(&format!("/{}", target_href))
            }) {
                target_href = found.href.clone();
                matched = true;
            }

            if !matched && !target_href.starts_with("local://") && !target_href.starts_with('/') {
                eprintln!(
                    "Warning: Calendar '{}' not found. Task will be saved to local recovery.",
                    target_href
                );
                target_href = "local://recovery".to_string();
            }

            task.calendar_href = target_href;

            let store = Arc::new(tokio::sync::Mutex::new(TaskStore::new(ctx.clone())));
            let client = Arc::new(tokio::sync::Mutex::new(None));
            let controller = cfait::controller::TaskController::new(store, client, ctx.clone());

            let uid = controller
                .create_task(task)
                .await
                .map_err(|e| anyhow::anyhow!(e))?;
            println!(
                "Task added successfully. (UID: {})",
                &uid[..std::cmp::min(8, uid.len())]
            );

            // Best-effort background sync of the journal
            if let Err(e) = maybe_sync(ctx.clone()).await {
                eprintln!("Warning: Background sync failed: {}", e);
            }
            return Ok(());
        }
        "list" | "search" => {
            // Parse arguments: support explicit --all which overrides hidden calendars and completed hiding
            let mut show_all = false;
            let mut query_parts: Vec<String> = Vec::new();
            // Iterate by reference so we don't move `args` (which is used later).
            for arg in args.iter().skip(2) {
                if arg == "--all" {
                    show_all = true;
                } else {
                    query_parts.push(arg.clone());
                }
            }
            let query = if command == "search" {
                query_parts.join(" ")
            } else {
                String::new()
            };

            let config = cfait::config::Config::load_with_credentials(ctx.as_ref()).unwrap_or_default();
            let store = build_store_cli(&ctx).await;

            let mut hidden: HashSet<String> = HashSet::new();
            let mut hide_completed = config.hide_completed;
            if !show_all {
                hidden.extend(config.hidden_calendars.into_iter());
                hidden.extend(config.disabled_calendars.into_iter());
            } else {
                hide_completed = false;
            }

            let cutoff_date = if show_all {
                None
            } else {
                config
                    .sort_cutoff_months
                    .map(|m| Utc::now() + chrono::Duration::days(m as i64 * 30))
            };

            // Local empty sets to satisfy FilterOptions references
            let selected_categories: HashSet<String> = HashSet::new();
            let selected_locations: HashSet<String> = HashSet::new();
            let expanded_done_groups: HashSet<String> = HashSet::new();

            let res = store.filter(FilterOptions {
                active_cal_href: None,
                hidden_calendars: &hidden,
                selected_categories: &selected_categories,
                selected_locations: &selected_locations,
                match_all_categories: false,
                search_term: &query,
                hide_completed_global: hide_completed,
                hide_fully_completed_tags: !show_all && config.hide_fully_completed_tags,
                cutoff_date,
                min_duration: None,
                max_duration: None,
                include_unset_duration: true,
                urgent_days: config.urgent_days_horizon,
                urgent_prio: config.urgent_priority_threshold,
                default_priority: config.default_priority,
                start_grace_period_days: config.start_grace_period_days,
                expanded_done_groups: &expanded_done_groups,
                collapsed_trees: &HashSet::new(),
                max_done_roots: usize::MAX,
                max_done_subtasks: usize::MAX,
                tag_aliases: &config.tag_aliases,
            });

            if res.items.is_empty() {
                println!("No tasks found.");
                return Ok(());
            }

            for item in res.items {
                if let cfait::store::TaskListItem::Task(t) = item {
                    let symbol = t.checkbox_symbol();
                    let indent = "  ".repeat(t.depth);
                    let smart = t.to_smart_string();
                    let summary_escaped = cfait::model::parser::escape_summary(&t.summary);
                    let metadata = smart.replacen(&summary_escaped, "", 1).trim().to_string();
                    let uid_short = &t.uid[..std::cmp::min(8, t.uid.len())];

                    let meta_str = if metadata.is_empty() {
                        String::new()
                    } else {
                        format!(" {}", metadata)
                    };
                    println!(
                        "{}{} {}{} [{}]",
                        indent, symbol, t.summary, meta_str, uid_short
                    );
                }
            }
            return Ok(());
        }
        "view" | "show" => {
            let store = build_store_cli(&ctx).await;
            let partial = args.get(2).map(|s| s.as_str()).unwrap_or("");
            let uid =
                resolve_uid(&store, partial).ok_or_else(|| anyhow::anyhow!("UID required"))?;
            let t = store.get_task_ref(&uid).ok_or_else(|| anyhow::anyhow!("Task not found"))?;

            println!("Summary:  {}", t.summary);
            println!("Status:   {:?} {}", t.status, t.checkbox_symbol());
            println!("UID:      {}", t.uid);
            if let Some(d) = &t.due {
                println!("Due:      {}", d.format_smart());
            }
            if !t.categories.is_empty() {
                println!("Tags:     {}", t.categories.join(", "));
            }
            if let Some(l) = &t.location {
                println!("Location: {}", l);
            }
            if !t.description.is_empty() {
                println!("\nDescription:\n{}", t.description);
            }
            return Ok(());
        }
        "start" | "pause" | "toggle" | "done" | "complete" => {
            let store = build_store_cli(&ctx).await;
            let partial_uid = args.get(2).cloned().unwrap_or_default();
            if partial_uid.is_empty() {
                eprintln!("Error: Missing UID.");
                std::process::exit(1);
            }
            let full_uid = match resolve_uid(&store, &partial_uid) {
                Some(uid) => uid,
                None => std::process::exit(1),
            };

            let config = cfait::config::Config::load_with_credentials(ctx.as_ref()).unwrap_or_default();
            let store_arc = Arc::new(tokio::sync::Mutex::new(store));
            let client_arc = Arc::new(tokio::sync::Mutex::new(None));
            let controller =
                cfait::controller::TaskController::new(store_arc, client_arc, ctx.clone());

            match command {
                "start" => {
                    let mut store_lock = controller.store.lock().await;
                    let intent = cfait::model::AppIntent::StartTask { uid: full_uid.clone() };
                    let actions = store_lock.apply_task_intent(&intent, &config);
                    drop(store_lock);
                    controller.persist_changes(actions).await.map_err(|e| anyhow::anyhow!(e))?;
                    println!("Task {} started.", partial_uid);
                }
                "pause" => {
                    let mut store_lock = controller.store.lock().await;
                    let intent = cfait::model::AppIntent::PauseTask { uid: full_uid.clone() };
                    let actions = store_lock.apply_task_intent(&intent, &config);
                    drop(store_lock);
                    controller.persist_changes(actions).await.map_err(|e| anyhow::anyhow!(e))?;
                    println!("Task {} paused.", partial_uid);
                }
                _ => {
                    let mut store_lock = controller.store.lock().await;
                    let intent = cfait::model::AppIntent::ToggleTask { uid: full_uid.clone() };
                    let actions = store_lock.apply_task_intent(&intent, &config);
                    drop(store_lock);
                    controller.persist_changes(actions).await.map_err(|e| anyhow::anyhow!(e))?;
                    println!("Task {} toggled.", partial_uid);
                }
            }

            // Best-effort background sync
            if let Err(e) = maybe_sync(ctx.clone()).await {
                eprintln!("Warning: Background sync failed: {}", e);
            }
            return Ok(());
        }
        "delete" | "rm" => {
            let partial_uid = args.get(2).cloned().unwrap_or_default();
            if partial_uid.is_empty() {
                eprintln!("Error: Missing UID.");
                std::process::exit(1);
            }
            let store = build_store_cli(&ctx).await;
            let full_uid = match resolve_uid(&store, &partial_uid) {
                Some(uid) => uid,
                None => std::process::exit(1),
            };

            let config = cfait::config::Config::load_with_credentials(ctx.as_ref()).unwrap_or_default();
            let store_arc = Arc::new(tokio::sync::Mutex::new(store));
            let client_arc = Arc::new(tokio::sync::Mutex::new(None));
            let controller =
                cfait::controller::TaskController::new(store_arc, client_arc, ctx.clone());

            let actions = {
                let mut store_lock = controller.store.lock().await;
                let intent = cfait::model::AppIntent::DeleteTask { uid: full_uid.clone() };
                store_lock.apply_task_intent(&intent, &config)
            };
            controller.persist_changes(actions).await.map_err(|e| anyhow::anyhow!(e))?;
            println!("Task {} deleted.", partial_uid);

            // Best-effort background sync
            if let Err(e) = maybe_sync(ctx.clone()).await {
                eprintln!("Warning: Background sync failed: {}", e);
            }
            return Ok(());
        }
        "" => {
            // No non-interactive command provided; fall through to start the interactive TUI.
        }
        _ => {
            eprintln!("Unknown command: {}", command);
            std::process::exit(1);
        }
    }

    // --- Start interactive TUI ---
    #[cfg(not(target_os = "android"))]
    let _ui_lock = cfait::storage::DaemonLock::acquire_shared(ctx.as_ref())
        .map_err(|e| eprintln!("Warning: Could not acquire shared UI lock: {}", e))
        .ok();

    cfait::tui::run(ctx).await
}