dictx 0.1.2

A fast, colorful terminal dictionary with offline indexes and optional AI explanations.
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
mod ai;
mod config;
mod interaction;
mod output;

use anyhow::{anyhow, Context, Result};
use clap::{Args, Parser, Subcommand, ValueEnum};
use config::{AppConfig, SourceConfig};
use dictx_core::{Query, SearchFilters, SearchRequest};
use dictx_index::{build_index, read_metadata, BuildOptions};
use dictx_parser::{parser_for, write_dxdict, DxdictMetadata};
use dictx_search::{parse_lookup_query, parse_search_query, DictSearcher, SearchResult};
use indicatif::{ProgressBar, ProgressStyle};
use output::{print_entries, print_json_result, print_source_list};
use std::fs;
use std::io::IsTerminal;
use std::path::PathBuf;

const DEFAULT_DISPLAY_LIMIT: usize = 3;
const SEARCH_PREFETCH_LIMIT: usize = 48;

#[derive(Debug, Parser)]
#[command(
    name = "dictx",
    version,
    about = "Rust 终端离线词库 + 可选在线 AI API 词典"
)]
struct Cli {
    #[arg(long, global = true, env = "DICTX_CONFIG")]
    config: Option<PathBuf>,

    #[arg(long, global = true)]
    no_color: bool,

    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// 初始化配置,自动发现当前目录及父目录下的可用词库
    Init(InitArgs),
    /// 精确查词,默认英译中
    #[command(alias = "l")]
    Lookup(LookupArgs),
    /// 中文查英文或中文释义反查
    Zh(ZhArgs),
    /// 全文、模糊、通配符搜索
    Search(SearchArgs),
    /// 构建或重建本地索引
    Build(BuildArgs),
    /// 词典源管理
    Source(SourceCommand),
    /// 配置管理
    Config(ConfigCommand),
    /// 在线 AI API 解释;只有显式调用此命令才联网
    Ai(AiArgs),
}

#[derive(Debug, Args)]
struct InitArgs {
    /// 覆盖已有配置
    #[arg(short, long)]
    force: bool,
}

#[derive(Debug, Args)]
struct LookupArgs {
    word: String,
    #[arg(short, long)]
    source: Option<String>,
    #[arg(short, long)]
    pos: Option<String>,
    #[arg(short, long, value_enum, default_value_t = OutputFormat::Rich)]
    format: OutputFormat,
    #[arg(short = 'n', long, default_value_t = DEFAULT_DISPLAY_LIMIT)]
    limit: usize,
    #[arg(long)]
    raw: bool,
}

#[derive(Debug, Args)]
struct ZhArgs {
    text: String,
    #[arg(short, long)]
    source: Option<String>,
    #[arg(short = 'n', long, default_value_t = DEFAULT_DISPLAY_LIMIT)]
    limit: usize,
    #[arg(short, long, value_enum, default_value_t = OutputFormat::Rich)]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct SearchArgs {
    query: String,
    #[arg(short, long)]
    source: Option<String>,
    #[arg(short, long)]
    pos: Option<String>,
    #[arg(short, long)]
    tag: Option<String>,
    #[arg(long)]
    collins_min: Option<u8>,
    #[arg(long)]
    freq_min: Option<u32>,
    #[arg(long)]
    freq_max: Option<u32>,
    #[arg(long)]
    oxford_only: bool,
    #[arg(short = 'n', long, default_value_t = DEFAULT_DISPLAY_LIMIT)]
    limit: usize,
    #[arg(long, default_value_t = 0)]
    offset: usize,
    #[arg(short, long, value_enum, default_value_t = OutputFormat::Rich)]
    format: OutputFormat,
}

#[derive(Debug, Args)]
struct BuildArgs {
    #[arg(short, long)]
    source: Option<String>,
    #[arg(short, long)]
    force: bool,
    #[arg(long, default_value_t = 128)]
    ram: usize,
}

#[derive(Debug, Args)]
struct SourceCommand {
    #[command(subcommand)]
    command: SourceSubcommand,
}

#[derive(Debug, Subcommand)]
enum SourceSubcommand {
    /// 列出词典源
    List,
    /// 推荐可下载的高质量开放词库
    Recommend,
    /// 添加词典源
    Add(SourceAddArgs),
    /// 导入词典文件到 DictX 管理目录并添加词典源
    Import(SourceImportArgs),
    /// 将外部词库转换为 DictX 专有 .dxdict 文件
    Pack(SourcePackArgs),
    /// 移除词典源
    Remove { name: String },
    /// 显示词典源详情
    Info { name: String },
}

