mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 20:17:45 +00:00
sort: refactor settings into dedicated struct
This commit is contained in:
parent
9c477a9ca8
commit
6488f168fb
1 changed files with 42 additions and 15 deletions
|
@ -29,7 +29,30 @@ static VERSION: &'static str = env!("CARGO_PKG_VERSION");
|
||||||
static DECIMAL_PT: char = '.';
|
static DECIMAL_PT: char = '.';
|
||||||
static THOUSANDS_SEP: char = ',';
|
static THOUSANDS_SEP: char = ',';
|
||||||
|
|
||||||
|
enum SortMode {
|
||||||
|
Numeric,
|
||||||
|
HumanReadable,
|
||||||
|
Default,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Settings {
|
||||||
|
mode: SortMode,
|
||||||
|
reverse: bool,
|
||||||
|
outfile: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Settings {
|
||||||
|
fn default() -> Settings {
|
||||||
|
Settings {
|
||||||
|
mode: SortMode::Default,
|
||||||
|
reverse: false,
|
||||||
|
outfile: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn uumain(args: Vec<String>) -> i32 {
|
pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
|
let mut settings: Settings = Default::default();
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = getopts::Options::new();
|
||||||
|
|
||||||
opts.optflag("n", "numeric-sort", "compare according to string numerical value");
|
opts.optflag("n", "numeric-sort", "compare according to string numerical value");
|
||||||
|
@ -63,10 +86,16 @@ With no FILE, or when FILE is -, read standard input.", NAME, VERSION);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let numeric = matches.opt_present("numeric-sort");
|
settings.mode = if matches.opt_present("numeric-sort") {
|
||||||
let human_readable = matches.opt_present("human-readable-sort");
|
SortMode::Numeric
|
||||||
let reverse = matches.opt_present("reverse");
|
} else if matches.opt_present("human-readable-sort") {
|
||||||
let outfile = matches.opt_str("output");
|
SortMode::HumanReadable
|
||||||
|
} else {
|
||||||
|
SortMode::Default
|
||||||
|
};
|
||||||
|
|
||||||
|
settings.reverse = matches.opt_present("reverse");
|
||||||
|
settings.outfile = matches.opt_str("output");
|
||||||
|
|
||||||
let mut files = matches.free;
|
let mut files = matches.free;
|
||||||
if files.is_empty() {
|
if files.is_empty() {
|
||||||
|
@ -74,12 +103,12 @@ With no FILE, or when FILE is -, read standard input.", NAME, VERSION);
|
||||||
files.push("-".to_owned());
|
files.push("-".to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
exec(files, numeric, human_readable, reverse, outfile);
|
exec(files, &settings);
|
||||||
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exec(files: Vec<String>, numeric: bool, human_readable: bool, reverse: bool, outfile: Option<String>) {
|
fn exec(files: Vec<String>, settings: &Settings) {
|
||||||
for path in &files {
|
for path in &files {
|
||||||
let (reader, _) = match open(path) {
|
let (reader, _) = match open(path) {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
|
@ -98,19 +127,17 @@ fn exec(files: Vec<String>, numeric: bool, human_readable: bool, reverse: bool,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if numeric {
|
match settings.mode {
|
||||||
lines.sort_by(numeric_compare);
|
SortMode::Numeric => lines.sort_by(numeric_compare),
|
||||||
} else if human_readable {
|
SortMode::HumanReadable => lines.sort_by(human_readable_size_compare),
|
||||||
lines.sort_by(human_readable_size_compare);
|
SortMode::Default => lines.sort()
|
||||||
} else {
|
|
||||||
lines.sort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let iter = lines.iter();
|
let iter = lines.iter();
|
||||||
if reverse {
|
if settings.reverse {
|
||||||
print_sorted(iter.rev(), &outfile);
|
print_sorted(iter.rev(), &settings.outfile);
|
||||||
} else {
|
} else {
|
||||||
print_sorted(iter, &outfile)
|
print_sorted(iter, &settings.outfile)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue