intent-engine 0.11.1

A command-line database service for tracking strategic intent, tasks, and events
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// Note: CurrentAction and EventCommands removed in v0.10.1 CLI simplification
// These functions are kept for potential Dashboard/MCP use but not exposed in CLI
// use crate::cli::{CurrentAction, EventCommands};
use crate::backend::{SearchBackend, TaskBackend};
use crate::cli_handlers::read_stdin;
use crate::cli_handlers::utils::{parse_status_keywords, parse_task_id_query};
use crate::error::{IntentError, Result};
use crate::events::EventManager;
use crate::project::ProjectContext;
use crate::report::ReportManager;
use crate::time_utils::parse_date_filter;
use crate::workspace::WorkspaceManager;
use std::path::PathBuf;

// Stub types for deprecated CLI commands (no longer in cli.rs)
#[allow(dead_code)]
pub enum CurrentAction {
    Set { task_id: i64 },
    Clear,
}

#[allow(dead_code)]
pub enum EventCommands {
    Add {
        task_id: Option<i64>,
        log_type: String,
        data_stdin: bool,
    },
    List {
        task_id: Option<i64>,
        log_type: Option<String>,
        since: Option<String>,
        limit: Option<i64>,
    },
}

pub async fn handle_current_command(
    set: Option<i64>,
    command: Option<CurrentAction>,
) -> Result<()> {
    let ctx = ProjectContext::load().await?;
    let workspace_mgr = WorkspaceManager::new(&ctx.pool);

    // Handle backward compatibility: --set flag takes precedence
    if let Some(task_id) = set {
        eprintln!("⚠️  Warning: 'ie current --set' is a low-level atomic command.");
        eprintln!(
            "   For normal use, prefer 'ie task start {}' which ensures data consistency.",
            task_id
        );
        eprintln!();
        let response = workspace_mgr.set_current_task(task_id, None).await?;
        println!("✓ Switched to task #{}", task_id);
        println!("{}", serde_json::to_string_pretty(&response)?);
        return Ok(());
    }

    // Handle subcommands
    match command {
        Some(CurrentAction::Set { task_id }) => {
            eprintln!("⚠️  Warning: 'ie current set' is a low-level atomic command.");
            eprintln!(
                "   For normal use, prefer 'ie task start {}' which ensures data consistency.",
                task_id
            );
            eprintln!();
            let response = workspace_mgr.set_current_task(task_id, None).await?;
            println!("✓ Switched to task #{}", task_id);
            println!("{}", serde_json::to_string_pretty(&response)?);
        },
        Some(CurrentAction::Clear) => {
            eprintln!("⚠️  Warning: 'ie current clear' is a low-level atomic command.");
            eprintln!("   For normal use, prefer 'ie task done' or 'ie task switch' which ensures data consistency.");
            eprintln!();
            workspace_mgr.clear_current_task(None).await?;
            println!("✓ Current task cleared");
        },
        None => {
            // Default: display current task in JSON format
            let response = workspace_mgr.get_current_task(None).await?;
            println!("{}", serde_json::to_string_pretty(&response)?);
        },
    }

    Ok(())
}

pub async fn handle_report_command(
    since: Option<String>,
    status: Option<String>,
    filter_name: Option<String>,
    filter_spec: Option<String>,
    summary_only: bool,
) -> Result<()> {
    let ctx = ProjectContext::load().await?;
    let report_mgr = ReportManager::new(&ctx.pool);

    let report = report_mgr
        .generate_report(since, status, filter_name, filter_spec, summary_only)
        .await?;
    println!("{}", serde_json::to_string_pretty(&report)?);

    Ok(())
}

