summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-01-28 20:05:55 +0100
committerqkzk <qu3nt1n@gmail.com>2023-01-28 20:05:55 +0100
commit4d56184397359fa1dbcf31d13c219ed67f389cde (patch)
tree1ed0264fa02e198c267d3fee6d85faac73072814 /src
parent8106476af986ee014e62b66626c39a7195b9e7be (diff)
use let else to remove nested if let statements
Diffstat (limited to 'src')
-rw-r--r--src/event_exec.rs94
-rw-r--r--src/status.rs34
-rw-r--r--src/tab.rs6
-rw-r--r--src/term_manager.rs21
-rw-r--r--src/tree.rs135
5 files changed, 146 insertions, 144 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs
index e592661..8031f70 100644
--- a/src/event_exec.rs
+++ b/src/event_exec.rs
@@ -96,11 +96,10 @@ impl EventExec {
match tab.mode {
Mode::Normal => {
- if let Some(file) = tab.path_content.selected() {
- let path = file.path.clone();
- status.toggle_flag_on_path(&path);
- Self::event_down_one_row(status.selected());
- }
+ let Some(file) = tab.path_content.selected() else { return Ok(()) };
+ let path = file.path.clone();
+ status.toggle_flag_on_path(&path);
+ Self::event_down_one_row(status.selected());
}
Mode::Tree => {
let path = tab.directory.tree.current_node.filepath();
@@ -260,22 +259,22 @@ impl EventExec {
/// If the user selected a directory, we jump inside it.
/// Otherwise, we jump to the parent and select the file.
pub fn exec_jump(status: &mut Status) -> FmResult<()> {
- if let Some(jump_target) = status.flagged.selected() {
- let jump_target = jump_target.to_owned();
- let target_dir = match jump_target.parent() {
- Some(parent) => parent,
- None => &jump_target,
- };
- let tab = status.selected();
- tab.input.clear();
- tab.history.push(target_dir);
- tab.path_content
- .change_directory(target_dir, &tab.filter, tab.show_hidden)?;
- let index = tab.find_jump_index(&jump_target).unwrap_or_default();
- tab.path_content.select_index(index);
- tab.set_window();
- tab.scroll_to(index);
- }
+ let Some(jump_target) = status.flagged.selected() else { return Ok(()) };
+ let jump_target = jump_target.to_owned();
+ let target_dir = match jump_target.parent() {
+ Some(parent) => parent,
+ None => &jump_target,
+ };
+ let tab = status.selected();
+ tab.input.clear();
+ tab.history.push(target_dir);
+ tab.path_content
+ .change_directory(target_dir, &tab.filter, tab.show_hidden)?;
+ let index = tab.find_jump_index(&jump_target).unwrap_or_default();
+ tab.path_content.select_index(index);
+ tab.set_window();
+ tab.scroll_to(index);
+
Ok(())
}
@@ -591,19 +590,19 @@ impl EventExec {
return Ok(());
}
let unmutable_tab = status.selected_non_mut();
- if let Some(file_info) = unmutable_tab.selected() {
- match file_info.file_kind {
- FileKind::NormalFile => {
- let preview =
- Preview::new(file_info, &unmutable_tab.path_content.users_cache, status)?;
- status.selected().set_mode(Mode::Preview);
- status.selected().window.reset(preview.len());
- status.selected().preview = preview;
- }
- FileKind::Directory => Self::event_tree(status)?,
- _ => (),
+ let Some(file_info) = unmutable_tab.selected() else { return Ok(()) };
+ match file_info.file_kind {
+ FileKind::NormalFile => {
+ let preview =
+ Preview::new(file_info, &unmutable_tab.path_content.users_cache, status)?;
+ status.selected().set_mode(Mode::Preview);
+ status.selected().window.reset(preview.len());
+ status.selected().preview = preview;
}
+ FileKind::Directory => Self::event_tree(status)?,
+ _ => (),
}
+
Ok(())
}
@@ -998,14 +997,13 @@ impl EventExec {
/// It obviously requires the `dragon-drop` command to be installed.
pub fn event_drag_n_drop(status: &mut Status) -> FmResult<()> {
let tab = status.selected_non_mut();
- if let Some(file) = tab.selected() {
- let path_str = file
- .path
- .to_str()
- .ok_or_else(|| FmError::custom("event drag n drop", "Couldn't read path"))?;
+ let Some(file) = tab.selected() else { return Ok(()) };
+ let path_str = file
+ .path
+ .to_str()
+ .ok_or_else(|| FmError::custom("event drag n drop", "Couldn't read path"))?;
- execute_in_child(DEFAULT_DRAGNDROP, &vec![path_str])?;
- }
+ execute_in_child(DEFAULT_DRAGNDROP, &vec![path_str])?;
Ok(())
}
@@ -1048,10 +1046,9 @@ impl EventExec {
match tab.mode {
Mode::Tree => (),
_ => {
- if let Some(searched) = tab.searched.clone() {
- let next_index = (tab.path_content.index + 1) % tab.path_content.content.len();
- tab.search_from(&searched, next_index);
- }
+ let Some(searched) = tab.searched.clone() else { return Ok(()) };
+ let next_index = (tab.path_content.index + 1) % tab.path_content.content.len();
+ tab.search_from(&searched, next_index);
}
}
Ok(())
@@ -1358,12 +1355,11 @@ impl EventExec {
/// Open a thumbnail of an image, scaled up to the whole window.
pub fn event_thumbnail(tab: &mut Tab) -> FmResult<()> {
if let Mode::Normal | Mode::Tree = tab.mode {
- if let Some(file_info) = tab.selected() {
- info!("selected {:?}", file_info);
- tab.preview = Preview::thumbnail(file_info.path.to_owned())?;
- tab.window.reset(tab.preview.len());
- tab.set_mode(Mode::Preview);
- }
+ let Some(file_info) = tab.selected() else { return Ok(())};
+ info!("selected {:?}", file_info);
+ tab.preview = Preview::thumbnail(file_info.path.to_owned())?;
+ tab.window.reset(tab.preview.len());
+ tab.set_mode(Mode::Preview);
}
Ok(())
}
diff --git a/src/status.rs b/src/status.rs
index c99f91f..136f31c 100644
--- a/src/status.rs
+++ b/src/status.rs
@@ -146,32 +146,26 @@ impl Status {
/// Replace the tab content with the first result of skim.
/// It calls skim, reads its output, then update the tab content.
pub fn skim_output_to_tab(&mut self) -> FmResult<()> {
- if let Some(skim_output) = self
- .skimer
- .no_source(
- self.selected_non_mut()
- .selected()
- .ok_or_else(|| FmError::custom("skim", "no selected file"))?
- .path
- .to_str()
- .ok_or_else(|| FmError::custom("skim", "skim error"))?,
- )
- .first()
- {
- self._update_tab_from_skim_output(skim_output)?;
- }
- Ok(())
+ let skim = self.skimer.no_source(
+ self.selected_non_mut()
+ .selected()
+ .ok_or_else(|| FmError::custom("skim", "no selected file"))?
+ .path
+ .to_str()
+ .ok_or_else(|| FmError::custom("skim", "skim error"))?,
+ );
+ let Some(output) = skim.first() else {return Ok(())};
+ self._update_tab_from_skim_output(output)
}
fn _update_tab_from_skim_output(&mut self, skim_outut: &Arc<dyn SkimItem>) -> FmResult<()> {
let path = fs::canonicalize(skim_outut.output().to_string())?;
let tab = self.selected();
if path.is_file() {
- if let Some(parent) = path.parent() {
- tab.set_pathcontent(parent)?;
- let filename = filename_from_path(&path)?;
- tab.search_from(filename, 0);
- }
+ let Some(parent) = path.parent() else { return Ok(()) };
+ tab.set_pathcontent(parent)?;
+ let filename = filename_from_path(&path)?;
+ tab.search_from(filename, 0);
} else if path.is_dir() {
tab.set_pathcontent(&path)?;
}
diff --git a/src/tab.rs b/src/tab.rs
index 3e4f98f..dd8ffe5 100644
--- a/src/tab.rs
+++ b/src/tab.rs
@@ -251,10 +251,8 @@ impl Tab {
/// Move to the parent of current path
pub fn move_to_parent(&mut self) -> FmResult<()> {
let path = self.path_content.path.clone();
- if let Some(parent) = path.parent() {
- self.set_pathcontent(parent)?;
- }
- Ok(())
+ let Some(parent) = path.parent() else { return Ok(()) };
+ self.set_pathcontent(parent)
}
/// Select the parent of current node.
diff --git a/src/term_manager.rs b/src/term_manager.rs
index d13e06c..045d969 100644
--- a/src/term_manager.rs
+++ b/src/term_manager.rs
@@ -108,33 +108,30 @@ impl<'a> WinMain<'a> {
/// In normal mode we display the path and number of files.
/// When a confirmation is needed we ask the user to input `'y'` or
/// something else.
+ /// Returns the result of the number of printed chars.
fn first_line(&self, tab: &Tab, disk_space: &str, canvas: &mut dyn Canvas) -> FmResult<()> {
draw_colored_strings(0, 0, self.create_first_row(tab, disk_space)?, canvas)
}
- fn second_line(&self, status: &Status, tab: &Tab, canvas: &mut dyn Canvas) -> FmResult<()> {
+ fn second_line(&self, status: &Status, tab: &Tab, canvas: &mut dyn Canvas) -> FmResult<usize> {
match tab.mode {
Mode::Normal => {
if !status.display_full {
- if let Some(file) = tab.selected() {
- self.second_line_detailed(file, status, canvas)?;
- }
+ let Some(file) = tab.selected() else { return Ok(0) };
+ self.second_line_detailed(file, status, canvas)
} else {
- self.second_line_simple(status, canvas)?;
+ self.second_line_simple(status, canvas)
}
}
Mode::Tree => {
- if let Some(file) = tab.selected() {
- self.second_line_detailed(file, status, canvas)?;
- }
+ let Some(file) = tab.selected() else { return Ok(0) };
+ self.second_line_detailed(file, status, canvas)
}
Mode::InputSimple(InputSimple::Filter) => {
- canvas.print_with_attr(1, 0, FILTER_PRESENTATION, ATTR_YELLOW_BOLD)?;
+ Ok(canvas.print_with_attr(1, 0, FILTER_PRESENTATION, ATTR_YELLOW_BOLD)?)
}
- _ => (),
+ _ => Ok(0),
}
-
- Ok(())
}
fn second_line_detailed(
diff --git a/src/tree.rs b/src/tree.rs
index 957720e..826e9a6 100644
--- a/src/tree.rs
+++ b/src/tree.rs
@@ -166,29 +166,15 @@ impl Tree {
parent_position: Vec<usize>,
) -> FmResult<Self> {
let sort_kind = SortKind::tree_default();
- let mut leaves = vec![];
- if let FileKind::Directory = fileinfo.file_kind {
- if max_depth > 0 {
- if let Some(mut files) =
- files_collection(&fileinfo, users_cache, display_hidden, filter_kind)
- {
- let len = files.len();
- sort_kind.sort(&mut files);
- for (index, fileinfo) in files.iter().enumerate() {
- let mut position = parent_position.clone();
- position.push(len - index - 1);
- leaves.push(Self::create_tree_from_fileinfo(
- fileinfo.to_owned(),
- max_depth - 1,
- users_cache,
- filter_kind,
- display_hidden,
- position,
- )?)
- }
- }
- }
- }
+ let leaves = Self::make_leaves(
+ &fileinfo,
+ max_depth,
+ users_cache,
+ display_hidden,
+ filter_kind,
+ &sort_kind,
+ parent_position.clone(),
+ )?;
let node = Node::from_fileinfo(fileinfo, parent_position);
let position = vec![0];
let current_node = node.clone();
@@ -201,6 +187,40 @@ impl Tree {
})
}
+ fn make_leaves(
+ fileinfo: &FileInfo,
+ max_depth: usize,
+ users_cache: &Rc<UsersCache>,
+ display_hidden: bool,
+ filter_kind: &FilterKind,
+ sort_kind: &SortKind,
+ parent_position: Vec<usize>,
+ ) -> FmResult<Vec<Tree>> {
+ if max_depth <= 0 {
+ return Ok(vec![]);
+ }
+ let mut leaves = vec![];
+ let FileKind::Directory = fileinfo.file_kind else { return Ok(vec![]) };
+ let Some(mut files) =
+ files_collection(fileinfo, users_cache, display_hidden, filter_kind)
+ else { return Ok(leaves) };
+ sort_kind.sort(&mut files);
+ for (index, fileinfo) in files.iter().enumerate() {
+ let mut position = parent_position.clone();
+ position.push(files.len() - index - 1);
+ leaves.push(Self::create_tree_from_fileinfo(
+ fileinfo.to_owned(),
+ max_depth - 1,
+ users_cache,
+ filter_kind,
+ display_hidden,
+ position,
+ )?)
+ }
+
+ Ok(leaves)
+ }
+
/// Sort the leaves with current sort kind.
pub fn sort(&mut self) {
let sort_kind = self.sort_kind.clone();
@@ -383,28 +403,27 @@ impl Tree {
let mut index = 0;
while !stack.is_empty() {
- if let Some((prefix, current)) = stack.pop() {
- if current.node.fileinfo.is_selected {
- selected_index = content.len();
- }
+ let Some((prefix, current)) = stack.pop() else { continue };
+ if current.node.fileinfo.is_selected {
+ selected_index = content.len();
+ }
- current.node.index = Some(index);
- index += 1;
- content.push((
- prefix.to_owned(),
- ColoredString::from_node(&current.node, colors),
- ));
-
- let first_prefix = first_prefix(prefix.clone());
- let other_prefix = other_prefix(prefix);
-
- if !current.node.folded {
- for (index, leaf) in current.leaves.iter_mut().enumerate() {
- if index == 0 {
- stack.push((first_prefix.clone(), leaf));
- } else {
- stack.push((other_prefix.clone(), leaf))
- }
+ current.node.index = Some(index);
+ index += 1;
+ content.push((
+ prefix.to_owned(),
+ ColoredString::from_node(&current.node, colors),
+ ));
+
+ let first_prefix = first_prefix(prefix.clone());
+ let other_prefix = other_prefix(prefix);
+
+ if !current.node.folded {
+ for (index, leaf) in current.leaves.iter_mut().enumerate() {
+ if index == 0 {
+ stack.push((first_prefix.clone(), leaf));
+ } else {
+ stack.push((other_prefix.clone(), leaf))
}
}
}
@@ -412,7 +431,7 @@ impl Tree {
(selected_index, content)
}
- /// Select the first node matching a char.
+ /// Select the first node matching a key.
/// We use a breath first search algorithm to ensure we select the less deep one.
pub fn select_first_match(&mut self, key: &str) -> Option<Vec<usize>> {
if self.node.fileinfo.filename.contains(key) {
@@ -420,9 +439,8 @@ impl Tree {
}
for tree in self.leaves.iter_mut() {
- if let Some(position) = tree.select_first_match(key) {
- return Some(position);
- }
+ let Some(position) = tree.select_first_match(key) else { continue };
+ return Some(position);
}
None
@@ -468,20 +486,19 @@ impl Tree {
let mut visited = self;
let mut counter = 0;
- while !stack.is_empty() {
- if let Some(current) = stack.pop() {
- counter += 1;
- visited = current;
- if counter == index {
- break;
- }
- if !current.node.folded {
- for leaf in current.leaves.iter() {
- stack.push(leaf);
- }
+ while let Some(current) = stack.pop() {
+ counter += 1;
+ visited = current;
+ if counter == index {
+ break;
+ }
+ if !current.node.folded {
+ for leaf in current.leaves.iter() {
+ stack.push(leaf);
}
}
}
+
visited.node.position.clone()
}
}