From 44a9819417945ac1799552611d09379099d0f5e0 Mon Sep 17 00:00:00 2001 From: Ben S Date: Tue, 22 Jul 2014 20:47:30 +0100 Subject: [PATCH] Add --oneline option Also, rename old 'lines' view to 'details' which makes more sense --- README.md | 1 + src/exa.rs | 21 +++++++++++++++------ src/file.rs | 2 +- src/options.rs | 15 +++++++++++---- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 799e2f4..20f240b 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Screenshot Options ------- +- **-1**, **--oneline**: display one entry per line - **-a**, **--all**: show dot files - **-b**, **--binary**: use binary (power of two) file sizes - **-g**, **--group**: show group as well as user diff --git a/src/exa.rs b/src/exa.rs index 5621174..66fe8dc 100644 --- a/src/exa.rs +++ b/src/exa.rs @@ -9,7 +9,7 @@ use std::os; use file::File; use dir::Dir; use column::{Column, Left}; -use options::{Options, Lines, Grid}; +use options::{Options, Details, Lines, Grid}; use unix::Unix; use ansi_term::{Paint, Plain, strip_formatting}; @@ -52,8 +52,9 @@ fn exa(opts: &Options) { Ok(dir) => { if print_dir_names { println!("{}:", dir_name); } match opts.view { - Lines(ref cols) => lines_view(opts, cols, dir), - Grid(bool) => grid_view(opts, bool, dir), + Details(ref cols) => details_view(opts, cols, dir), + Lines => lines_view(opts, dir), + Grid(across, width) => grid_view(opts, across, width, dir), } } Err(e) => { @@ -64,12 +65,20 @@ fn exa(opts: &Options) { } } -fn grid_view(options: &Options, across: bool, dir: Dir) { +fn lines_view(options: &Options, dir: Dir) { + let unsorted_files = dir.files(); + let files: Vec<&File> = options.transform_files(&unsorted_files); + + for file in files.iter() { + println!("{}", file.file_name()); + } +} + +fn grid_view(options: &Options, across: bool, console_width: uint, dir: Dir) { 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 (console_width, _) = term::dimensions().unwrap_or((80, 24)); let num_columns = (console_width + 1) / (max_column_length + 1); let count = files.len(); @@ -105,7 +114,7 @@ fn grid_view(options: &Options, across: bool, dir: Dir) { } } -fn lines_view(options: &Options, columns: &Vec, dir: Dir) { +fn details_view(options: &Options, columns: &Vec, dir: Dir) { let unsorted_files = dir.files(); let files: Vec<&File> = options.transform_files(&unsorted_files); diff --git a/src/file.rs b/src/file.rs index 0de2655..1cc57b1 100644 --- a/src/file.rs +++ b/src/file.rs @@ -141,7 +141,7 @@ impl<'a> File<'a> { } } - fn file_name(&self) -> String { + pub fn file_name(&self) -> String { let name = self.name.as_slice(); let displayed_name = self.file_colour().paint(name); if self.stat.kind == io::TypeSymlink { diff --git a/src/options.rs b/src/options.rs index 750c423..616c735 100644 --- a/src/options.rs +++ b/src/options.rs @@ -3,6 +3,7 @@ extern crate getopts; use file::File; use column::{Column, Permissions, FileName, FileSize, User, Group, HardLinks, Inode, Blocks}; use std::ascii::StrAsciiExt; +use term; pub enum SortField { Name, Extension, Size @@ -20,8 +21,9 @@ impl SortField { } pub enum View { - Lines(Vec), - Grid(bool), + Details(Vec), + Lines, + Grid(bool, uint), } pub struct Options { @@ -37,6 +39,7 @@ pub struct Options { impl Options { pub fn getopts(args: Vec) -> Result { let opts = [ + getopts::optflag("1", "oneline", "display one entry per line"), getopts::optflag("a", "all", "show dot-files"), getopts::optflag("b", "binary", "use binary prefixes in file sizes"), getopts::optflag("g", "group", "show group as well as user"), @@ -65,10 +68,14 @@ impl Options { fn view(matches: getopts::Matches) -> View { if matches.opt_present("long") { - Lines(Options::columns(matches)) + Details(Options::columns(matches)) + } + else if matches.opt_present("oneline") { + Lines } else { - Grid(matches.opt_present("across")) + let (console_width, _) = term::dimensions().unwrap_or((80, 24)); + Grid(matches.opt_present("across"), console_width) } }