helix-kanban 0.2.25

A terminal-based kanban board with file-based storage, multi-project support, Helix-style keybindings, and built-in MCP server for AI integration
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use crate::fs;
use crate::models::{ProjectType, Task};
use anyhow::Result;
use std::env;
use std::path::PathBuf;

/// 处理 CLI 命令
/// 返回 true 表示应该继续进入 TUI,false 表示已处理完毕应该退出
pub fn handle_cli() -> Result<bool> {
    // 确保全局配置文件存在
    let _ = fs::ensure_global_ai_config();
    let _ = fs::ensure_global_claude_md();

    let args: Vec<String> = env::args().collect();

    // 如果没有参数,进入 TUI 模式
    if args.len() < 2 {
        return Ok(true);
    }

    // 处理 CLI 命令
    match args[1].as_str() {
        // MCP server
        "mcp" => {
            if let Err(e) = crate::mcp::start_mcp_server() {
                eprintln!("MCP server error: {}", e);
                std::process::exit(1);
            }
            Ok(false)
        }
        // 新的结构化命令
        "project" => {
            if let Err(e) = handle_project_command(&args[1..]) {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
            Ok(false)
        }
        "task" => {
            if let Err(e) = handle_task_command(&args[1..]) {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
            Ok(false)
        }
        "status" => {
            if let Err(e) = handle_status_command(&args[1..]) {
                eprintln!("Error: {}", e);
                std::process::exit(1);
            }
            Ok(false)
        }
        // 向后兼容的旧命令
        "create" => {
            if args.len() < 3 {
                eprintln!("用法: hxk create <project-name>");
                std::process::exit(1);
            }
            cli_create(&args[2])?;
            Ok(false)
        }
        "list" => {
            cli_list()?;
            Ok(false)
        }
        "add" => {
            if args.len() < 3 {
                eprintln!("用法: hxk add <task-title>");
                std::process::exit(1);
            }
            cli_add(&args[2..])?;
            Ok(false)
        }
        "config" => {
            if args.len() < 3 {
                eprintln!("用法: hxk config <show|editor|viewer>");
                eprintln!("示例:");
                eprintln!("  hxk config show");
                eprintln!("  hxk config editor nvim");
                eprintln!("  hxk config viewer glow");
                std::process::exit(1);
            }
            match args[2].as_str() {
                "show" => crate::config::show_config()?,
                "editor" => {
                    if args.len() < 4 {
                        eprintln!("用法: hxk config editor <命令>");
                        std::process::exit(1);
                    }
                    crate::config::set_editor(args[3..].join(" "))?;
                }
                "viewer" => {
                    if args.len() < 4 {
                        eprintln!("用法: hxk config viewer <命令>");
                        std::process::exit(1);
                    }
                    crate::config::set_viewer(args[3..].join(" "))?;
                }
                _ => {
                    eprintln!("未知的配置选项: {}", args[2]);
                    eprintln!("可用选项: show, editor, viewer");
                    std::process::exit(1);
                }
            }
            Ok(false)
        }
        "--help" | "-h" => {
            print_help();
            Ok(false)
        }
        "--version" | "-V" | "-v" => {
            print_version();
            Ok(false)
        }
        _ => {
            eprintln!("未知命令: {}", args[1]);
            eprintln!("使用 'hxk --help' 查看帮助");
            std::process::exit(1);
        }
    }
}

// ============================================================================
// Project Commands
// ============================================================================

fn handle_project_command(args: &[String]) -> Result<(), String> {
    if args.len() < 2 {
        print_project_usage();
        return Ok(());
    }

    match args[1].as_str() {
        "list" => project_list(),
        "info" => {
            if args.len() < 3 {
                return Err("Missing project name\nUsage: hxk project info <name>".to_string());
            }
            project_info(&args[2])
        }
        "create" => {
            if args.len() < 3 {
                return Err(
                    "Missing project name\nUsage: hxk project create <name> [--local]".to_string(),
                );
            }
            let is_local = args.get(3).map(|s| s.as_str()) == Some("--local");
            project_create(&args[2], is_local)
        }
        "help" | "--help" | "-h" => {
            print_project_usage();
            Ok(())
        }
        cmd => Err(format!(
            "Unknown project command: {}\nRun 'hxk project help' for usage",
            cmd
        )),
    }
}

fn print_project_usage() {
    println!(
        "Kanban Project Commands

USAGE:
    hxk project <SUBCOMMAND>

SUBCOMMANDS:
    list              List all projects
    info <name>       Show project information
    create <name>     Create a new global project
    create <name> --local  Create a new local project

EXAMPLES:
    hxk project list
    hxk project info myproject
    hxk project create newproject
    hxk project create localproject --local"
    );
}

fn project_list() -> Result<(), String> {
    let projects = fs::load_all_projects().map_err(|e| e.to_string())?;

    if projects.is_empty() {
        println!("No projects found.");
        return Ok(());
    }

    println!("TYPE  NAME                             PATH");
    println!("----  ------------------------------  ------------------------------------");

    for project in projects {
        let type_marker = match project.project_type {
            ProjectType::Global => "[G]",
            ProjectType::Local => "[L]",
        };

        let path = project.path.to_string_lossy();
        println!("{:<4}  {:<30}  {}", type_marker, project.name, path);
    }

    Ok(())
}

fn project_info(name: &str) -> Result<(), String> {
    let projects = fs::load_all_projects().map_err(|e| e.to_string())?;
    let project = projects
        .iter()
        .find(|p| p.name == name)
        .ok_or_else(|| format!("Project '{}' not found", name))?;

    println!("Project: {}", project.name);
    println!(
        "Type: {}",
        match project.project_type {
            ProjectType::Global => "Global (~/.kanban/projects)",
            ProjectType::Local => "Local (.kanban)",
        }
    );
    println!("Path: {}", project.path.to_string_lossy());
    println!("\nStatuses:");
    println!("  NAME                DISPLAY");
    println!("  ------------------  --------------------");

    for status in &project.statuses {
        println!("  {:<18}  {}", status.name, status.display);
    }

    println!("\nTasks: {}", project.tasks.len());

    Ok(())
}

fn project_create(name: &str, is_local: bool) -> Result<(), String> {
    let path = if is_local {
        fs::create_local_project(name)?
    } else {
        fs::create_project(name)?
    };

    println!(
        "Created {} project: {}",
        if is_local { "local" } else { "global" },
        path.to_string_lossy()
    );

    Ok(())
}

// ============================================================================
// Task Commands
// ============================================================================

fn handle_task_command(args: &[String]) -> Result<(), String> {
    if args.len() < 2 {
        print_task_usage();
        return Ok(());
    }

    match args[1].as_str() {
        "list" => {
            if args.len() < 3 {
                return Err(
                    "Missing project name\nUsage: hxk task list <project> [--status <status>]"
                        .to_string(),
                );
            }
            let status = parse_flag(&args[3..], "--status");
            task_list(&args[2], status)
        }
        "show" => {
            if args.len() < 4 {
                return Err(
                    "Missing arguments\nUsage: hxk task show <project> <task-id>".to_string(),
                );
            }
            let task_id: u32 = args[3]
                .parse()
                .map_err(|_| "Invalid task ID (must be a number)".to_string())?;
            task_show(&args[2], task_id)
        }
        "create" => {
            if args.len() < 3 {
                return Err("Missing project name\nUsage: hxk task create <project> --status <status> --title <title> [--content <content>]".to_string());
            }
            let status =
                parse_flag(&args[3..], "--status").ok_or("Missing --status flag".to_string())?;
            let title =
                parse_flag(&args[3..], "--title").ok_or("Missing --title flag".to_string())?;
            let content = parse_flag(&args[3..], "--content");
            task_create(&args[2], &status, &title, content)
        }
        "update" => {
            if args.len() < 4 {
                return Err("Missing arguments\nUsage: hxk task update <project> <task-id> [--title <title>] [--content <content>] [--priority <priority>]".to_string());
            }
            let task_id: u32 = args[3]
                .parse()
                .map_err(|_| "Invalid task ID (must be a number)".to_string())?;
            let title = parse_flag(&args[4..], "--title");
            let content = parse_flag(&args[4..], "--content");
            let priority = parse_flag(&args[4..], "--priority");
            task_update(&args[2], task_id, title, content, priority)
        }
        "move" => {
            if args.len() < 4 {
                return Err(
                    "Missing arguments\nUsage: hxk task move <project> <task-id> --to <status>"
                        .to_string(),
                );
            }
            let task_id: u32 = args[3]
                .parse()
                .map_err(|_| "Invalid task ID (must be a number)".to_string())?;
            let to_status =
                parse_flag(&args[4..], "--to").ok_or("Missing --to flag".to_string())?;
            task_move(&args[2], task_id, &to_status)
        }
        "delete" => {
            if args.len() < 4 {
                return Err(
                    "Missing arguments\nUsage: hxk task delete <project> <task-id>".to_string(),
                );
            }
            let task_id: u32 = args[3]
                .parse()
                .map_err(|_| "Invalid task ID (must be a number)".to_string())?;
            task_delete(&args[2], task_id)
        }
        "help" | "--help" | "-h" => {
            print_task_usage();
            Ok(())
        }
        cmd => Err(format!(
            "Unknown task command: {}\nRun 'hxk task help' for usage",
            cmd
        )),
    }
}

fn print_task_usage() {
    println!(
        "Kanban Task Commands

USAGE:
    hxk task <SUBCOMMAND>

SUBCOMMANDS:
    list <project> [--status <status>]
        List tasks in a project

    show <project> <task-id>
        Show task details

    create <project> --status <status> --title <title> [--content <content>]
        Create a new task

    update <project> <task-id> [--title <title>] [--content <content>] [--priority <priority>]
        Update task properties

    move <project> <task-id> --to <status>
        Move task to a different status

    delete <project> <task-id>
        Delete a task

EXAMPLES:
    hxk task list myproject
    hxk task list myproject --status todo
    hxk task show myproject 42
    hxk task create myproject --status todo --title \"新任务\"
    hxk task update myproject 42 --priority high
    hxk task move myproject 42 --to doing
    hxk task delete myproject 42"
    );
}

fn parse_flag(args: &[String], flag: &str) -> Option<String> {
    args.iter()
        .position(|s| s == flag)
        .and_then(|i| args.get(i + 1)).cloned()
}

fn find_project_path(project_name: &str) -> Result<PathBuf, String> {
    let projects = fs::load_all_projects().map_err(|e| e.to_string())?;
    projects
        .iter()
        .find(|p| p.name == project_name)
        .map(|p| p.path.clone())
        .ok_or_else(|| format!("Project '{}' not found", project_name))
}

fn task_list(project_name: &str, filter_status: Option<String>) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;
    let project = fs::load_project(&project_path)?;

    let tasks: Vec<&Task> = if let Some(status) = filter_status {
        project
            .tasks
            .iter()
            .filter(|t| t.status == status)
            .collect()
    } else {
        project.tasks.iter().collect()
    };

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

    println!("ID    ORDER  TITLE                                PRIORITY  STATUS    TAGS");
    println!("----  -----  -----------------------------------  --------  --------  --------");

    for task in tasks {
        let priority = task.priority.as_deref().unwrap_or("-");
        let tags = task.tags.join(", ");
        let tags_display = if tags.is_empty() {
            "-".to_string()
        } else {
            tags
        };

        println!(
            "{:<4}  {:<5}  {:<35}  {:<8}  {:<8}  {}",
            task.id,
            task.order,
            truncate(&task.title, 35),
            priority,
            truncate(&task.status, 8),
            truncate(&tags_display, 20)
        );
    }

    Ok(())
}

fn task_show(project_name: &str, task_id: u32) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;
    let project = fs::load_project(&project_path)?;

    let task = project
        .tasks
        .iter()
        .find(|t| t.id == task_id)
        .ok_or_else(|| format!("Task {} not found", task_id))?;

    println!("Task #{}", task.id);
    println!("Title: {}", task.title);
    println!("Status: {}", task.status);
    println!("Order: {}", task.order);
    println!("Priority: {}", task.priority.as_deref().unwrap_or("-"));
    println!(
        "Tags: {}",
        if task.tags.is_empty() {
            "-".to_string()
        } else {
            task.tags.join(", ")
        }
    );
    println!("Created: {}", task.created);
    println!("File: {}", task.file_path.to_string_lossy());
    println!("\nContent:");
    println!("{}", task.content);

    Ok(())
}

