matrixr 1.1.0

Animated Matrix-style terminal screensaver
Documentation
use std::process::Command;

use super::AppError;

pub(super) struct ConsoleFontGuard {
    active: bool,
}

impl ConsoleFontGuard {
    pub(super) fn new(console_mode: bool) -> Result<Self, AppError> {
        if !console_mode {
            return Ok(Self { active: false });
        }

        #[cfg(target_os = "linux")]
        {
            let status = Command::new("setfont").arg("matrix").status();
            match status {
                Ok(result) if result.success() => Ok(Self { active: true }),
                Ok(_) => Err(AppError::Message(
                    "There was an error running setfont matrix.".to_string(),
                )),
                Err(err) => Err(AppError::Message(format!(
                    "Failed to run setfont matrix: {err}"
                ))),
            }
        }

        #[cfg(not(target_os = "linux"))]
        {
            Err(AppError::Message(
                "Option -l is only supported on Linux consoles.".to_string(),
            ))
        }
    }
}

impl Drop for ConsoleFontGuard {
    fn drop(&mut self) {
        if self.active {
            let _ = Command::new("setfont").status();
        }
    }
}