pub async fn handle_event_command(cmd: EventCommands) -> Result<()> {
    match cmd {
        EventCommands::Add {
            task_id,
            log_type,
            data_stdin,
        } => {
            let ctx = ProjectContext::load_or_init().await?;
            let project_path = ctx.root.to_string_lossy().to_string();
            let event_mgr = EventManager::with_project_path(&ctx.pool, project_path);

            let data = if data_stdin {
                read_stdin()?
            } else {
                return Err(IntentError::InvalidInput(
                    "--data-stdin is required".to_string(),
                ));
            };

            // Determine the target task ID
            let target_task_id = if let Some(id) = task_id {
                // Use the provided task_id
                id
            } else {
                // Fall back to current_task_id from sessions table for this session
                let session_id = crate::workspace::resolve_session_id(None);
                let current_task_id: Option<i64> = sqlx::query_scalar::<_, Option<i64>>(
                    "SELECT current_task_id FROM sessions WHERE session_id = ?",
                )
                .bind(&session_id)
                .fetch_optional(&ctx.pool)
                .await?
                .flatten();

                current_task_id
                    .ok_or_else(|| IntentError::InvalidInput(
                        "No current task is set and --task-id was not provided. Use 'current --set <ID>' to set a task first.".to_string(),
                    ))?
            };

            let event = event_mgr.add_event(target_task_id, log_type, data).await?;
            println!("{}", serde_json::to_string_pretty(&event)?);
        },

        EventCommands::List {
            task_id,
            limit,
            log_type,
            since,
        } => {
            let ctx = ProjectContext::load().await?;
            let event_mgr = EventManager::new(&ctx.pool);

            let events = event_mgr
                .list_events(task_id, limit, log_type, since)
                .await?;
            println!("{}", serde_json::to_string_pretty(&events)?);
        },
    }

    Ok(())
}

/// Safely truncate a UTF-8 string to a maximum number of characters
/// Returns the truncated string with "..." appended if truncation occurred
fn truncate_str(s: &str, max_chars: usize) -> String {
    let char_count = s.chars().count();
    if char_count <= max_chars {
        s.to_string()
    } else {
        let truncated: String = s.chars().take(max_chars - 3).collect();
        format!("{}...", truncated)
    }
}

