summaryrefslogtreecommitdiffstats
path: root/src/tree
diff options
context:
space:
mode:
authorCanop <cano.petrole@gmail.com>2022-03-31 20:51:11 +0200
committerCanop <cano.petrole@gmail.com>2022-03-31 20:51:11 +0200
commit51153d26ce0c4ab80c7b0678ddbec03b3b01b34a (patch)
treef9b0468a580d0ae854101eb389953ab6eba8c7f8 /src/tree
parent7019073b69a03aeb633ab0fabc84fecbf3482c2b (diff)
Sort by type, with dirs either first or last
3 new internals. With `:st` you can select to sort by type with directories first, sort by type with directories last, or not sorting. Fix #467
Diffstat (limited to 'src/tree')
-rw-r--r--src/tree/sort.rs13
-rw-r--r--src/tree/tree.rs32
2 files changed, 37 insertions, 8 deletions
diff --git a/src/tree/sort.rs b/src/tree/sort.rs
index 2f22e71..3450b0b 100644
--- a/src/tree/sort.rs
+++ b/src/tree/sort.rs
@@ -9,10 +9,19 @@ pub enum Sort {
Count,
Date,
Size,
+ TypeDirsFirst,
+ TypeDirsLast,
}
impl Sort {
- pub fn is_some(self) -> bool {
- !matches!(self, Sort::None)
+ pub fn prevent_deep_display(self) -> bool {
+ match self {
+ Self::None => false,
+ Self::Count => true,
+ Self::Date => true,
+ Self::Size => true,
+ Self::TypeDirsFirst => false,
+ Self::TypeDirsLast => false,
+ }
}
}
diff --git a/src/tree/tree.rs b/src/tree/tree.rs
index 0ff91fe..441c197 100644
--- a/src/tree/tree.rs
+++ b/src/tree/tree.rs
@@ -70,6 +70,7 @@ impl Tree {
/// - compute left branchs
pub fn after_lines_changed(&mut self) {
+
// we need to order the lines to build the tree.
// It's a little complicated because
// - we want a case insensitive sort
@@ -89,9 +90,31 @@ impl Tree {
let mut bid = line.bid;
loop {
if let Some(l) = bid_lines.get(&bid) {
+ let lower_name = l.path.file_name().map_or(
+ "".to_string(),
+ |name| name.to_string_lossy().to_lowercase(),
+ );
+ let sort_prefix = match self.options.sort {
+ Sort::TypeDirsFirst => {
+ if l.is_dir() {
+ " "
+ } else {
+ l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
+ }
+ }
+ Sort::TypeDirsLast => {
+ if l.is_dir() {
+ "~~~~~~~~~~~~~~"
+ } else {
+ l.path.extension().and_then(|s| s.to_str()).unwrap_or("")
+ }
+ }
+ _ => { "" }
+ };
sort_path = format!(
- "{}-{}/{}",
- l.path.to_string_lossy().to_lowercase(),
+ "{}{}-{}/{}",
+ sort_prefix,
+ lower_name,
bid.index(), // to be sure to separate paths having the same lowercase
sort_path,
);
@@ -462,9 +485,6 @@ impl Tree {
///
/// (does nothing if it's None)
fn sort_siblings(&mut self) {
- if !self.options.sort.is_some() {
- return;
- }
match self.options.sort {
Sort::Count => {
// we'll try to keep the same path selected
@@ -494,7 +514,7 @@ impl Tree {
});
self.try_select_path(&selected_path);
}
- Sort::None => {}
+ _ => {}
}
}