diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-10-29 22:10:01 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-10-29 22:10:33 +0100 |
commit | ce5150ae5bbe11c0be0953e1eac12d9f8f7431bc (patch) | |
tree | 5033a0e5b5c9c0d06f8355db0847d6aea100a3a0 /src | |
parent | 62b7792ea488680928ba3a927a95d0d784c7a84b (diff) |
preview instancation refactoring. Separate dirs for files
Diffstat (limited to 'src')
-rw-r--r-- | src/event_exec.rs | 36 | ||||
-rw-r--r-- | src/preview.rs | 16 | ||||
-rw-r--r-- | src/status.rs | 56 | ||||
-rw-r--r-- | src/tab.rs | 2 | ||||
-rw-r--r-- | src/term_manager.rs | 28 |
5 files changed, 82 insertions, 56 deletions
diff --git a/src/event_exec.rs b/src/event_exec.rs index 4679df5..6be910b 100644 --- a/src/event_exec.rs +++ b/src/event_exec.rs @@ -15,7 +15,6 @@ use crate::constant_strings_paths::{ CONFIG_PATH, DEFAULT_DRAGNDROP, DIFF, GIO, MEDIAINFO, NITROGEN, SSHFS_EXECUTABLE, }; use crate::cryptsetup::{lsblk_and_cryptsetup_installed, BlockDeviceAction}; -use crate::fileinfo::FileKind; use crate::filter::FilterKind; use crate::log::{read_log, write_log_line}; use crate::mocp::{is_mocp_installed, Mocp}; @@ -225,25 +224,7 @@ impl EventAction { /// more details on previewinga file. /// Does nothing if the directory is empty. pub fn preview(status: &mut Status, colors: &Colors) -> Result<()> { - if status.selected_non_mut().path_content.is_empty() { - return Ok(()); - } - let unmutable_tab = status.selected_non_mut(); - let Some(file_info) = unmutable_tab.selected() else { - return Ok(()); - }; - match file_info.file_kind { - FileKind::NormalFile => { - let preview = Preview::new(file_info).unwrap_or_default(); - status.selected().set_mode(Mode::Preview); - status.selected().window.reset(preview.len()); - status.selected().preview = preview; - } - FileKind::Directory => Self::tree(status, colors)?, - _ => (), - } - - Ok(()) + status.make_preview(colors) } /// Enter the delete mode. @@ -920,20 +901,7 @@ impl EventAction { /// Creates a tree in every mode but "Tree". /// It tree mode it will exit this view. pub fn tree(status: &mut Status, colors: &Colors) -> Result<()> { - if let Mode::Tree = status.selected_non_mut().mode { - { - let tab: &mut Tab = status.selected(); - tab.refresh_view() - }?; - status.selected().set_mode(Mode::Normal) - } else { - status.display_full = true; - status.selected().make_tree(colors)?; - status.selected().set_mode(Mode::Tree); - let len = status.selected_non_mut().directory.len(); - status.selected().window.reset(len); - } - Ok(()) + status.tree(colors) } /// Fold the current node of the tree. diff --git a/src/preview.rs b/src/preview.rs index bc00d82..013e1ba 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -26,7 +26,6 @@ use crate::decompress::{list_files_tar, list_files_zip}; use crate::fileinfo::{FileInfo, FileKind}; use crate::filter::FilterKind; use crate::opener::execute_and_capture_output_without_check; -use crate::status::Status; use crate::tree::{ColoredString, Tree}; use crate::utils::{clear_tmp_file, filename_from_path, is_program_in_path}; @@ -108,7 +107,7 @@ impl Preview { const CONTENT_INSPECTOR_MIN_SIZE: usize = 1024; /// Empty preview, holding nothing. - pub fn new_empty() -> Self { + pub fn empty() -> Self { clear_tmp_file(); Self::Empty } @@ -119,23 +118,27 @@ impl Preview { pub fn directory( file_info: &FileInfo, users_cache: &UsersCache, - status: &Status, + filter: &FilterKind, + show_hidden: bool, colors: &Colors, ) -> Result<Self> { Ok(Self::Directory(Directory::new( &file_info.path, users_cache, colors, - &status.selected_non_mut().filter, - status.selected_non_mut().show_hidden, + filter, + show_hidden, Some(2), )?)) } + /// Creates a new preview instance based on the filekind and the extension of /// the file. /// Sometimes it reads the content of the file, sometimes it delegates /// it to the display method. - pub fn new(file_info: &FileInfo) -> Result<Self> { + /// Directories aren't handled there since we need more arguments to create + /// their previews. + pub fn file(file_info: &FileInfo) -> Result<Self> { clear_tmp_file(); match file_info.file_kind { FileKind::Directory => Err(anyhow!( @@ -149,7 +152,6 @@ impl Preview { &file_info.path, extension, )?)), - // ExtensionKind::Pdf => Ok(Self::Pdf(PdfContent::new(&file_info.path))), ExtensionKind::Pdf => Ok(Self::Ueberzug(Ueberzug::make( &file_info.path, UeberzugKind::Pdf, diff --git a/src/status.rs b/src/status.rs index 6e8c2aa..becb3e7 100644 --- a/src/status.rs +++ b/src/status.rs @@ -576,6 +576,44 @@ impl Status { Ok(()) } + pub fn make_preview(&mut self, colors: &Colors) -> Result<()> { + if self.selected_non_mut().path_content.is_empty() { + return Ok(()); + } + let Some(file_info) = self.selected_non_mut().selected() else { + return Ok(()); + }; + match file_info.file_kind { + FileKind::NormalFile => { + let preview = Preview::file(file_info).unwrap_or_default(); + self.selected().set_mode(Mode::Preview); + self.selected().window.reset(preview.len()); + self.selected().preview = preview; + } + FileKind::Directory => self.tree(colors)?, + _ => (), + } + + Ok(()) + } + + pub fn tree(&mut self, colors: &Colors) -> Result<()> { + if let Mode::Tree = self.selected_non_mut().mode { + { + let tab: &mut Tab = self.selected(); + tab.refresh_view() + }?; + self.selected().set_mode(Mode::Normal) + } else { + self.display_full = true; + self.selected().make_tree(colors)?; + self.selected().set_mode(Mode::Tree); + let len = self.selected_non_mut().directory.len(); + self.selected().window.reset(len); + } + Ok(()) + } + /// 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 { @@ -587,22 +625,24 @@ impl Status { /// 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); - if !Self::display_wide_enough(&self.term)? { - self.tabs[1].preview = Preview::new_empty(); + self.tabs[1].preview = Preview::empty(); return Ok(()); } + self.tabs[1].set_mode(Mode::Preview); let fileinfo = self.tabs[0] .selected() .context("force preview: No file to select")?; let preview = match fileinfo.file_kind { - FileKind::Directory => { - let users_cache = &self.tabs[0].path_content.users_cache; - Preview::directory(fileinfo, users_cache, self, colors) - } - _ => Preview::new(fileinfo), + FileKind::Directory => Preview::directory( + fileinfo, + &self.tabs[0].path_content.users_cache, + &self.tabs[0].filter, + self.tabs[0].show_hidden, + colors, + ), + _ => Preview::file(fileinfo), }; self.tabs[1].preview = preview.unwrap_or_default(); @@ -140,7 +140,7 @@ impl Tab { pub fn refresh_params(&mut self) -> Result<()> { self.filter = FilterKind::All; self.input.reset(); - self.preview = Preview::new_empty(); + self.preview = Preview::empty(); self.completion.reset(); self.directory.clear(); Ok(()) diff --git a/src/term_manager.rs b/src/term_manager.rs index 9c7f30c..01277be 100644 --- a/src/term_manager.rs +++ b/src/term_manager.rs @@ -67,6 +67,12 @@ const ATTR_YELLOW_BOLD: Attr = Attr { effect: Effect::BOLD, }; +const ATTR_CYAN_BOLD: Attr = Attr { + fg: Color::CYAN, + bg: Color::Default, + effect: Effect::BOLD, +}; + /// Simple struct to read the events. pub struct EventReader { term: Arc<Term>, @@ -204,7 +210,7 @@ impl<'a> WinMain<'a> { let Some(file) = tab.selected() else { return Ok(0); }; - self.second_line_detailed(file, canvas) + self.second_line_detailed(file, status, canvas) } else { self.second_line_simple(status, canvas) } @@ -213,18 +219,28 @@ impl<'a> WinMain<'a> { } } - fn second_line_detailed(&self, file: &FileInfo, canvas: &mut dyn Canvas) -> Result<usize> { + fn second_line_detailed( + &self, + file: &FileInfo, + status: &Status, + canvas: &mut dyn Canvas, + ) -> Result<usize> { let owner_size = file.owner.len(); let group_size = file.group.len(); let mut attr = fileinfo_attr(file, self.colors); attr.effect ^= Effect::REVERSE; - Ok(canvas.print_with_attr(1, 0, &file.format(owner_size, group_size)?, attr)?) + + if status.flagged.contains(&file.path) { + canvas.print_with_attr(1, 0, "█", ATTR_CYAN_BOLD)?; + attr.effect |= Effect::BOLD; + } + Ok(canvas.print_with_attr(1, 1, &file.format(owner_size, group_size)?, attr)?) } fn second_line_simple(&self, status: &Status, canvas: &mut dyn Canvas) -> Result<usize> { Ok(canvas.print_with_attr( 1, - 0, + 1, &format!("{}", &status.selected_non_mut().filter), ATTR_YELLOW_BOLD, )?) @@ -370,7 +386,7 @@ impl<'a> WinMain<'a> { }; if status.flagged.contains(&file.path) { attr.effect |= Effect::BOLD; - canvas.print_with_attr(row, 0, "█", ATTR_YELLOW_BOLD)?; + canvas.print_with_attr(row, 0, "█", ATTR_CYAN_BOLD)?; } canvas.print_with_attr(row, 1, &string, attr)?; } @@ -397,7 +413,7 @@ impl<'a> WinMain<'a> { let mut attr = colored_string.attr; if status.flagged.contains(&colored_string.path) { attr.effect |= Effect::BOLD; - canvas.print_with_attr(row, 0, "█", ATTR_YELLOW_BOLD)?; + canvas.print_with_attr(row, 0, "█", ATTR_CYAN_BOLD)?; } let col_metadata = if status.display_full { |