yucky-utils/src/utils/volume.rs

109 lines
2.7 KiB
Rust
Raw Normal View History

2024-07-21 22:26:08 +00:00
use core::fmt;
use std::fmt::{Display, Result};
use clap::Subcommand;
2024-07-21 19:58:56 +00:00
use super::Direction;
2024-07-21 22:26:08 +00:00
#[derive(Subcommand)]
#[command(about = "", long_about = None)] //Volume command usage
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,
}
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),
}
}
fn step(directon: Direction) { //onscroll
2024-07-21 19:58:56 +00:00
set(5, directon);
}
2024-07-21 22:26:08 +00:00
fn toggle_mute(saved_volume: u8) { //onclick
2024-07-21 19:58:56 +00:00
use super::update_eww_var;
let volume = get_current_value();
set(saved_volume, Direction::Absolute);
update_eww_var("mute_save", &volume.to_string());
std::process::exit(0);
}
2024-07-21 22:26:08 +00:00
fn get_current_icon() -> Level { //for path var in image (add to defpoll)
2024-07-21 19:58:56 +00:00
match get_current_value() {
value if value <= 0 => Level::Mute,
value if value <= 25 => Level::Low,
value if value <= 75 => Level::Mid,
_ => Level::High,
}
}
2024-07-21 22:26:08 +00:00
fn get_current_value() -> u8 { //for value in circular-progrss (add to defpoll)
use std::process::Command;
2024-07-21 19:58:56 +00:00
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();
2024-07-21 22:26:08 +00:00
let value = value[3].trim();
let value = value.get(0..value.len() -2).unwrap().replace("%", "");
value.parse().unwrap()
2024-07-21 19:58:56 +00:00
}
fn set(value: u8, directon: Direction) {
2024-07-21 22:26:08 +00:00
use std::process::Command;
2024-07-21 19:58:56 +00:00
let mut value = value.to_string();
value.push('%');
match directon {
Direction::Up => value.push('+'),
Direction::Down => value.push('-'),
Direction::Absolute => (),
}
2024-07-21 22:26:08 +00:00
let _ = Command::new("amixer")
2024-07-21 19:58:56 +00:00
.arg("sset").arg("Master").arg(value).arg("> /dev/null")
.spawn();
}
enum Level {
High,
Mid,
Low,
Mute,
}
2024-07-21 22:26:08 +00:00
impl Display for Level {
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"),
}
}
}