Skip to content

Commit

Permalink
[refactor] keep CLI device names in an easy to read Vec
Browse files Browse the repository at this point in the history
Instead of reading from the clap arguments, store the unwrapped device list into
a Vec (potentially empty). This is convenient to check if a given device
name is one of the names given by the user.

`keyboard_devices` has also been updated to use this new variable.
  • Loading branch information
ajanon committed May 28, 2023
1 parent 3b19fc3 commit 82f9617
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
};
}

let arg_devices: Vec<&str> = args.values_of("device").unwrap_or_default().collect();

let keyboard_devices: Vec<Device> = {
if let Some(arg_devices) = args.values_of("device") {
// for device in arg_devices {
// let device_path = Path::new(device);
// if let Ok(device_to_use) = Device::open(device_path) {
// log::info!("Using device: {}", device_to_use.name().unwrap_or(device));
// keyboard_devices.push(device_to_use);
// }
// }
let arg_devices = arg_devices.collect::<Vec<&str>>();
if arg_devices.is_empty() {
log::trace!("Attempting to find all keyboard file descriptors.");
evdev::enumerate().map(|(_, device)| device).filter(check_device_is_keyboard).collect()
} else {
evdev::enumerate()
.map(|(_, device)| device)
.filter(|device| arg_devices.contains(&device.name().unwrap_or("")))
.collect()
} else {
log::trace!("Attempting to find all keyboard file descriptors.");
evdev::enumerate().map(|(_, device)| device).filter(check_device_is_keyboard).collect()
}
};

Expand Down

0 comments on commit 82f9617

Please sign in to comment.