gitlogue 0.11.0

A Git history screensaver - watch your code rewrite itself
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
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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    #[serde(default = "default_theme")]
    pub theme: String,
    #[serde(default = "default_speed")]
    pub speed: u64,
    #[serde(default = "default_background")]
    pub background: bool,
    #[serde(default = "default_order")]
    pub order: String,
    #[serde(default = "default_loop", rename = "loop", alias = "loop_playback")]
    pub loop_playback: bool,
    #[serde(default = "default_ignore_patterns")]
    pub ignore_patterns: Vec<String>,
    #[serde(default)]
    pub speed_rules: Vec<String>,
}

fn default_theme() -> String {
    "tokyo-night".to_string()
}

fn default_speed() -> u64 {
    30
}

fn default_background() -> bool {
    true
}

fn default_order() -> String {
    "random".to_string()
}

fn default_loop() -> bool {
    false
}

fn default_ignore_patterns() -> Vec<String> {
    Vec::new()
}

fn home_dir() -> Option<PathBuf> {
    #[cfg(test)]
    if let Some(path) = test_home::current() {
        return Some(path);
    }
    dirs::home_dir()
}

#[cfg(test)]
pub(crate) mod test_home {
    use std::path::{Path, PathBuf};
    use std::sync::{Mutex, MutexGuard, OnceLock, RwLock};

    fn serializer() -> &'static Mutex<()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(()))
    }

    fn override_state() -> &'static RwLock<Option<PathBuf>> {
        static STATE: OnceLock<RwLock<Option<PathBuf>>> = OnceLock::new();
        STATE.get_or_init(|| RwLock::new(None))
    }

    pub(crate) fn current() -> Option<PathBuf> {
        override_state().read().ok().and_then(|g| g.clone())
    }

    pub(crate) struct Guard {
        _lock: MutexGuard<'static, ()>,
    }

    impl Guard {
        pub(crate) fn new(path: &Path) -> Self {
            Self::with_override(Some(path.to_path_buf()))
        }

        #[cfg(test)]
        pub(crate) fn no_override() -> Self {
            Self::with_override(None)
        }

        fn with_override(path: Option<PathBuf>) -> Self {
            let lock = serializer().lock().expect("serializer poisoned");
            *override_state().write().expect("override poisoned") = path;
            Self { _lock: lock }
        }
    }

    impl Drop for Guard {
        fn drop(&mut self) {
            *override_state().write().expect("override poisoned") = None;
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            theme: default_theme(),
            speed: default_speed(),
            background: default_background(),
            order: default_order(),
            loop_playback: default_loop(),
            ignore_patterns: default_ignore_patterns(),
            speed_rules: Vec::new(),
        }
    }
}

impl Config {
    pub fn load() -> Result<Self> {
        let config_path = Self::config_path()?;

        if !config_path.exists() {
            return Ok(Self::default());
        }

        let contents = fs::read_to_string(&config_path)
            .with_context(|| format!("Failed to read config file: {}", config_path.display()))?;

        toml::from_str(&contents)
            .with_context(|| format!("Failed to parse config file: {}", config_path.display()))
    }

    pub fn save(&self) -> Result<()> {
        let config_path = Self::config_path()?;

        let contents = if config_path.exists() {
            // Load existing config and update values to preserve comments
            let existing = fs::read_to_string(&config_path).with_context(|| {
                format!("Failed to read config file: {}", config_path.display())
            })?;

            let mut doc = existing
                .parse::<toml_edit::DocumentMut>()
                .with_context(|| {
                    format!("Failed to parse config file: {}", config_path.display())
                })?;

            // Update values while preserving comments
            doc["theme"] = toml_edit::value(self.theme.as_str());
            doc["speed"] = toml_edit::value(self.speed as i64);
            doc["background"] = toml_edit::value(self.background);
            doc["order"] = toml_edit::value(self.order.as_str());
            doc["loop"] = toml_edit::value(self.loop_playback);
            // Update ignore_patterns as array
            let mut array = toml_edit::Array::new();
            for pattern in &self.ignore_patterns {
                array.push(pattern.as_str());
            }
            doc["ignore_patterns"] = toml_edit::value(array);

            // Update speed_rules as array
            let mut speed_array = toml_edit::Array::new();
            for rule in &self.speed_rules {
                speed_array.push(rule.as_str());
            }
            doc["speed_rules"] = toml_edit::value(speed_array);

            doc.to_string()
        } else {
            // Create new config with comments
            let patterns_str = if self.ignore_patterns.is_empty() {
                "[]".to_string()
            } else {
                let patterns: Vec<String> = self
                    .ignore_patterns
                    .iter()
                    .map(|p| format!("\"{}\"", p))
                    .collect();
                format!("[{}]", patterns.join(", "))
            };

            let speed_rules_str = if self.speed_rules.is_empty() {
                "[]".to_string()
            } else {
                let rules: Vec<String> = self
                    .speed_rules
                    .iter()
                    .map(|r| format!("\"{}\"", r))
                    .collect();
                format!("[{}]", rules.join(", "))
            };

            format!(
                "# gitlogue configuration file\n\
                 # All settings are optional and will use defaults if not specified\n\
                 \n\
                 # Theme to use for syntax highlighting\n\
                 theme = \"{}\"\n\
                 \n\
                 # Typing speed in milliseconds per character\n\
                 speed = {}\n\
                 \n\
                 # Show background colors (set to false for transparent background)\n\
                 background = {}\n\
                 \n\
                 # Commit playback order: random, asc, or desc\n\
                 order = \"{}\"\n\
                 \n\
                 # Loop the animation continuously\n\
                 loop = {}\n\
                 \n\
                 # Ignore patterns (gitignore syntax)\n\
                 # Examples: [\"*.png\", \"*.ipynb\", \"dist/**\"]\n\
                 ignore_patterns = {}\n\
                 \n\
                 # Speed rules for different file types (pattern:milliseconds)\n\
                 # Examples: [\"*.java:50\", \"*.xml:5\", \"*.rs:30\"]\n\
                 speed_rules = {}\n",
                self.theme,
                self.speed,
                self.background,
                self.order,
                self.loop_playback,
                patterns_str,
                speed_rules_str
            )
        };

        fs::write(&config_path, contents)
            .with_context(|| format!("Failed to write config file: {}", config_path.display()))
    }