fn task_create(
    project_name: &str,
    status: &str,
    title: &str,
    content: Option<String>,
) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;

    // Get next task ID
    let next_id = fs::get_next_task_id(&project_path)?;

    // Get max order for the status
    let max_order = fs::get_max_order_in_status(&project_path, status)?;
    let new_order = max_order + 1000;

    // Create task
    let mut task = Task::new(next_id, title.to_string(), status.to_string());
    task.order = new_order;
    task.content = content.unwrap_or_default();

    // Save task
    let file_path = fs::save_task(&project_path, &task)?;

    println!("Created task #{} in status '{}'", task.id, status);
    println!("File: {}", file_path.to_string_lossy());

    Ok(())
}

fn task_update(
    project_name: &str,
    task_id: u32,
    title: Option<String>,
    content: Option<String>,
    priority: Option<String>,
) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;
    let mut project = fs::load_project(&project_path)?;

    let task = project
        .tasks
        .iter_mut()
        .find(|t| t.id == task_id)
        .ok_or_else(|| format!("Task {} not found", task_id))?;

    let mut updated = false;

    if let Some(new_title) = title {
        task.title = new_title;
        updated = true;
    }

    if let Some(new_content) = content {
        task.content = new_content;
        updated = true;
    }

    if let Some(new_priority) = priority {
        task.priority = if new_priority == "none" || new_priority.is_empty() {
            None
        } else {
            Some(new_priority)
        };
        updated = true;
    }

    if updated {
        fs::save_task(&project_path, task)?;
        println!("Updated task #{}", task_id);
    } else {
        println!("No changes made to task #{}", task_id);
    }

    Ok(())
}

