main.rs 3.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 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 44 45
                if stat.kind == FileType::Directory && options.dir_action == DirAction::Tree {
                    files.push(File::with_stat(stat, &path, None, true));
                }
                else if stat.kind == FileType::Directory && options.dir_action != DirAction::AsFile {
B
Ben S 已提交
46
                    dirs.push(path);
B
Ben S 已提交
47 48
                }
                else {
B
Ben S 已提交
49
                    files.push(File::with_stat(stat, &path, None, false));
B
Ben S 已提交
50 51 52 53
                }
            }
            Err(e) => println!("{}: {}", file, e),
        }
B
Benjamin Sago 已提交
54

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

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

B
Ben S 已提交
60
    if !files.is_empty() {
61
        options.view(None, &files[], options.filter);
B
Ben S 已提交
62
    }
B
Ben S 已提交
63

B
Ben S 已提交
64 65 66 67 68 69 70 71 72 73
    // 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.
74 75 76 77 78
        if first {
            first = false;
        }
        else {
            print!("\n");
B
Ben S 已提交
79
        }
80

B
Ben S 已提交
81
        match Dir::readdir(&dir_path) {
B
Ben S 已提交
82
            Ok(ref dir) => {
B
Ben S 已提交
83 84
                let mut files = dir.files(false);
                options.transform_files(&mut files);
85

B
Ben S 已提交
86 87 88 89 90 91 92 93 94 95
                // 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 已提交
96
                if count > 1 {
B
Ben S 已提交
97
                    println!("{}:", dir_path.display());
98
                }
B
Ben S 已提交
99
                count += 1;
100

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

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

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