Clean up code

extract functions
rename exec functions to separate from Exec functionality
This commit is contained in:
GHOSCHT 2024-03-14 13:49:39 +01:00
parent f2cb1defa0
commit 7c842c457b
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82
2 changed files with 65 additions and 65 deletions

View file

@ -28,7 +28,7 @@ fn exec(in_channel: Receiver<KeyEvent>, config: Arc<Config>) {
}
}
fn exec_binary_functionality<F>(player_ids: &Option<Vec<String>>, state: u8, exec_closure: F)
fn execute_binary_functionality<F>(player_ids: &Option<Vec<String>>, state: u8, exec_closure: F)
where
F: Fn(Option<&str>) -> (),
{
@ -50,62 +50,9 @@ fn eval(key: NanoKeys, state: u8, config: &Arc<Config>) {
);
for action in actions {
match action {
KeyMapVariant::Mpris { ids, action } => match action {
MprisAction::Play => {
exec_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::play(player_id)
})
}
MprisAction::Pause => {
exec_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::pause(player_id)
})
}
MprisAction::PlayPause => {
exec_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::play_pause(player_id)
})
}
MprisAction::Stop => {
exec_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::stop(player_id)
})
}
MprisAction::Next => {
exec_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::next(player_id)
})
}
MprisAction::Previous => {
exec_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::previous(player_id)
})
}
MprisAction::Volume => {
let volume = state as f64 / 100.0;
match ids {
Some(some_ids) => some_ids.iter().for_each(|id| {
mpris_client::set_volume(Option::Some(id), volume)
}),
None => mpris_client::set_volume(Option::None, volume),
}
}
},
KeyMapVariant::PipeWire { ids } => {}
KeyMapVariant::Exec { command, args } => {
let output: Result<Output, std::io::Error> = match args {
Some(some_args) => Command::new(command).args(some_args).output(),
None => Command::new(command).output(),
};
match output {
Ok(out) => {
info!("{:?}", out);
}
Err(err) => {
error!("{:?}", err);
}
}
}
KeyMapVariant::Mpris { ids, action } => parse_mpris_action(state, ids, action),
KeyMapVariant::PulseAudio { ids } => {}
KeyMapVariant::Exec { command, args } => parse_exec_action(command, args),
}
}
}
@ -115,6 +62,59 @@ fn eval(key: NanoKeys, state: u8, config: &Arc<Config>) {
}
}
fn parse_mpris_action(state: u8, ids: &Option<Vec<String>>, action: &MprisAction) {
match action {
MprisAction::Play => execute_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::play(player_id)
}),
MprisAction::Pause => {
execute_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::pause(player_id)
})
}
MprisAction::PlayPause => {
execute_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::play_pause(player_id)
})
}
MprisAction::Stop => execute_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::stop(player_id)
}),
MprisAction::Next => execute_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::next(player_id)
}),
MprisAction::Previous => {
execute_binary_functionality(ids, state, |player_id: Option<&str>| {
mpris_client::previous(player_id)
})
}
MprisAction::Volume => {
let volume = state as f64 / 100.0;
match ids {
Some(some_ids) => some_ids
.iter()
.for_each(|id| mpris_client::set_volume(Option::Some(id), volume)),
None => mpris_client::set_volume(Option::None, volume),
}
}
}
}
fn parse_exec_action(command: &str, args: &Option<Vec<String>>) {
let output: Result<Output, std::io::Error> = match args {
Some(some_args) => Command::new(command).args(some_args).output(),
None => Command::new(command).output(),
};
match output {
Ok(out) => {
info!("{:?}", out);
}
Err(err) => {
error!("{:?}", err);
}
}
}
fn is_logical_true(state: u8) -> bool {
return state == 127;
}

View file

@ -48,7 +48,7 @@ where
}
}
fn exec_functionality<F>(player_id: Option<&str>, exec_closure: F)
fn execute_functionality<F>(player_id: Option<&str>, exec_closure: F)
where
F: Fn(&Player) -> (),
{
@ -59,43 +59,43 @@ where
}
pub fn play(player_id: Option<&str>) -> () {
exec_functionality(player_id, |player: &Player| {
execute_functionality(player_id, |player: &Player| {
let _ = player.play();
});
}
pub fn pause(player_id: Option<&str>) -> () {
exec_functionality(player_id, |player: &Player| {
execute_functionality(player_id, |player: &Player| {
let _ = player.pause();
});
}
pub fn play_pause(player_id: Option<&str>) -> () {
exec_functionality(player_id, |player: &Player| {
execute_functionality(player_id, |player: &Player| {
let _ = player.play_pause();
});
}
pub fn stop(player_id: Option<&str>) -> () {
exec_functionality(player_id, |player: &Player| {
execute_functionality(player_id, |player: &Player| {
let _ = player.stop();
});
}
pub fn next(player_id: Option<&str>) -> () {
exec_functionality(player_id, |player: &Player| {
execute_functionality(player_id, |player: &Player| {
let _ = player.next();
});
}
pub fn previous(player_id: Option<&str>) -> () {
exec_functionality(player_id, |player: &Player| {
execute_functionality(player_id, |player: &Player| {
let _ = player.previous();
});
}
pub fn set_volume(player_id: Option<&str>, volume: f64) -> () {
exec_functionality(player_id, |player: &Player| {
execute_functionality(player_id, |player: &Player| {
let _ = player.set_volume(volume);
});
}