omatrix 0.1.1

A Matrix-style terminal rain animation for Omarchy users
Documentation
use std::{
    env, fs,
    path::PathBuf,
    thread,
    time::{Duration, SystemTime, UNIX_EPOCH},
};

use anyhow::Result;
use omatrix::theme::{Theme, ThemeWatcher};

#[test]
fn matrix_fallback_is_dark_and_green() {
    let theme = Theme::matrix_fallback();

    assert!(!theme.is_light);
    assert!(theme.background.g <= 0x10);
    assert!(theme.accent.g > theme.accent.r);
    assert!(theme.accent.g > theme.accent.b);
    assert_eq!(theme.active_border, theme.accent);
}

#[test]
fn parses_omarchy_theme_toml() -> Result<()> {
    let theme = Theme::from_toml_str(&sample_theme("#1a1b26", "#a9b1d6", "#7aa2f7"))?;
    assert_eq!(theme.background.r, 0x1a);
    assert_eq!(theme.foreground.b, 0xd6);
    assert_eq!(theme.accent.g, 0xa2);
    assert!(!theme.is_light);
    Ok(())
}

#[test]
fn default_path_uses_omarchy_state_directory() {
    let home = env::var_os("HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("."));

    assert_eq!(
        Theme::default_path(),
        home.join(".local/state/omarchy/current/theme/colors.toml")
    );
}

#[test]
fn watcher_picks_up_theme_changes() -> Result<()> {
    let path = unique_temp_path("omatrix-theme");
    fs::write(&path, sample_theme("#1a1b26", "#a9b1d6", "#7aa2f7"))?;

    let mut watcher = ThemeWatcher::new(path.clone());
    assert!(!watcher.load()?.is_light);

    thread::sleep(Duration::from_millis(25));
    fs::write(&path, sample_theme("#ffffff", "#000000", "#205ea6"))?;

    let next = watcher.poll()?.expect("expected a theme reload");
    assert!(next.is_light);

    let _ = fs::remove_file(path);
    Ok(())
}

fn sample_theme(background: &str, foreground: &str, accent: &str) -> String {
    format!(
        "accent = \"{accent}\"\ncursor = \"{foreground}\"\nforeground = \"{foreground}\"\nbackground = \"{background}\"\nselection_foreground = \"{foreground}\"\nselection_background = \"{accent}\"\ncolor0 = \"{background}\"\ncolor1 = \"{foreground}\"\ncolor2 = \"{accent}\"\ncolor3 = \"{accent}\"\ncolor4 = \"{accent}\"\ncolor5 = \"{accent}\"\ncolor6 = \"{accent}\"\ncolor7 = \"{foreground}\"\ncolor8 = \"{background}\"\ncolor9 = \"{foreground}\"\ncolor10 = \"{accent}\"\ncolor11 = \"{accent}\"\ncolor12 = \"{accent}\"\ncolor13 = \"{accent}\"\ncolor14 = \"{accent}\"\ncolor15 = \"{foreground}\"\n"
    )
}

fn unique_temp_path(prefix: &str) -> PathBuf {
    let nanos = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("system clock should be after unix epoch")
        .as_nanos();
    std::env::temp_dir().join(format!("{prefix}-{nanos}.toml"))
}