//! TODO: Module level docs Volume use clap::Subcommand; use core::fmt; use std::fmt::{Display, Result}; use super::Direction; /// Defines the subcommands that can be used with the volume utility. #[derive(Subcommand)] pub enum Command { /// Adjust volume Up/Down by 5% Step { /// The direction to adjust the volume #[arg(value_enum)] directon: Direction, }, /// Adjust the volume to 0% or last volume ToggleMute { /// The value to set the volume to #[arg()] volume: u8, }, /// Get the name of the current icon CurrentIcon, /// Get the current volume percent CurrentValue, } /// Parses a command and passes it's arguments (if applicable) to the relevant method. pub fn parse_command(command: Command) { match command { Command::CurrentValue => println!("{}", get_current_value()), Command::CurrentIcon => println!("{}", get_current_icon()), Command::Step { directon } => step(directon), Command::ToggleMute { volume } => toggle_mute(volume), } } /// Increment or Deincrement the system volume by 5% fn step(directon: Direction) { //onscroll set(5, directon); } /// Takes [u8] (saved_volume) and replaces the current volume with the value. /// The current volume is stored within the `mute_save` eww variable. fn toggle_mute(mut saved_volume: u8) { //onclick use super::update_eww_var; let volume = get_current_value(); if saved_volume > 0 && volume > 0 { saved_volume = 0; } set(saved_volume, Direction::Absolute); update_eww_var("mute_save", &volume.to_string()); std::process::exit(0); } /// Returns the current [Level] corresponding to the current volume value. fn get_current_icon() -> Level { //for path var in image (add to defpoll) match get_current_value() { value if value <= 0 => Level::Mute, value if value <= 25 => Level::Low, value if value <= 75 => Level::Mid, _ => Level::High, } } /// Returns the current volume value fn get_current_value() -> u8 { //for value in circular-progrss (add to defpoll) use std::process::Command; let value = Command::new("amixer") .arg("sget").arg("Master") .output().unwrap().stdout; let value = String::from_utf8(value).unwrap(); let value: Vec<&str> = value.split("[").collect(); let value = value[3].trim(); let value = value.get(0..value.len() -2).unwrap().replace("%", ""); value.parse().unwrap() } /// Sets the current volume to [u8] (value) with respect to the [Direction]. /// If direction = Up | Down : the volume steps by the value. /// If direction = Absolute : the volume is set to the value%. fn set(value: u8, directon: Direction) { use std::process::Command; let mut value = value.to_string(); value.push('%'); match directon { Direction::Up => value.push('+'), Direction::Down => value.push('-'), Direction::Absolute => (), } let _ = Command::new("amixer") .arg("sset").arg("Master").arg(value).arg("> /dev/null") .spawn(); } /// Defines the levels the volume can be. /// These levels correspond to the icons that can be displayed. enum Level { High, Mid, Low, Mute, } impl Display for Level { /// Formats [Level] to a lowercase string. fn fmt(&self, formatter: &mut fmt::Formatter) -> Result { match self { Level::Mute => write!(formatter, "mute"), Level::Low => write!(formatter, "low"), Level::Mid => write!(formatter, "mid"), Level::High => write!(formatter, "high"), } } }