main.rs 2.3 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::io::FileType;
B
Ben S 已提交
12
use std::io::fs;
13
use std::os::{args, set_exit_status};
B
Ben S 已提交
14

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

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

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

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

B
Ben S 已提交
35 36 37
    // 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 已提交
38
    for file in options.path_strings() {
B
Ben S 已提交
39 40 41
        let path = Path::new(file);
        match fs::stat(&path) {
            Ok(stat) => {
B
Benjamin Sago 已提交
42
                if !options.list_dirs && stat.kind == FileType::Directory {
B
Ben S 已提交
43 44 45 46 47
                    dirs.push(file.clone());
                }
                else {
                    // May as well reuse the stat result from earlier
                    // instead of just using File::from_path().
B
Ben S 已提交
48
                    files.push(File::with_stat(stat, &path, None));
B
Ben S 已提交
49 50 51 52
                }
            }
            Err(e) => println!("{}: {}", file, e),
        }
B
Benjamin Sago 已提交
53

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

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

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

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

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

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

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

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

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