chaser 0.1.0

An automated file path synchronization tool that updates changed paths in configuration files in real time.
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
use anyhow::{Context, Result};
use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Config {
    pub watch_paths: Vec<String>,
    pub recursive: bool,
    pub ignore_patterns: Vec<String>,
    pub language: Option<String>,
    #[serde(default)]
    pub target_files: Vec<String>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            watch_paths: vec!["./test_files".to_string()],
            recursive: true,
            ignore_patterns: vec![
                "*.tmp".to_string(),
                "*.log".to_string(),
                ".git/**".to_string(),
                "target/**".to_string(),
            ],
            language: None,
            target_files: vec!["paths.json".to_string()],
        }
    }
}

impl Config {
    /// Get the config file path (cross-platform)
    pub fn config_file_path() -> Result<PathBuf> {
        let config_dir = dirs::config_dir().context("Failed to get config directory")?;
        let app_config_dir = config_dir.join("chaser");

        Self::ensure_config_dir_exists(&app_config_dir)?;
        Ok(app_config_dir.join("config.yaml"))
    }

    fn ensure_config_dir_exists(dir: &Path) -> Result<()> {
        if !dir.exists() {
            fs::create_dir_all(dir).context("Failed to create config directory")?;
        }
        Ok(())
    }

    /// Load config from file, create default if not exists
    pub fn load() -> Result<Self> {
        let config_path = Self::config_file_path()?;

        if config_path.exists() {
            let content = fs::read_to_string(&config_path).context("Failed to read config file")?;

            let config: Config =
                serde_yaml_ng::from_str(&content).context("Failed to parse config file")?;

            eprintln!(
                "{} {}",
                "✓".green(),
                format!("Loaded config from: {}", config_path.display()).bright_white()
            );
            Ok(config)
        } else {
            let default_config = Self::default();
            default_config.save()?;
            eprintln!(
                "{} {}",
                "✓".green(),
                format!("Created default config at: {}", config_path.display()).bright_white()
            );
            Ok(default_config)
        }
    }

    /// Save config to file
    pub fn save(&self) -> Result<()> {
        let config_path = Self::config_file_path()?;

        let content = serde_yaml_ng::to_string(self).context("Failed to serialize config")?;

        fs::write(&config_path, content).context("Failed to write config file")?;

        eprintln!(
            "{} {}",
            "✓".green(),
            format!("Config saved to: {}", config_path.display()).bright_white()
        );
        Ok(())
    }

    /// Add a watch path
    pub fn add_path(&mut self, path: String) -> Result<()> {
        if !self.watch_paths.contains(&path) {
            self.watch_paths.push(path.clone());
            println!("{}", crate::i18n::tf("msg_path_added", &[&path]).green());
        } else {
            println!("{}", crate::i18n::tf("msg_path_exists", &[&path]).yellow());
        }
        Ok(())
    }

    /// Remove a watch path
    pub fn remove_path(&mut self, path: &str) -> Result<()> {
        if let Some(pos) = self.watch_paths.iter().position(|p| p == path) {
            self.watch_paths.remove(pos);
            println!("{}", crate::i18n::tf("msg_path_removed", &[path]).green());
        } else {
            println!("{}", crate::i18n::tf("msg_path_not_found", &[path]).red());
        }
        Ok(())
    }

    /// List all watch paths
    pub fn list_paths(&self) {
        println!("{}", crate::i18n::t("ui_watch_paths").bright_cyan().bold());
        for (i, path) in self.watch_paths.iter().enumerate() {
            println!("  {}. {}", format!("{}", i + 1).bright_white(), path.cyan());
        }

        println!("\n{}", crate::i18n::t("ui_settings").bright_cyan().bold());
        println!(
            "  {}",
            crate::i18n::tf("ui_recursive", &[&self.recursive.to_string()]).bright_white()
        );
        println!(
            "  {}: [{}]",
            "Ignore patterns".bright_white(),
            self.ignore_patterns
                .iter()
                .map(|p| p.yellow().to_string())
                .collect::<Vec<_>>()
                .join(", ")
        );

        if let Some(ref lang) = self.language {
            println!("  {}: {}", "Language".bright_white(), lang.green());
        } else {
            println!(
                "  {}: {} {}",
                "Language".bright_white(),
                self.get_effective_language().green(),
                "(auto)".dimmed()
            );
        }
    }

