Skip to content

Commit

Permalink
trim sysfs string, interface name option
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Oct 18, 2024
1 parent a9c37d4 commit c64fc0f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Changelog

## [2.0.0]
## [2.0.0] - 2024-10-18

Big release after almost two years since the first commit: `cyme` is now native Rust\* by default! Thanks to support from [nusb](https://github.com/kevinmehall/nusb), the system profiler is much improved for all platforms.

Expand Down
15 changes: 9 additions & 6 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,11 @@ impl Block<InterfaceBlocks, Interface> for InterfaceBlocks {

fn len(&self, d: &[&Interface]) -> usize {
match self {
InterfaceBlocks::Name => d.iter().map(|d| d.name.len()).max().unwrap_or(0),
InterfaceBlocks::Name => d
.iter()
.flat_map(|d| d.name.as_ref().map(|s| s.width()))
.max()
.unwrap_or(0),
InterfaceBlocks::BaseClass => d
.iter()
.map(|d| d.class.to_string().len())
Expand Down Expand Up @@ -1437,11 +1441,10 @@ impl Block<InterfaceBlocks, Interface> for InterfaceBlocks {
) -> Option<String> {
match self {
InterfaceBlocks::Number => Some(format!("{:2}", interface.number)),
InterfaceBlocks::Name => Some(format!(
"{:pad$}",
interface.name,
pad = pad.get(self).unwrap_or(&0)
)),
InterfaceBlocks::Name => Some(match interface.name.as_ref() {
Some(v) => format!("{:pad$}", v, pad = pad.get(self).unwrap_or(&0)),
None => format!("{:pad$}", "-", pad = pad.get(self).unwrap_or(&0)),
}),
InterfaceBlocks::NumEndpoints => Some(format!("{:2}", interface.endpoints.len())),
InterfaceBlocks::PortPath => Some(format!(
"{:pad$}",
Expand Down
2 changes: 1 addition & 1 deletion src/lsusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ fn dump_interface(interface: &Interface, indent: usize) {
dump_value_string(
interface.string_index,
"iInterface",
&interface.name,
interface.name.as_ref().unwrap_or(&String::new()),
indent + 2,
LSUSB_DUMP_WIDTH,
);
Expand Down
2 changes: 1 addition & 1 deletion src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ fn get_sysfs_string(sysfs_name: &str, attr: &str) -> Option<String> {
#[cfg(target_os = "linux")]
return std::fs::read_to_string(format!("{}{}/{}", SYSFS_USB_PREFIX, sysfs_name, attr))
.ok()
.map(|s| s.to_string());
.map(|s| s.trim().to_string());
#[cfg(not(target_os = "linux"))]
return None;
}
Expand Down
12 changes: 5 additions & 7 deletions src/profiler/libusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,11 @@ impl LibUsbProfiler {
);

let interface = usb::Interface {
name: get_sysfs_string(&path, "interface")
.or_else(|| {
interface_desc
.description_string_index()
.and_then(|i| handle.get_descriptor_string(i))
})
.unwrap_or_default(),
name: get_sysfs_string(&path, "interface").or_else(|| {
interface_desc
.description_string_index()
.and_then(|i| handle.get_descriptor_string(i))
}),
string_index: interface_desc.description_string_index().unwrap_or(0),
number: interface_desc.interface_number(),
class: usb::BaseClass::from(interface_desc.class_code()),
Expand Down
12 changes: 5 additions & 7 deletions src/profiler/nusb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,11 @@ impl NusbProfiler {
.collect::<Vec<u8>>();

let interface = usb::Interface {
name: get_sysfs_string(&path, "interface")
.or_else(|| {
interface_alt
.string_index()
.and_then(|i| device.get_descriptor_string(i))
})
.unwrap_or_default(),
name: get_sysfs_string(&path, "interface").or_else(|| {
interface_alt
.string_index()
.and_then(|i| device.get_descriptor_string(i))
}),
string_index: interface_alt.string_index().unwrap_or(0),
number: interface_alt.interface_number(),
class: usb::BaseClass::from(interface_alt.class()),
Expand Down
2 changes: 1 addition & 1 deletion src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ impl Endpoint {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Interface {
/// Name from descriptor
pub name: String,
pub name: Option<String>,
/// Index of name string in descriptor - only useful for lsusb verbose print
#[serde(default)]
pub string_index: u8,
Expand Down

0 comments on commit c64fc0f

Please sign in to comment.