Skip to content

Commit

Permalink
pass filter to lsusb compat mode; correct padding
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Nov 16, 2022
1 parent cd35576 commit de543fc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ impl Blocks {
Blocks::TreePositions => Some(format!("{:pad$}", format!("{:}", d.location_id.tree_positions.iter().format("╌")), pad=pad.tree_positions)),
Blocks::BusPower => Some(match d.bus_power {
Some(v) => format!("{:3} mA", v),
None => format!("{:>3}", "-"),
None => format!("{:>6}", "-"),
}),
Blocks::BusPowerUsed => Some(match d.bus_power_used {
Some(v) => format!("{:3} mA", v),
None => format!("{:>3}", "-"),
None => format!("{:>6}", "-"),
}),
Blocks::ExtraCurrentUsed => Some(match d.extra_current_used {
Some(v) => format!("{:3} mA", v),
None => format!("{:>3}", "-"),
None => format!("{:>6}", "-"),
}),
Blocks::Bcd => Some(match d.bcd_device {
Some(v) => format!("{:>5.2}", v),
Expand Down
16 changes: 15 additions & 1 deletion src/lsusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct UsbDevice<'a> {
timeout: Duration
}

pub fn lsusb_verbose() -> libusb::Result<()> {
pub fn lsusb_verbose(filter: &Option<system_profiler::USBFilter>) -> libusb::Result<()> {
let timeout = Duration::from_secs(1);

let context = libusb::Context::new()?;
Expand All @@ -19,6 +19,20 @@ pub fn lsusb_verbose() -> libusb::Result<()> {
Err(_) => continue
};

if let Some(f) = filter {
if let Some(fvid) = f.vid {
if device_desc.vendor_id() != fvid {
continue;
}
}

if let Some(fpid) = f.pid {
if device_desc.product_id() != fpid {
continue;
}
}
}

let mut usb_device = {
match device.open() {
Ok(h) => {
Expand Down
28 changes: 21 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ struct Args {
#[arg(long, default_value_t = false)]
base10: bool,

/// Output as json format
/// Output as json format after filters applied
#[arg(long, default_value_t = false)]
json: bool,

Expand Down Expand Up @@ -131,14 +131,22 @@ fn abort_not_libusb() {
}
}

fn print_flat_lsusb(devices: &Vec<&system_profiler::USBDevice>) {
for d in devices {
println!("{:}", d);
fn print_flat_lsusb(devices: &Vec<&system_profiler::USBDevice>, filter: &Option<system_profiler::USBFilter>) {
if let Some(f) = filter {
for d in devices {
if f.is_match(&d) {
println!("{:}", d);
}
}
} else {
for d in devices {
println!("{:}", d);
}
}
}

fn main() {
let args = Args::parse();
let mut args = Args::parse();

match args.debug {
0 => (),
Expand Down Expand Up @@ -217,6 +225,12 @@ fn main() {
..Default::default()
};

// TODO verbose only supported by lsusb mode at the moment
if args.lsusb_verbose && !args.lsusb {
eprintln!("Forcing '--lsusb' compatibility mode, supply --lsusb to avoid this");
args.lsusb = true;
}

if args.json {
println!("{}", serde_json::to_string_pretty(&sp_usb).unwrap());
} else if !(args.lsusb_tree || args.tree) {
Expand All @@ -229,11 +243,11 @@ fn main() {
if args.lsusb_verbose {
abort_not_libusb();
#[cfg(feature = "libusb")]
lsusb::lsusb_verbose().unwrap_or_else(|e| {
lsusb::lsusb_verbose(&filter).unwrap_or_else(|e| {
eprintexit!(Error::new(ErrorKind::Other, format!("Failed to use lsusb verbose mode: {}", e)));
});
} else {
print_flat_lsusb(&devs);
print_flat_lsusb(&devs, &filter);
}
} else {
app::print_flattened_devices(&devs, &blocks, &print_settings);
Expand Down

0 comments on commit de543fc

Please sign in to comment.