#[allow(clippy::too_many_arguments)]
pub async fn handle_search_command(
    task_mgr: &(impl TaskBackend + SearchBackend),
    query: &str,
    include_tasks: bool,
    include_events: bool,
    limit: Option<i64>,
    offset: Option<i64>,
    since: Option<String>,
    until: Option<String>,
    format: &str,
) -> Result<()> {
    use chrono::{DateTime, Utc};

    // Parse date filters
    let since_dt: Option<DateTime<Utc>> = if let Some(ref s) = since {
        Some(parse_date_filter(s).map_err(IntentError::InvalidInput)?)
    } else {
        None
    };

    let until_dt: Option<DateTime<Utc>> = if let Some(ref u) = until {
        Some(parse_date_filter(u).map_err(IntentError::InvalidInput)?)
    } else {
        None
    };

    // Check if query is a #ID format (e.g., "#123", "#1")
    if let Some(task_id) = parse_task_id_query(query) {
        match task_mgr.get_task(task_id).await {
            Ok(task) => {
                if format == "json" {
                    println!("{}", serde_json::to_string_pretty(&task)?);
                } else {
                    let status_icon = match task.status.as_str() {
                        "todo" => "",
                        "doing" => "",
                        "done" => "",
                        _ => "?",
                    };
                    let parent_info = task
                        .parent_id
                        .map(|p| format!(" (parent: #{})", p))
                        .unwrap_or_default();
                    let priority_info = task
                        .priority
                        .map(|p| format!(" [P{}]", p))
                        .unwrap_or_default();
                    println!("Task #{}", task.id);
                    println!(
                        "  {} {}{}{}",
                        status_icon, task.name, parent_info, priority_info
                    );
                    if let Some(spec) = &task.spec {
                        if !spec.is_empty() {
                            println!("  Spec: {}", spec);
                        }
                    }
                    println!("  Owner: {}", task.owner);
                    if let Some(ts) = task.first_todo_at {
                        print!("  todo: {} ", ts.format("%Y-%m-%d %H:%M:%S"));
                    }
                    if let Some(ts) = task.first_doing_at {
                        print!("doing: {} ", ts.format("%Y-%m-%d %H:%M:%S"));
                    }
                    if let Some(ts) = task.first_done_at {
                        print!("done: {}", ts.format("%Y-%m-%d %H:%M:%S"));
                    }
                    if task.first_todo_at.is_some()
                        || task.first_doing_at.is_some()
                        || task.first_done_at.is_some()
                    {
                        println!();
                    }
                }
                return Ok(());
            },
            Err(_) => {
                // Task not found, fall through to FTS5 search
                // (user might be searching for "#123" as text)
            },
        }
    }

    // Check if query is a status keyword combination
    if let Some(statuses) = parse_status_keywords(query) {
        // Collect tasks for each status
        // When date filters are used, fetch more tasks initially
        // (we'll apply limit after filtering)
        const DATE_FILTER_FETCH_LIMIT: i64 = 10_000;
        let fetch_limit = if since_dt.is_some() || until_dt.is_some() {
            Some(DATE_FILTER_FETCH_LIMIT)
        } else {
            limit
        };

        let mut all_tasks = Vec::new();
        for status in &statuses {
            let result = task_mgr
                .find_tasks(Some(status.clone()), None, None, fetch_limit, offset)
                .await?;
            all_tasks.extend(result.tasks);
        }

        // Apply date filters based on status
        if since_dt.is_some() || until_dt.is_some() {
            all_tasks.retain(|task| {
                // Determine which timestamp to use based on task status
                let timestamp = match task.status.as_str() {
                    "done" => task.first_done_at,
                    "doing" => task.first_doing_at,
                    _ => task.first_todo_at, // todo or unknown
                };

                // If no timestamp, exclude from date-filtered results
                let Some(ts) = timestamp else {
                    return false;
                };

                // Check since filter
                if let Some(ref since) = since_dt {
                    if ts < *since {
                        return false;
                    }
                }

                // Check until filter
                if let Some(ref until) = until_dt {
                    if ts > *until {
                        return false;
                    }
                }

                true
            });
        }

        // Sort by priority, then by id
        all_tasks.sort_by(|a, b| {
            let pri_a = a.priority.unwrap_or(999);
            let pri_b = b.priority.unwrap_or(999);
            pri_a.cmp(&pri_b).then_with(|| a.id.cmp(&b.id))
        });

        // Apply limit if specified
        let limit = limit.unwrap_or(100) as usize;
        if all_tasks.len() > limit {
            all_tasks.truncate(limit);
        }

        if format == "json" {
            println!("{}", serde_json::to_string_pretty(&all_tasks)?);
        } else {
            // Text format: status filter results
            let status_str = statuses.join(", ");
            let date_filter_str = match (&since, &until) {
                (Some(s), Some(u)) => format!(" (from {} to {})", s, u),
                (Some(s), None) => format!(" (since {})", s),
                (None, Some(u)) => format!(" (until {})", u),
                (None, None) => String::new(),
            };
            println!(
                "Tasks with status [{}]{}: {} found",
                status_str,
                date_filter_str,
                all_tasks.len()
            );
            println!();
            for task in &all_tasks {
                let status_icon = match task.status.as_str() {
                    "todo" => "",
                    "doing" => "",
                    "done" => "",
                    _ => "?",
                };
                let parent_info = task
                    .parent_id
                    .map(|p| format!(" (parent: #{})", p))
                    .unwrap_or_default();
                let priority_info = task
                    .priority
                    .map(|p| format!(" [P{}]", p))
                    .unwrap_or_default();
                println!(
                    "  {} #{} {}{}{}",
                    status_icon, task.id, task.name, parent_info, priority_info
                );
                if let Some(spec) = &task.spec {
                    if !spec.is_empty() {
                        println!("      Spec: {}", truncate_str(spec, 60));
                    }
                }
                println!("      Owner: {}", task.owner);
                if let Some(ts) = task.first_todo_at {
                    print!("      todo: {} ", ts.format("%m-%d %H:%M:%S"));
                }
                if let Some(ts) = task.first_doing_at {
                    print!("doing: {} ", ts.format("%m-%d %H:%M:%S"));
                }
                if let Some(ts) = task.first_done_at {
                    print!("done: {}", ts.format("%m-%d %H:%M:%S"));
                }
                if task.first_todo_at.is_some()
                    || task.first_doing_at.is_some()
                    || task.first_done_at.is_some()
                {
                    println!();
                }
            }
        }
        return Ok(());
    }

    // Regular FTS5 search
    if since_dt.is_some() || until_dt.is_some() {
        eprintln!("Warning: --since/--until are ignored for fulltext search (only apply to status keyword queries)");
    }
    let results = task_mgr
        .search(
            query.to_string(),
            include_tasks,
            include_events,
            limit,
            offset,
        )
        .await?;

    if format == "json" {
        println!("{}", serde_json::to_string_pretty(&results)?);
    } else {
        use crate::db::models::SearchResult;

        // Text format: FTS5 search results
        println!(
            "Search: \"{}\"{} tasks, {} events (limit: {}, offset: {})",
            query, results.total_tasks, results.total_events, results.limit, results.offset
        );
        println!();

        for result in &results.results {
            match result {
                SearchResult::Task {
                    task,
                    match_field,
                    match_snippet,
                } => {
                    let status_icon = match task.status.as_str() {
                        "todo" => "",
                        "doing" => "",
                        "done" => "",
                        _ => "?",
                    };
                    let parent_info = task
                        .parent_id
                        .map(|p| format!(" (parent: #{})", p))
                        .unwrap_or_default();
                    let priority_info = task
                        .priority
                        .map(|p| format!(" [P{}]", p))
                        .unwrap_or_default();
                    println!(
                        "  {} #{} {} [match: {}]{}{}",
                        status_icon, task.id, task.name, match_field, parent_info, priority_info
                    );
                    if let Some(spec) = &task.spec {
                        if !spec.is_empty() {
                            println!("      Spec: {}", truncate_str(spec, 60));
                        }
                    }
                    if !match_snippet.is_empty() {
                        println!("      Snippet: {}", match_snippet);
                    }
                    println!("      Owner: {}", task.owner);
                    if let Some(ts) = task.first_todo_at {
                        print!("      todo: {} ", ts.format("%m-%d %H:%M:%S"));
                    }
                    if let Some(ts) = task.first_doing_at {
                        print!("doing: {} ", ts.format("%m-%d %H:%M:%S"));
                    }
                    if let Some(ts) = task.first_done_at {
                        print!("done: {}", ts.format("%m-%d %H:%M:%S"));
                    }
                    if task.first_todo_at.is_some()
                        || task.first_doing_at.is_some()
                        || task.first_done_at.is_some()
                    {
                        println!();
                    }
                },
                SearchResult::Event {
                    event,
                    task_chain,
                    match_snippet,
                } => {
                    let icon = match event.log_type.as_str() {
                        "decision" => "💡",
                        "blocker" => "🚫",
                        "milestone" => "🎯",
                        _ => "📝",
                    };
                    println!(
                        "  {} #{} [{}] (task #{}) {}",
                        icon,
                        event.id,
                        event.log_type,
                        event.task_id,
                        event.timestamp.format("%Y-%m-%d %H:%M:%S")
                    );
                    println!("      Message: {}", event.discussion_data);
                    if !match_snippet.is_empty() {
                        println!("      Snippet: {}", match_snippet);
                    }
                    if !task_chain.is_empty() {
                        let chain_str: Vec<String> = task_chain
                            .iter()
                            .map(|t| format!("#{} {}", t.id, t.name))
                            .collect();
                        println!("      Task chain: {}", chain_str.join(""));
                    }
                },
            }
        }

        if results.has_more {
            println!();
            println!(
                "  ... more results available (use --offset {})",
                results.offset + results.limit
            );
        }
    }
    Ok(())
}