    pub fn config_path() -> Result<PathBuf> {
        let config_dir = home_dir()
            .context("Failed to determine home directory")?
            .join(".config")
            .join("gitlogue");

        fs::create_dir_all(&config_dir).with_context(|| {
            format!(
                "Failed to create config directory: {}",
                config_dir.display()
            )
        })?;

        Ok(config_dir.join("config.toml"))
    }

    #[allow(dead_code)]
    pub fn themes_dir() -> Result<PathBuf> {
        let config_dir = home_dir()
            .context("Failed to determine home directory")?
            .join(".config")
            .join("gitlogue")
            .join("themes");

        fs::create_dir_all(&config_dir).with_context(|| {
            format!(
                "Failed to create themes directory: {}",
                config_dir.display()
            )
        })?;

        Ok(config_dir)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::time::{SystemTime, UNIX_EPOCH};

    struct TempHome {
        _override: super::test_home::Guard,
        path: PathBuf,
    }

    impl TempHome {
        fn new() -> Result<Self> {
            let path = env::temp_dir().join(format!(
                "gitlogue-config-tests-{}-{}",
                std::process::id(),
                SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos()
            ));

            fs::create_dir_all(&path)?;

            let _override = super::test_home::Guard::new(&path);
            Ok(Self { _override, path })
        }
    }

    impl Drop for TempHome {
        fn drop(&mut self) {
            let _ = fs::remove_dir_all(&self.path);
        }
    }

    #[test]
    fn home_dir_falls_back_to_dirs_home_dir_when_override_is_unset() {
        let _guard = super::test_home::Guard::no_override();
        assert_eq!(super::home_dir(), dirs::home_dir());
    }

    fn sample_config() -> Config {
        Config {
            theme: "nord".to_string(),
            speed: 12,
            background: false,
            order: "desc".to_string(),
            loop_playback: true,
            ignore_patterns: vec!["dist/**".to_string(), "*.png".to_string()],
            speed_rules: vec!["*.rs:10".to_string(), "*.md:40".to_string()],
        }
    }

    fn assert_config_eq(actual: &Config, expected: &Config) {
        assert_eq!(actual.theme, expected.theme);
        assert_eq!(actual.speed, expected.speed);
        assert_eq!(actual.background, expected.background);
        assert_eq!(actual.order, expected.order);
        assert_eq!(actual.loop_playback, expected.loop_playback);
        assert_eq!(actual.ignore_patterns, expected.ignore_patterns);
        assert_eq!(actual.speed_rules, expected.speed_rules);
    }

    #[test]
    fn load_returns_defaults_when_config_file_is_missing() -> Result<()> {
        let temp_home = TempHome::new()?;
        let config_path = Config::config_path()?;
        let config = Config::load()?;

        assert_eq!(
            config_path,
            temp_home.path.join(".config/gitlogue/config.toml")
        );
        assert!(!config_path.exists());
        assert_eq!(config.theme, "tokyo-night");
        assert_eq!(config.speed, 30);
        assert!(config.background);
        assert_eq!(config.order, "random");
        assert!(!config.loop_playback);
        assert!(config.ignore_patterns.is_empty());
        assert!(config.speed_rules.is_empty());

        Ok(())
    }

    #[test]
    fn load_fills_in_missing_fields_from_defaults() -> Result<()> {
        let _temp_home = TempHome::new()?;
        let config_path = Config::config_path()?;

        fs::write(&config_path, "theme = \"rose-pine\"\n")?;

        let config = Config::load()?;

        assert_eq!(config.theme, "rose-pine");
        assert_eq!(config.speed, 30);
        assert!(config.background);
        assert_eq!(config.order, "random");
        assert!(!config.loop_playback);
        assert!(config.ignore_patterns.is_empty());
        assert!(config.speed_rules.is_empty());

        Ok(())
    }

    #[test]
    fn load_accepts_legacy_loop_playback_key() -> Result<()> {
        let _temp_home = TempHome::new()?;
        let config_path = Config::config_path()?;

        fs::write(&config_path, "loop_playback = true\n")?;

        let config = Config::load()?;

        assert!(config.loop_playback);
        assert_eq!(config.theme, "tokyo-night");

        Ok(())
    }

    #[test]
    fn load_reports_parse_errors_with_config_path() -> Result<()> {
        let _temp_home = TempHome::new()?;
        let config_path = Config::config_path()?;

        fs::write(&config_path, "theme = [\n")?;

        let error = Config::load().unwrap_err().to_string();

        assert!(error.contains("Failed to parse config file"));
        assert!(error.contains(&config_path.display().to_string()));

        Ok(())
    }

    #[test]
    fn save_creates_commented_config_and_round_trips_values() -> Result<()> {
        let _temp_home = TempHome::new()?;
        let config = sample_config();
        let config_path = Config::config_path()?;

        config.save()?;

        let contents = fs::read_to_string(&config_path)?;

        assert!(contents.contains("# gitlogue configuration file"));
        assert!(contents.contains("theme = \"nord\""));
        assert!(contents.contains("ignore_patterns = [\"dist/**\", \"*.png\"]"));
        assert!(contents.contains("speed_rules = [\"*.rs:10\", \"*.md:40\"]"));

        let loaded = Config::load()?;
        assert_config_eq(&loaded, &config);

        Ok(())
    }

    #[test]
    fn save_writes_empty_arrays_for_default_collections() -> Result<()> {
        let _temp_home = TempHome::new()?;
        let config = Config::default();
        let config_path = Config::config_path()?;

        config.save()?;

        let contents = fs::read_to_string(&config_path)?;

        assert!(contents.contains("ignore_patterns = []"));
        assert!(contents.contains("speed_rules = []"));

        Ok(())
    }

    #[test]
    fn save_updates_existing_config_without_dropping_comments() -> Result<()> {
        let _temp_home = TempHome::new()?;
        let config_path = Config::config_path()?;
        let config = sample_config();

        fs::write(
            &config_path,
            "# top comment\n\
             theme = \"tokyo-night\"\n\
             # keep this comment\n\
             speed = 30\n\
             background = true\n\
             order = \"random\"\n\
             loop = false\n\
             ignore_patterns = [\"*.tmp\"]\n\
             speed_rules = [\"*.rs:30\"]\n",
        )?;

        config.save()?;

        let contents = fs::read_to_string(&config_path)?;

        assert!(contents.contains("# top comment"));
        assert!(contents.contains("# keep this comment"));
        assert!(contents.contains("theme = \"nord\""));

        let loaded = Config::load()?;
        assert_config_eq(&loaded, &config);

        Ok(())
    }

    #[test]
    fn save_reports_parse_errors_for_existing_invalid_config() -> Result<()> {
        let _temp_home = TempHome::new()?;
        let config_path = Config::config_path()?;

        fs::write(&config_path, "theme = [\n")?;

        let error = sample_config().save().unwrap_err().to_string();

        assert!(error.contains("Failed to parse config file"));
        assert!(error.contains(&config_path.display().to_string()));

        Ok(())
    }

    #[test]
    fn save_reports_read_errors_for_existing_config_directory() -> Result<()> {
        let temp_home = TempHome::new()?;
        let config_path = temp_home.path.join(".config/gitlogue/config.toml");

        fs::create_dir_all(&config_path)?;

        let error = sample_config().save().unwrap_err().to_string();

        assert!(error.contains("Failed to read config file"));
        assert!(error.contains(&config_path.display().to_string()));

        Ok(())
    }

    #[test]
    fn config_path_reports_create_dir_errors_with_target_path() -> Result<()> {
        let temp_home = TempHome::new()?;
        let config_dir = temp_home.path.join(".config/gitlogue");

        fs::create_dir_all(temp_home.path.join(".config"))?;
        fs::write(&config_dir, "occupied")?;

        let error = Config::config_path().unwrap_err().to_string();

        assert!(error.contains("Failed to create config directory"));
        assert!(error.contains(&config_dir.display().to_string()));

        Ok(())
    }

    #[test]
    fn themes_dir_reports_create_dir_errors_with_target_path() -> Result<()> {
        let temp_home = TempHome::new()?;
        let themes_dir = temp_home.path.join(".config/gitlogue/themes");

        fs::create_dir_all(temp_home.path.join(".config/gitlogue"))?;
        fs::write(&themes_dir, "occupied")?;

        let error = Config::themes_dir().unwrap_err().to_string();

        assert!(error.contains("Failed to create themes directory"));
        assert!(error.contains(&themes_dir.display().to_string()));

        Ok(())
    }

    #[test]
    fn themes_dir_is_created_under_the_config_home() -> Result<()> {
        let temp_home = TempHome::new()?;
        let themes_dir = Config::themes_dir()?;

        assert_eq!(themes_dir, temp_home.path.join(".config/gitlogue/themes"));
        assert!(themes_dir.is_dir());

        Ok(())
    }
}