main.rs 4.3 KB
Newer Older
B
Ben S 已提交
1
#![warn(trivial_casts, trivial_numeric_casts)]
B
Ben S 已提交
2
#![warn(unused_qualifications)]
3
#![warn(unused_results)]
B
Ben S 已提交
4

5
extern crate ansi_term;
6
extern crate datetime;
7
extern crate getopts;
8
extern crate libc;
9
extern crate locale;
10
extern crate natord;
B
Benjamin Sago 已提交
11
extern crate num_cpus;
12
extern crate number_prefix;
B
Ben S 已提交
13
extern crate scoped_threadpool;
B
Ben S 已提交
14
extern crate term_grid;
B
Ben S 已提交
15
extern crate unicode_width;
B
Ben S 已提交
16
extern crate users;
B
Ben S 已提交
17

B
Ben S 已提交
18 19
#[cfg(feature="git")] extern crate git2;
#[macro_use] extern crate lazy_static;
B
Ben S 已提交
20

B
Ben S 已提交
21
use std::env;
B
Ben S 已提交
22
use std::path::{Component, Path};
23
use std::process;
B
Ben S 已提交
24

B
Ben S 已提交
25 26
use dir::Dir;
use file::File;
27
use options::{Options, View};
B
Ben S 已提交
28

29
mod colours;
30 31 32 33 34 35 36
mod dir;
mod feature;
mod file;
mod filetype;
mod options;
mod output;
mod term;
B
Ben S 已提交
37

B
Ben S 已提交
38

B
Ben S 已提交
39
struct Exa {
40 41
    options: Options,
}
B
Benjamin Sago 已提交
42

B
Ben S 已提交
43
impl Exa {
B
Ben S 已提交
44
    fn run(&mut self, mut args_file_names: Vec<String>) {
B
Ben S 已提交
45 46
        let mut files = Vec::new();
        let mut dirs = Vec::new();
B
bp 已提交
47

B
Ben S 已提交
48 49 50 51
        if args_file_names.is_empty() {
            args_file_names.push(".".to_owned());
        }

B
Ben S 已提交
52 53 54 55 56 57 58 59 60 61
        for file_name in args_file_names.iter() {
            match File::from_path(Path::new(&file_name), None) {
                Err(e) => {
                    println!("{}: {}", file_name, e);
                },
                Ok(f) => {
                    if f.is_directory() && !self.options.dir_action.treat_dirs_as_files() {
                        match f.to_dir(self.options.should_scan_for_git()) {
                            Ok(d) => dirs.push(d),
                            Err(e) => println!("{}: {}", file_name, e),
62 63
                        }
                    }
B
Ben S 已提交
64 65
                    else {
                        files.push(f);
66
                    }
B
Ben S 已提交
67
                },
B
Ben S 已提交
68 69
            }
        }
B
Ben S 已提交
70

B
Benjamin Sago 已提交
71 72 73 74
        let no_files = files.is_empty();
        if !no_files {
            self.print_files(None, files);
        }
B
Ben S 已提交
75 76

        let is_only_dir = dirs.len() == 1;
B
Benjamin Sago 已提交
77
        self.print_dirs(dirs, no_files, is_only_dir);
78
    }
79

B
Ben S 已提交
80 81
    fn print_dirs(&self, dir_files: Vec<Dir>, mut first: bool, is_only_dir: bool) {
        for dir in dir_files {
82 83 84 85 86 87 88 89 90 91

            // Put a gap between directories, or between the list of files and the
            // first directory.
            if first {
                first = false;
            }
            else {
                print!("\n");
            }

B
Ben S 已提交
92 93 94
            if !is_only_dir {
                println!("{}:", dir.path.display());
            }
B
Ben S 已提交
95

B
Ben S 已提交
96 97 98 99 100 101 102 103
            let mut children = Vec::new();
            for file in dir.files() {
                match file {
                    Ok(file)       => children.push(file),
                    Err((path, e)) => println!("[{}: {}]", path.display(), e),
                }
            };

104 105
            self.options.filter.filter_files(&mut children);
            self.options.filter.sort_files(&mut children);
B
Ben S 已提交
106 107 108 109

            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) {
B
Ben S 已提交
110

B
Ben S 已提交
111 112 113 114 115
                    let mut child_dirs = Vec::new();
                    for child_dir in children.iter().filter(|f| f.is_directory()) {
                        match child_dir.to_dir(false) {
                            Ok(d)  => child_dirs.push(d),
                            Err(e) => println!("{}: {}", child_dir.path.display(), e),
116
                        }
B
Ben S 已提交
117 118
                    }

B
Ben S 已提交
119 120 121 122
                    self.print_files(Some(&dir), children);

                    if !child_dirs.is_empty() {
                        self.print_dirs(child_dirs, false, false);
123 124
                    }

B
Ben S 已提交
125
                    continue;
126
                }
B
Ben S 已提交
127 128 129 130
            }

            self.print_files(Some(&dir), children);

131 132
        }
    }
133

B
Ben S 已提交
134
    fn print_files(&self, dir: Option<&Dir>, files: Vec<File>) {
135
        match self.options.view {
B
Ben S 已提交
136
            View::Grid(g)         => g.view(&files),
B
Ben S 已提交
137
            View::Details(d)      => d.view(dir, files),
B
Ben S 已提交
138 139
            View::GridDetails(gd) => gd.view(dir, &files),
            View::Lines(l)        => l.view(&files),
140
        }
141
    }
142
}
143

B
Ben S 已提交
144

145
fn main() {
B
Ben S 已提交
146
    let args: Vec<String> = env::args().skip(1).collect();
147

B
Ben S 已提交
148
    match Options::getopts(&args) {
149
        Ok((options, paths)) => {
150
            let mut exa = Exa { options: options };
B
Ben S 已提交
151
            exa.run(paths);
152
        },
153
        Err(e) => {
154
            println!("{}", e);
155
            process::exit(e.error_code());
156
        },
157 158
    };
}