From eea85700e113440f332b7f7759865bbfac47c9f5 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 9 Jul 2025 19:15:46 +0300 Subject: [PATCH] refactor(move): DRY --- src/app/mod.rs | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index c27840e..e8595d3 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -169,18 +169,8 @@ impl<'a> App<'a> { } pub fn prepare_move_action(&mut self) { - if let Selected::Current(current_id) = self.selected(true) { - if let Some(torrent) = self - .torrents - .torrents - .iter() - .find(|t| t.id == Some(current_id)) - { - if let Some(download_dir) = &torrent.download_dir { - self.input = download_dir.clone(); - self.cursor_position = self.input.len(); - } - } + if let Some(download_dir) = self.get_current_downlaod_dir() { + self.set_input_from_path(&download_dir); } self.input_mode = true; } @@ -202,14 +192,14 @@ impl<'a> App<'a> { let selected_id = self .state .selected() - .and_then(|idx| torrents.get(idx).and_then(|torrent| torrent.id)); + .and_then(|idx| torrents.get(idx).and_then(|t| t.id)); if let Some(id) = selected_id { return Selected::Current(id); } } let selected_torrents = torrents .iter() - .filter_map(|torrent| torrent.id.filter(|id| self.torrents.selected.contains(id))) + .filter_map(|t| t.id.filter(|id| self.torrents.selected.contains(id))) .collect(); Selected::List(selected_torrents) } @@ -236,6 +226,32 @@ impl<'a> App<'a> { self.cursor_position = self.input.len(); } } + + fn get_current_downlaod_dir(&self) -> Option { + match self.selected(true) { + Selected::Current(current_id) => self + .torrents + .torrents + .iter() + .find(|&t| t.id == Some(current_id)) + .and_then(|t| t.download_dir.clone()), + _ => None, + } + } + + fn set_input_from_path(&mut self, download_dir: &str) { + let mut path_buf = PathBuf::from(download_dir); + + if path_buf.is_dir() && !download_dir.ends_with('/') { + path_buf.push("/"); + } + self.update_cursor(path_buf); + } + + fn update_cursor(&mut self, path: PathBuf) { + self.input = path.to_string_lossy().to_string(); + self.cursor_position = self.input.len(); + } } fn split_path_components(path: PathBuf) -> (PathBuf, String) {