1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

dd: correct progress line if 1 byte written

Correct the progress report written by `dd` if exactly one byte is
written so that it says "1 byte" instead of "1 bytes".
This commit is contained in:
Jeffrey Finkelstein 2022-11-28 00:33:52 -05:00
parent 122cdc270d
commit 54a36df9ab

View file

@ -151,20 +151,23 @@ impl ProgUpdate {
// If the number of bytes written is sufficiently large, then // If the number of bytes written is sufficiently large, then
// print a more concise representation of the number, like // print a more concise representation of the number, like
// "1.2 kB" and "1.0 KiB". // "1.2 kB" and "1.0 KiB".
if btotal < 1000 { match btotal {
write!( 1 => write!(
w,
"{}{} byte copied, {:.1} s, {}/s{}",
carriage_return, btotal, duration, transfer_rate, newline,
),
0..=999 => write!(
w, w,
"{}{} bytes copied, {:.1} s, {}/s{}", "{}{} bytes copied, {:.1} s, {}/s{}",
carriage_return, btotal, duration, transfer_rate, newline, carriage_return, btotal, duration, transfer_rate, newline,
) ),
} else if btotal < 1024 { 1000..=1023 => write!(
write!(
w, w,
"{}{} bytes ({}) copied, {:.1} s, {}/s{}", "{}{} bytes ({}) copied, {:.1} s, {}/s{}",
carriage_return, btotal, btotal_metric, duration, transfer_rate, newline, carriage_return, btotal, btotal_metric, duration, transfer_rate, newline,
) ),
} else { _ => write!(
write!(
w, w,
"{}{} bytes ({}, {}) copied, {:.1} s, {}/s{}", "{}{} bytes ({}, {}) copied, {:.1} s, {}/s{}",
carriage_return, carriage_return,
@ -174,7 +177,7 @@ impl ProgUpdate {
duration, duration,
transfer_rate, transfer_rate,
newline, newline,
) ),
} }
} }
@ -564,6 +567,11 @@ mod tests {
// The throughput still does not match GNU dd. // The throughput still does not match GNU dd.
assert_eq!(cursor.get_ref(), b"0 bytes copied, 1.0 s, 0.0 B/s\n"); assert_eq!(cursor.get_ref(), b"0 bytes copied, 1.0 s, 0.0 B/s\n");
let prog_update = prog_update_write(1);
let mut cursor = Cursor::new(vec![]);
prog_update.write_prog_line(&mut cursor, rewrite).unwrap();
assert_eq!(cursor.get_ref(), b"1 byte copied, 1.0 s, 0.0 B/s\n");
let prog_update = prog_update_write(999); let prog_update = prog_update_write(999);
let mut cursor = Cursor::new(vec![]); let mut cursor = Cursor::new(vec![]);
prog_update.write_prog_line(&mut cursor, rewrite).unwrap(); prog_update.write_prog_line(&mut cursor, rewrite).unwrap();