fn task_move(project_name: &str, task_id: u32, new_status: &str) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;
    let mut project = fs::load_project(&project_path)?;

    let task = project
        .tasks
        .iter_mut()
        .find(|t| t.id == task_id)
        .ok_or_else(|| format!("Task {} not found", task_id))?;

    let old_status = task.status.clone();
    task.status = new_status.to_string();

    // Move file and update task
    let new_path = fs::move_task(&project_path, task, new_status)?;
    task.file_path = new_path;

    println!(
        "Moved task #{} from '{}' to '{}'",
        task_id, old_status, new_status
    );

    Ok(())
}

fn task_delete(project_name: &str, task_id: u32) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;
    let project = fs::load_project(&project_path)?;

    let task = project
        .tasks
        .iter()
        .find(|t| t.id == task_id)
        .ok_or_else(|| format!("Task {} not found", task_id))?;

    fs::delete_task(&project_path, task)?;

    println!("Deleted task #{}", task_id);

    Ok(())
}

fn truncate(s: &str, max_len: usize) -> String {
    if s.chars().count() <= max_len {
        s.to_string()
    } else {
        format!("{}...", s.chars().take(max_len - 3).collect::<String>())
    }
}

// ============================================================================
// Status Commands
// ============================================================================

fn handle_status_command(args: &[String]) -> Result<(), String> {
    if args.len() < 2 {
        print_status_usage();
        return Ok(());
    }

    match args[1].as_str() {
        "list" => {
            if args.len() < 3 {
                return Err("Missing project name\nUsage: hxk status list <project>".to_string());
            }
            status_list(&args[2])
        }
        "create" => {
            if args.len() < 4 {
                return Err("Missing arguments\nUsage: hxk status create <project> <name> [--display <display-name>]".to_string());
            }
            let display = parse_flag(&args[4..], "--display").unwrap_or_else(|| args[3].clone());
            status_create(&args[2], &args[3], &display)
        }
        "rename" => {
            if args.len() < 5 {
                return Err(
                    "Missing arguments\nUsage: hxk status rename <project> <old-name> <new-name>"
                        .to_string(),
                );
            }
            status_rename(&args[2], &args[3], &args[4])
        }
        "delete" => {
            if args.len() < 4 {
                return Err("Missing arguments\nUsage: hxk status delete <project> <name> [--move-to <target>]".to_string());
            }
            let move_to = parse_flag(&args[4..], "--move-to");
            status_delete(&args[2], &args[3], move_to.as_deref())
        }
        "help" | "--help" | "-h" => {
            print_status_usage();
            Ok(())
        }
        cmd => Err(format!(
            "Unknown status command: {}\nRun 'hxk status help' for usage",
            cmd
        )),
    }
}

fn print_status_usage() {
    println!(
        "Kanban Status Commands

USAGE:
    hxk status <SUBCOMMAND>

SUBCOMMANDS:
    list <project>
        List all statuses in a project

    create <project> <name> [--display <display-name>]
        Create a new status column

    rename <project> <old-name> <new-name>
        Rename a status (both internal name and display name)

    delete <project> <name> [--move-to <target>]
        Delete a status (move tasks to target if specified)

EXAMPLES:
    hxk status list myproject
    hxk status create myproject review --display \"Review\"
    hxk status rename myproject todo backlog
    hxk status delete myproject archived --move-to done"
    );
}

fn status_list(project_name: &str) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;
    let project = fs::load_project(&project_path)?;

    if project.statuses.is_empty() {
        println!("No statuses found.");
        return Ok(());
    }

    println!("NAME                DISPLAY              TASKS");
    println!("------------------  -------------------  -----");

    for status in &project.statuses {
        let task_count = project
            .tasks
            .iter()
            .filter(|t| t.status == status.name)
            .count();

        println!(
            "{:<18}  {:<19}  {}",
            status.name, status.display, task_count
        );
    }

    Ok(())
}

fn status_create(project_name: &str, name: &str, display: &str) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;

    fs::status::create_status(&project_path, name, display)?;

    println!("Created status '{}' with display name '{}'", name, display);

    Ok(())
}

