2018-12-13 14:51:31 +01:00
|
|
|
use color::{self, Colors};
|
2019-01-20 11:22:14 +01:00
|
|
|
use display;
|
2019-01-16 17:39:20 +01:00
|
|
|
use flags::{Flags, IconTheme, WhenFlag};
|
2018-12-13 14:51:31 +01:00
|
|
|
use icon::{self, Icons};
|
2019-01-20 11:22:14 +01:00
|
|
|
use meta::Meta;
|
|
|
|
use sort;
|
|
|
|
use std::path::PathBuf;
|
2018-12-05 20:32:25 +01:00
|
|
|
use terminal_size::terminal_size;
|
2018-11-16 14:19:07 +01:00
|
|
|
|
2018-12-05 20:54:17 +01:00
|
|
|
pub struct Core {
|
2018-12-08 14:00:42 +01:00
|
|
|
flags: Flags,
|
2018-12-08 19:52:56 +01:00
|
|
|
icons: Icons,
|
2019-01-20 11:22:14 +01:00
|
|
|
//display: Display,
|
2018-12-05 20:12:33 +01:00
|
|
|
colors: Colors,
|
2018-11-16 14:19:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-05 20:54:17 +01:00
|
|
|
impl Core {
|
2018-12-13 16:50:47 +01:00
|
|
|
pub fn new(flags: Flags) -> Self {
|
2018-12-05 20:32:25 +01:00
|
|
|
// terminal_size allows us to know if the stdout is a tty or not.
|
2018-12-05 20:54:17 +01:00
|
|
|
let tty_available = terminal_size().is_some();
|
2018-12-05 20:32:25 +01:00
|
|
|
|
2018-12-08 14:00:42 +01:00
|
|
|
let mut inner_flags = flags;
|
2018-12-05 20:54:17 +01:00
|
|
|
|
2018-12-13 14:51:31 +01:00
|
|
|
let color_theme = match (tty_available, flags.color) {
|
2019-01-16 17:39:20 +01:00
|
|
|
(_, WhenFlag::Never) | (false, WhenFlag::Auto) => color::Theme::NoColor,
|
2018-12-13 14:51:31 +01:00
|
|
|
_ => color::Theme::Default,
|
|
|
|
};
|
|
|
|
|
2019-01-14 20:58:14 +01:00
|
|
|
let icon_theme = match (tty_available, flags.icon, flags.icon_theme) {
|
|
|
|
(_, WhenFlag::Never, _) | (false, WhenFlag::Auto, _) => icon::Theme::NoIcon,
|
2019-01-16 17:39:20 +01:00
|
|
|
(_, _, IconTheme::Fancy) => icon::Theme::Fancy,
|
|
|
|
(_, _, IconTheme::Unicode) => icon::Theme::Unicode,
|
2018-12-08 14:21:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if !tty_available {
|
2018-12-05 20:54:17 +01: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 14:00:42 +01:00
|
|
|
inner_flags.display_online = true;
|
2018-12-05 20:32:25 +01:00
|
|
|
};
|
|
|
|
|
2018-12-13 16:50:47 +01:00
|
|
|
Self {
|
2018-12-08 14:00:42 +01:00
|
|
|
flags,
|
2019-01-20 11:22:14 +01:00
|
|
|
//display: Display::new(inner_flags),
|
2018-12-13 14:51:31 +01:00
|
|
|
colors: Colors::new(color_theme),
|
|
|
|
icons: Icons::new(icon_theme),
|
2018-12-04 14:54:56 +01:00
|
|
|
}
|
2018-11-16 14:19:07 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 14:54:56 +01:00
|
|
|
pub fn run(self, paths: Vec<PathBuf>) {
|
2019-01-20 11:22:14 +01:00
|
|
|
let mut meta_list = self.fetch(paths);
|
|
|
|
|
|
|
|
self.sort(&mut meta_list);
|
|
|
|
|
|
|
|
self.display(meta_list)
|
2018-12-04 14:54:56 +01:00
|
|
|
}
|
2018-12-02 17:22:51 +01:00
|
|
|
|
2019-01-20 11:22:14 +01:00
|
|
|
fn fetch(&self, paths: Vec<PathBuf>) -> Vec<Meta> {
|
|
|
|
let mut meta_list = Vec::with_capacity(paths.len());
|
2018-12-19 21:00:21 +03:00
|
|
|
|
2019-01-20 11:22:14 +01:00
|
|
|
let depth = if self.flags.recursive || self.flags.display_tree {
|
|
|
|
self.flags.recursion_depth
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
2018-11-16 14:19:07 +01:00
|
|
|
|
2018-12-04 13:29:54 +01:00
|
|
|
for path in paths {
|
2019-01-20 11:22:14 +01:00
|
|
|
match Meta::from_path_recursive(&path.to_path_buf(), depth, self.flags.display_all) {
|
|
|
|
Ok(meta) => meta_list.push(meta),
|
2019-01-16 13:36:57 +01:00
|
|
|
Err(err) => println!("cannot access '{}': {}", path.display(), err),
|
2019-01-20 11:22:14 +01:00
|
|
|
};
|
2018-12-02 17:22:51 +01:00
|
|
|
}
|
2018-12-02 15:05:27 +01:00
|
|
|
|
2019-01-20 11:22:14 +01:00
|
|
|
meta_list
|
2018-11-24 17:42:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-20 11:22:14 +01: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 14:54:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 11:22:14 +01: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 17:42:39 +01:00
|
|
|
} else {
|
2019-01-20 11:22:14 +01:00
|
|
|
display::grid(metas, self.flags, &self.colors, &self.icons)
|
2018-11-24 12:02:39 +01:00
|
|
|
};
|
|
|
|
|
2019-01-20 11:22:14 +01:00
|
|
|
print!("{}", output);
|
2018-11-16 14:19:07 +01:00
|
|
|
}
|
|
|
|
}
|