2015-01-12 00:31:24 +00:00
|
|
|
#![allow(unstable)]
|
2015-01-05 14:41:43 +00:00
|
|
|
|
2014-07-01 18:00:36 +00:00
|
|
|
extern crate ansi_term;
|
2014-12-18 07:00:31 +00:00
|
|
|
extern crate number_prefix;
|
2014-07-21 21:05:39 +00:00
|
|
|
extern crate unicode;
|
2014-12-18 07:00:31 +00:00
|
|
|
extern crate users;
|
2014-07-21 21:05:39 +00:00
|
|
|
|
2015-01-27 15:01:17 +00:00
|
|
|
#[cfg(feature="git")]
|
|
|
|
extern crate git2;
|
|
|
|
|
2014-12-02 14:20:28 +00:00
|
|
|
use std::io::FileType;
|
2014-12-12 11:17:55 +00:00
|
|
|
use std::io::fs;
|
2015-01-12 21:14:27 +00:00
|
|
|
use std::os::{args, set_exit_status};
|
2014-05-04 20:33:14 +00:00
|
|
|
|
2014-12-12 11:17:55 +00:00
|
|
|
use dir::Dir;
|
|
|
|
use file::File;
|
2015-01-12 20:08:42 +00:00
|
|
|
use options::Options;
|
2014-12-12 11:17:55 +00:00
|
|
|
|
2014-05-04 20:33:14 +00:00
|
|
|
pub mod column;
|
2014-06-16 23:27:05 +00:00
|
|
|
pub mod dir;
|
2014-05-04 20:33:14 +00:00
|
|
|
pub mod file;
|
2014-06-17 08:35:40 +00:00
|
|
|
pub mod filetype;
|
2014-05-24 01:17:43 +00:00
|
|
|
pub mod options;
|
2015-01-12 20:08:42 +00:00
|
|
|
pub mod output;
|
2014-07-22 14:41:20 +00:00
|
|
|
pub mod term;
|
2014-05-04 16:01:54 +00:00
|
|
|
|
2015-01-12 19:48:07 +00:00
|
|
|
fn exa(options: &Options) {
|
2014-11-26 08:05:07 +00:00
|
|
|
let mut dirs: Vec<String> = vec![];
|
|
|
|
let mut files: Vec<File> = vec![];
|
2014-12-12 11:17:55 +00:00
|
|
|
|
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
|
|
|
|
2014-11-26 08:05:07 +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() {
|
2014-11-26 08:05:07 +00:00
|
|
|
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 {
|
2014-11-26 08:05:07 +00:00
|
|
|
dirs.push(file.clone());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// May as well reuse the stat result from earlier
|
|
|
|
// instead of just using File::from_path().
|
2014-12-12 14:06:48 +00:00
|
|
|
files.push(File::with_stat(stat, &path, None));
|
2014-11-26 08:05:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => println!("{}: {}", file, e),
|
|
|
|
}
|
2015-01-12 18:44:39 +00:00
|
|
|
|
2015-01-12 23:35:44 +00:00
|
|
|
count += 1;
|
2014-11-26 08:05:07 +00:00
|
|
|
}
|
2014-11-25 01:27:26 +00:00
|
|
|
|
2014-11-26 08:05:07 +00:00
|
|
|
let mut first = files.is_empty();
|
2014-11-25 01:27:26 +00:00
|
|
|
|
2014-11-26 08:05:07 +00:00
|
|
|
if !files.is_empty() {
|
2015-01-24 13:44:25 +00:00
|
|
|
options.view(files);
|
2014-11-26 08:05:07 +00:00
|
|
|
}
|
2014-12-12 11:17:55 +00:00
|
|
|
|
2014-12-12 14:06:48 +00:00
|
|
|
for dir_name in dirs.iter() {
|
2014-07-05 21:36:43 +00:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print!("\n");
|
2014-06-20 20:07:53 +00:00
|
|
|
}
|
2014-06-21 17:12:29 +00:00
|
|
|
|
2014-07-05 21:36:43 +00:00
|
|
|
match Dir::readdir(Path::new(dir_name.clone())) {
|
|
|
|
Ok(dir) => {
|
2014-07-22 21:19:51 +00:00
|
|
|
let unsorted_files = dir.files();
|
2015-01-12 19:48:07 +00:00
|
|
|
let files: Vec<File> = options.transform_files(unsorted_files);
|
2014-07-22 21:19:51 +00:00
|
|
|
|
2015-01-12 23:35:44 +00:00
|
|
|
if count > 1 {
|
2014-07-22 21:19:51 +00:00
|
|
|
println!("{}:", dir_name);
|
|
|
|
}
|
|
|
|
|
2015-01-24 13:44:25 +00:00
|
|
|
options.view(files);
|
2014-07-05 21:36:43 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("{}: {}", dir_name, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2014-06-21 17:12:29 +00:00
|
|
|
}
|
2014-07-05 21:36:43 +00:00
|
|
|
}
|
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),
|
2015-01-23 19:27:06 +00:00
|
|
|
Err(e) => {
|
2015-01-12 21:14:27 +00:00
|
|
|
println!("{}", e);
|
2015-01-23 19:27:06 +00:00
|
|
|
set_exit_status(e.error_code());
|
2015-01-12 23:31:30 +00:00
|
|
|
},
|
2015-01-12 21:14:27 +00:00
|
|
|
};
|
|
|
|
}
|