main.rs 3.2 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 getopts;
extern crate natord;
6 7
extern crate number_prefix;
extern crate users;
8

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

B
Ben S 已提交
12
use std::old_io::{fs, FileType};
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, DirAction};
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
    let mut dirs: Vec<Path> = vec![];
B
Ben S 已提交
29
    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
Ben S 已提交
38 39
    for file in options.path_strs.iter() {
        let path = Path::new(file);
B
Ben S 已提交
40 41
        match fs::stat(&path) {
            Ok(stat) => {
B
Ben S 已提交
42 43
                if stat.kind == FileType::Directory && options.dir_action != DirAction::AsFile {
                    dirs.push(path);
B
Ben S 已提交
44 45
                }
                else {
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 62 63 64 65 66 67 68 69 70
    // Directories are put on a stack rather than just being iterated through,
    // as the vector can change as more directories are added.
    loop {
        let dir_path = match dirs.pop() {
            None => break,
            Some(f) => f,
        };

        // Put a gap between directories, or between the list of files and the
        // first directory.
71 72 73 74 75
        if first {
            first = false;
        }
        else {
            print!("\n");
B
Ben S 已提交
76
        }
77

B
Ben S 已提交
78
        match Dir::readdir(&dir_path) {
B
Ben S 已提交
79
            Ok(ref dir) => {
80
                let unsorted_files = dir.files();
B
Benjamin Sago 已提交
81
                let files: Vec<File> = options.transform_files(unsorted_files);
82

B
Ben S 已提交
83 84 85 86 87 88 89 90 91 92
                // When recursing, add any directories to the dirs stack
                // backwards: the *last* element of the stack is used each
                // time, so by inserting them backwards, they get displayed in
                // the correct sort order.
                if options.dir_action == DirAction::Recurse {
                    for dir in files.iter().filter(|f| f.stat.kind == FileType::Directory).rev() {
                        dirs.push(dir.path.clone());
                    }
                }

B
Benjamin Sago 已提交
93
                if count > 1 {
B
Ben S 已提交
94
                    println!("{}:", dir_path.display());
95
                }
B
Ben S 已提交
96
                count += 1;
97

B
Ben S 已提交
98
                options.view(Some(dir), &files[]);
99 100
            }
            Err(e) => {
B
Ben S 已提交
101
                println!("{}: {}", dir_path.display(), e);
102 103 104
                return;
            }
        };
105
    }
106
}
107 108 109 110

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

111
    match Options::getopts(args.tail()) {
112
        Ok(options) => exa(&options),
113
        Err(e) => {
114
            println!("{}", e);
115
            set_exit_status(e.error_code());
116
        },
117 118
    };
}