    /// Load config with i18n messages (use after i18n is initialized)
    pub fn load_with_i18n() -> Result<Self> {
        let config_path = Self::config_file_path()?;

        if config_path.exists() {
            let content = fs::read_to_string(&config_path).context("Failed to read config file")?;

            let config: Config =
                serde_yaml_ng::from_str(&content).context("Failed to parse config file")?;

            println!(
                "{}",
                crate::i18n::tf(
                    "msg_config_loaded",
                    &[&config_path.display().to_string().cyan().to_string()]
                )
                .green()
            );
            Ok(config)
        } else {
            let default_config = Self::default();
            default_config.save_with_i18n()?;
            println!(
                "{}",
                crate::i18n::tf(
                    "msg_config_created",
                    &[&config_path.display().to_string().cyan().to_string()]
                )
                .green()
            );
            Ok(default_config)
        }
    }

    /// Save config with i18n messages (use after i18n is initialized)
    pub fn save_with_i18n(&self) -> Result<()> {
        let config_path = Self::config_file_path()?;

        let content = serde_yaml_ng::to_string(self).context("Failed to serialize config")?;

        fs::write(&config_path, content).context("Failed to write config file")?;

        println!(
            "{}",
            crate::i18n::tf(
                "msg_config_saved",
                &[&config_path.display().to_string().cyan().to_string()]
            )
            .green()
        );
        Ok(())
    }
    pub fn set_language(&mut self, language: Option<String>) -> Result<()> {
        self.language = language;
        Ok(())
    }

    /// Get effective language (config or system default)
    pub fn get_effective_language(&self) -> String {
        if let Some(ref lang) = self.language {
            lang.clone()
        } else {
            // Get system locale - simplified version
            if let Some(locale) = std::env::var("LANG").ok() {
                let locale_lower = locale.to_lowercase();
                if locale_lower.starts_with("zh")
                    && (locale_lower.contains("cn") || locale_lower.contains("hans"))
                {
                    "zh-cn".to_string()
                } else {
                    "en".to_string()
                }
            } else {
                "en".to_string()
            }
        }
    }

    /// Validate paths exist
    pub fn validate_paths(&self) -> Vec<String> {
        let mut invalid_paths = Vec::new();

        for path in &self.watch_paths {
            if !Path::new(path).exists() {
                invalid_paths.push(path.clone());
            }
        }

        invalid_paths
    }

    /// Add a target file
    pub fn add_target_file(&mut self, target_file: String) -> Result<()> {
        if !self.target_files.contains(&target_file) {
            self.target_files.push(target_file);
        }
        Ok(())
    }

    /// Remove a target file
    pub fn remove_target_file(&mut self, target_file: &str) -> Result<()> {
        self.target_files.retain(|p| p != target_file);
        Ok(())
    }

    /// List all target files
    pub fn list_target_files(&self) -> &Vec<String> {
        &self.target_files
    }

