summaryrefslogtreecommitdiffstats
path: root/src/display
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2020-12-14 08:42:58 +0100
committerCanop <cano.petrole@gmail.com>2020-12-14 08:42:58 +0100
commit1480427fefc1f96c7b6c8aace861024d7da753a3 (patch)
treecf3320957e946321515f356b8a4a0c37feec651b /src/display
parent2c06c7ca0a580618d932325e15d8d40e7640f24d (diff)
improve determination of whether a space is needed left of the tree
Cols order setting has been moved from context to tree options which is both more logical and simpler to deal with.
Diffstat (limited to 'src/display')
-rw-r--r--src/display/col.rs43
-rw-r--r--src/display/displayable_tree.rs28
2 files changed, 47 insertions, 24 deletions
diff --git a/src/display/col.rs b/src/display/col.rs
index 04edb57..270a2f2 100644
--- a/src/display/col.rs
+++ b/src/display/col.rs
@@ -1,5 +1,8 @@
use {
- crate::errors::ConfError,
+ crate::{
+ errors::ConfError,
+ tree::Tree,
+ },
serde::Deserialize,
std::{
convert::TryFrom,
@@ -38,7 +41,7 @@ pub enum Col {
Name,
}
-pub type Cols = [Col;COLS_COUNT];
+pub type Cols = [Col; COLS_COUNT];
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
@@ -67,8 +70,8 @@ impl FromStr for Col {
let s = s.to_lowercase();
match s.as_ref() {
"m" | "mark" => Ok(Self::Mark),
- "g" | "git" => Ok(Self::Git),
- "b" | "branch" => Ok(Self::Branch),
+ "g" | "git" => Ok(Self::Git),
+ "b" | "branch" => Ok(Self::Branch),
"p" | "permission" => Ok(Self::Permission),
"d" | "date" => Ok(Self::Date),
"s" | "size" => Ok(Self::Size),
@@ -76,20 +79,48 @@ impl FromStr for Col {
"n" | "name" => Ok(Self::Name),
_ => Err(ConfError::InvalidCols {
details: format!("column not recognized : {}", s),
- })
+ }),
}
}
}
impl Col {
+ /// return the index of the column among the complete Cols ordered list
pub fn index_in(self, cols: &Cols) -> Option<usize> {
for (idx, col) in cols.iter().enumerate() {
- if *col==self {
+ if *col == self {
return Some(idx);
}
}
None
}
+ /// tell whether this column should have an empty character left
+ pub fn needs_left_margin(self) -> bool {
+ match self {
+ Col::Mark => false,
+ Col::Git => false,
+ Col::Size => true,
+ Col::Date => true,
+ Col::Permission => true,
+ Col::Count => false,
+ Col::Branch => false,
+ Col::Name => false,
+ }
+ }
+ pub fn is_visible(self, tree: &Tree) -> bool {
+ let tree_options = &tree.options;
+ match self {
+ Col::Mark => tree_options.show_selection_mark,
+ Col::Git => tree.git_status.is_some(),
+ Col::Size => tree_options.show_sizes,
+ Col::Date => tree_options.show_dates,
+ Col::Permission => tree_options.show_permissions,
+ Col::Count => tree_options.show_counts,
+ Col::Branch => true,
+ Col::Name => true,
+ }
+
+ }
}
impl TryFrom<&ColsConf> for Cols {
diff --git a/src/display/displayable_tree.rs b/src/display/displayable_tree.rs
index fcb1a3e..09c54d9 100644
--- a/src/display/displayable_tree.rs
+++ b/src/display/displayable_tree.rs
@@ -1,7 +1,6 @@
use {
super::{
Col,
- Cols,
CropWriter,
GitStatusDisplay,
SPACE_FILLING, BRANCH_FILLING,
@@ -40,8 +39,6 @@ pub struct DisplayableTree<'s, 't> {
pub skin: &'s StyleMap,
pub area: termimad::Area,
pub in_app: bool, // if true we show the selection and scrollbar
- pub cols: &'s Cols,
- pub show_selection_mark: bool,
pub ext_colors: &'s ExtColorMap,
}
@@ -50,15 +47,12 @@ impl<'s, 't> DisplayableTree<'s, 't> {
pub fn out_of_app(
tree: &'t Tree,
skin: &'s StyleMap,
- cols: &'s Cols,
ext_colors: &'s ExtColorMap,
width: u16,
) -> DisplayableTree<'s, 't> {
DisplayableTree {
tree,
skin,
- cols,
- show_selection_mark: false,
ext_colors,
area: termimad::Area {
left: 0,
@@ -402,6 +396,8 @@ impl<'s, 't> DisplayableTree<'s, 't> {
self.write_root_line(&mut cw, self.in_app && tree.selection == 0)?;
f.queue(SetBackgroundColor(Color::Reset))?;
+ let visible_cols = tree.visible_cols();
+
// we compute the length of the dates, depending on the format
let date_len = if tree.options.show_dates {
let date_time: DateTime<Local> = Local::now();
@@ -423,7 +419,6 @@ impl<'s, 't> DisplayableTree<'s, 't> {
let mut selected = false;
let mut cw = CropWriter::new(f, self.area.width as usize);
let cw = &mut cw;
- let add_left_margin = self.cols[0] != Col::Mark || !self.show_selection_mark;
if line_index < tree.lines.len() {
let line = &tree.lines[line_index];
selected = self.in_app && line_index == tree.selection;
@@ -434,17 +429,17 @@ impl<'s, 't> DisplayableTree<'s, 't> {
} else {
&self.skin.default
};
- if add_left_margin {
+ if visible_cols[0].needs_left_margin() {
cw.queue_char(space_style, ' ')?;
}
- for col in self.cols {
+ for col in &visible_cols {
let void_len = match col {
- Col::Mark if self.show_selection_mark => {
+ Col::Mark => {
self.write_line_selection_mark(cw, &label_style, selected)?
}
- Col::Git if !tree.git_status.is_none() => {
+ Col::Git => {
self.write_line_git_status(cw, line, selected)?
}
@@ -454,11 +449,11 @@ impl<'s, 't> DisplayableTree<'s, 't> {
}
#[cfg(not(any(target_family = "windows", target_os = "android")))]
- Col::Permission if tree.options.show_permissions => {
+ Col::Permission => {
perm_writer.write_permissions(cw, line, selected)?
}
- Col::Date if tree.options.show_dates => {
+ Col::Date => {
if let Some(seconds) = line.sum.and_then(|sum| sum.to_valid_seconds()) {
self.write_date(cw, seconds, selected)?
} else {
@@ -466,7 +461,7 @@ impl<'s, 't> DisplayableTree<'s, 't> {
}
}
- Col::Size if tree.options.show_sizes => {
+ Col::Size => {
if tree.options.sort.is_some() {
// as soon as there's only one level displayed we can show the size bars
self.write_line_size_with_bar(cw, line, &label_style, total_size, selected)?
@@ -475,7 +470,7 @@ impl<'s, 't> DisplayableTree<'s, 't> {
}
}
- Col::Count if tree.options.show_counts => {
+ Col::Count => {
self.write_line_count(cw, line, selected)?
}
@@ -484,9 +479,6 @@ impl<'s, 't> DisplayableTree<'s, 't> {
self.write_line_label(cw, line, &label_style, pattern_object, selected)?
}
- _ => {
- 0 // we don't write the intercol
- }
};
// void: intercol & replacing missing cells
if in_branch && void_len > 2 {