#[derive(Debug, Args)]
struct SourceAddArgs {
    name: String,
    path: PathBuf,
    #[arg(short = 'f', long)]
    format: String,
    #[arg(long)]
    display: Option<String>,
    #[arg(long, default_value_t = true)]
    enabled: bool,
}

#[derive(Debug, Args)]
struct SourceImportArgs {
    name: String,
    path: PathBuf,
    #[arg(short = 'f', long)]
    format: String,
    #[arg(long)]
    display: Option<String>,
    #[arg(long, default_value_t = true)]
    enabled: bool,
    #[arg(long)]
    force: bool,
}

#[derive(Debug, Args)]
struct SourcePackArgs {
    input: PathBuf,
    output: PathBuf,
    #[arg(short = 'f', long)]
    format: String,
    #[arg(long)]
    name: Option<String>,
    #[arg(long)]
    display: Option<String>,
    #[arg(long)]
    force: bool,
    /// 按词条数拆分输出多个 .dxdict 分片
    #[arg(long)]
    shard_size: Option<usize>,
}

#[derive(Debug, Args)]
struct ConfigCommand {
    #[command(subcommand)]
    command: ConfigSubcommand,
}

#[derive(Debug, Subcommand)]
enum ConfigSubcommand {
    Show,
    Path,
    Paths,
    Get { key: String },
    Set { key: String, value: String },
}

#[derive(Debug, Args)]
struct AiArgs {
    text: String,
    #[arg(long)]
    context: Option<String>,
    #[arg(long)]
    model: Option<String>,
}

#[derive(Debug, Clone, Copy, ValueEnum, PartialEq, Eq)]
enum OutputFormat {
    Rich,
    Plain,
    Json,
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    let command = cli.command.unwrap_or(Command::Config(ConfigCommand {
        command: ConfigSubcommand::Show,
    }));

    match command {
        Command::Init(args) => init(cli.config, args.force),
        Command::Lookup(args) => {
            let config = AppConfig::load_or_create(cli.config.as_deref())?;
            let mut request = SearchRequest::new(parse_lookup_query(&args.word));
            request.limit = args.limit;
            request.filters.pos = args.pos;
            run_search(
                &config,
                args.source.as_deref(),
                request,
                args.format,
                args.raw,
                cli.no_color,
            )
        }
        Command::Zh(args) => {
            let config = AppConfig::load_or_create(cli.config.as_deref())?;
            let mut request = SearchRequest::new(Query::Chinese { text: args.text });
            request.limit = args.limit;
            run_search(
                &config,
                args.source.as_deref(),
                request,
                args.format,
                false,
                cli.no_color,
            )
        }
        Command::Search(args) => {
            let config = AppConfig::load_or_create(cli.config.as_deref())?;
            let mut request = SearchRequest::new(parse_search_query(
                &args.query,
                config.search.fuzzy_distance,
            ));
            request.limit = args.limit;
            request.offset = args.offset;
            request.filters = SearchFilters {
                source: None,
                pos: args.pos,
                tag: args.tag,
                collins_min: args.collins_min,
                freq_min: args.freq_min,
                freq_max: args.freq_max,
                oxford_only: args.oxford_only,
            };
            run_search(
                &config,
                args.source.as_deref(),
                request,
                args.format,
                false,
                cli.no_color,
            )
        }
        Command::Build(args) => {
            let config = AppConfig::load_or_create(cli.config.as_deref())?;
            build_sources(&config, args)
        }
        Command::Source(args) => source_command(cli.config, args.command),
        Command::Config(args) => config_command(cli.config, args.command),
        Command::Ai(args) => {
            let config = AppConfig::load_or_create(cli.config.as_deref())?;
            ai::run_ai(&config, args, !cli.no_color && config.output.color)
        }
    }
}

