lsd/src/core.rs

100 lines
2.9 KiB
Rust
Raw Normal View History

2018-12-13 13:51:31 +00:00
use color::{self, Colors};
2019-01-20 10:22:14 +00:00
use display;
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;
use terminal_size::terminal_size;
pub struct Core {
flags: Flags,
2018-12-08 18:52:56 +00:00
icons: Icons,
2019-01-20 10:22:14 +00:00
//display: Display,
colors: Colors,
}
impl Core {
2018-12-13 15:50:47 +00:00
pub fn new(flags: Flags) -> Self {
// terminal_size allows us to know if the stdout is a tty or not.
let tty_available = terminal_size().is_some();
let mut inner_flags = flags;
2018-12-13 13:51:31 +00:00
let color_theme = match (tty_available, flags.color) {
(_, WhenFlag::Never) | (false, WhenFlag::Auto) => color::Theme::NoColor,
2018-12-13 13:51:31 +00:00
_ => color::Theme::Default,
};
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 {
// 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).
inner_flags.display_online = true;
};
2018-12-13 15:50:47 +00:00
Self {
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-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());
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-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);
}
}