diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-01-31 15:28:09 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-01-31 15:28:09 +0100 |
commit | 56ee9aa5138bf47665082eb941fba33fad81aa49 (patch) | |
tree | 386ce51f0a3f0a60dccf2dafab44a770c84d23f5 | |
parent | b7ee0cd136cbb8fbc63fc7093dd6ef512d97d579 (diff) |
new window for treesrefactor-colors
-rw-r--r-- | src/event_exec.rs | 8 | ||||
-rw-r--r-- | src/preview.rs | 59 | ||||
-rw-r--r-- | src/term_manager.rs | 37 | ||||
-rw-r--r-- | src/tree.rs | 14 |
4 files changed, 86 insertions, 32 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs index 049b8714..0fd28c67 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -1495,9 +1495,11 @@ impl EventExec { /// Has no effect on "file" nodes. pub fn event_tree_fold(status: &mut Status, colors: &Colors) -> FmResult<()> { let tab = status.selected(); - tab.directory.content[tab.directory.selected_index] - .1 - .toggle_fold(); + if let Mode::Tree = tab.mode { + tab.directory.content[tab.directory.selected_index] + .1 + .toggle_fold(); + } Ok(()) // let tab = status.selected(); diff --git a/src/preview.rs b/src/preview.rs index 643d74bd..21f1ff6d 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -688,34 +688,52 @@ impl Directory { let mut selected_index = 0; let mut index = 0; - while !stack.is_empty() { - let Some((prefix, current, parent_index)) = stack.pop() else { continue }; + while let Some((prefix, current, parent_index)) = stack.pop() { if current.node.fileinfo.is_selected { selected_index = content.len(); } current.node.index = Some(index); content.push(( - prefix.to_owned(), - ColoredString::from_node(¤t.node, colors, parent_index), + prefix.clone(), + ColoredString::from_node(¤t.node, colors, parent_index, current.leaves.len()), )); let first_prefix = first_prefix(prefix.clone()); let other_prefix = other_prefix(prefix); - if !current.node.folded { - for (child_index, leaf) in current.leaves.iter_mut().enumerate() { - if child_index == 0 { - stack.push((first_prefix.clone(), leaf, index)); - } else { - stack.push((other_prefix.clone(), leaf, index)) - } + for (child_index, leaf) in current.leaves.iter_mut().enumerate() { + if child_index == 0 { + stack.push((first_prefix.clone(), leaf, index)); + } else { + stack.push((other_prefix.clone(), leaf, index)) } } index += 1; } (selected_index, content) } + + pub fn window(&self) -> (usize, Vec<(usize, &ColoredPair)>) { + let length = self.content.len(); + let mut win = vec![]; + let mut display_index = 0; + let mut content_index = 0; + let mut selected_display_index = 0; + + while content_index < length { + if display_index == self.selected_index { + selected_display_index = content_index; + } + win.push((display_index, &self.content[content_index])); + if self.content[content_index].1.folded { + content_index += self.content[content_index].1.nb_children + }; + content_index += 1; + display_index += 1; + } + (selected_display_index, win) + } } /// Common trait for many preview methods which are just a bunch of lines with @@ -736,7 +754,7 @@ impl Window<Vec<SyntaxedString>> for HLContent { top: usize, bottom: usize, length: usize, - ) -> std::iter::Take<Skip<Enumerate<Iter<'_, Vec<SyntaxedString>>>>> { + ) -> Take<Skip<Enumerate<Iter<'_, Vec<SyntaxedString>>>>> { self.content .iter() .enumerate() @@ -745,6 +763,21 @@ impl Window<Vec<SyntaxedString>> for HLContent { } } +// impl Window<(String, ColoredString)> for Directory { +// fn window( +// &self, +// top: usize, +// bottom: usize, +// length: usize, +// ) -> Take<Skip<Enumerate<Iter<'_, (String, ColoredString)>>>> { +// self.content +// .iter() +// .enumerate() +// .skip(top) +// .take(min(length, bottom + 1)) +// } +// } + macro_rules! impl_window { ($t:ident, $u:ident) => { impl Window<$u> for $t { @@ -772,7 +805,7 @@ impl_window!(PdfContent, String); impl_window!(ZipContent, String); impl_window!(ExifContent, String); impl_window!(MediaContent, String); -impl_window!(Directory, ColoredPair); +// impl_window!(Directory, ColoredPair); fn is_ext_zip(ext: &str) -> bool { matches!( diff --git a/src/term_manager.rs b/src/term_manager.rs index 95ba5e75..6d303bad 100644 --- a/src/term_manager.rs +++ b/src/term_manager.rs @@ -242,9 +242,15 @@ impl<'a> WinMain<'a> { let line_number_width = 3; let (_, height) = canvas.size()?; - let (top, bottom, len) = tab.directory.calculate_tree_window(height); - for (i, (prefix, colored_string)) in tab.directory.window(top, bottom, len) { - let row = i + ContentWindow::WINDOW_MARGIN_TOP - top; + let (selected_index, win) = tab.directory.window(); + let skip = if selected_index < 10 { + 0 + } else { + selected_index - 10 + }; + + for (i, (prefix, colored_string)) in win.iter().skip(skip).take(height) { + let row = i + ContentWindow::WINDOW_MARGIN_TOP - skip; let col = canvas.print(row, line_number_width, prefix)?; let mut attr = colored_string.attr; if status.flagged.contains(&colored_string.path) { @@ -324,18 +330,19 @@ impl<'a> WinMain<'a> { } } Preview::Directory(directory) => { - for (i, (prefix, colored_string)) in - (directory).window(tab.window.top, tab.window.bottom, length) - { - let row = calc_line_row(i, tab); - let col = canvas.print(row, line_number_width, prefix)?; - canvas.print_with_attr( - row, - line_number_width + col + 1, - &colored_string.text, - colored_string.attr, - )?; - } + todo!() + // for (i, (prefix, colored_string)) in + // (directory).window(tab.window.top, tab.window.bottom, length) + // { + // let row = calc_line_row(i, tab); + // let col = canvas.print(row, line_number_width, prefix)?; + // canvas.print_with_attr( + // row, + // line_number_width + col + 1, + // &colored_string.text, + // colored_string.attr, + // )?; + // } } Preview::Archive(text) => impl_preview!(text, tab, length, canvas, line_number_width), Preview::Exif(text) => impl_preview!(text, tab, length, canvas, line_number_width), diff --git a/src/tree.rs b/src/tree.rs index 7503c87e..a609559e 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -22,6 +22,7 @@ pub struct ColoredString { pub path: std::path::PathBuf, pub parent_index: usize, pub folded: bool, + pub nb_children: usize, } impl ColoredString { @@ -31,6 +32,7 @@ impl ColoredString { path: std::path::PathBuf, parent_index: usize, folded: bool, + nb_children: usize, ) -> Self { Self { text, @@ -38,6 +40,7 @@ impl ColoredString { path, parent_index, folded, + nb_children, } } @@ -45,7 +48,12 @@ impl ColoredString { self.parent_index = parent_index } - pub fn from_node(current_node: &Node, colors: &Colors, parent_index: usize) -> Self { + pub fn from_node( + current_node: &Node, + colors: &Colors, + parent_index: usize, + nb_children: usize, + ) -> Self { let mut text = Self::fold_symbols(current_node); text.push_str(¤t_node.filename()); Self::new( @@ -54,6 +62,7 @@ impl ColoredString { current_node.filepath(), parent_index, false, + nb_children, ) } @@ -92,6 +101,7 @@ pub struct Node { pub folded: bool, is_dir: bool, pub index: Option<usize>, + pub nb_descendant: usize, } impl Node { @@ -134,6 +144,7 @@ impl Node { position: parent_position, folded: false, index: None, + nb_descendant: 0, } } } @@ -281,6 +292,7 @@ impl Tree { folded: false, is_dir: false, index: None, + nb_descendant: 0, }; let leaves = vec![]; let position = vec![0]; |