exa/src/exa.rs

115 lines
2.9 KiB
Rust
Raw Normal View History

#![allow(unstable)]
extern crate ansi_term;
extern crate number_prefix;
extern crate unicode;
extern crate users;
use std::io::FileType;
use std::io::fs;
2015-01-12 21:14:27 +00:00
use std::os::{args, set_exit_status};
use dir::Dir;
use file::File;
2015-01-12 20:08:42 +00:00
use options::Options;
2015-01-12 21:14:27 +00:00
use options::Error::*;
pub mod column;
pub mod dir;
pub mod file;
pub mod filetype;
pub mod options;
2015-01-12 20:08:42 +00:00
pub mod output;
pub mod term;
2015-01-12 19:48:07 +00:00
fn exa(options: &Options) {
let mut dirs: Vec<String> = vec![];
let mut files: Vec<File> = vec![];
2015-01-12 18:44:39 +00:00
// It's only worth printing out directory names if the user supplied
// more than one of them.
let mut print_dir_names = false;
// Separate the user-supplied paths into directories and files.
// Files are shown first, and then each directory is expanded
// and listed second.
2015-01-12 19:48:07 +00:00
for file in options.path_strings() {
let path = Path::new(file);
match fs::stat(&path) {
Ok(stat) => {
2015-01-12 19:48:07 +00:00
if !options.list_dirs && stat.kind == FileType::Directory {
dirs.push(file.clone());
}
else {
// May as well reuse the stat result from earlier
// instead of just using File::from_path().
files.push(File::with_stat(stat, &path, None));
}
}
Err(e) => println!("{}: {}", file, e),
}
2015-01-12 18:44:39 +00:00
print_dir_names = true;
}
let mut first = files.is_empty();
if !files.is_empty() {
2015-01-12 20:08:42 +00:00
options.view.view(files);
}
for dir_name in dirs.iter() {
if first {
first = false;
}
else {
print!("\n");
}
match Dir::readdir(Path::new(dir_name.clone())) {
Ok(dir) => {
let unsorted_files = dir.files();
2015-01-12 19:48:07 +00:00
let files: Vec<File> = options.transform_files(unsorted_files);
if print_dir_names {
println!("{}:", dir_name);
}
2015-01-12 20:08:42 +00:00
options.view.view(files);
}
Err(e) => {
println!("{}: {}", dir_name, e);
return;
}
};
}
}
2015-01-12 21:14:27 +00:00
fn main() {
let args: Vec<String> = args();
2015-01-12 21:47:05 +00:00
match Options::getopts(args.tail()) {
2015-01-12 21:14:27 +00:00
Ok(options) => exa(&options),
Err(Help(text)) => {
println!("{}", text);
set_exit_status(2);
},
Err(InvalidOptions(e)) => {
println!("{}", e);
set_exit_status(3);
},
2015-01-12 23:31:30 +00:00
Err(Conflict(a, b)) => {
println!("Option --{} conflicts with option {}", a, b);
set_exit_status(3);
},
Err(Useless(a, false, b)) => {
println!("Option --{} is useless without option --{}", a, b);
set_exit_status(3);
},
Err(Useless(a, true, b)) => {
println!("Option --{} is useless given option --{}", a, b);
set_exit_status(3);
},
2015-01-12 21:14:27 +00:00
};
}