omatrix 0.1.1

A Matrix-style terminal rain animation for Omarchy users
Documentation
use std::{ffi::OsString, path::PathBuf};

use anyhow::Result;
use clap::{Arg, Command, value_parser};

use crate::theme::Theme;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Cli {
    pub theme_path: PathBuf,
    pub theme_path_was_explicit: bool,
    pub seed: Option<u64>,
}

pub fn parse() -> Result<Cli> {
    parse_from(std::env::args_os())
}

pub fn parse_from<I, T>(args: I) -> Result<Cli>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    let default_theme = Theme::default_path().to_string_lossy().into_owned();
    let matches = Command::new("omatrix")
        .about("A Matrix-style terminal rain that tracks the active Omarchy theme")
        .arg(Arg::new("theme").long("theme").value_name("PATH"))
        .arg(
            Arg::new("seed")
                .long("seed")
                .value_name("N")
                .value_parser(value_parser!(u64)),
        )
        .get_matches_from(args);

    let theme_path_was_explicit = matches.get_one::<String>("theme").is_some();

    Ok(Cli {
        theme_path: PathBuf::from(
            matches
                .get_one::<String>("theme")
                .map(String::as_str)
                .unwrap_or(default_theme.as_str()),
        ),
        theme_path_was_explicit,
        seed: matches.get_one::<u64>("seed").copied(),
    })
}

#[cfg(test)]
mod tests {
    use super::parse_from;

    #[test]
    fn tracks_when_theme_path_is_explicit() {
        let cli = parse_from(["omatrix", "--theme", "/tmp/theme.toml"]).unwrap();

        assert!(cli.theme_path_was_explicit);
    }

    #[test]
    fn default_theme_path_is_not_explicit() {
        let cli = parse_from(["omatrix"]).unwrap();

        assert!(!cli.theme_path_was_explicit);
    }
}