pub async fn handle_doctor_command() -> Result<()> {
    use crate::cli_handlers::dashboard::{check_dashboard_health, DASHBOARD_PORT};

    // Get database path info
    let db_path_info = ProjectContext::get_database_path_info();

    // Print database location
    println!("Database:");
    if let Some(db_path) = &db_path_info.final_database_path {
        println!("  {}", db_path);
    } else {
        println!("  Not found");
    }
    println!();

    // Print ancestor directories with databases
    let dirs_with_db: Vec<&String> = db_path_info
        .directories_checked
        .iter()
        .filter(|d| d.has_intent_engine)
        .map(|d| &d.path)
        .collect();

    if !dirs_with_db.is_empty() {
        println!("Ancestor directories with databases:");
        for dir in dirs_with_db {
            println!("  {}", dir);
        }
    } else {
        println!("Ancestor directories with databases: None");
    }
    println!();

    // Check dashboard status
    print!("Dashboard: ");
    let dashboard_health = check_dashboard_health(DASHBOARD_PORT).await;
    if dashboard_health {
        println!("Running (http://127.0.0.1:{})", DASHBOARD_PORT);
    } else {
        println!("Not running (start with 'ie dashboard start')");
    }

    Ok(())
}

pub async fn handle_init_command(at: Option<String>, force: bool) -> Result<()> {
    use serde_json::json;

    // Determine target directory
    let target_dir = if let Some(path) = &at {
        let p = PathBuf::from(path);
        if !p.exists() {
            return Err(IntentError::InvalidInput(format!(
                "Directory does not exist: {}",
                path
            )));
        }
        if !p.is_dir() {
            return Err(IntentError::InvalidInput(format!(
                "Path is not a directory: {}",
                path
            )));
        }
        p
    } else {
        // Use current working directory
        std::env::current_dir().expect("Failed to get current directory")
    };

    let intent_dir = target_dir.join(".intent-engine");

    // Check if already exists
    if intent_dir.exists() && !force {
        let error_msg = format!(
            ".intent-engine already exists at {}\nUse --force to re-initialize",
            intent_dir.display()
        );
        return Err(IntentError::InvalidInput(error_msg));
    }

    // Perform initialization
    let ctx = ProjectContext::initialize_project_at(target_dir).await?;

    // Success output
    let result = json!({
        "success": true,
        "root": ctx.root.display().to_string(),
        "database_path": ctx.db_path.display().to_string(),
        "message": "Intent-Engine initialized successfully"
    });

    println!("{}", serde_json::to_string_pretty(&result)?);
    Ok(())
}