fn init(config_path: Option<PathBuf>, force: bool) -> Result<()> {
    let path = AppConfig::resolve_path(config_path.as_deref())?;
    if path.exists() && !force {
        let mut existing = AppConfig::load_or_create(Some(&path))?;
        if existing.sources.is_empty() {
            let detected = AppConfig::default_with_detected_sources()?.sources;
            if detected.is_empty() {
                return Err(anyhow!(
                    "配置已存在但没有词典源: {}\n当前目录、父目录及 DictX 词库目录没有发现可用词库。\n请使用 dictx source import <NAME> <PATH> --format <FORMAT> 导入,或使用 dictx source add <NAME> <PATH> --format <FORMAT> 引用外部文件。",
                    path.display()
                ));
            }
            existing.sources = detected;
            existing.save_to(&path)?;
            println!("已更新配置: {}", path.display());
            print_init_next_steps(&existing);
            return Ok(());
        }
        let source_summary = existing
            .sources
            .iter()
            .map(|source| format!("  - {} ({})", source.name, source.format))
            .collect::<Vec<_>>()
            .join("\n");
        return Err(anyhow!(
            "配置已存在: {}\n当前已配置 {} 个词典源:\n{}\n\n常用操作:\n  - 查看详情: dictx source list\n  - 构建索引: dictx build\n  - 重新自动发现并覆盖: dictx init --force",
            path.display(),
            existing.sources.len(),
            source_summary
        ));
    }
    let config = AppConfig::default_with_detected_sources()?;
    if config.sources.is_empty() {
        return Err(anyhow!(
            "没有自动发现词典源。\n请把词库放入 DictX 词库目录后运行 dictx init --force,或使用 dictx source import/add 手动添加。"
        ));
    }
    config.save_to(&path)?;
    println!("已写入配置: {}", path.display());
    print_init_next_steps(&config);
    Ok(())
}

fn run_search(
    config: &AppConfig,
    source_filter: Option<&str>,
    mut request: SearchRequest,
    format: OutputFormat,
    raw: bool,
    no_color: bool,
) -> Result<()> {
    let sources = config.enabled_sources(source_filter);
    if sources.is_empty() {
        return Err(anyhow!(
            "没有可用词典源。请先运行 dictx init 或 dictx source add。"
        ));
    }

    request.limit = request.limit.max(1);
    let display_limit = request.limit;
    let mut fetch_request = request.clone();
    if matches!(format, OutputFormat::Rich | OutputFormat::Plain) && !raw {
        fetch_request.limit = display_limit.max(SEARCH_PREFETCH_LIMIT);
    }

    let mut all = Vec::new();
    let mut elapsed = 0u128;
    let mut searched_sources = 0usize;
    let mut missing_indexes = Vec::new();
    for source in sources {
        let index_dir = config.index_dir_for(&source.name);
        if !index_dir.exists() {
            missing_indexes.push((source.name.clone(), index_dir));
            continue;
        }
        let searcher = DictSearcher::open(&index_dir)
            .with_context(|| format!("打开索引失败: {}", index_dir.display()))?;
        let result = searcher.search(&fetch_request)?;
        searched_sources += 1;
        elapsed += result.elapsed_ms;
        all.extend(result.entries);
    }

    if searched_sources == 0 {
        return Err(missing_index_error(source_filter, &missing_indexes));
    }

    all.sort_by(|left, right| {
        right
            .score
            .partial_cmp(&left.score)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| left.entry.word_lower.cmp(&right.entry.word_lower))
    });
    let total = all.len();
    let all = all
        .into_iter()
        .take(fetch_request.limit)
        .collect::<Vec<_>>();
    let result = SearchResult {
        entries: all,
        total,
        elapsed_ms: elapsed,
    };

    match format {
        OutputFormat::Json => print_json_result(&request, &result, raw),
        OutputFormat::Plain => {
            print_entries(&request, &result, false, raw, display_limit, false);
            Ok(())
        }
        OutputFormat::Rich => {
            let color = !no_color && config.output.color;
            let interactive = !raw && std::io::stdout().is_terminal();
            let rendered = print_entries(&request, &result, color, raw, display_limit, interactive);
            if raw {
                return Ok(());
            }
            if interactive {
                interaction::run(config, &result, rendered, color)
            } else {
                Ok(())
            }
        }
    }
}

