1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

fold: preserve backspace and overwritten chars in output

This commit is contained in:
Daniel Rocco 2021-04-05 20:39:43 -04:00
parent cc4f32d87a
commit 0b731dfd1a
2 changed files with 74 additions and 5 deletions

View file

@ -232,13 +232,9 @@ fn fold_file<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) {
last_space = if spaces { Some(char_count) } else { None }; last_space = if spaces { Some(char_count) } else { None };
} }
'\x08' => { '\x08' => {
// FIXME: does not match GNU's handling of backspace
if col_count > 0 { if col_count > 0 {
col_count -= 1; col_count -= 1;
char_count -= 1;
output.truncate(char_count);
} }
continue;
} }
'\r' => { '\r' => {
// FIXME: does not match GNU's handling of carriage return // FIXME: does not match GNU's handling of carriage return
@ -258,7 +254,7 @@ fn fold_file<T: Read>(mut file: BufReader<T>, spaces: bool, width: usize) {
char_count += 1; char_count += 1;
} }
if col_count > 0 { if char_count > 0 {
print!("{}", output); print!("{}", output);
output.truncate(0); output.truncate(0);
} }

View file

@ -248,6 +248,43 @@ fn test_fold_at_word_boundary_only_whitespace_preserve_final_newline() {
.stdout_is(" \n \n"); .stdout_is(" \n \n");
} }
#[test]
fn test_backspace_should_be_preserved() {
new_ucmd!().pipe_in("\x08").succeeds().stdout_is("\x08");
}
#[test]
fn test_backspaced_char_should_be_preserved() {
new_ucmd!().pipe_in("x\x08").succeeds().stdout_is("x\x08");
}
#[test]
fn test_backspace_should_decrease_column_count() {
new_ucmd!()
.arg("-w2")
.pipe_in("1\x08345")
.succeeds()
.stdout_is("1\x0834\n5");
}
#[test]
fn test_backspace_should_not_decrease_column_count_past_zero() {
new_ucmd!()
.arg("-w2")
.pipe_in("1\x08\x083456")
.succeeds()
.stdout_is("1\x08\x0834\n56");
}
#[test]
fn test_backspace_is_not_word_boundary() {
new_ucmd!()
.args(&["-w10", "-s"])
.pipe_in("foobar\x086789abcdef")
.succeeds()
.stdout_is("foobar\x086789a\nbcdef");
}
// //
// bytewise tests // bytewise tests
@ -397,3 +434,39 @@ fn test_bytewise_fold_at_word_boundary_only_whitespace_preserve_final_newline()
.succeeds() .succeeds()
.stdout_is(" \n \n"); .stdout_is(" \n \n");
} }
#[test]
fn test_bytewise_backspace_should_be_preserved() {
new_ucmd!()
.arg("-b")
.pipe_in("\x08")
.succeeds()
.stdout_is("\x08");
}
#[test]
fn test_bytewise_backspaced_char_should_be_preserved() {
new_ucmd!()
.arg("-b")
.pipe_in("x\x08")
.succeeds()
.stdout_is("x\x08");
}
#[test]
fn test_bytewise_backspace_should_not_decrease_column_count() {
new_ucmd!()
.args(&["-w2", "-b"])
.pipe_in("1\x08345")
.succeeds()
.stdout_is("1\x08\n34\n5");
}
#[test]
fn test_bytewise_backspace_is_not_word_boundary() {
new_ucmd!()
.args(&["-w10", "-s", "-b"])
.pipe_in("foobar\x0889abcdef")
.succeeds()
.stdout_is("foobar\x0889a\nbcdef");
}