Skip to content

Commit

Permalink
fix numerical width formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Feb 27, 2023
1 parent 84de41a commit e11d811
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,6 @@ impl Block<ConfigurationBlocks, USBConfiguration> for ConfigurationBlocks {
.map(|d| d.attributes_string().len())
.max()
.unwrap_or(0),
// two possible icons
ConfigurationBlocks::IconAttributes => 2,
_ => self.block_length().len()
}
}
Expand Down Expand Up @@ -781,7 +779,7 @@ impl Block<ConfigurationBlocks, USBConfiguration> for ConfigurationBlocks {
ConfigurationBlocks::IconAttributes => Some(format!(
"{:pad$}",
attributes_to_icons(&config.attributes, settings),
pad = pad.get(self).unwrap_or(&3)
pad = pad.get(self).unwrap_or(&0)
)),
// _ => None,
}
Expand All @@ -807,6 +805,8 @@ impl Block<ConfigurationBlocks, USBConfiguration> for ConfigurationBlocks {
ConfigurationBlocks::Number => BlockLength::Fixed(2),
ConfigurationBlocks::NumInterfaces => BlockLength::Fixed(2),
ConfigurationBlocks::MaxPower => BlockLength::Fixed(6),
// two possible icons and a space between
ConfigurationBlocks::IconAttributes => BlockLength::Fixed(3),
_ => BlockLength::Variable(self.heading().len()),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/system_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ impl fmt::Display for DeviceSpeed {
if f.alternate() {
write!(f, "{}", v)
} else {
write!(f, "{:3} {:3}", "-", "-")
write!(f, "{:5} {:4}", "-", "-")
}
}
}
Expand Down
32 changes: 24 additions & 8 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,36 @@ pub struct NumericalUnit<T> {

impl fmt::Display for NumericalUnit<u32> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:} {:}", self.value, self.unit)
if let Some(width) = f.width() {
let actual_width = width - self.unit.len() - 1;
write!(f, "{:actual_width$} {:}", self.value, self.unit)
} else {
write!(f, "{:} {:}", self.value, self.unit)
}
}
}

impl fmt::Display for NumericalUnit<f32> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// If we received a precision, we use it.
write!(
f,
"{1:.*} {2}",
f.precision().unwrap_or(2),
self.value,
self.unit
)
if let Some(width) = f.width() {
let actual_width = width - self.unit.len() - 1;
write!(
f,
"{1:actual_width$.*} {2}",
f.precision().unwrap_or(2),
self.value,
self.unit
)
} else {
write!(
f,
"{1:.*} {2}",
f.precision().unwrap_or(2),
self.value,
self.unit
)
}
}
}

Expand Down

0 comments on commit e11d811

Please sign in to comment.