Merge pull request #1219 from kupospelov/master

join: implement option to suppress joined lines
This commit is contained in:
Alex Lyon 2018-06-12 09:50:06 -07:00 committed by GitHub
commit eedfc3ef57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 9 deletions

View file

@ -47,6 +47,7 @@ struct Settings {
key1: usize, key1: usize,
key2: usize, key2: usize,
print_unpaired: FileNum, print_unpaired: FileNum,
print_joined: bool,
ignore_case: bool, ignore_case: bool,
separator: Sep, separator: Sep,
autoformat: bool, autoformat: bool,
@ -62,6 +63,7 @@ impl Default for Settings {
key1: 0, key1: 0,
key2: 0, key2: 0,
print_unpaired: FileNum::None, print_unpaired: FileNum::None,
print_joined: true,
ignore_case: false, ignore_case: false,
separator: Sep::Whitespaces, separator: Sep::Whitespaces,
autoformat: false, autoformat: false,
@ -459,6 +461,12 @@ When FILE1 or FILE2 (not both) is -, read standard input.",
FILENUM is 1 or 2, corresponding to FILE1 or FILE2", FILENUM is 1 or 2, corresponding to FILE1 or FILE2",
), ),
) )
.arg(
Arg::with_name("v")
.short("v")
.value_name("FILENUM")
.help("like -a FILENUM, but suppress joined output lines"),
)
.arg( .arg(
Arg::with_name("e") Arg::with_name("e")
.short("e") .short("e")
@ -539,14 +547,14 @@ FILENUM is 1 or 2, corresponding to FILE1 or FILE2",
let key2 = parse_field_number_option(matches.value_of("2")); let key2 = parse_field_number_option(matches.value_of("2"));
let mut settings: Settings = Default::default(); let mut settings: Settings = Default::default();
settings.print_unpaired = match matches.value_of("a") {
Some(value) => match value { if let Some(value) = matches.value_of("v") {
"1" => FileNum::File1, settings.print_unpaired = parse_file_number(value);
"2" => FileNum::File2, settings.print_joined = false;
value => crash!(1, "invalid file number: '{}'", value), } else if let Some(value) = matches.value_of("a") {
}, settings.print_unpaired = parse_file_number(value);
None => FileNum::None, }
};
settings.ignore_case = matches.is_present("i"); settings.ignore_case = matches.is_present("i");
settings.key1 = get_field_number(keys, key1); settings.key1 = get_field_number(keys, key1);
settings.key2 = get_field_number(keys, key2); settings.key2 = get_field_number(keys, key2);
@ -652,7 +660,11 @@ fn exec(file1: &str, file2: &str, settings: &Settings) -> i32 {
Ordering::Equal => { Ordering::Equal => {
let next_line1 = state1.extend(&input); let next_line1 = state1.extend(&input);
let next_line2 = state2.extend(&input); let next_line2 = state2.extend(&input);
state1.combine(&state2, &repr);
if settings.print_joined {
state1.combine(&state2, &repr);
}
state1.reset(next_line1); state1.reset(next_line1);
state2.reset(next_line2); state2.reset(next_line2);
} }
@ -694,6 +706,14 @@ fn parse_field_number(value: &str) -> usize {
} }
} }
fn parse_file_number(value: &str) -> FileNum {
match value {
"1" => FileNum::File1,
"2" => FileNum::File2,
value => crash!(1, "invalid file number: '{}'", value),
}
}
fn parse_field_number_option(value: Option<&str>) -> Option<usize> { fn parse_field_number_option(value: Option<&str>) -> Option<usize> {
Some(parse_field_number(value?)) Some(parse_field_number(value?))
} }

View file

@ -0,0 +1,3 @@
1 a
8 h
9 i

View file

@ -85,6 +85,18 @@ fn unpaired_lines() {
.succeeds().stdout_only_fixture("unpaired_lines.expected"); .succeeds().stdout_only_fixture("unpaired_lines.expected");
} }
#[test]
fn suppress_joined() {
new_ucmd!()
.arg("fields_3.txt")
.arg("fields_2.txt")
.arg("-1")
.arg("2")
.arg("-v")
.arg("2")
.succeeds().stdout_only_fixture("suppress_joined.expected");
}
#[test] #[test]
fn case_insensitive() { fn case_insensitive() {
new_ucmd!() new_ucmd!()