2015-01-29 14:06:17 +00:00
|
|
|
#![feature(collections, core, io, libc, os, path, std_misc)]
|
2015-01-05 14:41:43 +00:00
|
|
|
|
2014-07-01 18:00:36 +00:00
|
|
|
extern crate ansi_term;
|
2015-01-31 16:10:40 +00:00
|
|
|
extern crate getopts;
|
|
|
|
extern crate natord;
|
2014-12-18 07:00:31 +00:00
|
|
|
extern crate number_prefix;
|
|
|
|
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;
|
|
|
|
|
2015-01-28 16:55:34 +00:00
|
|
|
use std::old_io::{fs, FileType};
|
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-31 16:10:40 +00:00
|
|
|
use options::{Options, DirAction};
|
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) {
|
2015-02-01 02:14:31 +00:00
|
|
|
let mut dirs: Vec<Path> = vec![];
|
2014-11-26 08:05:07 +00:00
|
|
|
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-02-01 02:14:31 +00:00
|
|
|
for file in options.path_strs.iter() {
|
|
|
|
let path = Path::new(file);
|
2014-11-26 08:05:07 +00:00
|
|
|
match fs::stat(&path) {
|
|
|
|
Ok(stat) => {
|
2015-02-03 13:27:23 +00:00
|
|
|
if stat.kind == FileType::Directory && options.dir_action == DirAction::Tree {
|
|
|
|
files.push(File::with_stat(stat, &path, None, true));
|
|
|
|
}
|
|
|
|
else if stat.kind == FileType::Directory && options.dir_action != DirAction::AsFile {
|
2015-02-01 02:14:31 +00:00
|
|
|
dirs.push(path);
|
2014-11-26 08:05:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-02-03 13:27:23 +00:00
|
|
|
files.push(File::with_stat(stat, &path, None, false));
|
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-02-03 21:26:21 +00:00
|
|
|
options.view(None, &files[], options.filter);
|
2014-11-26 08:05:07 +00:00
|
|
|
}
|
2014-12-12 11:17:55 +00:00
|
|
|
|
2015-02-01 02:14:31 +00:00
|
|
|
// Directories are put on a stack rather than just being iterated through,
|
|
|
|
// as the vector can change as more directories are added.
|
|
|
|
loop {
|
|
|
|
let dir_path = match dirs.pop() {
|
|
|
|
None => break,
|
|
|
|
Some(f) => f,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Put a gap between directories, or between the list of files and the
|
|
|
|
// first directory.
|
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
|
|
|
|
2015-02-01 02:14:31 +00:00
|
|
|
match Dir::readdir(&dir_path) {
|
2015-01-28 22:02:25 +00:00
|
|
|
Ok(ref dir) => {
|
2015-02-04 15:47:52 +00:00
|
|
|
let mut files = dir.files(false);
|
|
|
|
options.transform_files(&mut files);
|
2014-07-22 21:19:51 +00:00
|
|
|
|
2015-02-01 02:14:31 +00:00
|
|
|
// When recursing, add any directories to the dirs stack
|
|
|
|
// backwards: the *last* element of the stack is used each
|
|
|
|
// time, so by inserting them backwards, they get displayed in
|
|
|
|
// the correct sort order.
|
|
|
|
if options.dir_action == DirAction::Recurse {
|
|
|
|
for dir in files.iter().filter(|f| f.stat.kind == FileType::Directory).rev() {
|
|
|
|
dirs.push(dir.path.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 23:35:44 +00:00
|
|
|
if count > 1 {
|
2015-02-01 02:14:31 +00:00
|
|
|
println!("{}:", dir_path.display());
|
2014-07-22 21:19:51 +00:00
|
|
|
}
|
2015-02-01 02:14:31 +00:00
|
|
|
count += 1;
|
2014-07-22 21:19:51 +00:00
|
|
|
|
2015-02-03 21:26:21 +00:00
|
|
|
options.view(Some(dir), &files[], options.filter);
|
2014-07-05 21:36:43 +00:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2015-02-01 02:14:31 +00:00
|
|
|
println!("{}: {}", dir_path.display(), e);
|
2014-07-05 21:36:43 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|