main.rs 2.4 KB
Newer Older
B
Benjamin Sago 已提交
1
#![allow(unstable)]
B
Ben S 已提交
2

3
extern crate ansi_term;
4
extern crate number_prefix;
5
extern crate unicode;
6
extern crate users;
7

B
Ben S 已提交
8 9 10
#[cfg(feature="git")]
extern crate git2;

B
Ben S 已提交
11
use std::old_io::{fs, FileType};
12
use std::os::{args, set_exit_status};
B
Ben S 已提交
13

B
Ben S 已提交
14 15
use dir::Dir;
use file::File;
16
use options::Options;
B
Ben S 已提交
17

B
Ben S 已提交
18
pub mod column;
B
Ben S 已提交
19
pub mod dir;
B
Ben S 已提交
20
pub mod file;
21
pub mod filetype;
B
Ben S 已提交
22
pub mod options;
23
pub mod output;
24
pub mod term;
B
Ben S 已提交
25

B
Benjamin Sago 已提交
26
fn exa(options: &Options) {
B
Ben S 已提交
27 28
    let mut dirs: Vec<String> = vec![];
    let mut files: Vec<File> = vec![];
B
Ben S 已提交
29

B
Benjamin Sago 已提交
30 31
    // It's only worth printing out directory names if the user supplied
    // more than one of them.
B
Benjamin Sago 已提交
32
    let mut count = 0;
B
Benjamin Sago 已提交
33

B
Ben S 已提交
34 35 36
    // Separate the user-supplied paths into directories and files.
    // Files are shown first, and then each directory is expanded
    // and listed second.
B
Benjamin Sago 已提交
37
    for file in options.path_strings() {
B
Ben S 已提交
38 39 40
        let path = Path::new(file);
        match fs::stat(&path) {
            Ok(stat) => {
B
Benjamin Sago 已提交
41
                if !options.list_dirs && stat.kind == FileType::Directory {
B
Ben S 已提交
42 43 44 45 46
                    dirs.push(file.clone());
                }
                else {
                    // May as well reuse the stat result from earlier
                    // instead of just using File::from_path().
B
Ben S 已提交
47
                    files.push(File::with_stat(stat, &path, None));
B
Ben S 已提交
48 49 50 51
                }
            }
            Err(e) => println!("{}: {}", file, e),
        }
B
Benjamin Sago 已提交
52

B
Benjamin Sago 已提交
53
        count += 1;
B
Ben S 已提交
54
    }
B
Ben S 已提交
55

B
Ben S 已提交
56
    let mut first = files.is_empty();
B
Ben S 已提交
57

B
Ben S 已提交
58
    if !files.is_empty() {
B
Ben S 已提交
59
        options.view(None, files);
B
Ben S 已提交
60
    }
B
Ben S 已提交
61

B
Ben S 已提交
62
    for dir_name in dirs.iter() {
63 64 65 66 67
        if first {
            first = false;
        }
        else {
            print!("\n");
B
Ben S 已提交
68
        }
69

70
        match Dir::readdir(Path::new(dir_name.clone())) {
B
Ben S 已提交
71
            Ok(ref dir) => {
72
                let unsorted_files = dir.files();
B
Benjamin Sago 已提交
73
                let files: Vec<File> = options.transform_files(unsorted_files);
74

B
Benjamin Sago 已提交
75
                if count > 1 {
76 77 78
                    println!("{}:", dir_name);
                }

B
Ben S 已提交
79
                options.view(Some(dir), files);
80 81 82 83 84 85
            }
            Err(e) => {
                println!("{}: {}", dir_name, e);
                return;
            }
        };
86
    }
87
}
88 89 90 91

fn main() {
    let args: Vec<String> = args();

92
    match Options::getopts(args.tail()) {
93
        Ok(options) => exa(&options),
94
        Err(e) => {
95
            println!("{}", e);
96
            set_exit_status(e.error_code());
97
        },
98 99
    };
}