    /// Validate target files have at least one entry
    pub fn validate_target_files(&self) -> Result<()> {
        if self.target_files.is_empty() {
            anyhow::bail!("At least one target file must be configured");
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serial_test::serial;
    use std::env;
    use tempfile::TempDir;

    fn create_test_config_with_temp_dir() -> (Config, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let config_dir = temp_dir.path().join("chaser");
        fs::create_dir_all(&config_dir).unwrap();

        let mut config = Config::default();
        config.watch_paths = vec![temp_dir.path().to_string_lossy().to_string()];

        (config, temp_dir)
    }

    #[test]
    fn test_config_default() {
        let config = Config::default();
        assert_eq!(config.watch_paths, vec!["./test_files"]);
        assert_eq!(config.recursive, true);
        assert_eq!(
            config.ignore_patterns,
            vec!["*.tmp", "*.log", ".git/**", "target/**"]
        );
        assert_eq!(config.language, None);
    }

    #[test]
    fn test_config_serialization() {
        let config = Config::default();
        let yaml_str = serde_yaml_ng::to_string(&config).unwrap();
        assert!(yaml_str.contains("watch_paths"));
        assert!(yaml_str.contains("recursive"));
        assert!(yaml_str.contains("ignore_patterns"));

        let deserialized: Config = serde_yaml_ng::from_str(&yaml_str).unwrap();
        assert_eq!(config, deserialized);
    }

    #[test]
    #[serial]
    fn test_config_file_path() {
        // Test config file path generation
        let result = Config::config_file_path();
        assert!(result.is_ok());

        let path = result.unwrap();
        assert!(path.to_string_lossy().contains("chaser"));
        assert!(path.file_name().unwrap() == "config.yaml");
    }

    #[test]
    fn test_add_path() {
        let mut config = Config::default();
        let initial_count = config.watch_paths.len();

        // Test adding new path
        let result = config.add_path("./new_path".to_string());
        assert!(result.is_ok());
        assert_eq!(config.watch_paths.len(), initial_count + 1);
        assert!(config.watch_paths.contains(&"./new_path".to_string()));

        // Test adding duplicate path (should not increase count)
        let result = config.add_path("./new_path".to_string());
        assert!(result.is_ok());
        assert_eq!(config.watch_paths.len(), initial_count + 1);
    }

    #[test]
    fn test_remove_path() {
        let mut config = Config::default();
        config.watch_paths.push("./removable_path".to_string());
        let initial_count = config.watch_paths.len();

        // Test removing existing path
        let result = config.remove_path("./removable_path");
        assert!(result.is_ok());
        assert_eq!(config.watch_paths.len(), initial_count - 1);
        assert!(!config.watch_paths.contains(&"./removable_path".to_string()));

        // Test removing non-existent path
        let result = config.remove_path("./non_existent_path");
        assert!(result.is_ok());
        assert_eq!(config.watch_paths.len(), initial_count - 1);
    }

    #[test]
    fn test_set_language() {
        let mut config = Config::default();

        // Test setting language
        let result = config.set_language(Some("en".to_string()));
        assert!(result.is_ok());
        assert_eq!(config.language, Some("en".to_string()));

        // Test setting None
        let result = config.set_language(None);
        assert!(result.is_ok());
        assert_eq!(config.language, None);
    }

    #[test]
    fn test_get_effective_language() {
        let mut config = Config::default();

        // Test with no language set (should return system locale or default)
        let effective = config.get_effective_language();
        assert!(effective == "en" || effective == "zh-cn");

        // Test with language set
        config.language = Some("zh-cn".to_string());
        assert_eq!(config.get_effective_language(), "zh-cn");

        config.language = Some("en".to_string());
        assert_eq!(config.get_effective_language(), "en");
    }

    #[test]
    fn test_get_effective_language_with_env() {
        let config = Config::default();

        // Test with LANG environment variable
        unsafe {
            env::set_var("LANG", "zh_CN.UTF-8");
        }
        assert_eq!(config.get_effective_language(), "zh-cn");

        unsafe {
            env::set_var("LANG", "en_US.UTF-8");
        }
        assert_eq!(config.get_effective_language(), "en");

        unsafe {
            env::set_var("LANG", "fr_FR.UTF-8");
        }
        assert_eq!(config.get_effective_language(), "en");

        // Clean up
        unsafe {
            env::remove_var("LANG");
        }
    }

    #[test]
    fn test_validate_paths() {
        let (mut config, temp_dir) = create_test_config_with_temp_dir();

        // Add some paths - one valid, one invalid
        let valid_path = temp_dir.path().to_string_lossy().to_string();
        let invalid_path = "/definitely/does/not/exist".to_string();

        config.watch_paths = vec![valid_path.clone(), invalid_path.clone()];

        let invalid_paths = config.validate_paths();
        assert_eq!(invalid_paths.len(), 1);
        assert_eq!(invalid_paths[0], invalid_path);

        // Test with all valid paths
        config.watch_paths = vec![valid_path];
        let invalid_paths = config.validate_paths();
        assert_eq!(invalid_paths.len(), 0);

        // Test with all invalid paths
        config.watch_paths = vec!["/invalid1".to_string(), "/invalid2".to_string()];
        let invalid_paths = config.validate_paths();
        assert_eq!(invalid_paths.len(), 2);
    }

    #[test]
    #[serial] // Serialize because we're dealing with config files
    fn test_save_and_load() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.yaml");

        // Create a test config
        let mut original_config = Config::default();
        original_config.watch_paths = vec!["./test1".to_string(), "./test2".to_string()];
        original_config.recursive = false;
        original_config.ignore_patterns = vec!["*.test".to_string()];
        original_config.language = Some("zh-cn".to_string());

        // Save config
        let yaml_content = serde_yaml_ng::to_string(&original_config).unwrap();
        fs::write(&config_path, yaml_content).unwrap();

        // Load config
        let content = fs::read_to_string(&config_path).unwrap();
        let loaded_config: Config = serde_yaml_ng::from_str(&content).unwrap();

        assert_eq!(original_config, loaded_config);
    }

    #[test]
    fn test_config_clone() {
        let config1 = Config::default();
        let config2 = config1.clone();
        assert_eq!(config1, config2);

        // Modify one and ensure they're different
        let mut config3 = config1.clone();
        config3.recursive = false;
        assert_ne!(config1, config3);
    }

    #[test]
    fn test_config_debug() {
        let config = Config::default();
        let debug_str = format!("{:?}", config);
        assert!(debug_str.contains("Config"));
        assert!(debug_str.contains("watch_paths"));
        assert!(debug_str.contains("recursive"));
    }
}