main.rs 4.3 KB
Newer Older
B
Ben S 已提交
1
#![feature(iter_arith)]
2
#![feature(convert, fs_mode)]
B
Ben S 已提交
3
#![feature(slice_splits, vec_resize)]
4

B
Ben S 已提交
5
#![warn(trivial_casts, trivial_numeric_casts)]
6
#![warn(unused_extern_crates, unused_qualifications)]
B
Ben S 已提交
7

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

B
Ben S 已提交
21 22 23
#[cfg(feature="git")]
extern crate git2;

B
Ben S 已提交
24

B
Ben S 已提交
25
use std::env;
B
Ben S 已提交
26
use std::path::{Component, Path};
27
use std::process;
B
Ben S 已提交
28

B
Ben S 已提交
29 30
use dir::Dir;
use file::File;
31
use options::{Options, View};
B
Ben S 已提交
32

33
mod colours;
34 35 36 37 38 39 40 41
mod column;
mod dir;
mod feature;
mod file;
mod filetype;
mod options;
mod output;
mod term;
B
Ben S 已提交
42

B
Ben S 已提交
43

B
Ben S 已提交
44
struct Exa {
45 46
    options: Options,
}
B
Benjamin Sago 已提交
47

B
Ben S 已提交
48 49 50 51
impl Exa {
    fn run(&mut self, args_file_names: &[String]) {
        let mut files = Vec::new();
        let mut dirs = Vec::new();
B
bp 已提交
52

B
Ben S 已提交
53 54 55 56 57 58 59 60 61 62
        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),
63 64
                        }
                    }
B
Ben S 已提交
65 66
                    else {
                        files.push(f);
67
                    }
B
Ben S 已提交
68
                },
B
Ben S 已提交
69 70
            }
        }
B
Ben S 已提交
71

B
Ben S 已提交
72 73 74 75 76
        let any_files = files.is_empty();
        self.print_files(None, files);

        let is_only_dir = dirs.len() == 1;
        self.print_dirs(dirs, any_files, is_only_dir);
77
    }
78

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

            // 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 已提交
91 92 93
            if !is_only_dir {
                println!("{}:", dir.path.display());
            }
B
Ben S 已提交
94

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

            self.options.filter_files(&mut children);
            self.options.sort_files(&mut children);

            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 已提交
109

B
Ben S 已提交
110 111 112 113 114
                    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),
115
                        }
B
Ben S 已提交
116 117
                    }

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

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

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

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

130 131
        }
    }
132

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

B
Ben S 已提交
143

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

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