diff --git a/src/main.rs b/src/main.rs index 059d8e5..5d75417 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,11 @@ struct MuteArgs {} /// Get the current volume #[derive(Debug, Clone, Copy, PartialEq, FromArgs)] #[argh(subcommand, name = "get")] -struct GetArgs {} +struct GetArgs { + /// output bare value + #[argh(switch, short='b')] + bare: bool, +} /// Set the volume #[derive(Debug, Clone, Copy, PartialEq, FromArgs)] @@ -59,21 +63,30 @@ fn main() -> Result<(), Box> { 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 volume_step = pulse::volume::Volume(pulse::volume::Volume::NORMAL.0 / 20); match args.command { 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(_) => { - 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(_) => { handler.set_device_mute_by_index(default_device.index, !muted); } - Subcommand::Get(_) => { - println!("Volume: {}", default_device.volume.avg()); + 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()); + } return Ok(()); } Subcommand::Set(SetArgs { volume }) => {