summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre Peltier <pierre.peltier@adevinta.com>2019-10-23 18:13:58 +0200
committerAbin Simon <abinsimon10@gmail.com>2019-12-06 11:35:03 +0530
commit03adf997cc3461abb5bc6c1c29732980012ab29d (patch)
tree411e275685cb60c8a250e175630e96d61a3797a0
parent2ee9f8b46a25b1c5443497a93c91a6ed0a93f749 (diff)
Remove the short/long output and use only the blocks
-rw-r--r--src/app.rs11
-rw-r--r--src/core.rs2
-rw-r--r--src/display.rs30
-rw-r--r--src/flags.rs87
4 files changed, 67 insertions, 63 deletions
diff --git a/src/app.rs b/src/app.rs
index 203e48f..37b8c0b 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -185,8 +185,15 @@ pub fn build() -> App<'static, 'static> {
.multiple(true)
.number_of_values(1)
.require_delimiter(true)
- .possible_values(&["permission", "user", "group", "size", "date", "name"])
- .default_value("permission,user,group,size,date,name")
+ .possible_values(&[
+ "permission",
+ "user",
+ "group",
+ "size",
+ "date",
+ "name",
+ "name-with-symlink",
+ ])
.help("Specify the blocks that will be displayed and in what order"),
)
.arg(
diff --git a/src/core.rs b/src/core.rs
index 4591f04..74abc77 100644
--- a/src/core.rs
+++ b/src/core.rs
@@ -56,7 +56,7 @@ impl Core {
//
// Most of the programs does not handle correctly the ansi colors
// or require a raw output (like the `wc` command).
- inner_flags.layout = Layout::OneLine { long: false };
+ inner_flags.layout = Layout::OneLine;
};
Self {
diff --git a/src/display.rs b/src/display.rs
index 81a7518..3649594 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -1,5 +1,5 @@
use crate::color::Colors;
-use crate::flags::{Block, Display, Flags, Layout};
+use crate::flags::{Block, Display, Flags};
use crate::icon::Icons;
use crate::meta::{FileType, Meta};
use ansi_term::{ANSIString, ANSIStrings};
@@ -57,12 +57,7 @@ fn inner_display_one_line(
output.push_str(" ");
}
- if let Layout::OneLine { long: true } = flags.layout {
- output += &get_long_output(&meta, &colors, &icons, &flags, &padding_rules);
- } else {
- output += &get_short_output(&meta, &colors, &icons, &flags);
- }
-
+ output += &get_output(&meta, &colors, &icons, &flags, &padding_rules);
output.push('\n');
}
@@ -98,6 +93,8 @@ fn inner_display_grid(
) -> String {
let mut output = String::new();
+ let padding_rules = get_padding_rules(&metas, flags, icons);
+
let mut grid = Grid::new(GridOptions {
filling: Filling::Spaces(2),
direction: Direction::TopToBottom,
@@ -115,7 +112,7 @@ fn inner_display_grid(
continue;
}
- let line_output = get_short_output(&meta, &colors, &icons, &flags);
+ let line_output = get_output(&meta, &colors, &icons, &flags, &padding_rules);
grid.add(Cell {
width: get_visible_width(&line_output),
contents: line_output,
@@ -185,11 +182,7 @@ fn inner_display_tree(
output += " ";
}
- if let Layout::Tree { long: true } = flags.layout {
- output += &get_long_output(&meta, &colors, &icons, &flags, &padding_rules);
- } else {
- output += &get_short_output(&meta, &colors, &icons, &flags);
- }
+ output += &get_output(&meta, &colors, &icons, &flags, &padding_rules);
output += "\n";
if meta.content.is_some() {
@@ -242,16 +235,7 @@ fn display_folder_path(meta: &Meta) -> String {
output
}
-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),
- ];
-
- ANSIStrings(strings).to_string()
-}
-
-fn get_long_output(
+fn get_output(
meta: &Meta,
colors: &Colors,
icons: &Icons,
diff --git a/src/flags.rs b/src/flags.rs
index 32a9bce..8813cca 100644
--- a/src/flags.rs
+++ b/src/flags.rs
@@ -18,7 +18,6 @@ pub struct Flags {
pub icon_theme: IconTheme,
pub recursion_depth: usize,
pub blocks: Vec<Block>,
- pub no_symlink: bool,
pub total_size: bool,
pub ignore_globs: GlobSet,
}
@@ -32,8 +31,12 @@ 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 ignore_globs_inputs: Vec<&str> = matches.values_of("ignore-glob").unwrap().collect();
+ let blocks_inputs: Vec<&str> = if let Some(blocks) = matches.values_of("blocks") {
+ blocks.collect()
+ } else {
+ vec![]
+ };
let display = if matches.is_present("all") {
Display::DisplayAll
@@ -52,38 +55,36 @@ impl Flags {
} else {
SortFlag::Name
};
+
let sort_order = if matches.is_present("reverse") {
SortOrder::Reverse
} else {
SortOrder::Default
};
+
let layout = if matches.is_present("tree") {
- Layout::Tree {
- long: matches.is_present("long"),
- }
+ Layout::Tree
} else if matches.is_present("long") {
- Layout::OneLine { long: true }
+ Layout::OneLine
} else if matches.is_present("oneline") {
- Layout::OneLine { long: false }
+ Layout::OneLine
+ } else if blocks_inputs.len() > 1 {
+ Layout::OneLine
} else {
Layout::Grid
};
+
let recursive = matches.is_present("recursive");
- let recursion_input = matches.values_of("depth").and_then(|values| values.last());
- let recursion_depth = match recursion_input {
- Some(str) if recursive || layout == Layout::Tree {
- long: matches.is_present("long"),
- } => {
- match str.parse::<usize>() {
- Ok(val) => val,
- Err(_) => {
- return Err(Error::with_description(
- "The argument '--depth' requires a valid positive number",
- ErrorKind::ValueValidation,
- ));
- }
+ let recursion_depth = match matches.value_of("depth") {
+ Some(str) if recursive || layout == Layout::Tree => match str.parse::<usize>() {
+ Ok(val) => val,
+ Err(_) => {
+ return Err(Error::with_description(
+ "The argument '--depth' requires a valid positive number",
+ ErrorKind::ValueValidation,
+ ));
}
- }
+ },
Some(_) => {
return Err(Error::with_description(
"The argument '--depth' requires '--tree' or '--recursive'",
@@ -92,7 +93,28 @@ impl Flags {
}
None => usize::max_value(),
};
- let no_symlink = matches.is_present("no-symlink");
+
+ let mut blocks: Vec<Block> = if !blocks_inputs.is_empty() {
+ blocks_inputs.into_iter().map(|b| Block::from(b)).collect()
+ } else if matches.is_present("long") {
+ vec![
+ Block::Permission,
+ Block::User,
+ Block::Group,
+ Block::Size,
+ Block::Date,
+ Block::Name,
+ ]
+ } else {
+ vec![Block::NameWithSymlink]
+ };
+
+ if matches.is_present("no-symlink") {
+ if let Ok(idx) = blocks.binary_search(&Block::NameWithSymlink) {
+ blocks[idx] = Block::Name;
+ }
+ }
+
let total_size = matches.is_present("total-size");
let mut ignore_globs_builder = GlobSetBuilder::new();
@@ -128,8 +150,8 @@ impl Flags {
sort_by,
sort_order,
size: SizeFlag::from(size_inputs[size_inputs.len() - 1]),
- blocks: blocks_inputs.into_iter().map(Block::from).collect(),
ignore_globs,
+ blocks,
// Take only the last value
date: if classic_mode {
DateFlag::Date
@@ -153,7 +175,6 @@ impl Flags {
} else {
DirOrderFlag::from(dir_order_inputs[dir_order_inputs.len() - 1])
},
- no_symlink,
total_size,
})
}
@@ -176,15 +197,7 @@ impl Default for Flags {
prefix_indent: false,
icon: WhenFlag::Auto,
icon_theme: IconTheme::Fancy,
- blocks: vec![
- Block::Permission,
- Block::User,
- Block::Group,
- Block::Size,
- Block::Date,
- Block::Name,
- ],
- no_symlink: false,
+ blocks: vec![],
total_size: false,
ignore_globs: GlobSet::empty(),
}
@@ -331,8 +344,8 @@ impl<'a> From<&'a str> for IconTheme {
#[derive(Clone, Debug, Copy, PartialEq, Eq)]
pub enum Layout {
Grid,
- Tree { long: bool },
- OneLine { long: bool },
+ Tree,
+ OneLine,
}
#[cfg(test)]
@@ -366,7 +379,7 @@ mod test {
#[test]
fn test_duplicate_depth() {
let matches = app::build()
- .get_matches_from_safe(vec!["lsd", "--tree", "--depth", "1", "--depth", "2"])
+ .get_matches_from_safe(vec!["lsd", "--tree", "--depth", "1", "--depth", "2"])
.unwrap();
let res = Flags::from_matches(&matches);
@@ -377,7 +390,7 @@ mod test {
#[test]
fn test_missing_depth() {
let matches = app::build()
- .get_matches_from_safe(vec!["lsd", "--tree"])
+ .get_matches_from_safe(vec!["lsd", "--tree"])
.unwrap();
let res = Flags::from_matches(&matches);