subtitle_translator_cli/
utils.rs

1use std::path::PathBuf;
2
3use glob::glob;
4use indicatif::{ProgressBar, ProgressStyle};
5
6/// Get all subtitle files in a directory
7///
8/// Parameters:
9/// - file_path: String
10///
11pub fn get_all_files(file_path: &str) -> Result<Vec<PathBuf>, &str> {
12    let mut files = Vec::new();
13
14    for entry in glob(file_path).expect("Failed to read glob pattern") {
15        match entry {
16            Ok(path) => files.push(path),
17            Err(e) => println!("{:?}", e),
18        }
19    }
20
21    Ok(files)
22}
23
24pub fn read_file_trim_bom(contents: &str) -> String {
25    let bom = "\u{FEFF}";
26    if contents.starts_with(bom) {
27        contents[bom.len()..].to_string()
28    } else {
29        contents.to_string()
30    }
31}
32
33pub fn sort_and_extract_translations(
34    translated_combined_text: &mut Vec<(usize, String)>,
35) -> Vec<String> {
36    translated_combined_text.sort_by_key(|k| k.0);
37
38    translated_combined_text
39        .into_iter()
40        .map(|(_, text)| text.to_owned())
41        .collect()
42}
43
44pub fn pb_init(total: u64) -> ProgressBar {
45    let pb = ProgressBar::new(total);
46    pb.set_style(
47        ProgressStyle::with_template(
48            "{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {percent}%",
49        )
50        .unwrap()
51        .progress_chars("#>-"),
52    );
53    pb
54}