Move file-sorting code outside the view functions

This commit is contained in:
Ben S 2014-07-22 22:19:51 +01:00
parent e2f9a80ca5
commit 527528e81f

View File

@ -50,11 +50,17 @@ fn exa(opts: &Options) {
match Dir::readdir(Path::new(dir_name.clone())) { match Dir::readdir(Path::new(dir_name.clone())) {
Ok(dir) => { Ok(dir) => {
if print_dir_names { println!("{}:", dir_name); } let unsorted_files = dir.files();
let files: Vec<&File> = opts.transform_files(&unsorted_files);
if print_dir_names {
println!("{}:", dir_name);
}
match opts.view { match opts.view {
Details(ref cols) => details_view(opts, cols, dir), Details(ref cols) => details_view(opts, cols, files),
Lines => lines_view(opts, dir), Lines => lines_view(files),
Grid(across, width) => grid_view(opts, across, width, dir), Grid(across, width) => grid_view(across, width, files),
} }
} }
Err(e) => { Err(e) => {
@ -65,19 +71,13 @@ fn exa(opts: &Options) {
} }
} }
fn lines_view(options: &Options, dir: Dir) { fn lines_view(files: Vec<&File>) {
let unsorted_files = dir.files();
let files: Vec<&File> = options.transform_files(&unsorted_files);
for file in files.iter() { for file in files.iter() {
println!("{}", file.file_name()); println!("{}", file.file_name());
} }
} }
fn grid_view(options: &Options, across: bool, console_width: uint, dir: Dir) { fn grid_view(across: bool, console_width: uint, files: Vec<&File>) {
let unsorted_files = dir.files();
let files: Vec<&File> = options.transform_files(&unsorted_files);
let max_column_length = files.iter().map(|f| f.file_name_width()).max().unwrap(); let max_column_length = files.iter().map(|f| f.file_name_width()).max().unwrap();
let num_columns = (console_width + 1) / (max_column_length + 1); let num_columns = (console_width + 1) / (max_column_length + 1);
let count = files.len(); let count = files.len();
@ -114,10 +114,7 @@ fn grid_view(options: &Options, across: bool, console_width: uint, dir: Dir) {
} }
} }
fn details_view(options: &Options, columns: &Vec<Column>, dir: Dir) { fn details_view(options: &Options, columns: &Vec<Column>, files: Vec<&File>) {
let unsorted_files = dir.files();
let files: Vec<&File> = options.transform_files(&unsorted_files);
// The output gets formatted into columns, which looks nicer. To // The output gets formatted into columns, which looks nicer. To
// do this, we have to write the results into a table, instead of // do this, we have to write the results into a table, instead of
// displaying each file immediately, then calculating the maximum // displaying each file immediately, then calculating the maximum