fn status_rename(project_name: &str, old_name: &str, new_name: &str) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;

    fs::status::rename_status(&project_path, old_name, new_name, new_name)?;

    println!("Renamed status from '{}' to '{}'", old_name, new_name);

    Ok(())
}

fn status_delete(project_name: &str, name: &str, move_to: Option<&str>) -> Result<(), String> {
    let project_path = find_project_path(project_name)?;

    fs::status::delete_status(&project_path, name, move_to)?;

    if let Some(target) = move_to {
        println!("Deleted status '{}' and moved tasks to '{}'", name, target);
    } else {
        println!("Deleted status '{}'", name);
    }

    Ok(())
}

// ============================================================================
// Legacy Commands (Backward Compatibility)
// ============================================================================

/// 创建本地项目
fn cli_create(name: &str) -> Result<()> {
    // 直接创建 .kanban 目录
    match fs::create_local_project(name) {
        Ok(path) => {
            println!("✓ 已创建本地看板");
            println!("  项目名: {}", name);
            println!("  位置: {}", path.display());
            Ok(())
        }
        Err(e) => {
            eprintln!("错误: {}", e);
            std::process::exit(1);
        }
    }
}

/// 列出所有项目
fn cli_list() -> Result<()> {
    println!("全局项目 (~/.kanban/projects):");
    println!("{}", "=".repeat(40));

    let global_projects = fs::list_project_dirs()?;
    if global_projects.is_empty() {
        println!("  (无)");
    } else {
        for (i, path) in global_projects.iter().enumerate() {
            if let Some(name) = path.file_name() {
                println!("  {}. [G] {}", i + 1, name.to_string_lossy());
            }
        }
    }

    println!("\n本地项目 (当前目录 .kanban):");
    println!("{}", "=".repeat(40));

    let local_projects = fs::list_local_project_dirs()?;
    if local_projects.is_empty() {
        println!("  (无)");
        println!("\n提示: 使用 'kanban create <name>' 创建本地看板");
    } else {
        for path in local_projects.iter() {
            // 读取项目名称
            if let Ok(config) = fs::load_project_config(path) {
                println!("  1. [L] {}", config.name);
            }
        }
    }

    Ok(())
}

