diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-10-18 06:56:28 +0200 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-10-18 06:56:28 +0200 |
commit | a8715f9e7c7c5f6be9951d00925a1e3761f7e6cb (patch) | |
tree | ad0ff1dfd1339f79ac56a92c74a4c993559450d3 /src | |
parent | 21f816f414453c6add452f86bae9ac5c821111e4 (diff) |
second pane follow. Not clean, makes skim hang
Diffstat (limited to 'src')
-rw-r--r-- | src/action_map.rs | 24 | ||||
-rw-r--r-- | src/event_dispatch.rs | 6 | ||||
-rw-r--r-- | src/event_exec.rs | 164 | ||||
-rw-r--r-- | src/preview.rs | 4 | ||||
-rw-r--r-- | src/skim.rs | 8 | ||||
-rw-r--r-- | src/status.rs | 27 | ||||
-rw-r--r-- | src/term_manager.rs | 41 |
7 files changed, 170 insertions, 104 deletions
diff --git a/src/action_map.rs b/src/action_map.rs index 92cc4f8..3fa3d1a 100644 --- a/src/action_map.rs +++ b/src/action_map.rs @@ -127,20 +127,20 @@ impl ActionMap { ActionMap::Diff => EventAction::diff(status), ActionMap::DragNDrop => EventAction::drag_n_drop(status), ActionMap::EncryptedDrive => EventAction::encrypted_drive(status), - ActionMap::End => EventAction::end(current_tab, colors), + ActionMap::End => EventAction::end(status, colors), ActionMap::Enter => EventAction::enter(status, colors), ActionMap::Exec => EventAction::exec(current_tab), ActionMap::Filter => EventAction::filter(current_tab), ActionMap::FlagAll => EventAction::flag_all(status), - ActionMap::FuzzyFind => EventAction::fuzzyfind(status), + ActionMap::FuzzyFind => EventAction::fuzzyfind(status, colors), ActionMap::FuzzyFindHelp => EventAction::fuzzyfind_help(status), - ActionMap::FuzzyFindLine => EventAction::fuzzyfind_line(status), - ActionMap::GoRoot => EventAction::go_root(current_tab), - ActionMap::GoStart => EventAction::go_start(status), + ActionMap::FuzzyFindLine => EventAction::fuzzyfind_line(status, colors), + ActionMap::GoRoot => EventAction::go_root(status, colors), + ActionMap::GoStart => EventAction::go_start(status, colors), ActionMap::Goto => EventAction::goto(current_tab), ActionMap::Help => EventAction::help(status), ActionMap::History => EventAction::history(current_tab), - ActionMap::Home => EventAction::home(current_tab), + ActionMap::Home => EventAction::home(status, colors), ActionMap::Jump => EventAction::jump(status), ActionMap::KeyHome => EventAction::key_home(status, colors), ActionMap::Log => EventAction::log(current_tab), @@ -149,7 +149,7 @@ impl ActionMap { ActionMap::MediaInfo => EventAction::mediainfo(current_tab), ActionMap::MocpAddToPlayList => EventAction::mocp_add_to_playlist(current_tab), ActionMap::MocpClearPlaylist => EventAction::mocp_clear_playlist(), - ActionMap::MocpGoToSong => EventAction::mocp_go_to_song(current_tab), + ActionMap::MocpGoToSong => EventAction::mocp_go_to_song(status, colors), ActionMap::MocpNext => EventAction::mocp_next(), ActionMap::MocpPrevious => EventAction::mocp_previous(), ActionMap::MocpTogglePause => EventAction::mocp_toggle_pause(status), @@ -163,8 +163,8 @@ impl ActionMap { ActionMap::NvimSetAddress => EventAction::set_nvim_server(status), ActionMap::OpenConfig => EventAction::open_config(status), ActionMap::OpenFile => EventAction::open_file(status), - ActionMap::PageDown => EventAction::page_down(current_tab, colors), - ActionMap::PageUp => EventAction::page_up(current_tab, colors), + ActionMap::PageDown => EventAction::page_down(status, colors), + ActionMap::PageUp => EventAction::page_up(status, colors), ActionMap::Preview => EventAction::preview(status, colors), ActionMap::Quit => EventAction::quit(current_tab), ActionMap::RefreshIfNeeded => EventAction::refresh_if_needed(current_tab), @@ -175,7 +175,7 @@ impl ActionMap { ActionMap::ResetMode => EventAction::reset_mode(current_tab), ActionMap::ReverseFlags => EventAction::reverse_flags(status), ActionMap::Search => EventAction::search(current_tab), - ActionMap::SearchNext => EventAction::search_next(current_tab), + ActionMap::SearchNext => EventAction::search_next(status, colors), ActionMap::SetWallpaper => EventAction::set_wallpaper(current_tab), ActionMap::Shell => EventAction::shell(status), ActionMap::ShellCommand => EventAction::shell_command(current_tab), @@ -188,11 +188,11 @@ impl ActionMap { ActionMap::ToggleDualPane => EventAction::toggle_dualpane(status), ActionMap::ToggleFlag => EventAction::toggle_flag(status), ActionMap::ToggleHidden => EventAction::toggle_hidden(status, colors), - ActionMap::TogglePreviewSecond => EventAction::toggle_preview_second(status), + ActionMap::TogglePreviewSecond => EventAction::toggle_preview_second(status, colors), ActionMap::TrashEmpty => EventAction::trash_empty(status), ActionMap::TrashMoveFile => EventAction::trash_move_file(status), ActionMap::TrashOpen => EventAction::trash_open(status), - ActionMap::TrashRestoreFile => LeaveMode::trash(status), + ActionMap::TrashRestoreFile => LeaveMode::trash(status, colors), ActionMap::Tree => EventAction::tree(status, colors), ActionMap::TreeFold => EventAction::tree_fold(current_tab, colors), ActionMap::TreeFoldAll => EventAction::tree_fold_all(current_tab, colors), diff --git a/src/event_dispatch.rs b/src/event_dispatch.rs index c5a55e3..8331a9f 100644 --- a/src/event_dispatch.rs +++ b/src/event_dispatch.rs @@ -59,11 +59,7 @@ impl EventDispatcher { Event::Key(key) => self.key_matcher(status, key, colors)?, _ => (), }; - if status.dual_pane && status.preview_second { - status.force_preview(colors) - } else { - Ok(()) - } + Ok(()) } fn key_matcher(&self, status: &mut Status, key: Key, colors: &Colors) -> Result<()> { diff --git a/src/event_exec.rs b/src/event_exec.rs index d2b761c..db48e95 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -491,21 +491,26 @@ impl EventAction { } /// Move to $HOME aka ~. - pub fn home(tab: &mut Tab) -> Result<()> { + pub fn home(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); let home_cow = shellexpand::tilde("~"); let home: &str = home_cow.borrow(); let path = std::fs::canonicalize(home)?; - tab.set_pathcontent(&path) + tab.set_pathcontent(&path)?; + status.update_second_pane_for_preview(colors) } - pub fn go_root(tab: &mut Tab) -> Result<()> { + pub fn go_root(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); let root_path = std::path::PathBuf::from("/"); - tab.set_pathcontent(&root_path) + tab.set_pathcontent(&root_path)?; + status.update_second_pane_for_preview(colors) } - pub fn go_start(status: &mut Status) -> Result<()> { + pub fn go_start(status: &mut Status, colors: &Colors) -> Result<()> { let start_folder = status.start_folder.clone(); - status.selected().set_pathcontent(&start_folder) + status.selected().set_pathcontent(&start_folder)?; + status.update_second_pane_for_preview(colors) } /// Executes a `dragon-drop` command on the selected file. @@ -527,7 +532,8 @@ impl EventAction { Ok(()) } - pub fn search_next(tab: &mut Tab) -> Result<()> { + pub fn search_next(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); match tab.mode { Mode::Tree => (), _ => { @@ -536,6 +542,7 @@ impl EventAction { }; let next_index = (tab.path_content.index + 1) % tab.path_content.content.len(); tab.search_from(&searched, next_index); + status.update_second_pane_for_preview(colors)?; } } Ok(()) @@ -561,7 +568,7 @@ impl EventAction { Mode::Tree => tab.tree_select_prev(colors)?, _ => (), }; - Ok(()) + status.update_second_pane_for_preview(colors) } /// Move down one row in modes allowing movements. @@ -583,7 +590,7 @@ impl EventAction { Mode::Tree => status.selected().select_next(colors)?, _ => (), }; - Ok(()) + status.update_second_pane_for_preview(colors) } /// Move to parent in normal mode, @@ -591,15 +598,15 @@ impl EventAction { pub fn move_left(status: &mut Status, colors: &Colors) -> Result<()> { let tab = status.selected(); match tab.mode { - Mode::Normal => tab.move_to_parent(), - Mode::Tree => tab.tree_select_parent(colors), + Mode::Normal => tab.move_to_parent()?, + Mode::Tree => tab.tree_select_parent(colors)?, Mode::InputSimple(_) | Mode::InputCompleted(_) => { tab.input.cursor_left(); - Ok(()) } - _ => Ok(()), + _ => (), } + status.update_second_pane_for_preview(colors) } /// Move to child if any or open a regular file in normal mode. @@ -608,7 +615,10 @@ impl EventAction { let tab: &mut Tab = status.selected(); match tab.mode { Mode::Normal => LeaveMode::open_file(status), - Mode::Tree => tab.select_first_child(colors), + Mode::Tree => { + tab.select_first_child(colors)?; + status.update_second_pane_for_preview(colors) + } Mode::InputSimple(_) | Mode::InputCompleted(_) => { tab.input.cursor_right(); Ok(()) @@ -651,34 +661,51 @@ impl EventAction { Mode::Tree => tab.tree_go_to_root(colors)?, _ => tab.input.cursor_start(), }; - Ok(()) + status.update_second_pane_for_preview(colors) } /// Move to the bottom in any mode. - pub fn end(tab: &mut Tab, colors: &Colors) -> Result<()> { + pub fn end(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); match tab.mode { Mode::Normal | Mode::Preview => tab.go_bottom(), Mode::Tree => tab.tree_go_to_bottom_leaf(colors)?, _ => tab.input.cursor_end(), }; - Ok(()) + status.update_second_pane_for_preview(colors) } /// Move up 10 lines in normal mode and preview. - pub fn page_up(tab: &mut Tab, colors: &Colors) -> Result<()> { + pub fn page_up(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); match tab.mode { - Mode::Normal | Mode::Preview => tab.page_up(), - Mode::Tree => tab.tree_page_up(colors)?, + Mode::Normal => { + tab.page_up(); + status.update_second_pane_for_preview(colors)?; + } + Mode::Preview => tab.page_up(), + Mode::Tree => { + tab.tree_page_up(colors)?; + status.update_second_pane_for_preview(colors)?; + } _ => (), }; Ok(()) } /// Move down 10 lines in normal & preview mode. - pub fn page_down(tab: &mut Tab, colors: &Colors) -> Result<()> { + pub fn page_down(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); match tab.mode { - Mode::Normal | Mode::Preview => tab.page_down(), - Mode::Tree => tab.tree_page_down(colors)?, + Mode::Normal => { + tab.page_down(); + status.update_second_pane_for_preview(colors)?; + } + Mode::Preview => tab.page_down(), + Mode::Tree => { + tab.tree_page_down(colors)?; + status.update_second_pane_for_preview(colors)?; + } _ => (), }; Ok(()) @@ -715,12 +742,12 @@ impl EventAction { Mode::InputSimple(InputSimple::Remote) => LeaveMode::remote(status.selected())?, Mode::Navigate(Navigate::Jump) => { must_refresh = false; - LeaveMode::jump(status)? + LeaveMode::jump(status, colors)? } - Mode::Navigate(Navigate::History) => LeaveMode::history(status.selected())?, - Mode::Navigate(Navigate::Shortcut) => LeaveMode::shortcut(status.selected())?, - Mode::Navigate(Navigate::Trash) => LeaveMode::trash(status)?, - Mode::Navigate(Navigate::Bulk) => LeaveMode::bulk(status)?, + Mode::Navigate(Navigate::History) => LeaveMode::history(status, colors)?, + Mode::Navigate(Navigate::Shortcut) => LeaveMode::shortcut(status, colors)?, + Mode::Navigate(Navigate::Trash) => LeaveMode::trash(status, colors)?, + Mode::Navigate(Navigate::Bulk) => LeaveMode::bulk(status, colors)?, Mode::Navigate(Navigate::ShellMenu) => LeaveMode::shellmenu(status)?, Mode::Navigate(Navigate::CliInfo) => { must_refresh = false; @@ -729,14 +756,16 @@ impl EventAction { } Mode::Navigate(Navigate::EncryptedDrive) => (), Mode::Navigate(Navigate::Marks(MarkAction::New)) => LeaveMode::marks_update(status)?, - Mode::Navigate(Navigate::Marks(MarkAction::Jump)) => LeaveMode::marks_jump(status)?, + Mode::Navigate(Navigate::Marks(MarkAction::Jump)) => { + LeaveMode::marks_jump(status, colors)? + } Mode::Navigate(Navigate::Compress) => LeaveMode::compress(status)?, Mode::InputCompleted(InputCompleted::Exec) => LeaveMode::exec(status.selected())?, Mode::InputCompleted(InputCompleted::Search) => { must_refresh = false; - LeaveMode::search(status.selected(), colors)? + LeaveMode::search(status, colors)? } - Mode::InputCompleted(InputCompleted::Goto) => LeaveMode::goto(status.selected())?, + Mode::InputCompleted(InputCompleted::Goto) => LeaveMode::goto(status, colors)?, Mode::InputCompleted(InputCompleted::Command) => LeaveMode::command(status, colors)?, Mode::Normal => LeaveMode::open_file(status)?, Mode::Tree => LeaveMode::tree(status, colors)?, @@ -761,10 +790,10 @@ impl EventAction { pub fn tab(status: &mut Status) -> Result<()> { match status.selected().mode { Mode::InputCompleted(_) => { - let tab: &mut Tab = status.selected(); + let tab = status.selected(); tab.input.replace(tab.completion.current_proposition()) } - Mode::Normal | Mode::Tree => status.next(), + Mode::Normal | Mode::Tree | Mode::Preview => status.next(), _ => (), }; Ok(()) @@ -773,20 +802,22 @@ impl EventAction { /// Change tab in normal mode. pub fn backtab(status: &mut Status) -> Result<()> { match status.selected().mode { - Mode::Normal | Mode::Tree => status.prev(), + Mode::Normal | Mode::Tree | Mode::Preview => status.prev(), _ => (), }; Ok(()) } /// Start a fuzzy find with skim. - pub fn fuzzyfind(status: &mut Status) -> Result<()> { - status.skim_output_to_tab() + pub fn fuzzyfind(status: &mut Status, colors: &Colors) -> Result<()> { + status.skim_output_to_tab()?; + status.update_second_pane_for_preview(colors) } /// Start a fuzzy find for a specific line with skim. - pub fn fuzzyfind_line(status: &mut Status) -> Result<()> { - status.skim_line_output_to_tab() + pub fn fuzzyfind_line(status: &mut Status, colors: &Colors) -> Result<()> { + status.skim_line_output_to_tab()?; + status.update_second_pane_for_preview(colors) } /// Start a fuzzy find for a keybinding with skim. @@ -813,7 +844,8 @@ impl EventAction { /// Refresh the current view, reloading the files. Move the selection to top. pub fn refreshview(status: &mut Status, colors: &Colors) -> Result<()> { status.encrypted_devices.update()?; - status.refresh_status(colors) + status.refresh_status(colors)?; + status.update_second_pane_for_preview(colors) } /// Refresh the view if files were modified in current directory. @@ -1009,9 +1041,9 @@ impl EventAction { } /// Toggle the second pane between preview & normal mode (files). - pub fn toggle_preview_second(status: &mut Status) -> Result<()> { + pub fn toggle_preview_second(status: &mut Status, colors: &Colors) -> Result<()> { status.preview_second = !status.preview_second; - Ok(()) + status.update_second_pane_for_preview(colors) } /// Set the current selected file as wallpaper with `nitrogen`. @@ -1055,12 +1087,15 @@ impl EventAction { } /// Add a song or a folder to MOC playlist. Start it first... - pub fn mocp_go_to_song(tab: &mut Tab) -> Result<()> { + pub fn mocp_go_to_song(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); if !is_mocp_installed() { write_log_line("mocp isn't installed".to_owned()); return Ok(()); } - Mocp::go_to_song(tab) + Mocp::go_to_song(tab)?; + + status.update_second_pane_for_preview(colors) } /// Toggle play/pause on MOC. @@ -1157,11 +1192,11 @@ pub struct LeaveMode {} impl LeaveMode { /// Restore a file from the trash if possible. /// Parent folders are created if needed. - pub fn trash(status: &mut Status) -> Result<()> { + pub fn trash(status: &mut Status, colors: &Colors) -> Result<()> { status.trash.restore()?; status.selected().reset_mode(); status.selected().refresh_view()?; - Ok(()) + status.update_second_pane_for_preview(colors) } /// Open the file with configured opener or enter the directory. @@ -1178,7 +1213,7 @@ impl LeaveMode { } /// Jump to the current mark. - pub fn marks_jump(status: &mut Status) -> Result<()> { + pub fn marks_jump(status: &mut Status, colors: &Colors) -> Result<()> { let marks = status.marks.clone(); let tab = status.selected(); if let Some((_, path)) = marks.selected() { @@ -1187,7 +1222,7 @@ impl LeaveMode { tab.window.reset(tab.path_content.content.len()); tab.input.reset(); } - Ok(()) + status.update_second_pane_for_preview(colors) } /// Update the selected mark with the current path. @@ -1209,8 +1244,9 @@ impl LeaveMode { Ok(()) } - pub fn bulk(status: &mut Status) -> Result<()> { - status.bulk.execute_bulk(status) + pub fn bulk(status: &mut Status, colors: &Colors) -> Result<()> { + status.bulk.execute_bulk(status)?; + status.update_second_pane_for_preview(colors) } pub fn shellmenu(status: &mut Status) -> Result<()> { @@ -1258,7 +1294,7 @@ impl LeaveMode { /// Execute a jump to the selected flagged file. /// If the user selected a directory, we jump inside it. /// Otherwise, we jump to the parent and select the file. - pub fn jump(status: &mut Status) -> Result<()> { + pub fn jump(status: &mut Status, colors: &Colors) -> Result<()> { let Some(jump_target) = status.flagged.selected() else { return Ok(()); }; @@ -1270,8 +1306,7 @@ impl LeaveMode { status.selected().set_pathcontent(target_dir)?; let index = status.selected().path_content.select_file(&jump_target); status.selected().scroll_to(index); - - Ok(()) + status.update_second_pane_for_preview(colors) } /// Select the first file matching the typed regex in current dir. @@ -1389,7 +1424,8 @@ impl LeaveMode { /// ie. If you typed `"jpg"` before, it will move to the first file /// whose filename contains `"jpg"`. /// The current order of files is used. - pub fn search(tab: &mut Tab, colors: &Colors) -> Result<()> { + pub fn search(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); let searched = tab.input.string(); tab.input.reset(); if searched.is_empty() { @@ -1408,21 +1444,21 @@ impl LeaveMode { tab.directory.tree.select_root() }; tab.directory.make_preview(colors); - Ok(()) } _ => { let next_index = tab.path_content.index; tab.search_from(&searched, next_index); - Ok(()) } - } + }; + status.update_second_pane_for_preview(colors) } /// Move to the folder typed by the user. /// The first completion proposition is used, `~` expansion is done. /// If no result were found, no cd is done and we go back to normal mode /// silently. - pub fn goto(tab: &mut Tab) -> Result<()> { + pub fn goto(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); if tab.completion.is_empty() { return Ok(()); } @@ -1432,12 +1468,13 @@ impl LeaveMode { tab.history.push(&path); tab.set_pathcontent(&path)?; tab.window.reset(tab.path_content.content.len()); - Ok(()) + status.update_second_pane_for_preview(colors) } /// Move to the selected shortcut. /// It may fail if the user has no permission to visit the path. - pub fn shortcut(tab: &mut Tab) -> Result<()> { + pub fn shortcut(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); tab.input.reset(); let path = tab .shortcut @@ -1446,12 +1483,14 @@ impl LeaveMode { .clone(); tab.history.push(&path); tab.set_pathcontent(&path)?; - tab.refresh_view() + tab.refresh_view()?; + status.update_second_pane_for_preview(colors) } /// Move back to a previously visited path. /// It may fail if the user has no permission to visit the path - pub fn history(tab: &mut Tab) -> Result<()> { + pub fn history(status: &mut Status, colors: &Colors) -> Result<()> { + let tab = status.selected(); tab.input.reset(); let path = tab .history @@ -1460,7 +1499,8 @@ impl LeaveMode { .clone(); tab.set_pathcontent(&path)?; tab.history.drop_queue(); - tab.refresh_view() + tab.refresh_view()?; + status.update_second_pane_for_preview(colors) } /// Execute the selected node if it's a file else enter the directory. diff --git a/src/preview.rs b/src/preview.rs index 38cb390..f76e0b8 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -1022,6 +1022,7 @@ impl Directory { pub fn select_root(&mut self, colors: &Colors) -> Result<()> { self.tree.select_root(); (self.selected_index, self.content) = self.tree.into_navigable_content(colors); + self.update_tree_position_from_index(colors)?; Ok(()) } @@ -1090,6 +1091,7 @@ impl Directory { pub fn select_first_child(&mut self, colors: &Colors) -> Result<()> { self.tree.select_first_child()?; (self.selected_index, self.content) = self.tree.into_navigable_content(colors); + self.update_tree_position_from_index(colors)?; Ok(()) } @@ -1097,6 +1099,7 @@ impl Directory { pub fn select_parent(&mut self, colors: &Colors) -> Result<()> { self.tree.select_parent()?; (self.selected_index, self.content) = self.tree.into_navigable_content(colors); + self.update_tree_position_from_index(colors)?; Ok(()) } @@ -1104,6 +1107,7 @@ impl Directory { pub fn go_to_bottom_leaf(&mut self, colors: &Colors) -> Result<()> { self.tree.go_to_bottom_leaf()?; (self.selected_index, self.content) = self.tree.into_navigable_content(colors); + self.update_tree_position_from_index(colors)?; Ok(()) } diff --git a/src/skim.rs b/src/skim.rs index f35b0eb..b78041e 100644 --- a/src/skim.rs +++ b/src/skim.rs @@ -44,11 +44,11 @@ impl Skimer { /// Call skim on its term. /// Returns the file whose line match a pattern from current folder using ripgrep or grep. - pub fn search_line_in_file(&self) -> Vec<Arc<dyn SkimItem>> { + pub fn search_line_in_file(&self, path_str: &str) -> Vec<Arc<dyn SkimItem>> { self.skim .run_internal( None, - "".to_owned(), + path_str.to_owned(), None, Some(self.file_matcher.to_owned()), ) @@ -89,7 +89,9 @@ impl SkimItem for StringWrapper { fn pick_first_installed<'a>(commands: &'a [&'a str]) -> Option<&'a str> { for command in commands { - let Some(program) = command.split_whitespace().next() else { continue }; + let Some(program) = command.split_whitespace().next() else { + continue; + }; if is_program_in_path(program) { return Some(command); } diff --git a/src/status.rs b/src/status.rs index aa48a50..d2cf265 100644 --- a/src/status.rs +++ b/src/status.rs @@ -224,7 +224,11 @@ impl Status { /// It calls skim, reads its output, then update the tab content. /// The output is splited at `:` since we only care about the path, not the line number. pub fn skim_line_output_to_tab(&mut self) -> Result<()> { - let skim = self.skimer.search_line_in_file(); + let skim = self.skimer.search_line_in_file( + self.selected_non_mut() + .path_content_str() + .context("Couldn't parse current directory")?, + ); let Some(output) = skim.first() else { return Ok(()); }; @@ -474,14 +478,27 @@ impl Status { Ok(()) } - /// Force a preview on the second pane - pub fn force_preview(&mut self, colors: &Colors) -> Result<()> { - let fileinfo = &self.tabs[0] + /// Check if the second pane should display a preview and force it. + pub fn update_second_pane_for_preview(&mut self, colors: &Colors) -> Result<()> { + if self.index == 0 && self.preview_second { + self.set_second_pane_for_preview(colors)?; + }; + Ok(()) + } + + /// Force preview the selected file of the first pane in the second pane. + /// Doesn't check if it has do. + pub fn set_second_pane_for_preview(&mut self, colors: &Colors) -> Result<()> { + self.tabs[1].set_mode(Mode::Preview); + + let fileinfo = self.tabs[0] .selected() .context("force preview: No file to select")?; let users_cache = &self.tabs[0].path_content.users_cache; - self.tabs[0].preview = + self.tabs[1].preview = Preview::new(fileinfo, users_cache, self, colors).unwrap_or_default(); + + self.tabs[1].window.reset(self.tabs[1].preview.len()); Ok(()) } diff --git a/src/term_manager.rs b/src/term_manager.rs index e3cfbd4..f9775c2 100644 --- a/src/term_manager.rs +++ b/src/term_manager.rs @@ -146,7 +146,7 @@ impl<'a> Draw for WinMain<'a> { _ => self.files(self.status, self.tab, canvas), }, }?; - self.first_line(self.tab, self.disk_space, canvas)?; + self.first_line(self.disk_space, canvas)?; Ok(()) } } @@ -173,9 +173,8 @@ impl<'a> WinMain<'a> { } fn preview_as_second_pane(&self, canvas: &mut dyn Canvas) -> Result<()> { - let tab = &self.status.tabs[0]; - let (_, height) = canvas.size()?; - self.preview(tab, &tab.preview.window_for_second_pane(height), canvas)?; + let tab = &self.status.tabs[1]; + self.preview(tab, &tab.window, canvas)?; draw_colored_strings(0, 0, self.default_preview_first_line(tab), canvas, false)?; Ok(()) } @@ -187,11 +186,11 @@ impl<'a> WinMain<'a> { /// something else. /// Returns the result of the number of printed chars. /// The colors are reversed when the tab is selected. It gives a visual indication of where he is. - fn first_line(&self, tab: &Tab, disk_space: &str, canvas: &mut dyn Canvas) -> Result<()> { + fn first_line(&self, disk_space: &str, canvas: &mut dyn Canvas) -> Result<()> { draw_colored_strings( 0, 0, - self.create_first_row(tab, disk_space)?, + self.create_first_row(disk_space)?, canvas, self.attributes.is_selected, ) @@ -289,21 +288,29 @@ impl<'a> WinMain<'a> { ] } + fn pick_previewed_fileinfo(&self) -> Option<&FileInfo> { + if self.status.dual_pane && self.status.preview_second { + self.status.tabs[0].selected() + } else { + self.status.selected_non_mut().selected() + } + } + fn default_preview_first_line(&self, tab: &Tab) -> Vec<String> { - match tab.path_content.selected() { - Some(fileinfo) => { - let mut strings = vec![" Preview ".to_owned()]; - if !tab.preview.is_empty() { - strings.push(format!(" {} / {} ", tab.window.bottom, tab.preview.len())); - }; - strings.push(format!(" {} ", fileinfo.path.to_string_lossy())); - strings - } - None => vec!["".to_owned()], + if let Some(fileinfo) = self.pick_previewed_fileinfo() { + let mut strings = vec![" Preview ".to_owned()]; + if !tab.preview.is_empty() { + strings.push(format!(" {} / {} ", tab.window.bottom, tab.preview.len())); + }; + strings.push(format!(" {} ", fileinfo.path.display())); + strings + } else { + vec!["".to_owned()] } } - fn create_first_row(&self, tab: &Tab, disk_space: &str) -> Result<Vec<String>> { + fn create_first_row(&self, disk_space: &str) -> Result<Vec<String>> { + let tab = self.status.selected_non_mut(); let first_row = match tab.mode { Mode::Normal | Mode::Tree => self.normal_first_row(disk_space)?, Mode::Preview => match &tab.preview { |