2018-12-13 13:51:31 +00:00
|
|
|
use color::{self, Colors};
|
2019-01-20 10:22:14 +00:00
|
|
|
use display;
|
2019-01-16 16:39:20 +00:00
|
|
|
use flags::{Flags, IconTheme, WhenFlag};
|
2018-12-13 13:51:31 +00:00
|
|
|
use icon::{self, Icons};
|
2019-01-20 10:22:14 +00:00
|
|
|
use meta::Meta;
|
|
|
|
use sort;
|
|
|
|
use std::path::PathBuf;
|
2018-12-05 19:32:25 +00:00
|
|
|
use terminal_size::terminal_size;
|
2018-11-16 13:19:07 +00:00
|
|
|
|
2018-12-05 19:54:17 +00:00
|
|
|
pub struct Core {
|
2018-12-08 13:00:42 +00:00
|
|
|
flags: Flags,
|
2018-12-08 18:52:56 +00:00
|
|
|
icons: Icons,
|
2019-01-20 10:22:14 +00:00
|
|
|
//display: Display,
|
2018-12-05 19:12:33 +00:00
|
|
|
colors: Colors,
|
2018-11-16 13:19:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 19:54:17 +00:00
|
|
|
impl Core {
|
2018-12-13 15:50:47 +00:00
|
|
|
pub fn new(flags: Flags) -> Self {
|
2018-12-05 19:32:25 +00:00
|
|
|
// terminal_size allows us to know if the stdout is a tty or not.
|
2018-12-05 19:54:17 +00:00
|
|
|
let tty_available = terminal_size().is_some();
|
2018-12-05 19:32:25 +00:00
|
|
|
|
2018-12-08 13:00:42 +00:00
|
|
|
let mut inner_flags = flags;
|
2018-12-05 19:54:17 +00:00
|
|
|
|
2019-01-29 17:16:55 +00:00
|
|
|
let color_theme = match (tty_available, flags.color) {
|
|
|
|
(_, WhenFlag::Never) | (false, WhenFlag::Auto) => color::Theme::NoColor,
|
|
|
|
_ => color::Theme::Default,
|
2018-12-13 13:51:31 +00:00
|
|
|
};
|
|
|
|
|
2019-01-29 17:16:55 +00:00
|
|
|
let icon_theme = match (tty_available, flags.icon, flags.icon_theme) {
|
|
|
|
(_, WhenFlag::Never, _) | (false, WhenFlag::Auto, _) => icon::Theme::NoIcon,
|
|
|
|
(_, _, IconTheme::Fancy) => icon::Theme::Fancy,
|
|
|
|
(_, _, IconTheme::Unicode) => icon::Theme::Unicode,
|
2018-12-08 13:21:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if !tty_available {
|
2018-12-05 19:54:17 +00:00
|
|
|
// The output is not a tty, this means the command is piped. (ex: lsd -l | less)
|
|
|
|
//
|
|
|
|
// Most of the programs does not handle correctly the ansi colors
|
|
|
|
// or require a raw output (like the `wc` command).
|
2018-12-08 13:00:42 +00:00
|
|
|
inner_flags.display_online = true;
|
2018-12-05 19:32:25 +00:00
|
|
|
};
|
|
|
|
|
2018-12-13 15:50:47 +00:00
|
|
|
Self {
|
2018-12-08 13:00:42 +00:00
|
|
|
flags,
|
2019-01-20 10:22:14 +00:00
|
|
|
//display: Display::new(inner_flags),
|
2018-12-13 13:51:31 +00:00
|
|
|
colors: Colors::new(color_theme),
|
|
|
|
icons: Icons::new(icon_theme),
|
2018-12-04 13:54:56 +00:00
|
|
|
}
|
2018-11-16 13:19:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 13:54:56 +00:00
|
|
|
pub fn run(self, paths: Vec<PathBuf>) {
|
2019-01-20 10:22:14 +00:00
|
|
|
let mut meta_list = self.fetch(paths);
|
|
|
|
|
|
|
|
self.sort(&mut meta_list);
|
|
|
|
|
|
|
|
self.display(meta_list)
|
2018-12-04 13:54:56 +00:00
|
|
|
}
|
2018-12-02 16:22:51 +00:00
|
|
|
|
2019-01-20 10:22:14 +00:00
|
|
|
fn fetch(&self, paths: Vec<PathBuf>) -> Vec<Meta> {
|
|
|
|
let mut meta_list = Vec::with_capacity(paths.len());
|
2018-12-19 18:00:21 +00:00
|
|
|
|
2019-01-20 10:22:14 +00:00
|
|
|
let depth = if self.flags.recursive || self.flags.display_tree {
|
|
|
|
self.flags.recursion_depth
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
2018-11-16 13:19:07 +00:00
|
|
|
|
2018-12-04 12:29:54 +00:00
|
|
|
for path in paths {
|
2019-01-20 10:22:14 +00:00
|
|
|
match Meta::from_path_recursive(&path.to_path_buf(), depth, self.flags.display_all) {
|
|
|
|
Ok(meta) => meta_list.push(meta),
|
2019-01-16 12:36:57 +00:00
|
|
|
Err(err) => println!("cannot access '{}': {}", path.display(), err),
|
2019-01-20 10:22:14 +00:00
|
|
|
};
|
2018-12-02 16:22:51 +00:00
|
|
|
}
|
2018-12-02 14:05:27 +00:00
|
|
|
|
2019-01-20 10:22:14 +00:00
|
|
|
meta_list
|
2018-11-24 16:42:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 10:22:14 +00:00
|
|
|
fn sort(&self, metas: &mut Vec<Meta>) {
|
|
|
|
metas.sort_unstable_by(|a, b| sort::by_meta(a, b, self.flags));
|
|
|
|
|
|
|
|
for meta in metas {
|
|
|
|
if let Some(ref mut content) = meta.content {
|
|
|
|
self.sort(content);
|
2018-12-04 13:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 10:22:14 +00:00
|
|
|
fn display(&self, metas: Vec<Meta>) {
|
|
|
|
let output = if self.flags.display_online || self.flags.display_long {
|
|
|
|
display::one_line(metas, self.flags, &self.colors, &self.icons)
|
|
|
|
} else if self.flags.display_tree {
|
|
|
|
display::tree(metas, self.flags, &self.colors, &self.icons)
|
2018-11-24 16:42:39 +00:00
|
|
|
} else {
|
2019-01-20 10:22:14 +00:00
|
|
|
display::grid(metas, self.flags, &self.colors, &self.icons)
|
2018-11-24 11:02:39 +00:00
|
|
|
};
|
|
|
|
|
2019-01-20 10:22:14 +00:00
|
|
|
print!("{}", output);
|
2018-11-16 13:19:07 +00:00
|
|
|
}
|
|
|
|
}
|