Add --oneline option

Also, rename old 'lines' view to 'details' which makes more sense
This commit is contained in:
Ben S 2014-07-22 20:47:30 +01:00
parent d15529301f
commit 44a9819417
4 changed files with 28 additions and 11 deletions

View File

@ -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

View File

@ -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<Column>, dir: Dir) {
fn details_view(options: &Options, columns: &Vec<Column>, dir: Dir) {
let unsorted_files = dir.files();
let files: Vec<&File> = options.transform_files(&unsorted_files);

View File

@ -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 {

View File

@ -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<Column>),
Grid(bool),
Details(Vec<Column>),
Lines,
Grid(bool, uint),
}
pub struct Options {
@ -37,6 +39,7 @@ pub struct Options {
impl Options {
pub fn getopts(args: Vec<String>) -> Result<Options, getopts::Fail_> {
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)
}
}