mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 19:47:45 +00:00
comm: add --output-delimiter
This commit is contained in:
parent
c841b518d1
commit
e216c5ca79
1 changed files with 14 additions and 7 deletions
21
comm/comm.rs
21
comm/comm.rs
|
@ -21,21 +21,27 @@ use std::path::Path;
|
||||||
static NAME : &'static str = "comm";
|
static NAME : &'static str = "comm";
|
||||||
static VERSION : &'static str = "1.0.0";
|
static VERSION : &'static str = "1.0.0";
|
||||||
|
|
||||||
fn delim(col: int, opts: &getopts::Matches) -> ~str {
|
fn mkdelim(col: uint, opts: &getopts::Matches) -> StrBuf {
|
||||||
let mut s = StrBuf::new();
|
let mut s = StrBuf::new();
|
||||||
|
let delim = match opts.opt_str("output-delimiter") {
|
||||||
|
Some(d) => d.clone(),
|
||||||
|
None => "\t".to_strbuf(),
|
||||||
|
};
|
||||||
|
|
||||||
if col > 1 && !opts.opt_present("1") {
|
if col > 1 && !opts.opt_present("1") {
|
||||||
s.push_str("\t");
|
s.push_str(delim.as_slice());
|
||||||
}
|
}
|
||||||
if col > 2 && !opts.opt_present("2") {
|
if col > 2 && !opts.opt_present("2") {
|
||||||
s.push_str("\t");
|
s.push_str(delim.as_slice());
|
||||||
}
|
}
|
||||||
|
|
||||||
s.to_owned()
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
fn comm(a: &mut Box<Buffer>, b: &mut Box<Buffer>, opts: &getopts::Matches) {
|
fn comm(a: &mut Box<Buffer>, b: &mut Box<Buffer>, opts: &getopts::Matches) {
|
||||||
|
|
||||||
|
let delim = Vec::from_fn(4, |col| mkdelim(col, opts));
|
||||||
|
|
||||||
let mut ra = a.read_line();
|
let mut ra = a.read_line();
|
||||||
let mut rb = b.read_line();
|
let mut rb = b.read_line();
|
||||||
|
|
||||||
|
@ -50,19 +56,19 @@ fn comm(a: &mut Box<Buffer>, b: &mut Box<Buffer>, opts: &getopts::Matches) {
|
||||||
match ord {
|
match ord {
|
||||||
Less => {
|
Less => {
|
||||||
if !opts.opt_present("1") {
|
if !opts.opt_present("1") {
|
||||||
print!("{}{}", delim(1, opts), ra.unwrap());
|
print!("{}{}", delim.get(1), ra.unwrap());
|
||||||
}
|
}
|
||||||
ra = a.read_line();
|
ra = a.read_line();
|
||||||
}
|
}
|
||||||
Greater => {
|
Greater => {
|
||||||
if !opts.opt_present("2") {
|
if !opts.opt_present("2") {
|
||||||
print!("{}{}", delim(2, opts), rb.unwrap());
|
print!("{}{}", delim.get(2), rb.unwrap());
|
||||||
}
|
}
|
||||||
rb = b.read_line();
|
rb = b.read_line();
|
||||||
}
|
}
|
||||||
Equal => {
|
Equal => {
|
||||||
if !opts.opt_present("3") {
|
if !opts.opt_present("3") {
|
||||||
print!("{}{}", delim(3, opts), ra.unwrap());
|
print!("{}{}", delim.get(3), ra.unwrap());
|
||||||
}
|
}
|
||||||
ra = a.read_line();
|
ra = a.read_line();
|
||||||
rb = b.read_line();
|
rb = b.read_line();
|
||||||
|
@ -87,6 +93,7 @@ pub fn main() {
|
||||||
getopts::optflag("1", "", "suppress column 1 (lines uniq to FILE1)"),
|
getopts::optflag("1", "", "suppress column 1 (lines uniq to FILE1)"),
|
||||||
getopts::optflag("2", "", "suppress column 2 (lines uniq to FILE2)"),
|
getopts::optflag("2", "", "suppress column 2 (lines uniq to FILE2)"),
|
||||||
getopts::optflag("3", "", "suppress column 3 (lines that appear in both files)"),
|
getopts::optflag("3", "", "suppress column 3 (lines that appear in both files)"),
|
||||||
|
getopts::optopt("", "output-delimiter", "separate columns with STR", "STR"),
|
||||||
getopts::optflag("h", "help", "display this help and exit"),
|
getopts::optflag("h", "help", "display this help and exit"),
|
||||||
getopts::optflag("V", "version", "output version information and exit"),
|
getopts::optflag("V", "version", "output version information and exit"),
|
||||||
];
|
];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue