Add header row with -h option

This commit is contained in:
Ben S 2014-06-23 18:26:35 +01:00
parent 48284e0aef
commit 038143682c
3 changed files with 22 additions and 1 deletions

View File

@ -26,6 +26,19 @@ impl Column {
_ => Left,
}
}
pub fn header(&self) -> &'static str {
match *self {
Permissions => "Permissions",
FileName => "Name",
FileSize(_) => "Size",
Blocks => "Blocks",
User(_) => "User",
Group => "Group",
HardLinks => "Links",
Inode => "inode",
}
}
}
// An Alignment is used to pad a string to a certain length, letting

7
exa.rs
View File

@ -8,6 +8,7 @@ use file::File;
use dir::Dir;
use options::Options;
use unix::Unix;
use colours::Plain;
pub mod colours;
pub mod column;
@ -72,10 +73,14 @@ fn exa(options: &Options, print_header: bool, string: String) {
let mut cache = Unix::empty_cache();
let table: Vec<Vec<String>> = files.iter()
let mut table: Vec<Vec<String>> = files.iter()
.map(|f| options.columns.iter().map(|c| f.display(c, &mut cache)).collect())
.collect();
if options.header {
table.unshift(options.columns.iter().map(|c| Plain.underline().paint(c.header())).collect());
}
// Each column needs to have its invisible colour-formatting
// characters stripped before it has its width calculated, or the
// width will be incorrect and the columns won't line up properly.

View File

@ -16,6 +16,7 @@ pub struct Options {
pub reverse: bool,
pub dirs: Vec<String>,
pub columns: Vec<Column>,
pub header: bool,
}
impl SortField {
@ -35,6 +36,7 @@ impl Options {
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"),
getopts::optflag("h", "header", "show a header row at the top"),
getopts::optflag("i", "inode", "show each file's inode number"),
getopts::optflag("l", "links", "show number of hard links"),
getopts::optflag("r", "reverse", "reverse order of files"),
@ -47,6 +49,7 @@ impl Options {
Ok(matches) => Ok(Options {
showInvisibles: matches.opt_present("all"),
reverse: matches.opt_present("reverse"),
header: matches.opt_present("header"),
sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
dirs: matches.free.clone(),
columns: Options::columns(matches),