main.rs 2.4 KB
Newer Older
B
Ben S 已提交
1
#![feature(collections, core, io, libc, os, path, std_misc)]
B
Ben S 已提交
2

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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