main.rs 2.8 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 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

35 36
    let mut stack = options.path_strs.clone();

B
Ben S 已提交
37 38 39
    // Separate the user-supplied paths into directories and files.
    // Files are shown first, and then each directory is expanded
    // and listed second.
40 41 42 43 44 45 46
    loop {
        let file = match stack.pop() {
            None => break,
            Some(f) => f,
        };

        let path = Path::new(file.clone());
B
Ben S 已提交
47 48
        match fs::stat(&path) {
            Ok(stat) => {
49 50 51 52 53 54
                if stat.kind == FileType::Directory {
                    match options.dir_action {
                        DirAction::AsFile  => files.push(File::with_stat(stat, &path, None)),
                        DirAction::List    => dirs.push(file.clone()),
                        DirAction::Recurse => { /* todo */ },
                    }
B
Ben S 已提交
55 56 57 58
                }
                else {
                    // May as well reuse the stat result from earlier
                    // instead of just using File::from_path().
B
Ben S 已提交
59
                    files.push(File::with_stat(stat, &path, None));
B
Ben S 已提交
60 61 62 63
                }
            }
            Err(e) => println!("{}: {}", file, e),
        }
B
Benjamin Sago 已提交
64

B
Benjamin Sago 已提交
65
        count += 1;
B
Ben S 已提交
66
    }
B
Ben S 已提交
67

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

B
Ben S 已提交
70
    if !files.is_empty() {
B
Ben S 已提交
71
        options.view(None, files);
B
Ben S 已提交
72
    }
B
Ben S 已提交
73

B
Ben S 已提交
74
    for dir_name in dirs.iter() {
75 76 77 78 79
        if first {
            first = false;
        }
        else {
            print!("\n");
B
Ben S 已提交
80
        }
81

82
        match Dir::readdir(Path::new(dir_name.clone())) {
B
Ben S 已提交
83
            Ok(ref dir) => {
84
                let unsorted_files = dir.files();
B
Benjamin Sago 已提交
85
                let files: Vec<File> = options.transform_files(unsorted_files);
86

B
Benjamin Sago 已提交
87
                if count > 1 {
88 89 90
                    println!("{}:", dir_name);
                }

B
Ben S 已提交
91
                options.view(Some(dir), files);
92 93 94 95 96 97
            }
            Err(e) => {
                println!("{}: {}", dir_name, e);
                return;
            }
        };
98
    }
99
}
100 101 102 103

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

104
    match Options::getopts(args.tail()) {
105
        Ok(options) => exa(&options),
106
        Err(e) => {
107
            println!("{}", e);
108
            set_exit_status(e.error_code());
109
        },
110 111
    };
}