pub async fn handle_session_restore(
    include_events: usize,
    workspace: Option<String>,
) -> Result<()> {
    use crate::session_restore::SessionRestoreManager;

    // If workspace path is specified, change to that directory
    if let Some(ws_path) = workspace {
        std::env::set_current_dir(&ws_path)?;
    }

    // Try to load project context
    let ctx = match ProjectContext::load().await {
        Ok(ctx) => ctx,
        Err(_) => {
            // Workspace not found
            let result = crate::session_restore::SessionRestoreResult {
                status: crate::session_restore::SessionStatus::Error,
                workspace_path: std::env::current_dir()
                    .ok()
                    .and_then(|p| p.to_str().map(String::from)),
                current_task: None,
                parent_task: None,
                siblings: None,
                children: None,
                recent_events: None,
                suggested_commands: Some(vec![
                    "ie workspace init".to_string(),
                    "ie help".to_string(),
                ]),
                stats: None,
                recommended_task: None,
                top_pending_tasks: None,
                error_type: Some(crate::session_restore::ErrorType::WorkspaceNotFound),
                message: Some("No Intent-Engine workspace found in current directory".to_string()),
                recovery_suggestion: Some(
                    "Run 'ie workspace init' to create a new workspace".to_string(),
                ),
            };
            println!("{}", serde_json::to_string_pretty(&result)?);
            return Ok(());
        },
    };

    let restore_mgr = SessionRestoreManager::new(&ctx.pool);
    let result = restore_mgr.restore(include_events).await?;

    println!("{}", serde_json::to_string_pretty(&result)?);

    Ok(())
}

