mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
head: add support for -z/--zero-terminated
This commit is contained in:
parent
4574b2b58d
commit
13e61c3234
4 changed files with 21 additions and 2 deletions
|
@ -29,6 +29,7 @@ enum FilterMode {
|
||||||
struct Settings {
|
struct Settings {
|
||||||
mode: FilterMode,
|
mode: FilterMode,
|
||||||
verbose: bool,
|
verbose: bool,
|
||||||
|
zero_terminated: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Settings {
|
impl Default for Settings {
|
||||||
|
@ -36,6 +37,7 @@ impl Default for Settings {
|
||||||
Settings {
|
Settings {
|
||||||
mode: FilterMode::Lines(10),
|
mode: FilterMode::Lines(10),
|
||||||
verbose: false,
|
verbose: false,
|
||||||
|
zero_terminated: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,6 +71,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
)
|
)
|
||||||
.optflag("q", "quiet", "never print headers giving file names")
|
.optflag("q", "quiet", "never print headers giving file names")
|
||||||
.optflag("v", "verbose", "always print headers giving file names")
|
.optflag("v", "verbose", "always print headers giving file names")
|
||||||
|
.optflag("z", "zero-terminated", "line delimiter is NUL, not newline")
|
||||||
.optflag("h", "help", "display this help and exit")
|
.optflag("h", "help", "display this help and exit")
|
||||||
.optflag("V", "version", "output version information and exit")
|
.optflag("V", "version", "output version information and exit")
|
||||||
.parse(new_args);
|
.parse(new_args);
|
||||||
|
@ -113,6 +116,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
|
|
||||||
let quiet = matches.opt_present("q");
|
let quiet = matches.opt_present("q");
|
||||||
let verbose = matches.opt_present("v");
|
let verbose = matches.opt_present("v");
|
||||||
|
settings.zero_terminated = matches.opt_present("z");
|
||||||
let files = matches.free;
|
let files = matches.free;
|
||||||
|
|
||||||
// GNU implementation allows multiple declarations of "-q" and "-v" with the
|
// GNU implementation allows multiple declarations of "-q" and "-v" with the
|
||||||
|
@ -129,6 +133,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
settings.verbose = true;
|
settings.verbose = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
let mut buffer = BufReader::new(stdin());
|
let mut buffer = BufReader::new(stdin());
|
||||||
head(&mut buffer, &settings);
|
head(&mut buffer, &settings);
|
||||||
|
@ -203,8 +208,14 @@ fn head<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FilterMode::Lines(count) => {
|
FilterMode::Lines(count) => {
|
||||||
for line in reader.lines().take(count) {
|
if settings.zero_terminated {
|
||||||
println!("{}", line.unwrap());
|
for line in reader.split(0).take(count) {
|
||||||
|
print!("{}\0", String::from_utf8(line.unwrap()).unwrap())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for line in reader.lines().take(count) {
|
||||||
|
println!("{}", line.unwrap());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FilterMode::NLines(count) => {
|
FilterMode::NLines(count) => {
|
||||||
|
|
|
@ -86,6 +86,14 @@ fn test_verbose() {
|
||||||
.stdout_is_fixture("lorem_ipsum_verbose.expected");
|
.stdout_is_fixture("lorem_ipsum_verbose.expected");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_zero_terminated() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["-z", "zero_terminated.txt"])
|
||||||
|
.run()
|
||||||
|
.stdout_is_fixture("zero_terminated.expected");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn test_spams_newline() {
|
fn test_spams_newline() {
|
||||||
|
|
BIN
tests/fixtures/head/zero_terminated.expected
vendored
Normal file
BIN
tests/fixtures/head/zero_terminated.expected
vendored
Normal file
Binary file not shown.
BIN
tests/fixtures/head/zero_terminated.txt
vendored
Normal file
BIN
tests/fixtures/head/zero_terminated.txt
vendored
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue