Skip to content

Commit a4bcdb5

Browse files
committed
fix missing suffix in output
1 parent 02768bc commit a4bcdb5

2 files changed

Lines changed: 29 additions & 12 deletions

File tree

src/uu/df/src/blocks.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,21 @@ pub(crate) enum BlockSize {
131131
///
132132
/// The number must be positive.
133133
Bytes(u64),
134+
/// for when we need to display suffix along with blocksize
135+
PrefixedBytes(u64, String),
134136
}
135137

136138
impl BlockSize {
137139
/// Returns the associated value
138140
pub(crate) fn as_u64(&self) -> u64 {
139141
match *self {
140-
Self::Bytes(n) => n,
142+
Self::Bytes(n) | Self::PrefixedBytes(n, _) => n,
141143
}
142144
}
143145

144146
pub(crate) fn to_header(&self) -> String {
145147
match self {
146-
Self::Bytes(n) => {
148+
Self::Bytes(n) | Self::PrefixedBytes(n, _) => {
147149
if n % 1024 == 0 && n % 1000 != 0 {
148150
to_magnitude_and_suffix(*n as u128, SuffixType::Iec, false)
149151
} else {
@@ -166,7 +168,11 @@ pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSi
166168
let bytes = parse_size_u64(s)?;
167169

168170
if bytes > 0 {
169-
Ok(BlockSize::Bytes(bytes))
171+
if s.chars().all(char::is_alphabetic) {
172+
Ok(BlockSize::PrefixedBytes(bytes, s.clone()))
173+
} else {
174+
Ok(BlockSize::Bytes(bytes))
175+
}
170176
} else {
171177
Err(ParseSizeError::ParseFailure(format!("{}", s.quote())))
172178
}
@@ -184,7 +190,7 @@ pub(crate) fn read_block_size(matches: &ArgMatches) -> Result<BlockSize, ParseSi
184190
impl fmt::Display for BlockSize {
185191
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
186192
match self {
187-
Self::Bytes(n) => {
193+
Self::Bytes(n) | Self::PrefixedBytes(n, _) => {
188194
let s = if n % 1024 == 0 && n % 1000 != 0 {
189195
to_magnitude_and_suffix(*n as u128, SuffixType::Iec, true)
190196
} else {

src/uu/df/src/table.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,7 @@ impl BytesCell {
215215
fn new(bytes: u64, block_size: &BlockSize) -> Self {
216216
Self {
217217
bytes,
218-
scaled: {
219-
let BlockSize::Bytes(d) = block_size;
220-
(bytes as f64 / *d as f64).ceil() as u64
221-
},
218+
scaled: (bytes as f64 / block_size.as_u64() as f64).ceil() as u64,
222219
}
223220
}
224221
}
@@ -308,14 +305,21 @@ impl<'a> RowFormatter<'a> {
308305
let size = bytes_column.scaled;
309306
let s = if let Some(h) = self.options.human_readable {
310307
let size = if self.is_total_row {
311-
let BlockSize::Bytes(d) = self.options.block_size;
308+
let d = self.options.block_size.as_u64();
312309
d * size
313310
} else {
314311
bytes_column.bytes
315312
};
316313
to_magnitude_and_suffix(size.into(), SuffixType::HumanReadable(h), true)
317-
} else {
318-
size.to_string()
314+
}else {
315+
match &self.options.block_size {
316+
/// if it has a suffix, append it to the size (but not on the 'total' row)
317+
BlockSize::PrefixedBytes(_, suffix) if !self.is_total_row => {
318+
format!("{}{}", size, suffix)
319+
}
320+
/// else, just print the raw number as before
321+
_ => size.to_string(),
322+
}
319323
};
320324
Cell::from_ascii_string(s)
321325
}
@@ -327,7 +331,14 @@ impl<'a> RowFormatter<'a> {
327331
let s = if let Some(h) = self.options.human_readable {
328332
to_magnitude_and_suffix(size, SuffixType::HumanReadable(h), true)
329333
} else {
330-
size.to_string()
334+
match &self.options.block_size {
335+
/// if it has a suffix, append it to the size (but not on the 'total' row)
336+
BlockSize::PrefixedBytes(_, suffix) if !self.is_total_row => {
337+
format!("{}{}", size, suffix)
338+
}
339+
/// else, just print the raw number as before
340+
_ => size.to_string(),
341+
}
331342
};
332343
Cell::from_ascii_string(s)
333344
}

0 commit comments

Comments
 (0)