/// 快速添加任务
fn cli_add(args: &[String]) -> Result<()> {
    let title = args.join(" ");

    // 查找第一个本地项目
    let local_projects = fs::list_local_project_dirs()?;

    let project_path = if let Some(first) = local_projects.first() {
        first.clone()
    } else {
        eprintln!("错误: 当前目录没有本地项目");
        eprintln!("使用 'kanban create <name>' 创建项目");
        std::process::exit(1);
    };

    // 获取下一个任务 ID
    let next_id = fs::get_next_task_id(&project_path).map_err(|e| anyhow::anyhow!(e))?;

    // 获取 todo 状态的最大 order
    let max_order =
        fs::get_max_order_in_status(&project_path, "todo").map_err(|e| anyhow::anyhow!(e))?;
    let new_order = max_order + 1000;

    // 创建任务
    let mut task = Task::new(next_id, title.clone(), "todo".to_string());
    task.order = new_order;

    // 保存任务
    fs::save_task(&project_path, &task).map_err(|e| anyhow::anyhow!(e))?;

    if let Some(project_name) = project_path.file_name() {
        println!("✓ 已添加任务到项目 '{}':", project_name.to_string_lossy());
        println!("  {}", title);
    }

    Ok(())
}

/// 打印帮助信息
fn print_help() {
    println!("Helix Kanban (hxk) - 终端看板工具\n");
    println!("用法:");
    println!("  hxk                     启动 TUI 界面");
    println!("  hxk <命令> [参数]         运行 CLI 命令");
    println!("  hxk --help              显示此帮助信息");
    println!("  hxk --version           显示版本信息\n");

    println!("MCP Server:");
    println!("  hxk mcp                              启动 MCP server (用于 AI 集成)\n");

    println!("结构化命令(推荐):");
    println!("  hxk project list                     列出项目");
    println!("  hxk task list <project>              列出任务");
    println!("  hxk task show <project> <id>        显示任务详情");
    println!("  hxk task create <project> --status <status> --title <title>  创建任务");
    println!("  hxk status list <project>            列出状态列");
    println!("  hxk status create <project> <name> [--display <name>]  创建状态\n");

    println!("简单命令(功能有限):");
    println!("  hxk list                              列出项目");
    println!("  hxk create <名称>                     创建项目");
    println!("  hxk add <标题>                       添加任务");
    println!("  hxk config show                      显示配置");
    println!("  hxk config editor <命令>             设置编辑器");
    println!("  hxk config viewer <命令>             设置预览器\n");

    println!("详细用法:");
    println!("  hxk project --help     查看项目管理命令");
    println!("  hxk task --help        查看任务管理命令");
    println!("  hxk status --help      查看状态管理命令\n");

    println!("项目类型:");
    println!("  [G] 全局项目       存储在 ~/.kanban/projects/项目名/");
    println!("  [L] 本地项目       存储在当前目录 .kanban/\n");

    println!("示例:");
    println!("  # 交互式 TUI");
    println!("  hxk\n");

    println!("  # 列出项目和配置");
    println!("  hxk list");
    println!("  hxk config show\n");

    println!("  # MCP Server (AI 集成)");
    println!("  hxk mcp");
    println!("  配置到 ~/.claude/chaoshi.json:");
    println!("  {{");
    println!("    \"mcpServers\": {{");
    println!("      \"helix-kanban\": {{");
    println!("        \"command\": \"hxk\",");
    println!("        \"args\": [\"mcp\"]");
    println!("      }}");
    println!("    }}");
    println!("  }}\n");

    println!("  # AI 协作");
    println!("  在 TUI 中按 Space+p+i 复制项目信息到剪贴板");
    println!("  AI 文档: ~/.kanban/CLAUDE.md\n");

    println!("  # 传统命令");
    println!("  hxk create 我的项目");
    println!("  hxk add 实现新功能");
    println!("  hxk config editor nvim\n");

    println!("AI 协作:");
    println!("  在 TUI 中按 Space+p+i 复制项目看板路径");
    println!("  AI 配置文件: ~/.kanban/.ai-config.json(自动生成)");
    println!("  CLI 命令提供简洁的文本输出,易于 AI 解析和操作");
}

/// 打印版本信息
fn print_version() {
    const VERSION: &str = env!("CARGO_PKG_VERSION");
    const NAME: &str = env!("CARGO_PKG_NAME");
    println!("{} {}", NAME, VERSION);
}