fn missing_index_error(
    source_filter: Option<&str>,
    missing_indexes: &[(String, PathBuf)],
) -> anyhow::Error {
    let mut message =
        String::from("没有可查询索引。已启用词典源,但当前索引目录还没有构建完成。\n");
    if !missing_indexes.is_empty() {
        message.push_str("\n缺少索引:\n");
        for (name, path) in missing_indexes {
            message.push_str(&format!("  - {name}: {}\n", path.display()));
        }
    }
    message.push_str("\n请先运行: ");
    if let Some(source) = source_filter {
        message.push_str(&format!("dictx build --source {source}"));
    } else {
        message.push_str("dictx build");
    }
    message.push_str(
        "\n如果刚执行过 dictx init 或 dictx init --force,这一步是首次查询前必须执行的索引构建。",
    );
    anyhow!(message)
}

fn build_sources(config: &AppConfig, args: BuildArgs) -> Result<()> {
    fs::create_dir_all(&config.index_dir)?;
    let sources = config.enabled_sources(args.source.as_deref());
    if sources.is_empty() {
        return Err(anyhow!("没有匹配的启用词典源"));
    }

    for source in sources {
        let parser = parser_for(&source.format)?;
        let report = parser.validate(&source.path)?;
        if !report.valid {
            return Err(anyhow!(
                "数据源 {} 校验失败: {}",
                source.name,
                report.issues.join("; ")
            ));
        }

        let index_dir = config.index_dir_for(&source.name);
        let spinner = ProgressBar::new_spinner();
        spinner.set_style(
            ProgressStyle::with_template("{spinner:.cyan} {msg}")
                .unwrap_or_else(|_| ProgressStyle::default_spinner()),
        );
        spinner.enable_steady_tick(std::time::Duration::from_millis(80));
        spinner.set_message(format!(
            "构建 {} ({})...",
            source.display_name(),
            source_location(&source)
        ));

        let entries = parser.parse(&source.path)?;
        let stats = build_index(
            &index_dir,
            entries,
            &BuildOptions {
                ram_mb: args.ram,
                force: args.force,
                source_name: source.name.clone(),
                source_path: Some(source.path.clone()),
            },
        )?;
        spinner.finish_and_clear();
        println!(
            "{}: {} 条,{}{} ms",
            source.display_name(),
            stats.entries,
            human_bytes(stats.index_bytes),
            stats.elapsed_ms
        );
    }
    Ok(())
}

