mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
Merge branch 'master' into fold-backspace-and-carriage-return
This commit is contained in:
commit
b2b45d8af2
8 changed files with 126 additions and 87 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1861,6 +1861,7 @@ dependencies = [
|
||||||
name = "uu_fold"
|
name = "uu_fold"
|
||||||
version = "0.0.6"
|
version = "0.0.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"clap",
|
||||||
"uucore",
|
"uucore",
|
||||||
"uucore_procs",
|
"uucore_procs",
|
||||||
]
|
]
|
||||||
|
|
|
@ -15,6 +15,7 @@ edition = "2018"
|
||||||
path = "src/fold.rs"
|
path = "src/fold.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
clap = "2.33"
|
||||||
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
uucore = { version=">=0.0.8", package="uucore", path="../../uucore" }
|
||||||
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }
|
||||||
|
|
||||||
|
|
|
@ -10,48 +10,71 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
|
use clap::{App, Arg};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{stdin, BufRead, BufReader, Read};
|
use std::io::{stdin, BufRead, BufReader, Read};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
const TAB_WIDTH: usize = 8;
|
const TAB_WIDTH: usize = 8;
|
||||||
|
|
||||||
|
static NAME: &str = "fold";
|
||||||
|
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
static SYNTAX: &str = "[OPTION]... [FILE]...";
|
static SYNTAX: &str = "[OPTION]... [FILE]...";
|
||||||
static SUMMARY: &str = "Writes each file (or standard input if no files are given)
|
static SUMMARY: &str = "Writes each file (or standard input if no files are given)
|
||||||
to standard output whilst breaking long lines";
|
to standard output whilst breaking long lines";
|
||||||
static LONG_HELP: &str = "";
|
|
||||||
|
mod options {
|
||||||
|
pub const BYTES: &str = "bytes";
|
||||||
|
pub const SPACES: &str = "spaces";
|
||||||
|
pub const WIDTH: &str = "width";
|
||||||
|
pub const FILE: &str = "file";
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
let args = args.collect_str();
|
let args = args.collect_str();
|
||||||
|
|
||||||
let (args, obs_width) = handle_obsolete(&args[..]);
|
let (args, obs_width) = handle_obsolete(&args[..]);
|
||||||
let matches = app!(SYNTAX, SUMMARY, LONG_HELP)
|
let matches = App::new(executable!())
|
||||||
.optflag(
|
.name(NAME)
|
||||||
"b",
|
.version(VERSION)
|
||||||
"bytes",
|
.usage(SYNTAX)
|
||||||
"count using bytes rather than columns (meaning control characters \
|
.about(SUMMARY)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name(options::BYTES)
|
||||||
|
.long(options::BYTES)
|
||||||
|
.short("b")
|
||||||
|
.help(
|
||||||
|
"count using bytes rather than columns (meaning control characters \
|
||||||
such as newline are not treated specially)",
|
such as newline are not treated specially)",
|
||||||
|
)
|
||||||
|
.takes_value(false),
|
||||||
)
|
)
|
||||||
.optflag(
|
.arg(
|
||||||
"s",
|
Arg::with_name(options::SPACES)
|
||||||
"spaces",
|
.long(options::SPACES)
|
||||||
"break lines at word boundaries rather than a hard cut-off",
|
.short("s")
|
||||||
|
.help("break lines at word boundaries rather than a hard cut-off")
|
||||||
|
.takes_value(false),
|
||||||
)
|
)
|
||||||
.optopt(
|
.arg(
|
||||||
"w",
|
Arg::with_name(options::WIDTH)
|
||||||
"width",
|
.long(options::WIDTH)
|
||||||
"set WIDTH as the maximum line width rather than 80",
|
.short("w")
|
||||||
"WIDTH",
|
.help("set WIDTH as the maximum line width rather than 80")
|
||||||
|
.value_name("WIDTH")
|
||||||
|
.allow_hyphen_values(true)
|
||||||
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
.parse(args);
|
.arg(Arg::with_name(options::FILE).hidden(true).multiple(true))
|
||||||
|
.get_matches_from(args.clone());
|
||||||
|
|
||||||
let bytes = matches.opt_present("b");
|
let bytes = matches.is_present(options::BYTES);
|
||||||
let spaces = matches.opt_present("s");
|
let spaces = matches.is_present(options::SPACES);
|
||||||
let poss_width = if matches.opt_present("w") {
|
let poss_width = match matches.value_of(options::WIDTH) {
|
||||||
matches.opt_str("w")
|
Some(v) => Some(v.to_owned()),
|
||||||
} else {
|
None => obs_width,
|
||||||
obs_width
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let width = match poss_width {
|
let width = match poss_width {
|
||||||
Some(inp_width) => match inp_width.parse::<usize>() {
|
Some(inp_width) => match inp_width.parse::<usize>() {
|
||||||
Ok(width) => width,
|
Ok(width) => width,
|
||||||
|
@ -59,11 +82,12 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||||
},
|
},
|
||||||
None => 80,
|
None => 80,
|
||||||
};
|
};
|
||||||
let files = if matches.free.is_empty() {
|
|
||||||
vec!["-".to_owned()]
|
let files = match matches.values_of(options::FILE) {
|
||||||
} else {
|
Some(v) => v.map(|v| v.to_owned()).collect(),
|
||||||
matches.free
|
None => vec!["-".to_owned()],
|
||||||
};
|
};
|
||||||
|
|
||||||
fold(files, bytes, spaces, width);
|
fold(files, bytes, spaces, width);
|
||||||
|
|
||||||
0
|
0
|
||||||
|
|
|
@ -166,39 +166,30 @@ mod tests {
|
||||||
let mut input = PeekReader::new(Cursor::new(&data));
|
let mut input = PeekReader::new(Cursor::new(&data));
|
||||||
let mut sut = InputDecoder::new(&mut input, 8, 2, ByteOrder::Little);
|
let mut sut = InputDecoder::new(&mut input, 8, 2, ByteOrder::Little);
|
||||||
|
|
||||||
match sut.peek_read() {
|
// Peek normal length
|
||||||
Ok(mut mem) => {
|
let mut mem = sut.peek_read().unwrap();
|
||||||
assert_eq!(8, mem.length());
|
|
||||||
|
|
||||||
assert_eq!(-2.0, mem.read_float(0, 8));
|
assert_eq!(8, mem.length());
|
||||||
assert_eq!(-2.0, mem.read_float(4, 4));
|
|
||||||
assert_eq!(0xc000000000000000, mem.read_uint(0, 8));
|
|
||||||
assert_eq!(0xc0000000, mem.read_uint(4, 4));
|
|
||||||
assert_eq!(0xc000, mem.read_uint(6, 2));
|
|
||||||
assert_eq!(0xc0, mem.read_uint(7, 1));
|
|
||||||
assert_eq!(&[0, 0xc0], mem.get_buffer(6));
|
|
||||||
assert_eq!(&[0, 0xc0, 0xff, 0xff], mem.get_full_buffer(6));
|
|
||||||
|
|
||||||
let mut copy: Vec<u8> = Vec::new();
|
assert_eq!(-2.0, mem.read_float(0, 8));
|
||||||
mem.clone_buffer(&mut copy);
|
assert_eq!(-2.0, mem.read_float(4, 4));
|
||||||
assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0], copy);
|
assert_eq!(0xc000000000000000, mem.read_uint(0, 8));
|
||||||
|
assert_eq!(0xc0000000, mem.read_uint(4, 4));
|
||||||
|
assert_eq!(0xc000, mem.read_uint(6, 2));
|
||||||
|
assert_eq!(0xc0, mem.read_uint(7, 1));
|
||||||
|
assert_eq!(&[0, 0xc0], mem.get_buffer(6));
|
||||||
|
assert_eq!(&[0, 0xc0, 0xff, 0xff], mem.get_full_buffer(6));
|
||||||
|
|
||||||
mem.zero_out_buffer(7, 8);
|
let mut copy: Vec<u8> = Vec::new();
|
||||||
assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(6));
|
mem.clone_buffer(&mut copy);
|
||||||
}
|
assert_eq!(vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0], copy);
|
||||||
Err(e) => {
|
|
||||||
assert!(false, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match sut.peek_read() {
|
mem.zero_out_buffer(7, 8);
|
||||||
Ok(mem) => {
|
assert_eq!(&[0, 0, 0xff, 0xff], mem.get_full_buffer(6));
|
||||||
assert_eq!(2, mem.length());
|
|
||||||
assert_eq!(0xffff, mem.read_uint(0, 2));
|
// Peek tail
|
||||||
}
|
let mem = sut.peek_read().unwrap();
|
||||||
Err(e) => {
|
assert_eq!(2, mem.length());
|
||||||
assert!(false, e);
|
assert_eq!(0xffff, mem.read_uint(0, 2));
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,7 +108,7 @@ macro_rules! safe_write(
|
||||||
($fd:expr, $($args:tt)+) => (
|
($fd:expr, $($args:tt)+) => (
|
||||||
match write!($fd, $($args)+) {
|
match write!($fd, $($args)+) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(f) => panic!(f.to_string())
|
Err(f) => panic!("{}", f)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
@ -118,7 +118,7 @@ macro_rules! safe_writeln(
|
||||||
($fd:expr, $($args:tt)+) => (
|
($fd:expr, $($args:tt)+) => (
|
||||||
match writeln!($fd, $($args)+) {
|
match writeln!($fd, $($args)+) {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(f) => panic!(f.to_string())
|
Err(f) => panic!("{}", f)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
|
@ -534,3 +534,12 @@ fn test_bytewise_carriage_return_is_not_word_boundary() {
|
||||||
.succeeds()
|
.succeeds()
|
||||||
.stdout_is("fizz\rb\nuzz\rfi\nzzbuzz");
|
.stdout_is("fizz\rb\nuzz\rfi\nzzbuzz");
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_obsolete_syntax() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-5")
|
||||||
|
.arg("-s")
|
||||||
|
.arg("space_separated_words.txt")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_is("test1\n \ntest2\n \ntest3\n \ntest4\n \ntest5\n \ntest6\n ");
|
||||||
|
}
|
|
@ -70,10 +70,10 @@ fn convert_path<'a>(path: &'a str) -> Cow<'a, str> {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_relpath_with_from_no_d() {
|
fn test_relpath_with_from_no_d() {
|
||||||
for test in TESTS.iter() {
|
let scene = TestScenario::new(util_name!());
|
||||||
let scene = TestScenario::new(util_name!());
|
let at = &scene.fixtures;
|
||||||
let at = &scene.fixtures;
|
|
||||||
|
|
||||||
|
for test in TESTS.iter() {
|
||||||
let from: &str = &convert_path(test.from);
|
let from: &str = &convert_path(test.from);
|
||||||
let to: &str = &convert_path(test.to);
|
let to: &str = &convert_path(test.to);
|
||||||
let expected: &str = &convert_path(test.expected);
|
let expected: &str = &convert_path(test.expected);
|
||||||
|
@ -92,10 +92,10 @@ fn test_relpath_with_from_no_d() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_relpath_with_from_with_d() {
|
fn test_relpath_with_from_with_d() {
|
||||||
for test in TESTS.iter() {
|
let scene = TestScenario::new(util_name!());
|
||||||
let scene = TestScenario::new(util_name!());
|
let at = &scene.fixtures;
|
||||||
let at = &scene.fixtures;
|
|
||||||
|
|
||||||
|
for test in TESTS.iter() {
|
||||||
let from: &str = &convert_path(test.from);
|
let from: &str = &convert_path(test.from);
|
||||||
let to: &str = &convert_path(test.to);
|
let to: &str = &convert_path(test.to);
|
||||||
let pwd = at.as_string();
|
let pwd = at.as_string();
|
||||||
|
@ -103,63 +103,75 @@ fn test_relpath_with_from_with_d() {
|
||||||
at.mkdir_all(from);
|
at.mkdir_all(from);
|
||||||
|
|
||||||
// d is part of subpath -> expect relative path
|
// d is part of subpath -> expect relative path
|
||||||
let mut result = scene
|
let mut result_stdout = scene
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg(to)
|
.arg(to)
|
||||||
.arg(from)
|
.arg(from)
|
||||||
.arg(&format!("-d{}", pwd))
|
.arg(&format!("-d{}", pwd))
|
||||||
.run();
|
.succeeds()
|
||||||
assert!(result.success);
|
.stdout_move_str();
|
||||||
// relax rules for windows test environment
|
// relax rules for windows test environment
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
assert!(Path::new(&result.stdout).is_relative());
|
assert!(Path::new(&result_stdout).is_relative());
|
||||||
|
|
||||||
// d is not part of subpath -> expect absolut path
|
// d is not part of subpath -> expect absolut path
|
||||||
result = scene.ucmd().arg(to).arg(from).arg("-dnon_existing").run();
|
result_stdout = scene
|
||||||
assert!(result.success);
|
.ucmd()
|
||||||
assert!(Path::new(&result.stdout).is_absolute());
|
.arg(to)
|
||||||
|
.arg(from)
|
||||||
|
.arg("-dnon_existing")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_move_str();
|
||||||
|
assert!(Path::new(&result_stdout).is_absolute());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_relpath_no_from_no_d() {
|
fn test_relpath_no_from_no_d() {
|
||||||
for test in TESTS.iter() {
|
let scene = TestScenario::new(util_name!());
|
||||||
let scene = TestScenario::new(util_name!());
|
let at = &scene.fixtures;
|
||||||
let at = &scene.fixtures;
|
|
||||||
|
|
||||||
|
for test in TESTS.iter() {
|
||||||
let to: &str = &convert_path(test.to);
|
let to: &str = &convert_path(test.to);
|
||||||
at.mkdir_all(to);
|
at.mkdir_all(to);
|
||||||
|
|
||||||
let result = scene.ucmd().arg(to).run();
|
let result_stdout = scene.ucmd().arg(to).succeeds().stdout_move_str();
|
||||||
assert!(result.success);
|
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
assert_eq!(result.stdout, format!("{}\n", to));
|
assert_eq!(result_stdout, format!("{}\n", to));
|
||||||
// relax rules for windows test environment
|
// relax rules for windows test environment
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
assert!(result.stdout.ends_with(&format!("{}\n", to)));
|
assert!(result_stdout.ends_with(&format!("{}\n", to)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_relpath_no_from_with_d() {
|
fn test_relpath_no_from_with_d() {
|
||||||
for test in TESTS.iter() {
|
let scene = TestScenario::new(util_name!());
|
||||||
let scene = TestScenario::new(util_name!());
|
let at = &scene.fixtures;
|
||||||
let at = &scene.fixtures;
|
|
||||||
|
|
||||||
|
for test in TESTS.iter() {
|
||||||
let to: &str = &convert_path(test.to);
|
let to: &str = &convert_path(test.to);
|
||||||
let pwd = at.as_string();
|
let pwd = at.as_string();
|
||||||
at.mkdir_all(to);
|
at.mkdir_all(to);
|
||||||
|
|
||||||
// d is part of subpath -> expect relative path
|
// d is part of subpath -> expect relative path
|
||||||
let mut result = scene.ucmd().arg(to).arg(&format!("-d{}", pwd)).run();
|
let mut result_stdout = scene
|
||||||
assert!(result.success);
|
.ucmd()
|
||||||
|
.arg(to)
|
||||||
|
.arg(&format!("-d{}", pwd))
|
||||||
|
.succeeds()
|
||||||
|
.stdout_move_str();
|
||||||
// relax rules for windows test environment
|
// relax rules for windows test environment
|
||||||
#[cfg(not(windows))]
|
#[cfg(not(windows))]
|
||||||
assert!(Path::new(&result.stdout).is_relative());
|
assert!(Path::new(&result_stdout).is_relative());
|
||||||
|
|
||||||
// d is not part of subpath -> expect absolut path
|
// d is not part of subpath -> expect absolut path
|
||||||
result = scene.ucmd().arg(to).arg("-dnon_existing").run();
|
result_stdout = scene
|
||||||
assert!(result.success);
|
.ucmd()
|
||||||
assert!(Path::new(&result.stdout).is_absolute());
|
.arg(to)
|
||||||
|
.arg("-dnon_existing")
|
||||||
|
.succeeds()
|
||||||
|
.stdout_move_str();
|
||||||
|
assert!(Path::new(&result_stdout).is_absolute());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
1
tests/fixtures/fold/space_separated_words.txt
vendored
Normal file
1
tests/fixtures/fold/space_separated_words.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
test1 test2 test3 test4 test5 test6
|
Loading…
Add table
Add a link
Reference in a new issue