Add --ignore option

Close #46
This commit is contained in:
Takashi Kokubun 2021-12-27 22:13:14 -08:00
parent 9f69ef8bb6
commit a5f2185766
No known key found for this signature in database
GPG Key ID: 6FFC433B12EE23DD
2 changed files with 15 additions and 6 deletions

View File

@ -50,7 +50,7 @@ pub fn output_device() -> Result<VirtualDevice, Box<dyn Error>> {
Ok(device)
}
pub fn input_devices(device_opts: &Vec<String>) -> Result<Vec<Device>, Box<dyn Error>> {
pub fn input_devices(device_opts: &Vec<String>, ignore_opts: &Vec<String>) -> Result<Vec<Device>, Box<dyn Error>> {
let mut path_devices = list_devices()?;
let mut paths: Vec<String> = path_devices.keys().map(|e| e.clone()).collect();
paths.sort_by(|a, b| device_index(a).partial_cmp(&device_index(b)).unwrap());
@ -65,22 +65,30 @@ pub fn input_devices(device_opts: &Vec<String>) -> Result<Vec<Device>, Box<dyn E
println!("{}", SEPARATOR);
if device_opts.is_empty() {
println!("Selected keyboards automatically since --device options weren't specified:");
print!("Selected keyboards automatically since --device options weren't specified");
} else {
println!("Selected devices matching {:?}:", device_opts);
print!("Selected devices matching {:?}", device_opts);
};
if ignore_opts.is_empty() {
println!(":")
} else {
println!(", ignoring {:?}:", ignore_opts);
}
for path in &paths {
if let Some(device) = path_devices.get(path) {
let matched = if device_opts.is_empty() {
is_keyboard(device)
} else {
match_device(path, device, device_opts)
};
} && (ignore_opts.is_empty() || !match_device(path, device, ignore_opts));
if !matched {
path_devices.remove(path);
}
}
}
if path_devices.is_empty() {
return Err("No device was selected!".into());
}
println!("{}", SEPARATOR);
for (path, device) in path_devices.iter() {

View File

@ -28,7 +28,8 @@ fn main() {
let program = argv[0].clone();
let mut opts = Options::new();
opts.optmulti("", "device", "device name or path", "NAME");
opts.optmulti("", "device", "Include device name or path", "NAME");
opts.optmulti("", "ignore", "Exclude device name or path", "NAME");
opts.optflag("h", "help", "print this help menu");
let args = match opts.parse(&argv[1..]) {
@ -49,7 +50,7 @@ fn main() {
Err(e) => abort(&format!("Failed to load config '{}': {}", filename, e)),
};
let input_devices = match input_devices(&args.opt_strs("device")) {
let input_devices = match input_devices(&args.opt_strs("device"), &args.opt_strs("ignore")) {
Ok(input_devices) => input_devices,
Err(e) => abort(&format!("Failed to prepare input devices: {}", e)),
};