exa/src/main.rs

122 lines
3.4 KiB
Rust
Raw Normal View History

2015-01-29 14:06:17 +00:00
#![feature(collections, core, io, libc, os, path, std_misc)]
extern crate ansi_term;
extern crate getopts;
extern crate natord;
extern crate number_prefix;
extern crate users;
#[cfg(feature="git")]
extern crate git2;
use std::old_io::{fs, FileType};
2015-01-12 21:14:27 +00:00
use std::os::{args, set_exit_status};
use dir::Dir;
use file::File;
use options::{Options, DirAction};
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) {
2015-02-01 02:14:31 +00:00
let mut dirs: Vec<Path> = 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-02-01 02:14:31 +00:00
for file in options.path_strs.iter() {
let path = Path::new(file);
match fs::stat(&path) {
Ok(stat) => {
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);
}
else {
files.push(File::with_stat(stat, &path, None, false));
}
}
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() {
options.view(None, &files[], options.filter);
}
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.
if first {
first = false;
}
else {
print!("\n");
}
2015-02-01 02:14:31 +00:00
match Dir::readdir(&dir_path) {
Ok(ref dir) => {
let mut files = dir.files(false);
options.transform_files(&mut files);
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());
}
2015-02-01 02:14:31 +00:00
count += 1;
options.view(Some(dir), &files[], options.filter);
}
Err(e) => {
2015-02-01 02:14:31 +00:00
println!("{}: {}", dir_path.display(), 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
};
}