pub fn handle_logs_command(
    mode: Option<String>,
    level: Option<String>,
    since: Option<String>,
    until: Option<String>,
    limit: Option<usize>,
    follow: bool,
    export: String,
) -> Result<()> {
    use crate::logs::{
        follow_logs, format_entry_json, format_entry_text, parse_duration, query_logs, LogQuery,
    };

    // Build query
    let mut query = LogQuery {
        mode,
        level,
        limit,
        ..Default::default()
    };

    if let Some(since_str) = since {
        query.since = parse_duration(&since_str);
        if query.since.is_none() {
            return Err(IntentError::InvalidInput(format!(
                "Invalid duration format: {}. Use format like '1h', '24h', '7d'",
                since_str
            )));
        }
    }

    if let Some(until_str) = until {
        use chrono::DateTime;
        match DateTime::parse_from_rfc3339(&until_str) {
            Ok(dt) => query.until = Some(dt.with_timezone(&chrono::Utc)),
            Err(e) => {
                return Err(IntentError::InvalidInput(format!(
                    "Invalid timestamp format: {}. Error: {}",
                    until_str, e
                )))
            },
        }
    }

    // Handle follow mode
    if follow {
        return follow_logs(&query).map_err(IntentError::IoError);
    }

    // Query logs
    let entries = query_logs(&query).map_err(IntentError::IoError)?;

    if entries.is_empty() {
        eprintln!("No log entries found matching the criteria");
        return Ok(());
    }

    // Display results
    match export.as_str() {
        "json" => {
            println!("[");
            for (i, entry) in entries.iter().enumerate() {
                print!("  {}", format_entry_json(entry));
                if i < entries.len() - 1 {
                    println!(",");
                } else {
                    println!();
                }
            }
            println!("]");
        },
        _ => {
            for entry in entries {
                println!("{}", format_entry_text(&entry));
            }
        },
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    // ============================================================================
    // truncate_str tests (UTF-8 safe truncation)
    // ============================================================================

    #[test]
    fn test_truncate_str_ascii() {
        // Short string - no truncation
        assert_eq!(truncate_str("hello", 10), "hello");

        // Exact length - no truncation
        assert_eq!(truncate_str("hello", 5), "hello");

        // Needs truncation
        assert_eq!(truncate_str("hello world", 8), "hello...");
    }

    #[test]
    fn test_truncate_str_chinese() {
        // Short Chinese string - no truncation
        assert_eq!(truncate_str("你好", 10), "你好");

        // Chinese string needs truncation
        // "根据覆盖缺口分析补充" = 10 chars, truncate to 8 means keep 5 + "..."
        let chinese = "根据覆盖缺口分析补充";
        let result = truncate_str(chinese, 8);
        assert_eq!(result, "根据覆盖缺...");
        assert!(!result.contains('\u{FFFD}')); // No replacement chars
    }

    #[test]
    fn test_truncate_str_mixed() {
        // Mixed ASCII and Chinese
        let mixed = "Task: 实现用户认证功能";
        let result = truncate_str(mixed, 12);
        assert_eq!(result, "Task: 实现用...");
    }

    #[test]
    fn test_truncate_str_edge_cases() {
        // Empty string
        assert_eq!(truncate_str("", 10), "");

        // Max chars less than 3 (edge case for "...")
        assert_eq!(truncate_str("hello", 3), "...");

        // Single char with truncation
        assert_eq!(truncate_str("hello", 4), "h...");
    }

    #[test]
    fn test_truncate_str_emoji() {
        // Emoji (multi-byte UTF-8)
        let emoji = "🚀🎉🔥💡";
        let result = truncate_str(emoji, 4);
        assert_eq!(result, "🚀🎉🔥💡"); // No truncation needed

        let result = truncate_str(emoji, 3);
        assert_eq!(result, "..."); // All replaced by ...
    }
}