summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: fd9e52708a62954ecdaf0b288e047d7b52d2cee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#![feature(iter_arith)]
#![feature(convert, fs_mode)]
#![feature(slice_splits, vec_resize)]

extern crate ansi_term;
extern crate datetime;
extern crate getopts;
extern crate libc;
extern crate locale;
extern crate natord;
extern crate num_cpus;
extern crate number_prefix;
extern crate pad;
extern crate scoped_threadpool;
extern crate term_grid;
extern crate unicode_width;
extern crate users;

#[cfg(feature="git")]
extern crate git2;


use std::env;
use std::path::{Component, Path};
use std::process;

use dir::Dir;
use file::File;
use options::{Options, View};

mod colours;
mod column;
mod dir;
mod feature;
mod file;
mod filetype;
mod options;
mod output;
mod term;


#[cfg(not(test))]
struct Exa {
    options: Options,
}

#[cfg(not(test))]
impl Exa {
    fn new(options: Options) -> Exa {
        Exa { options: options }
    }

    fn run(&mut self, args_file_names: &[String]) {
        let mut files = Vec::new();
        let mut dirs = Vec::new();

        for file_name in args_file_names.iter() {
            match File::from_path(Path::new(&file_name), None) {
                Err(e) => {
                    println!("{}: {}", file_name, e);
                },
                Ok(f) => {
                    if f.is_directory() && !self.options.dir_action.treat_dirs_as_files() {
                        match f.to_dir(self.options.should_scan_for_git()) {
                            Ok(d) => dirs.push(d),
                            Err(e) => println!("{}: {}", file_name, e),
                        }
                    }
                    else {
                        files.push(f);
                    }
                },
            }
        }

        let any_files = files.is_empty();
        self.print_files(None, files);

        let is_only_dir = dirs.len() == 1;
        self.print_dirs(dirs, any_files, is_only_dir);
    }

    fn print_dirs(&self, dir_files: Vec<Dir>, mut first: bool, is_only_dir: bool) {
        for dir in dir_files {

            // Put a gap between directories, or between the list of files and the
            // first directory.
            if first {
                first = false;
            }
            else {
                print!("\n");
            }

            if !is_only_dir {
                println!("{}:", dir.path.display());
            }

            let mut children = Vec::new();
            for file in dir.files() {
                match file {
                    Ok(file)       => children.push(file),
                    Err((path, e)) => println!("[{}: {}]", path.display(), e),
                }
            };

            self.options.filter_files(&mut children);
            self.options.sort_files(&mut children);

            if let Some(recurse_opts) = self.options.dir_action.recurse_options() {
                let depth = dir.path.components().filter(|&c| c != Component::CurDir).count() + 1;
                if !recurse_opts.tree && !recurse_opts.is_too_deep(depth) {

                    let mut child_dirs = Vec::new();
                    for child_dir in children.iter().filter(|f| f.is_directory()) {
                        match child_dir.to_dir(false) {
                            Ok(d)  => child_dirs.push(d),
                            Err(e) => println!("{}: {}", child_dir.path.display(), e),
                        }
                    }

                    self.print_files(Some(&dir), children);

                    if !child_dirs.is_empty() {
                        self.print_dirs(child_dirs, false, false);
                    }

                    continue;
                }
            }

            self.print_files(Some(&dir), children);

        }
    }

    fn print_files(&self, dir: Option<&Dir>, files: Vec<File>) {
        match self.options.view {
            View::Grid(g)         => g.view(&files),
            View::Details(d)      => d.view(dir, files),
            View::GridDetails(gd) => gd.view(dir, &files),
            View::Lines(l)        => l.view(&files),
        }
    }
}


#[cfg(not(test))]
fn main() {
    let args: Vec<String> = env::args().skip(1).collect();

    match Options::getopts(&args) {
        Ok((options, paths)) => {
            let mut exa = Exa::new(options);
            exa.run(&paths);
        },
        Err(e) => {
            println!("{}", e);
            process::exit(e.error_code());
        },
    };
}