Add sort option

And move options struct into its own module
This commit is contained in:
Ben S 2014-05-24 02:17:43 +01:00
parent fdf58e887b
commit cb1b8bc2d7
3 changed files with 57 additions and 16 deletions

26
exa.rs
View File

@ -8,16 +8,14 @@ use std::io::fs;
use file::File; use file::File;
use column::defaultColumns; use column::defaultColumns;
use options::{Options, SortField, Name};
pub mod colours; pub mod colours;
pub mod column; pub mod column;
pub mod format; pub mod format;
pub mod file; pub mod file;
pub mod unix; pub mod unix;
pub mod options;
struct Options {
showInvisibles: bool,
}
fn main() { fn main() {
let args: Vec<StrBuf> = os::args().iter() let args: Vec<StrBuf> = os::args().iter()
@ -25,7 +23,8 @@ fn main() {
.collect(); .collect();
let opts = ~[ let opts = ~[
getopts::optflag("a", "all", "show dot-files") getopts::optflag("a", "all", "show dot-files"),
getopts::optopt("s", "sort", "field to sort by", "WORD"),
]; ];
let matches = match getopts::getopts(args.tail(), opts) { let matches = match getopts::getopts(args.tail(), opts) {
@ -34,7 +33,8 @@ fn main() {
}; };
let opts = Options { let opts = Options {
showInvisibles: matches.opt_present("all") showInvisibles: matches.opt_present("all"),
sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
}; };
let strs = if matches.free.is_empty() { let strs = if matches.free.is_empty() {
@ -49,18 +49,18 @@ fn main() {
} }
} }
fn list(opts: Options, path: Path) { fn list(options: Options, path: Path) {
let mut files = match fs::readdir(&path) { let paths = match fs::readdir(&path) {
Ok(files) => files, Ok(paths) => paths,
Err(e) => fail!("readdir: {}", e), Err(e) => fail!("readdir: {}", e),
}; };
files.sort_by(|a, b| a.filename_str().cmp(&b.filename_str()));
let mut files = paths.iter().map(|path| File::from_path(path)).collect();
options.sort(&mut files);
let columns = defaultColumns(); let columns = defaultColumns();
let table: Vec<Vec<StrBuf>> = files.iter() let table: Vec<Vec<StrBuf>> = files.iter()
.map(|p| File::from_path(p)) .filter(|&f| options.show(f))
.filter(|f| !f.is_dotfile() || opts.showInvisibles )
.map(|f| columns.iter().map(|c| f.display(c)).collect()) .map(|f| columns.iter().map(|c| f.display(c)).collect())
.collect(); .collect();

View File

@ -10,9 +10,9 @@ use unix::{get_user_name, get_group_name};
// only to determine what kind of file it is, so carry the `stat` // only to determine what kind of file it is, so carry the `stat`
// result around with the file for safe keeping. // result around with the file for safe keeping.
pub struct File<'a> { pub struct File<'a> {
name: &'a str, pub name: &'a str,
path: &'a Path, pub path: &'a Path,
stat: io::FileStat, pub stat: io::FileStat,
} }
impl<'a> File<'a> { impl<'a> File<'a> {

41
options.rs Normal file
View File

@ -0,0 +1,41 @@
use file::File;
pub enum SortField {
Name, Size
}
pub struct Options {
pub showInvisibles: bool,
pub sortField: SortField,
}
impl SortField {
pub fn from_word(word: StrBuf) -> SortField {
match word.as_slice() {
"name" => Name,
"size" => Size,
_ => fail!("Invalid sorting order"),
}
}
fn sort(&self, files: &mut Vec<File>) {
match *self {
Name => files.sort_by(|a, b| a.name.cmp(&b.name)),
Size => files.sort_by(|a, b| a.stat.size.cmp(&b.stat.size)),
}
}
}
impl Options {
pub fn sort(&self, files: &mut Vec<File>) {
self.sortField.sort(files);
}
pub fn show(&self, f: &File) -> bool {
if self.showInvisibles {
true
} else {
!f.name.starts_with(".")
}
}
}