diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-01-28 20:05:55 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-01-28 20:05:55 +0100 |
commit | 4d56184397359fa1dbcf31d13c219ed67f389cde (patch) | |
tree | 1ed0264fa02e198c267d3fee6d85faac73072814 | |
parent | 8106476af986ee014e62b66626c39a7195b9e7be (diff) |
use let else to remove nested if let statements
-rw-r--r-- | src/event_exec.rs | 94 | ||||
-rw-r--r-- | src/status.rs | 34 | ||||
-rw-r--r-- | src/tab.rs | 6 | ||||
-rw-r--r-- | src/term_manager.rs | 21 | ||||
-rw-r--r-- | src/tree.rs | 135 |
5 files changed, 146 insertions, 144 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs index e592661f..8031f707 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 c99f91ff..136f31cd 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)?; } @@ -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 d13e06cf..045d969e 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 957720e3..826e9a6f 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(¤t.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(¤t.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() } } |