Skip to content

Commit 5bd0c23

Browse files
authored
Merge pull request #19 from acheronfail/feat/fmt-alignment
feat: impl padding for Display trait for ByteSize
2 parents 9d8e13f + f1fe355 commit 5bd0c23

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Diff for: src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn to_string(bytes: u64, si_prefix: bool) -> String {
205205

206206
impl Display for ByteSize {
207207
fn fmt(&self, f: &mut Formatter) -> Result {
208-
write!(f, "{}", to_string(self.0, false))
208+
f.pad(&to_string(self.0, false))
209209
}
210210
}
211211

@@ -318,6 +318,18 @@ mod tests {
318318
assert_display("609.0 PB", ByteSize::pb(609));
319319
}
320320

321+
#[test]
322+
fn test_display_alignment() {
323+
assert_eq!("|357 B |", format!("|{:10}|", ByteSize(357)));
324+
assert_eq!("| 357 B|", format!("|{:>10}|", ByteSize(357)));
325+
assert_eq!("|357 B |", format!("|{:<10}|", ByteSize(357)));
326+
assert_eq!("| 357 B |", format!("|{:^10}|", ByteSize(357)));
327+
328+
assert_eq!("|-----357 B|", format!("|{:->10}|", ByteSize(357)));
329+
assert_eq!("|357 B-----|", format!("|{:-<10}|", ByteSize(357)));
330+
assert_eq!("|--357 B---|", format!("|{:-^10}|", ByteSize(357)));
331+
}
332+
321333
fn assert_to_string(expected: &str, b: ByteSize, si: bool) {
322334
assert_eq!(expected.to_string(), b.to_string_as(si));
323335
}

0 commit comments

Comments
 (0)