1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

Refactor wc settings into dedicated struct.

This commit is contained in:
Joseph Crail 2015-06-03 01:35:05 -04:00
parent acaa1d18a7
commit ac8d972ed9

View file

@ -25,8 +25,44 @@ use std::str::from_utf8;
#[macro_use] #[macro_use]
mod util; mod util;
struct Settings {
show_bytes: bool,
show_chars: bool,
show_lines: bool,
show_words: bool,
show_max_line_length: bool,
}
impl Settings {
fn new(matches: &Matches) -> Settings {
let settings = Settings {
show_bytes: matches.opt_present("bytes"),
show_chars: matches.opt_present("chars"),
show_lines: matches.opt_present("lines"),
show_words: matches.opt_present("words"),
show_max_line_length: matches.opt_present("L"),
};
if settings.show_bytes
|| settings.show_chars
|| settings.show_lines
|| settings.show_words
|| settings.show_max_line_length {
return settings;
}
Settings {
show_bytes: true,
show_chars: false,
show_lines: true,
show_words: true,
show_max_line_length: false,
}
}
}
struct Result { struct Result {
filename: String, title: String,
bytes: usize, bytes: usize,
chars: usize, chars: usize,
lines: usize, lines: usize,
@ -73,7 +109,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
matches.free.push("-".to_string()); matches.free.push("-".to_string());
} }
match wc(&matches) { let settings = Settings::new(&matches);
match wc(matches.free, &settings) {
Ok(()) => ( /* pass */ ), Ok(()) => ( /* pass */ ),
Err(e) => return e Err(e) => return e
} }
@ -93,7 +131,7 @@ fn is_word_seperator(byte: u8) -> bool {
byte == SPACE || byte == TAB || byte == CR || byte == SYN || byte == FF byte == SPACE || byte == TAB || byte == CR || byte == SYN || byte == FF
} }
pub fn wc(matches: &Matches) -> StdResult<(), i32> { fn wc(files: Vec<String>, settings: &Settings) -> StdResult<(), i32> {
let mut total_line_count: usize = 0; let mut total_line_count: usize = 0;
let mut total_word_count: usize = 0; let mut total_word_count: usize = 0;
let mut total_char_count: usize = 0; let mut total_char_count: usize = 0;
@ -103,7 +141,7 @@ pub fn wc(matches: &Matches) -> StdResult<(), i32> {
let mut results = vec!(); let mut results = vec!();
let mut max_str_len: usize = 0; let mut max_str_len: usize = 0;
for path in matches.free.iter() { for path in files.iter() {
let mut reader = try!(open(&path[..])); let mut reader = try!(open(&path[..]));
let mut line_count: usize = 0; let mut line_count: usize = 0;
@ -154,7 +192,7 @@ pub fn wc(matches: &Matches) -> StdResult<(), i32> {
} }
results.push(Result { results.push(Result {
filename: path.to_string(), title: path.to_string(),
bytes: byte_count, bytes: byte_count,
chars: char_count, chars: char_count,
lines: line_count, lines: line_count,
@ -176,47 +214,43 @@ pub fn wc(matches: &Matches) -> StdResult<(), i32> {
} }
for result in results.iter() { for result in results.iter() {
print_stats(&result.filename[..], result.lines, result.words, result.chars, result.bytes, result.max_line_length, matches, max_str_len); print_stats(settings, &result, max_str_len);
} }
if matches.free.len() > 1 { if files.len() > 1 {
print_stats("total", total_line_count, total_word_count, total_char_count, total_byte_count, total_longest_line_length, matches, max_str_len); let result = Result {
title: "total".to_string(),
bytes: total_byte_count,
chars: total_char_count,
lines: total_line_count,
words: total_word_count,
max_line_length: total_longest_line_length,
};
print_stats(settings, &result, max_str_len);
} }
Ok(()) Ok(())
} }
fn print_stats(filename: &str, line_count: usize, word_count: usize, char_count: usize, fn print_stats(settings: &Settings, result: &Result, max_str_len: usize) {
byte_count: usize, longest_line_length: usize, matches: &Matches, max_str_len: usize) { if settings.show_lines {
if matches.opt_present("lines") { print!("{:1$}", result.lines, max_str_len);
print!("{:1$}", line_count, max_str_len);
} }
if matches.opt_present("words") { if settings.show_words {
print!("{:1$}", word_count, max_str_len); print!("{:1$}", result.words, max_str_len);
} }
if matches.opt_present("bytes") { if settings.show_bytes {
print!("{:1$}", byte_count, max_str_len); print!("{:1$}", result.bytes, max_str_len);
} }
if matches.opt_present("chars") { if settings.show_chars {
print!("{:1$}", char_count, max_str_len); print!("{:1$}", result.chars, max_str_len);
} }
if matches.opt_present("max-line-length") { if settings.show_max_line_length {
print!("{:1$}", longest_line_length, max_str_len); print!("{:1$}", result.max_line_length, max_str_len);
} }
// defaults if result.title != "-" {
if !matches.opt_present("bytes") println!(" {}", result.title);
&& !matches.opt_present("chars")
&& !matches.opt_present("lines")
&& !matches.opt_present("words")
&& !matches.opt_present("max-line-length") {
print!("{:1$}", line_count, max_str_len);
print!("{:1$}", word_count, max_str_len + 1);
print!("{:1$}", byte_count, max_str_len + 1);
}
if filename != "-" {
println!(" {}", filename);
} }
else { else {
println!(""); println!("");