subx_cli/commands/
detect_encoding_command.rs1use crate::Result;
2use crate::core::formats::encoding::EncodingDetector;
3use log::error;
4use std::path::Path;
5
6pub fn detect_encoding_command(file_paths: &[String], verbose: bool) -> Result<()> {
8 let detector = EncodingDetector::new()?;
9 for file in file_paths {
10 if !Path::new(file).exists() {
11 error!("檔案不存在: {}", file);
12 continue;
13 }
14 match detector.detect_file_encoding(file) {
15 Ok(info) => {
16 let name = Path::new(file)
17 .file_name()
18 .and_then(|n| n.to_str())
19 .unwrap_or(file);
20 println!("檔案: {}", name);
21 println!(
22 " 編碼: {:?} (信心度: {:.1}%) BOM: {}",
23 info.charset,
24 info.confidence * 100.0,
25 if info.bom_detected { "是" } else { "否" }
26 );
27 let sample = if verbose {
28 info.sample_text.clone()
29 } else if info.sample_text.len() > 50 {
30 format!("{}...", &info.sample_text[..47])
31 } else {
32 info.sample_text.clone()
33 };
34 println!(" 樣本文字: {}\n", sample);
35 }
36 Err(e) => error!("檢測 {} 編碼失敗: {}", file, e),
37 }
38 }
39 Ok(())
40}