summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierre Peltier <pierre.peltier@adevinta.com>2019-10-24 17:18:42 +0200
committerAbin Simon <abinsimon10@gmail.com>2019-12-06 11:35:03 +0530
commit6ca2bd948d5af61a2f7584eda1bec4c4d1923586 (patch)
tree57a958f62ec996293a38ef9a9cadf01ae3126337
parentf6c3e684639a9265fa132f02561333dd4aaa9c92 (diff)
Merge the display functions
-rw-r--r--src/core.rs11
-rw-r--r--src/display.rs96
2 files changed, 24 insertions, 83 deletions
diff --git a/src/core.rs b/src/core.rs
index 74abc77..60c73e7 100644
--- a/src/core.rs
+++ b/src/core.rs
@@ -134,13 +134,12 @@ impl Core {
}
fn display(&self, metas: &[Meta]) {
- let output = match self.flags.layout {
- Layout::OneLine { .. } => {
- 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),
+ let output = if self.flags.layout == Layout::Tree {
+ display::tree(&metas, &self.flags, &self.colors, &self.icons)
+ } else {
+ display::grid(&metas, &self.flags, &self.colors, &self.icons)
};
+
print!("{}", output);
}
}
diff --git a/src/display.rs b/src/display.rs
index 0ec1d7c..9cd90ee 100644
--- a/src/display.rs
+++ b/src/display.rs
@@ -1,5 +1,5 @@
use crate::color::{ColoredString, Colors};
-use crate::flags::{Block, Display, Flags};
+use crate::flags::{Block, Display, Flags, Layout};
use crate::icon::Icons;
use crate::meta::{FileType, Meta};
use ansi_term::{ANSIString, ANSIStrings};
@@ -13,10 +13,6 @@ const LINE: &str = "\u{2502} "; // "├ "
const CORNER: &str = "\u{2514}\u{2500}\u{2500}"; // "└──"
const BLANK: &str = " ";
-pub fn one_line(metas: &[Meta], flags: &Flags, colors: &Colors, icons: &Icons) -> String {
- inner_display_one_line(metas, &flags, colors, icons, 0)
-}
-
pub fn grid(metas: &[Meta], flags: &Flags, colors: &Colors, icons: &Icons) -> String {
let term_width = match terminal_size() {
Some((w, _)) => Some(w.0 as usize),
@@ -30,19 +26,24 @@ pub fn tree(metas: &[Meta], flags: &Flags, colors: &Colors, icons: &Icons) -> St
inner_display_tree(metas, &flags, colors, icons, 0, "")
}
-fn inner_display_one_line(
+fn inner_display_grid(
metas: &[Meta],
flags: &Flags,
colors: &Colors,
icons: &Icons,
depth: usize,
+ term_width: Option<usize>,
) -> String {
let mut output = String::new();
let padding_rules = get_padding_rules(&metas, flags);
let mut grid = Grid::new(GridOptions {
filling: Filling::Spaces(1),
- direction: Direction::LeftToRight,
+ direction: if flags.layout == Layout::OneLine {
+ Direction::LeftToRight
+ } else {
+ Direction::TopToBottom
+ },
});
// The first iteration (depth == 0) corresponds to the inputs given by the
@@ -73,80 +74,21 @@ fn inner_display_one_line(
}
}
- output += &grid.fit_into_columns(flags.blocks.len()).to_string();
-
- let should_display_folder_path = should_display_folder_path(depth, &metas);
-
- // print the folder content
- for meta in metas {
- if meta.content.is_some() {
- if should_display_folder_path {
- output += &display_folder_path(&meta);
+ if flags.layout == Layout::Grid {
+ if let Some(tw) = term_width {
+ if let Some(gridded_output) = grid.fit_into_width(tw) {
+ output += &gridded_output.to_string();
+ } else {
+ //does not fit into grid, usually because (some) filename(s)
+ //are longer or almost as long as term_width
+ //print line by line instead!
+ output += &grid.fit_into_columns(1).to_string();
}
-
- output += &inner_display_one_line(
- meta.content.as_ref().unwrap(),
- &flags,
- colors,
- icons,
- depth + 1,
- );
- }
- }
-
- output
-}
-
-fn inner_display_grid(
- metas: &[Meta],
- flags: &Flags,
- colors: &Colors,
- icons: &Icons,
- depth: usize,
- term_width: Option<usize>,
-) -> String {
- let mut output = String::new();
-
- let padding_rules = get_padding_rules(&metas, flags);
-
- let mut grid = Grid::new(GridOptions {
- filling: Filling::Spaces(2),
- direction: Direction::TopToBottom,
- });
-
- // The first iteration (depth == 0) corresponds to the inputs given by the
- // user. We defer displaying directories given by the user unless we've been
- // asked to display the directory itself (rather than its contents).
- let skip_dirs = (depth == 0) && (flags.display != Display::DisplayDirectoryItself);
-
- // print the files first.
- for meta in metas {
- // Maybe skip showing the directory meta now; show its contents later.
- if let (true, FileType::Directory { .. }) = (skip_dirs, meta.file_type) {
- continue;
- }
-
- for block in get_output(&meta, &colors, &icons, &flags, &padding_rules) {
- let block_str = block.to_string();
-
- grid.add(Cell {
- width: get_visible_width(&block_str),
- contents: block_str,
- });
- }
- }
-
- if let Some(tw) = term_width {
- if let Some(gridded_output) = grid.fit_into_width(tw) {
- output += &gridded_output.to_string();
} else {
- //does not fit into grid, usually because (some) filename(s)
- //are longer or almost as long as term_width
- //print line by line instead!
output += &grid.fit_into_columns(1).to_string();
}
} else {
- output += &grid.fit_into_columns(1).to_string();
+ output += &grid.fit_into_columns(flags.blocks.len()).to_string();
}
let should_display_folder_path = should_display_folder_path(depth, &metas);
@@ -159,7 +101,7 @@ fn inner_display_grid(
}
output += &inner_display_grid(
- &meta.content.as_ref().unwrap(),
+ meta.content.as_ref().unwrap(),
&flags,
colors,
icons,