enable only certain blocks using --blocks

This commit is contained in:
Abin Simon 2019-03-30 16:28:36 +05:30 committed by Pierre Peltier
parent d300d197f0
commit dd5e8733f2
7 changed files with 138 additions and 62 deletions

View file

@ -165,6 +165,23 @@ pub fn build() -> App<'static, 'static> {
.number_of_values(1)
.help("Sort the directories then the files"),
)
.arg(
Arg::with_name("blocks")
.long("blocks")
.multiple(true)
.possible_values(&[
"type",
"permission",
"user",
"group",
"size",
"date",
"name",
])
.require_delimiter(true)
.number_of_values(1)
.help("Specify the blocks that will be displayed"),
)
.arg(
Arg::with_name("classic")
.long("classic")

View file

@ -26,7 +26,7 @@ impl Core {
#[cfg(target_os = "windows")]
let console_color_ok = ansi_term::enable_ansi_support().is_ok();
let mut inner_flags = flags;
let mut inner_flags = flags.clone();
let color_theme = match (tty_available && console_color_ok, flags.color) {
(_, WhenFlag::Never) | (false, WhenFlag::Auto) => color::Theme::NoColor,
@ -110,7 +110,7 @@ impl Core {
}
fn sort(&self, metas: &mut Vec<Meta>) {
metas.sort_unstable_by(|a, b| sort::by_meta(a, b, self.flags));
metas.sort_unstable_by(|a, b| sort::by_meta(a, b, self.flags.clone()));
for meta in metas {
if let Some(ref mut content) = meta.content {
@ -122,10 +122,10 @@ impl Core {
fn display(&self, metas: Vec<Meta>) {
let output = match self.flags.layout {
Layout::OneLine { .. } => {
display::one_line(metas, self.flags, &self.colors, &self.icons)
display::one_line(metas, &self.flags, &self.colors, &self.icons)
}
Layout::Tree { .. } => display::tree(metas, self.flags, &self.colors, &self.icons),
Layout::Grid => display::grid(metas, self.flags, &self.colors, &self.icons),
Layout::Tree { .. } => display::tree(metas, &self.flags, &self.colors, &self.icons),
Layout::Grid => display::grid(metas, &self.flags, &self.colors, &self.icons),
};
print!("{}", output);
}

View file

@ -1,5 +1,5 @@
use crate::color::Colors;
use crate::flags::{Display, Flags, Layout};
use crate::flags::{Display, Flags, Layout, Block};
use crate::icon::Icons;
use crate::meta::{FileType, Meta};
use ansi_term::{ANSIString, ANSIStrings};
@ -20,26 +20,26 @@ struct PaddingRules {
date: usize,
}
pub fn one_line(metas: Vec<Meta>, flags: Flags, colors: &Colors, icons: &Icons) -> String {
inner_display_one_line(metas, flags, colors, icons, 0)
pub fn one_line(metas: Vec<Meta>, flags: &Flags, colors: &Colors, icons: &Icons) -> String {
inner_display_one_line(metas, &flags, colors, icons, 0)
}
pub fn grid(metas: Vec<Meta>, flags: Flags, colors: &Colors, icons: &Icons) -> String {
pub fn grid(metas: Vec<Meta>, flags: &Flags, colors: &Colors, icons: &Icons) -> String {
let term_width = match terminal_size() {
Some((w, _)) => Some(w.0 as usize),
None => None,
};
inner_display_grid(metas, flags, colors, icons, 0, term_width)
inner_display_grid(metas, &flags, colors, icons, 0, term_width)
}
pub fn tree(metas: Vec<Meta>, flags: Flags, colors: &Colors, icons: &Icons) -> String {
inner_display_tree(metas, flags, colors, icons, 0, "")
pub fn tree(metas: Vec<Meta>, flags: &Flags, colors: &Colors, icons: &Icons) -> String {
inner_display_tree(metas, &flags, colors, icons, 0, "")
}
fn inner_display_one_line(
metas: Vec<Meta>,
flags: Flags,
flags: &Flags,
colors: &Colors,
icons: &Icons,
depth: usize,
@ -53,8 +53,8 @@ fn inner_display_one_line(
padding_rules = Some(PaddingRules {
user: detect_user_length(&metas),
group: detect_group_length(&metas),
size: detect_size_lengths(&metas, flags),
date: detect_date_length(&metas, flags),
size: detect_size_lengths(&metas, &flags),
date: detect_date_length(&metas, &flags),
})
}
@ -71,9 +71,9 @@ fn inner_display_one_line(
}
if let Layout::OneLine { long: true } = flags.layout {
output += &get_long_output(&meta, &colors, &icons, flags, padding_rules.unwrap());
output += &get_long_output(&meta, &colors, &icons, &flags, padding_rules.unwrap());
} else {
output += &get_short_output(&meta, &colors, &icons, flags);
output += &get_short_output(&meta, &colors, &icons, &flags);
}
output.push('\n');
@ -89,7 +89,7 @@ fn inner_display_one_line(
}
output +=
&inner_display_one_line(meta.content.unwrap(), flags, colors, icons, depth + 1);
&inner_display_one_line(meta.content.unwrap(), &flags, colors, icons, depth + 1);
}
}
@ -98,7 +98,7 @@ fn inner_display_one_line(
fn inner_display_grid(
metas: Vec<Meta>,
flags: Flags,
flags: &Flags,
colors: &Colors,
icons: &Icons,
depth: usize,
@ -123,7 +123,7 @@ fn inner_display_grid(
continue;
}
let line_output = get_short_output(&meta, &colors, &icons, flags);
let line_output = get_short_output(&meta, &colors, &icons, &flags);
grid.add(Cell {
width: get_visible_width(&line_output),
contents: line_output,
@ -154,7 +154,7 @@ fn inner_display_grid(
output += &inner_display_grid(
meta.content.unwrap(),
flags,
&flags,
colors,
icons,
depth + 1,
@ -168,7 +168,7 @@ fn inner_display_grid(
fn inner_display_tree(
metas: Vec<Meta>,
flags: Flags,
flags: &Flags,
colors: &Colors,
icons: &Icons,
depth: usize,
@ -204,9 +204,9 @@ fn inner_display_tree(
}
if let Layout::Tree { long: true } = flags.layout {
output += &get_long_output(&meta, &colors, &icons, flags, padding_rules.unwrap());
output += &get_long_output(&meta, &colors, &icons, &flags, padding_rules.unwrap());
} else {
output += &get_short_output(&meta, &colors, &icons, flags);
output += &get_short_output(&meta, &colors, &icons, &flags);
}
output += "\n";
@ -223,7 +223,7 @@ fn inner_display_tree(
output += &inner_display_tree(
meta.content.unwrap(),
flags,
&flags,
colors,
icons,
depth + 1,
@ -260,10 +260,10 @@ fn display_folder_path(meta: &Meta) -> String {
output
}
fn get_short_output(meta: &Meta, colors: &Colors, icons: &Icons, flags: Flags) -> String {
fn get_short_output(meta: &Meta, colors: &Colors, icons: &Icons, flags: &Flags) -> String {
let strings: &[ANSIString] = &[
meta.name.render(colors, icons),
meta.indicator.render(flags),
meta.indicator.render(&flags),
];
ANSIStrings(strings).to_string()
@ -273,28 +273,52 @@ fn get_long_output(
meta: &Meta,
colors: &Colors,
icons: &Icons,
flags: Flags,
flags: &Flags,
padding_rules: PaddingRules,
) -> String {
let strings: &[ANSIString] = &[
meta.file_type.render(colors),
meta.permissions.render(colors),
ANSIString::from(" "),
meta.owner.render_user(colors, padding_rules.user),
ANSIString::from(" "),
meta.owner.render_group(colors, padding_rules.group),
ANSIString::from(" "),
meta.size
.render(colors, padding_rules.size.0, padding_rules.size.1, flags),
ANSIString::from(" "),
meta.date.render(colors, padding_rules.date, flags),
ANSIString::from(" "),
meta.name.render(colors, icons),
meta.indicator.render(flags),
meta.symlink.render(colors),
];
let mut strings: Vec<ANSIString> = Vec::new();
for block in flags.blocks.iter() {
match block {
Block::Permission => {
strings.push(meta.file_type.render(colors));
strings.push(meta.permissions.render(colors));
}
Block::User => strings.push(meta.owner.render_user(colors, padding_rules.user)),
Block::Group => strings.push(meta.owner.render_group(colors, padding_rules.group)),
Block::Size => strings.push(meta.size.render(
colors,
padding_rules.size.0,
padding_rules.size.1,
&flags
)),
Block::Date => strings.push(meta.date.render(colors, padding_rules.date, &flags)),
Block::Name => {
strings.push(meta.name.render(colors, icons));
strings.push(meta.indicator.render(&flags));
strings.push(meta.symlink.render(colors));
}
};
strings.push(ANSIString::from(" ")); // TODO do not add this space to the end
}
// let strings: &[ANSIString] = &[
// meta.file_type.render(colors),
// meta.permissions.render(colors),
// ANSIString::from(" "),
// meta.owner.render_user(colors, padding_rules.user),
// ANSIString::from(" "),
// meta.owner.render_group(colors, padding_rules.group),
// ANSIString::from(" "),
// meta.size
// .render(colors, padding_rules.size.0, padding_rules.size.1),
// ANSIString::from(" "),
// meta.date.render(colors, padding_rules.date, flags),
// ANSIString::from(" "),
// meta.name.render(colors, icons),
// meta.indicator.render(flags),
// meta.symlink.render(colors),
// ];
ANSIStrings(strings).to_string()
ANSIStrings(&strings).to_string()
}
fn get_visible_width(input: &str) -> usize {
@ -337,19 +361,19 @@ fn detect_group_length(metas: &[Meta]) -> usize {
max
}
fn detect_date_length(metas: &[Meta], flags: Flags) -> usize {
fn detect_date_length(metas: &[Meta], flags: &Flags) -> usize {
let mut max_value_length: usize = 0;
for meta in metas {
if meta.date.date_string(flags).len() > max_value_length {
max_value_length = meta.date.date_string(flags).len();
if meta.date.date_string(&flags).len() > max_value_length {
max_value_length = meta.date.date_string(&flags).len();
}
}
max_value_length
}
fn detect_size_lengths(metas: &[Meta], flags: Flags) -> (usize, usize) {
fn detect_size_lengths(metas: &[Meta], flags: &Flags) -> (usize, usize) {
let mut max_value_length: usize = 0;
let mut max_unit_size: usize = 0;
@ -358,8 +382,8 @@ fn detect_size_lengths(metas: &[Meta], flags: Flags) -> (usize, usize) {
max_value_length = meta.size.render_value().len();
}
if meta.size.render_unit(flags).len() > max_unit_size {
max_unit_size = meta.size.render_unit(flags).len();
if meta.size.render_unit(&flags).len() > max_unit_size {
max_unit_size = meta.size.render_unit(&flags).len();
}
}

View file

@ -1,6 +1,6 @@
use clap::{ArgMatches, Error, ErrorKind};
#[derive(Clone, Debug, Copy)]
#[derive(Clone, Debug)]
pub struct Flags {
pub display: Display,
pub layout: Layout,
@ -15,6 +15,7 @@ pub struct Flags {
pub icon: WhenFlag,
pub icon_theme: IconTheme,
pub recursion_depth: usize,
pub blocks: Vec<Block>,
}
impl Flags {
@ -26,6 +27,7 @@ impl Flags {
let size_inputs: Vec<&str> = matches.values_of("size").unwrap().collect();
let date_inputs: Vec<&str> = matches.values_of("date").unwrap().collect();
let dir_order_inputs: Vec<&str> = matches.values_of("group-dirs").unwrap().collect();
let blocks_inputs: Vec<&str> = matches.values_of("blocks").unwrap().collect();
let display = if matches.is_present("all") {
Display::DisplayAll
@ -97,6 +99,7 @@ impl Flags {
sort_by,
sort_order,
size: SizeFlag::from(size_inputs[size_inputs.len() - 1]),
blocks: blocks_inputs.into_iter().map(|b| Block::from(b)).collect(),
// Take only the last value
date: if classic_mode {
DateFlag::Date
@ -139,6 +142,39 @@ impl Default for Flags {
color: WhenFlag::Auto,
icon: WhenFlag::Auto,
icon_theme: IconTheme::Fancy,
blocks: vec![
Block::Permission,
Block::User,
Block::Group,
Block::Size,
Block::Date,
Block::Name,
],
}
}
}
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum Block {
// Type,
Permission,
User,
Group,
Size,
Date,
Name,
}
impl<'a> From<&'a str> for Block {
fn from(block: &'a str) -> Self {
match block {
// "type" => Block::Type,
"permission" => Block::Permission,
"user" => Block::User,
"group" => Block::Group,
"size" => Block::Size,
"date" => Block::Date,
"name" => Block::Name,
_ => panic!("invalid \"time\" flag: {}", block),
}
}
}
@ -189,7 +225,6 @@ pub enum WhenFlag {
Auto,
Never,
}
impl<'a> From<&'a str> for WhenFlag {
fn from(when: &'a str) -> Self {
match when {

View file

@ -25,7 +25,7 @@ impl<'a> From<&'a Metadata> for Date {
}
impl Date {
pub fn render(&self, colors: &Colors, date_alignment: usize, flags: Flags) -> ColoredString {
pub fn render(&self, colors: &Colors, date_alignment: usize, flags: &Flags) -> ColoredString {
let mut content = String::with_capacity(date_alignment + 1);
let now = time::now();
@ -38,7 +38,7 @@ impl Date {
elem = &Elem::Older;
}
let date_string = &self.date_string(flags);
let date_string = &self.date_string(&flags);
content += date_string;
for _ in 0..(date_alignment - date_string.len()) {
@ -47,7 +47,7 @@ impl Date {
colors.colorize(content, elem)
}
pub fn date_string(&self, flags: Flags) -> String {
pub fn date_string(&self, flags: &Flags) -> String {
match flags.date {
DateFlag::Date => self.0.ctime().to_string(),
DateFlag::Relative => format!("{}", HumanTime::from(self.0 - time::now())),

View file

@ -22,7 +22,7 @@ impl From<FileType> for Indicator {
}
impl Indicator {
pub fn render(&self, flags: Flags) -> ColoredString {
pub fn render(&self, flags: &Flags) -> ColoredString {
if flags.display_indicators {
ANSIString::from(self.0)
} else {

View file

@ -69,12 +69,12 @@ impl Size {
colors: &Colors,
value_alignment: usize,
unit_alignment: usize,
flags: Flags,
flags: &Flags,
) -> ColoredString {
let mut content = String::with_capacity(value_alignment + unit_alignment + 1);
let value = self.render_value();
let unit = self.render_unit(flags);
let unit = self.render_unit(&flags);
for _ in 0..(value_alignment - value.len()) {
content.push(' ');
@ -84,7 +84,7 @@ impl Size {
if flags.size != SizeFlag::Short {
content.push(' ');
}
content += &self.render_unit(flags);
content += &self.render_unit(&flags);
for _ in 0..(unit_alignment - unit.len()) {
content.push(' ');
@ -128,7 +128,7 @@ impl Size {
}
}
pub fn render_unit(&self, flags: Flags) -> String {
pub fn render_unit(&self, flags: &Flags) -> String {
match flags.size {
SizeFlag::Default => match self.unit {
Unit::None => String::from("-"),