Add flag to output bare percentage

This commit is contained in:
Erwin Boskma 2023-07-03 14:50:35 +02:00
parent ad5eb2b935
commit 8af15ab62d
Signed by: erwin
SSH key fingerprint: SHA256:9LmFDe1C6jSrEyqxxvX8NtJBmcbB105XoqyUZF092bg

View file

@ -39,7 +39,11 @@ struct MuteArgs {}
/// Get the current volume /// Get the current volume
#[derive(Debug, Clone, Copy, PartialEq, FromArgs)] #[derive(Debug, Clone, Copy, PartialEq, FromArgs)]
#[argh(subcommand, name = "get")] #[argh(subcommand, name = "get")]
struct GetArgs {} struct GetArgs {
/// output bare value
#[argh(switch, short='b')]
bare: bool,
}
/// Set the volume /// Set the volume
#[derive(Debug, Clone, Copy, PartialEq, FromArgs)] #[derive(Debug, Clone, Copy, PartialEq, FromArgs)]
@ -59,21 +63,30 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut handler = SinkController::create()?; let mut handler = SinkController::create()?;
let default_device = handler.get_default_device()?; let mut default_device = handler.get_default_device()?;
let muted = default_device.mute; let muted = default_device.mute;
let volume_step = pulse::volume::Volume(pulse::volume::Volume::NORMAL.0 / 20);
match args.command { match args.command {
Subcommand::Up(_) => { Subcommand::Up(_) => {
handler.increase_device_volume_by_percent(default_device.index, 0.05); if default_device.volume.avg().0 < pulse::volume::Volume::NORMAL.0 {
default_device.volume.inc_clamp(volume_step, pulse::volume::Volume::NORMAL);
handler.set_device_volume_by_index(default_device.index, &default_device.volume);
}
} }
Subcommand::Down(_) => { Subcommand::Down(_) => {
handler.decrease_device_volume_by_percent(default_device.index, 0.05); default_device.volume.decrease(volume_step);
handler.set_device_volume_by_index(default_device.index, &default_device.volume);
} }
Subcommand::Mute(_) => { Subcommand::Mute(_) => {
handler.set_device_mute_by_index(default_device.index, !muted); handler.set_device_mute_by_index(default_device.index, !muted);
} }
Subcommand::Get(_) => { Subcommand::Get(GetArgs { bare }) => {
if bare {
print!("{}", ((default_device.volume.avg().0 as f64 / pulse::volume::Volume::NORMAL.0 as f64) * 100.0).round());
} else {
println!("Volume: {}", default_device.volume.avg()); println!("Volume: {}", default_device.volume.avg());
}
return Ok(()); return Ok(());
} }
Subcommand::Set(SetArgs { volume }) => { Subcommand::Set(SetArgs { volume }) => {