2014-05-04 16:01:54 +00:00
|
|
|
extern crate getopts;
|
2014-05-03 10:30:37 +00:00
|
|
|
use std::os;
|
2014-05-04 20:33:14 +00:00
|
|
|
use std::io::fs;
|
|
|
|
|
|
|
|
use file::File;
|
2014-05-04 20:35:10 +00:00
|
|
|
use column::defaultColumns;
|
2014-05-03 10:30:37 +00:00
|
|
|
|
2014-05-04 20:33:14 +00:00
|
|
|
pub mod colours;
|
|
|
|
pub mod column;
|
|
|
|
pub mod format;
|
|
|
|
pub mod file;
|
2014-05-05 10:29:50 +00:00
|
|
|
pub mod unix;
|
2014-05-03 10:30:37 +00:00
|
|
|
|
2014-05-04 16:01:54 +00:00
|
|
|
struct Options {
|
|
|
|
showInvisibles: bool,
|
|
|
|
}
|
|
|
|
|
2014-05-03 10:30:37 +00:00
|
|
|
fn main() {
|
2014-05-21 23:16:05 +00:00
|
|
|
let args: Vec<StrBuf> = os::args().iter()
|
|
|
|
.map(|x| x.to_strbuf())
|
|
|
|
.collect();
|
|
|
|
|
2014-05-04 16:01:54 +00:00
|
|
|
let opts = ~[
|
|
|
|
getopts::optflag("a", "all", "show dot-files")
|
|
|
|
];
|
|
|
|
|
|
|
|
let matches = match getopts::getopts(args.tail(), opts) {
|
|
|
|
Ok(m) => m,
|
2014-05-04 20:35:10 +00:00
|
|
|
Err(f) => fail!("Invalid options\n{}", f.to_err_msg()),
|
2014-05-04 16:01:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let opts = Options {
|
|
|
|
showInvisibles: matches.opt_present("all")
|
|
|
|
};
|
|
|
|
|
2014-05-05 09:51:24 +00:00
|
|
|
let strs = if matches.free.is_empty() {
|
2014-05-21 23:16:05 +00:00
|
|
|
vec!("./".to_strbuf())
|
2014-05-05 09:51:24 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
matches.free.clone()
|
|
|
|
};
|
2014-05-04 16:01:54 +00:00
|
|
|
|
|
|
|
for dir in strs.move_iter() {
|
|
|
|
list(opts, Path::new(dir))
|
2014-05-03 10:30:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:01:54 +00:00
|
|
|
fn list(opts: Options, path: Path) {
|
2014-05-04 15:28:42 +00:00
|
|
|
let mut files = match fs::readdir(&path) {
|
|
|
|
Ok(files) => files,
|
|
|
|
Err(e) => fail!("readdir: {}", e),
|
|
|
|
};
|
|
|
|
files.sort_by(|a, b| a.filename_str().cmp(&b.filename_str()));
|
|
|
|
for subpath in files.iter() {
|
|
|
|
let file = File::from_path(subpath);
|
|
|
|
|
2014-05-05 09:51:24 +00:00
|
|
|
if file.is_dotfile() && !opts.showInvisibles {
|
2014-05-04 16:01:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-04 20:33:14 +00:00
|
|
|
let columns = defaultColumns();
|
2014-05-03 13:26:49 +00:00
|
|
|
|
2014-05-04 20:33:14 +00:00
|
|
|
let mut cells = columns.iter().map(|c| file.display(c));
|
2014-05-03 10:30:37 +00:00
|
|
|
|
2014-05-03 11:15:35 +00:00
|
|
|
let mut first = true;
|
|
|
|
for cell in cells {
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
print!(" ");
|
|
|
|
}
|
|
|
|
print!("{}", cell);
|
|
|
|
}
|
|
|
|
print!("\n");
|
2014-05-03 10:30:37 +00:00
|
|
|
}
|
|
|
|
}
|