fn source_command(config_path: Option<PathBuf>, command: SourceSubcommand) -> Result<()> {
    let path = AppConfig::resolve_path(config_path.as_deref())?;
    let mut config = AppConfig::load_or_create(Some(&path))?;
    match command {
        SourceSubcommand::List => {
            print_source_list(&config)?;
        }
        SourceSubcommand::Recommend => print_recommended_sources(),
        SourceSubcommand::Add(args) => {
            if config.sources.iter().any(|source| source.name == args.name) {
                return Err(anyhow!("词典源已存在: {}", args.name));
            }
            config.sources.push(SourceConfig {
                name: args.name,
                path: args.path,
                format: args.format,
                display: args.display,
                enabled: args.enabled,
            });
            config.save_to(&path)?;
            println!("已添加词典源");
        }
        SourceSubcommand::Import(args) => {
            let source_path = args.path.canonicalize().with_context(|| {
                format!("无法读取词典文件,请检查路径: {}", args.path.display())
            })?;
            let parser = parser_for(&args.format)?;
            let report = parser.validate(&source_path)?;
            if !report.valid {
                return Err(anyhow!("词典文件校验失败: {}", report.issues.join("; ")));
            }
            let filename = source_path
                .file_name()
                .ok_or_else(|| anyhow!("词典路径缺少文件名: {}", source_path.display()))?;
            let target_dir = config.dict_dir.join(&args.name);
            let target_path = target_dir.join(format!("{}.dxdict", args.name));
            if target_path.exists() && !args.force {
                return Err(anyhow!(
                    "目标词库已存在: {}\n如需覆盖,请追加 --force。",
                    target_path.display()
                ));
            }
            if config.sources.iter().any(|source| source.name == args.name) && !args.force {
                return Err(anyhow!(
                    "词典源已存在: {}\n如需替换,请追加 --force。",
                    args.name
                ));
            }
            fs::create_dir_all(&target_dir)?;
            let count = write_dxdict(
                &target_path,
                &DxdictMetadata {
                    name: args.name.clone(),
                    display: args.display.clone(),
                    source_format: Some(args.format.clone()),
                },
                parser.parse(&source_path)?,
            )?;

            let imported = SourceConfig {
                name: args.name,
                path: target_path.clone(),
                format: "dxdict".to_string(),
                display: args.display,
                enabled: args.enabled,
            };
            config.sources.retain(|source| source.name != imported.name);
            config.sources.push(imported);
            config.save_to(&path)?;
            println!(
                "已导入词典源: {} ({} 条,来源 {})",
                target_path.display(),
                count,
                filename.to_string_lossy()
            );
            println!(
                "下一步: dictx build --source {}",
                config.sources.last().unwrap().name
            );
        }
        SourceSubcommand::Pack(args) => {
            let source_path = args.input.canonicalize().with_context(|| {
                format!("无法读取词典文件,请检查路径: {}", args.input.display())
            })?;
            if args.output.exists() && !args.force {
                return Err(anyhow!(
                    "目标文件已存在: {}\n如需覆盖,请追加 --force。",
                    args.output.display()
                ));
            }
            if let Some(parent) = args.output.parent() {
                fs::create_dir_all(parent)?;
            }
            let parser = parser_for(&args.format)?;
            let report = parser.validate(&source_path)?;
            if !report.valid {
                return Err(anyhow!("词典文件校验失败: {}", report.issues.join("; ")));
            }
            let name = args.name.unwrap_or_else(|| {
                args.output
                    .file_stem()
                    .and_then(|value| value.to_str())
                    .unwrap_or("dictx")
                    .to_string()
            });
            let metadata = DxdictMetadata {
                name,
                display: args.display,
                source_format: Some(args.format),
            };
            if let Some(shard_size) = args.shard_size {
                let shards = write_dxdict_shards(
                    &args.output,
                    &metadata,
                    parser.parse(&source_path)?,
                    shard_size,
                )?;
                let total = shards.iter().map(|(_, count)| *count).sum::<usize>();
                println!("已生成 DictX 专有词库分片:");
                for (path, count) in &shards {
                    println!("  - {} ({} 条)", path.display(), count);
                }
                println!("词条数: {total}");
            } else {
                let count = write_dxdict(&args.output, &metadata, parser.parse(&source_path)?)?;
                println!("已生成 DictX 专有词库: {}", args.output.display());
                println!("词条数: {count}");
            }
        }
        SourceSubcommand::Remove { name } => {
            let before = config.sources.len();
            config.sources.retain(|source| source.name != name);
            if config.sources.len() == before {
                return Err(anyhow!("没有找到词典源: {name}"));
            }
            config.save_to(&path)?;
            println!("已移除词典源: {name}");
        }
        SourceSubcommand::Info { name } => {
            let source = config
                .sources
                .iter()
                .find(|source| source.name == name)
                .ok_or_else(|| anyhow!("没有找到词典源: {name}"))?;
            println!("{}", toml::to_string_pretty(source)?);
            if let Some(meta) = read_metadata(&config.index_dir_for(&source.name))? {
                println!(
                    "索引: {} 条, {}",
                    meta.entries,
                    human_bytes(meta.index_bytes)
                );
            } else {
                println!("索引: 未构建");
            }
        }
    }
    Ok(())
}

fn print_init_next_steps(config: &AppConfig) {
    println!(
        "配置文件: {}",
        AppConfig::resolve_path(None)
            .map(|p| p.display().to_string())
            .unwrap_or_else(|_| "<未知>".to_string())
    );
    println!("词库目录: {}", config.dict_dir.display());
    println!("索引目录: {}", config.index_dir.display());
    println!();
    println!("已发现 {} 个词典源:", config.sources.len());
    for source in &config.sources {
        println!(
            "  - {} ({})  {}",
            source.name,
            source.format,
            source_location(source)
        );
    }
    println!();
    println!("下一步:");
    println!("  1. 构建索引: dictx build");
    println!("  2. 查询测试: dictx lookup apple / dictx zh 老师");
    println!("  3. 查看词源: dictx source list");
    println!("  4. 导入词库: dictx source import ecdict /path/to/ecdict.csv --format ecdict");
    println!("  5. 获取更多开放词库: dictx source recommend");
    println!();
    println!("注意: init 只写入配置,不会自动生成索引;首次查询前必须先运行 dictx build。");
}

fn print_recommended_sources() {
    println!("推荐开放词库:");
    println!();
    println!("1. ECDICT 英汉双解");
    println!("   用途: 英->中主词库,含音标、词频、考试标签、柯林斯/牛津标注");
    println!("   许可: MIT");
    println!("   地址: https://github.com/skywind3000/ECDICT");
    println!("   导入: dictx source import ecdict /path/to/ecdict.csv --format ecdict");
    println!();
    println!("2. CC-CEDICT 汉英词典");
    println!("   用途: 中->英补充词库,适合中文词、短语、拼音查询");
    println!("   许可: CC BY-SA");
    println!("   地址: https://cc-cedict.org/wiki/");
    println!("   导入: dictx source import cc_cedict /path/to/cedict_ts.u8 --format cedict");
    println!();
    println!("3. open-dict-data");
    println!("   用途: 多语言开放词典集合,可作为后续适配来源");
    println!("   地址: https://open-dict-data.github.io/");
    println!();
    println!("说明:");
    println!("   - DictX 发布包已内置新世纪汉英与金山词霸词库,可直接 init + build。");
    println!("   - source import 会将外部词库转换为 DictX 专有 .dxdict 格式后纳入管理目录。");
    println!("   - source pack 只生成 .dxdict 文件,不修改配置,适合批量整理词库。");
}

fn config_command(config_path: Option<PathBuf>, command: ConfigSubcommand) -> Result<()> {
    let path = AppConfig::resolve_path(config_path.as_deref())?;
    let mut config = AppConfig::load_or_create(Some(&path))?;
    match command {
        ConfigSubcommand::Show => {
            println!("{}", toml::to_string_pretty(&config)?);
        }
        ConfigSubcommand::Path => {
            println!("{}", path.display());
        }
        ConfigSubcommand::Paths => {
            println!("config = {}", path.display());
            println!("dict_dir = {}", config.dict_dir.display());
            println!("index_dir = {}", config.index_dir.display());
        }
        ConfigSubcommand::Get { key } => {
            println!(
                "{}",
                config
                    .get_key(&key)
                    .ok_or_else(|| anyhow!("未知配置项: {key}"))?
            );
        }
        ConfigSubcommand::Set { key, value } => {
            config.set_key(&key, &value)?;
            config.save_to(&path)?;
            println!("已更新 {key}");
        }
    }
    Ok(())
}

fn human_bytes(bytes: u64) -> String {
    const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
    let mut value = bytes as f64;
    let mut unit = 0;
    while value >= 1024.0 && unit < UNITS.len() - 1 {
        value /= 1024.0;
        unit += 1;
    }
    if unit == 0 {
        format!("{} {}", bytes, UNITS[unit])
    } else {
        format!("{value:.1} {}", UNITS[unit])
    }
}

fn source_location(source: &SourceConfig) -> String {
    let value = source.path.to_string_lossy();
    if value.starts_with("builtin:") {
        format!("内置包 {value}")
    } else {
        source.path.display().to_string()
    }
}

fn write_dxdict_shards<I>(
    output: &std::path::Path,
    metadata: &DxdictMetadata,
    entries: I,
    shard_size: usize,
) -> Result<Vec<(PathBuf, usize)>>
where
    I: IntoIterator<Item = dictx_core::Result<dictx_core::DictEntry>>,
{
    if shard_size == 0 {
        return Err(anyhow!("--shard-size 必须大于 0"));
    }

    let mut shards = Vec::new();
    let mut buffer = Vec::with_capacity(shard_size.min(8192));
    let mut shard_index = 0usize;

    for entry in entries {
        buffer.push(entry?);
        if buffer.len() >= shard_size {
            let path = shard_path(output, shard_index);
            let count = write_dxdict(&path, metadata, buffer.drain(..).map(|entry| Ok(entry)))?;
            shards.push((path, count));
            shard_index += 1;
        }
    }

    if !buffer.is_empty() || shards.is_empty() {
        let path = shard_path(output, shard_index);
        let count = write_dxdict(&path, metadata, buffer.drain(..).map(|entry| Ok(entry)))?;
        shards.push((path, count));
    }

    Ok(shards)
}

fn shard_path(output: &std::path::Path, shard_index: usize) -> PathBuf {
    let parent = output.parent().unwrap_or_else(|| std::path::Path::new(""));
    let stem = output
        .file_stem()
        .and_then(|value| value.to_str())
        .unwrap_or("dictx");
    parent.join(format!("{stem}-{shard_index:03}.dxdict"))
}