exa/src/main.rs

98 lines
2.3 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;
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.
2015-01-12 23:35:44 +00:00
let mut count = 0;
2015-01-12 18:44:39 +00:00
// 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
2015-01-12 23:35:44 +00:00
count += 1;
}
let mut first = files.is_empty();
if !files.is_empty() {
2015-01-24 13:44:25 +00:00
options.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);
2015-01-12 23:35:44 +00:00
if count > 1 {
println!("{}:", dir_name);
}
2015-01-24 13:44:25 +00:00
options.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(e) => {
2015-01-12 21:14:27 +00:00
println!("{}", e);
set_exit_status(e.error_code());
2015-01-12 23:31:30 +00:00
},
2015-01-12 21:14:27 +00:00
};
}