summaryrefslogtreecommitdiffstats
path: root/src/tab.rs
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-11-05 22:28:17 +0100
committerqkzk <qu3nt1n@gmail.com>2023-11-05 22:28:17 +0100
commite5a0a45532d3f537780f3f56b48ad1d20e9df1e9 (patch)
treee61e873ff29e3f3a034123386624826cbcdae79f /src/tab.rs
parentcd52d2a0379b51861fd9b18a90db41809b655c68 (diff)
Tree first draft. No refactor, no test, no doc
Diffstat (limited to 'src/tab.rs')
-rw-r--r--src/tab.rs111
1 files changed, 40 insertions, 71 deletions
diff --git a/src/tab.rs b/src/tab.rs
index 2abae8f..ddf6f68 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -13,13 +13,13 @@ use crate::history::History;
use crate::input::Input;
use crate::mode::{InputSimple, Mode};
use crate::opener::execute_in_child;
-use crate::preview::{Directory, Preview};
+use crate::preview::Preview;
use crate::selectable_content::SelectableContent;
use crate::shortcut::Shortcut;
use crate::sort::SortKind;
-use crate::trees::FileSystem;
+use crate::trees::{calculate_tree_window, FileSystem};
use crate::users::Users;
-use crate::utils::{filename_from_path, row_to_window_index, set_clipboard};
+use crate::utils::{row_to_window_index, set_clipboard};
/// Holds every thing about the current tab of the application.
/// Most of the mutation is done externally.
@@ -50,8 +50,6 @@ pub struct Tab {
pub shortcut: Shortcut,
/// Last searched string
pub searched: Option<String>,
- /// Optional tree view
- pub directory: Directory,
/// The filter use before displaying files
pub filter: FilterKind,
/// Visited directories
@@ -76,7 +74,6 @@ impl Tab {
} else {
path.parent().context("")?
};
- let directory = Directory::empty(start_dir, &users)?;
let filter = FilterKind::All;
let show_hidden = args.all || settings.all;
let mut path_content = PathContent::new(start_dir, &users, &filter, show_hidden)?;
@@ -106,7 +103,6 @@ impl Tab {
preview,
shortcut,
searched,
- directory,
filter,
show_hidden,
history,
@@ -135,7 +131,7 @@ impl Tab {
if matches!(self.previous_mode, Mode::Tree) =>
{
self.completion
- .search_from_tree(&self.input.string(), &self.directory.content)
+ .search_from_tree(&self.input.string(), &self.tree.paths())
}
Mode::InputCompleted(InputCompleted::Command) => {
self.completion.command(&self.input.string())
@@ -150,7 +146,7 @@ impl Tab {
self.input.reset();
self.preview = Preview::empty();
self.completion.reset();
- self.directory.clear();
+ self.tree = FileSystem::empty();
Ok(())
}
@@ -303,12 +299,6 @@ impl Tab {
}
}
- /// Select the root node of the tree.
- pub fn tree_select_root(&mut self) -> Result<()> {
- self.directory.unselect_children();
- self.directory.select_root()
- }
-
/// Move to the parent of current path
pub fn move_to_parent(&mut self) -> Result<()> {
let path = self.path_content.path.clone();
@@ -342,13 +332,8 @@ impl Tab {
/// Select the parent of current node.
/// If we were at the root node, move to the parent and make a new tree.
- pub fn tree_select_parent(&mut self) -> Result<()> {
- self.directory.unselect_children();
- if self.directory.tree.position.len() <= 1 {
- self.move_to_parent()?;
- self.make_tree(None)?
- }
- self.directory.select_parent()
+ pub fn tree_select_parent(&mut self) {
+ self.tree.select_parent()
}
/// Move down 10 times in the tree
@@ -378,12 +363,6 @@ impl Tab {
Ok(())
}
- /// Select the first child if any.
- pub fn tree_select_first_child(&mut self) -> Result<()> {
- self.directory.unselect_children();
- self.directory.select_first_child()
- }
-
/// Go to the last leaf.
pub fn tree_go_to_bottom_leaf(&mut self) -> Result<()> {
self.tree.select_last();
@@ -395,19 +374,10 @@ impl Tab {
/// if the selected node is a directory, that's it.
/// else, it is the parent of the selected node.
/// In other modes, it's the current path of pathcontent.
- pub fn current_directory_path(&mut self) -> &path::Path {
+ pub fn current_directory_path(&mut self) -> Option<&path::Path> {
match self.mode {
- Mode::Tree => {
- let path = &self.directory.tree.current_node.fileinfo.path;
- if path.is_dir() {
- return path;
- }
- let Some(parent) = path.parent() else {
- return path::Path::new("/");
- };
- parent
- }
- _ => &self.path_content.path,
+ Mode::Tree => return self.tree.directory_of_selected(),
+ _ => Some(&self.path_content.path),
}
}
@@ -417,16 +387,23 @@ impl Tab {
/// In normal mode it's the current working directory.
pub fn directory_of_selected(&self) -> Result<&path::Path> {
match self.mode {
- Mode::Tree => self.directory.tree.directory_of_selected(),
+ Mode::Tree => self.tree.directory_of_selected().context("No parent"),
_ => Ok(&self.path_content.path),
}
}
/// Optional Fileinfo of the selected element.
- pub fn selected(&self) -> Option<&FileInfo> {
+ pub fn selected(&self) -> Result<FileInfo> {
match self.mode {
- Mode::Tree => Some(&self.directory.tree.current_node.fileinfo),
- _ => self.path_content.selected(),
+ Mode::Tree => {
+ let node = self.tree.selected_node().context("no selected node")?;
+ node.fileinfo(&self.users)
+ }
+ _ => Ok(self
+ .path_content
+ .selected()
+ .context("no selected file")?
+ .to_owned()),
}
}
@@ -536,11 +513,6 @@ impl Tab {
Ok(())
}
- /// Select the first child of the current node and reset the display.
- pub fn select_first_child(&mut self) -> Result<()> {
- self.tree_select_first_child()
- }
-
/// Copy the selected filename to the clipboard. Only the filename.
pub fn filename_to_clipboard(&self) -> Result<()> {
set_clipboard(
@@ -659,15 +631,12 @@ impl Tab {
}
fn tree_select_row(&mut self, row: u16, term_height: usize) -> Result<()> {
- let screen_index = row_to_window_index(row) + 1;
- // term.height = canvas.height + 2 rows for the canvas border
- let (top, _, _) = self.directory.calculate_tree_window(term_height - 2);
+ let screen_index = row_to_window_index(row);
+ let (selected_index, content) = self.tree.into_navigable_content(&self.users);
+ let (top, _, _) = calculate_tree_window(selected_index, term_height, term_height);
let index = screen_index + top;
- self.directory.tree.unselect_children();
- self.directory.tree.position = self.directory.tree.position_from_index(index);
- let (_, _, node) = self.directory.tree.select_from_position()?;
- self.directory.make_preview();
- self.directory.tree.current_node = node;
+ let (_, _, colored_path) = content.get(index).context("no selected file")?;
+ self.tree.select_from_path(&colored_path.path);
Ok(())
}
@@ -723,21 +692,21 @@ impl Tab {
}
pub fn rename(&mut self) -> Result<()> {
- if self.selected().is_some() {
- let old_name = match self.mode {
- Mode::Tree => self.directory.tree.current_node.filename(),
- _ => filename_from_path(
- &self
- .path_content
- .selected()
- .context("Event rename: no file in current directory")?
- .path,
- )?
- .to_owned(),
+ let old_name: String = if matches!(self.mode, Mode::Tree) {
+ self.tree
+ .selected_path()
+ .file_name()
+ .context("no filename")?
+ .to_string_lossy()
+ .into()
+ } else {
+ let Ok(fileinfo) = self.selected() else {
+ return Ok(());
};
- self.input.replace(&old_name);
- self.set_mode(Mode::InputSimple(InputSimple::Rename));
- }
+ fileinfo.filename.to_owned()
+ };
+ self.input.replace(&old_name);
+ self.set_mode(Mode::InputSimple(InputSimple::Rename));
Ok(())
}
}