exa/src/main.rs

208 lines
5.8 KiB
Rust
Raw Normal View History

#![feature(convert, fs_mode)]
#![feature(slice_splits, vec_resize)]
extern crate ansi_term;
extern crate datetime;
extern crate getopts;
extern crate libc;
extern crate locale;
extern crate natord;
extern crate num_cpus;
extern crate number_prefix;
extern crate pad;
extern crate term_grid;
2015-06-05 02:04:56 +00:00
extern crate threadpool;
2015-04-23 12:46:37 +00:00
extern crate unicode_width;
2015-06-08 20:33:39 +00:00
extern crate users;
2015-04-23 12:46:37 +00:00
#[cfg(feature="git")]
extern crate git2;
2015-06-08 20:33:39 +00:00
use std::env;
use std::fs;
use std::path::{Component, Path, PathBuf};
use std::process;
2015-06-08 20:33:39 +00:00
use std::sync::mpsc::channel;
2015-06-05 02:04:56 +00:00
use threadpool::ThreadPool;
use dir::Dir;
use file::File;
use options::{Options, View};
mod colours;
mod column;
mod dir;
mod feature;
mod file;
mod filetype;
mod options;
mod output;
mod term;
2015-06-05 02:04:56 +00:00
2015-02-26 07:42:37 +00:00
#[cfg(not(test))]
2015-05-12 14:38:12 +00:00
struct Exa<'dir> {
count: usize,
options: Options,
dirs: Vec<PathBuf>,
2015-05-12 14:38:12 +00:00
files: Vec<File<'dir>>,
}
2015-01-12 18:44:39 +00:00
2015-02-26 07:42:37 +00:00
#[cfg(not(test))]
2015-05-12 14:38:12 +00:00
impl<'dir> Exa<'dir> {
fn new(options: Options) -> Exa<'dir> {
Exa {
count: 0,
options: options,
dirs: Vec::new(),
files: Vec::new(),
}
}
fn load(&mut self, files: &[String]) {
2015-06-05 02:04:56 +00:00
// Separate the user-supplied paths into directories and files.
// Files are shown first, and then each directory is expanded
// and listed second.
let is_tree = self.options.dir_action.is_tree() || self.options.dir_action.is_as_file();
let total_files = files.len();
// Communication between consumer thread and producer threads
2015-05-12 14:38:12 +00:00
enum StatResult<'dir> {
File(File<'dir>),
2015-05-21 15:09:16 +00:00
Dir(PathBuf),
Error
}
2015-06-05 02:04:56 +00:00
let pool = ThreadPool::new(8 * num_cpus::get());
let (tx, rx) = channel();
for file in files.iter() {
2015-06-05 02:04:56 +00:00
let tx = tx.clone();
let file = file.clone();
// Spawn producer thread
2015-06-05 02:04:56 +00:00
pool.execute(move || {
let path = Path::new(&*file);
2015-06-05 02:04:56 +00:00
let _ = tx.send(match fs::metadata(&path) {
2015-05-16 17:16:35 +00:00
Ok(metadata) => {
if !metadata.is_dir() {
StatResult::File(File::with_metadata(metadata, &path, None, false))
}
else if is_tree {
2015-05-16 17:16:35 +00:00
StatResult::File(File::with_metadata(metadata, &path, None, true))
}
else {
2015-05-21 15:09:16 +00:00
StatResult::Dir(path.to_path_buf())
}
}
Err(e) => {
println!("{}: {}", file, e);
StatResult::Error
}
});
});
}
2015-06-05 02:04:56 +00:00
// Spawn consumer thread
for result in rx.iter().take(total_files) {
match result {
StatResult::File(file) => self.files.push(file),
StatResult::Dir(path) => self.dirs.push(path),
StatResult::Error => ()
}
self.count += 1;
}
}
fn print_files(&self) {
if !self.files.is_empty() {
self.print(None, &self.files[..]);
}
}
fn print_dirs(&mut self) {
let mut first = self.files.is_empty();
// 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 self.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");
}
match Dir::readdir(&dir_path) {
Ok(ref dir) => {
let mut files = dir.files(false);
self.options.transform_files(&mut files);
// 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 let Some(recurse_opts) = self.options.dir_action.recurse_options() {
let depth = dir_path.components().filter(|&c| c != Component::CurDir).count() + 1;
if !recurse_opts.tree && !recurse_opts.is_too_deep(depth) {
for dir in files.iter().filter(|f| f.is_directory()).rev() {
self.dirs.push(dir.path.clone());
}
}
2015-02-01 02:14:31 +00:00
}
if self.count > 1 {
println!("{}:", dir_path.display());
}
self.count += 1;
self.print(Some(dir), &files[..]);
}
Err(e) => {
println!("{}: {}", dir_path.display(), e);
return;
}
};
}
}
fn print(&self, dir: Option<&Dir>, files: &[File]) {
match self.options.view {
View::Grid(g) => g.view(files),
View::Details(d) => d.view(dir, files),
View::GridDetails(gd) => gd.view(dir, files),
View::Lines(l) => l.view(files),
}
}
}
2015-01-12 21:14:27 +00:00
2015-06-08 20:33:39 +00:00
2015-02-26 07:42:37 +00:00
#[cfg(not(test))]
2015-01-12 21:14:27 +00:00
fn main() {
let args: Vec<String> = env::args().skip(1).collect();
2015-01-12 21:14:27 +00:00
match Options::getopts(&args) {
Ok((options, paths)) => {
let mut exa = Exa::new(options);
exa.load(&paths);
exa.print_files();
exa.print_dirs();
},
Err(e) => {
2015-01-12 21:14:27 +00:00
println!("{}", e);
process::exit(e.error_code());
2015-01-12 23:31:30 +00:00
},
2015-01-12 21:14:27 +00:00
};
}