diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-08-31 12:13:55 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-08-31 12:13:55 -0400 |
commit | 39b131a910ab8da1df2385280367e3fcc0750fb6 (patch) | |
tree | aeb6640522e08a5242cbc2143fd294dc5cedb407 /src/ui | |
parent | 0ca4ffd657f1a2884ca8422be8a78baa42f00de4 (diff) |
more fine grain error handling for previewing directories
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/views/tui_folder_view.rs | 59 | ||||
-rw-r--r-- | src/ui/widgets/mod.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_message.rs | 27 |
3 files changed, 71 insertions, 17 deletions
diff --git a/src/ui/views/tui_folder_view.rs b/src/ui/views/tui_folder_view.rs index dd5e2e7..f1f4c88 100644 --- a/src/ui/views/tui_folder_view.rs +++ b/src/ui/views/tui_folder_view.rs @@ -6,10 +6,11 @@ use tui::text::Span; use tui::widgets::{Block, Borders, Paragraph, Widget, Wrap}; use crate::context::AppContext; +use crate::preview::preview_default::PreviewState; use crate::ui; use crate::ui::widgets::{ - TuiDirList, TuiDirListDetailed, TuiDirListLoading, TuiFilePreview, TuiFooter, TuiTabBar, - TuiTopBar, + TuiDirList, TuiDirListDetailed, TuiDirListLoading, TuiFilePreview, TuiFooter, TuiMessage, + TuiTabBar, TuiTopBar, }; use crate::ui::PreviewArea; @@ -34,6 +35,7 @@ impl<'a> Widget for TuiFolderView<'a> { let preview_context = self.context.preview_context_ref(); let curr_tab = self.context.tab_context_ref().curr_tab_ref(); let curr_tab_id = self.context.tab_context_ref().curr_tab_id(); + let curr_tab_cwd = curr_tab.cwd(); let curr_list = curr_tab.curr_list_ref(); let child_list = curr_tab.child_list_ref(); @@ -139,27 +141,50 @@ impl<'a> Widget for TuiFolderView<'a> { TuiFooter::new(list).render(rect, buf); } } + } else { + match curr_tab.history_metadata_ref().get(curr_tab_cwd) { + Some(PreviewState::Loading) => { + TuiDirListLoading::new().render(layout_rect[1], buf); + } + Some(PreviewState::Error { message }) => { + TuiMessage::new(&message, Style::default().fg(Color::Red)) + .render(layout_rect[1], buf); + } + None => {} + } } if let Some(list) = child_list.as_ref() { TuiDirList::new(list, true).render(layout_rect[2], buf); - } else if curr_entry.is_some() { - let preview_area = calculate_preview(self.context, layout_rect[2]); - if let Some(preview_area) = preview_area { - let area = Rect { - x: preview_area.preview_area.x, - y: preview_area.preview_area.y, - width: preview_area.preview_area.width, - height: preview_area.preview_area.height, - }; - if let Some(Some(preview)) = - preview_context.get_preview_ref(&preview_area.file_preview_path) - { - TuiFilePreview::new(preview).render(area, buf); + } else if let Some(entry) = curr_entry { + match curr_tab.history_metadata_ref().get(entry.file_path()) { + Some(PreviewState::Loading) => { + TuiDirListLoading::new().render(layout_rect[2], buf); + } + Some(PreviewState::Error { message }) => { + TuiMessage::new(&message, Style::default().fg(Color::Red)) + .render(layout_rect[2], buf); + } + None => { + let preview_area = calculate_preview(self.context, layout_rect[2]); + if let Some(preview_area) = preview_area { + let area = Rect { + x: preview_area.preview_area.x, + y: preview_area.preview_area.y, + width: preview_area.preview_area.width, + height: preview_area.preview_area.height, + }; + if let Some(Some(preview)) = + preview_context.get_preview_ref(&preview_area.file_preview_path) + { + TuiFilePreview::new(preview).render(area, buf); + } + } } } } else { - TuiDirListLoading::new().render(layout_rect[2], buf); + TuiMessage::new("Error loading directory", Style::default().fg(Color::Red)) + .render(layout_rect[2], buf); } let topbar_width = area.width; @@ -169,7 +194,7 @@ impl<'a> Widget for TuiFolderView<'a> { width: topbar_width, height: 1, }; - TuiTopBar::new(self.context, curr_tab.cwd()).render(rect, buf); + TuiTopBar::new(self.context, curr_tab_cwd).render(rect, buf); // render tabs if self.context.tab_context_ref().len() > 1 { diff --git a/src/ui/widgets/mod.rs b/src/ui/widgets/mod.rs index ab74629..1871da6 100644 --- a/src/ui/widgets/mod.rs +++ b/src/ui/widgets/mod.rs @@ -5,6 +5,7 @@ mod tui_file_preview; mod tui_footer; mod tui_help; mod tui_menu; +mod tui_message; mod tui_prompt; mod tui_tab; mod tui_text; @@ -18,6 +19,7 @@ pub use self::tui_file_preview::TuiFilePreview; pub use self::tui_footer::TuiFooter; pub use self::tui_help::{get_keymap_table, TuiHelp}; pub use self::tui_menu::TuiMenu; +pub use self::tui_message::TuiMessage; pub use self::tui_prompt::TuiPrompt; pub use self::tui_tab::TuiTabBar; pub use self::tui_text::TuiMultilineText; diff --git a/src/ui/widgets/tui_message.rs b/src/ui/widgets/tui_message.rs new file mode 100644 index 0000000..d4d245c --- /dev/null +++ b/src/ui/widgets/tui_message.rs @@ -0,0 +1,27 @@ +use tui::buffer::Buffer; +use tui::layout::Rect; +use tui::style::Style; +use tui::widgets::Widget; + +pub struct TuiMessage<'a> { + message: &'a str, + style: Style, +} + +impl<'a> TuiMessage<'a> { + pub fn new(message: &'a str, style: Style) -> Self { + Self { message, style } + } +} + +impl<'a> Widget for TuiMessage<'a> { + fn render(self, area: Rect, buf: &mut Buffer) { + if area.width < 4 || area.height < 1 { + return; + } + let x = area.left(); + let y = area.top(); + + buf.set_stringn(x, y, self.message, area.width as usize, self.style); + } +} |