From c2de16c1e1d7cc8f5cc40f3ca977e01c6e1a9f2a Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Sat, 21 Mar 2020 12:39:54 -0400 Subject: wrap fs::FileType with in-house solution - This allows for efficient symlink lookup through caching - Theme processing happens upfront rather than recomputing styling over and over --- src/ui/widgets/tui_dirlist.rs | 55 ++++++++++++---------- src/ui/widgets/tui_dirlist_detailed.rs | 83 +++++++++++++++++++--------------- src/ui/widgets/tui_footer.rs | 11 ++--- 3 files changed, 82 insertions(+), 67 deletions(-) (limited to 'src/ui') diff --git a/src/ui/widgets/tui_dirlist.rs b/src/ui/widgets/tui_dirlist.rs index 5eeb23b..8840bda 100644 --- a/src/ui/widgets/tui_dirlist.rs +++ b/src/ui/widgets/tui_dirlist.rs @@ -54,12 +54,13 @@ impl<'a> Widget for TuiDirList<'a> { let name = entry.file_name(); let name_width = name.width(); - let mut style = entry.get_style(); - if i == screen_index { - style = style.modifier(Modifier::REVERSED); - } + let style = if i == screen_index { + entry.get_style().modifier(Modifier::REVERSED) + } else { + entry.get_style() + }; - let file_type = entry.metadata.file_type; + let file_type = &entry.metadata.file_type; if file_type.is_dir() { if name_width <= area_width { buf.set_stringn(x, y + i as u16, name, area_width, style); @@ -67,27 +68,33 @@ impl<'a> Widget for TuiDirList<'a> { buf.set_stringn(x, y + i as u16, name, area_width - 1, style); buf.set_string(x + area_width as u16 - 1, y + i as u16, "…", style); } - continue; - } - if name_width < area_width { - buf.set_stringn(x, y + i as u16, name, area_width, style); } else { - match name.rfind('.') { - None => { - buf.set_stringn(x, y + i as u16, name, area_width, style); - } - Some(p_ind) => { - let ext_width = name[p_ind..].width(); - let file_name_width = area_width - ext_width - 1; + if name_width < area_width { + buf.set_stringn(x, y + i as u16, name, area_width, style); + } else { + match name.rfind('.') { + None => { + buf.set_stringn(x, y + i as u16, name, area_width, style); + } + Some(p_ind) => { + let ext_width = name[p_ind..].width(); + let file_name_width = area_width - ext_width - 1; - buf.set_stringn(x, y + i as u16, &name[..p_ind], file_name_width, style); - buf.set_string(x + file_name_width as u16, y + i as u16, "…", style); - buf.set_string( - x + file_name_width as u16 + 1, - y + i as u16, - &name[p_ind..], - style, - ); + buf.set_stringn( + x, + y + i as u16, + &name[..p_ind], + file_name_width, + style, + ); + buf.set_string(x + file_name_width as u16, y + i as u16, "…", style); + buf.set_string( + x + file_name_width as u16 + 1, + y + i as u16, + &name[p_ind..], + style, + ); + } } } } diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs index 5ec8633..ac7da28 100644 --- a/src/ui/widgets/tui_dirlist_detailed.rs +++ b/src/ui/widgets/tui_dirlist_detailed.rs @@ -21,11 +21,7 @@ impl<'a> TuiDirListDetailed<'a> { impl<'a> Widget for TuiDirListDetailed<'a> { fn draw(&mut self, area: Rect, buf: &mut Buffer) { - if area.width < 1 || area.height < 1 { - return; - } - - if area.width < 4 { + if area.width < 4 || area.height < 1 { return; } @@ -58,12 +54,13 @@ impl<'a> Widget for TuiDirListDetailed<'a> { let name = entry.file_name(); let name_width = name.width(); - let mut style = entry.get_style(); - if i == screen_index { - style = style.modifier(Modifier::REVERSED); - } + let style = if i == screen_index { + entry.get_style().modifier(Modifier::REVERSED) + } else { + entry.get_style() + }; - let file_type = entry.metadata.file_type; + let file_type = &entry.metadata.file_type; if file_type.is_dir() { if name_width <= area_width { buf.set_stringn(x, y + i as u16, name, area_width, style); @@ -71,38 +68,50 @@ impl<'a> Widget for TuiDirListDetailed<'a> { buf.set_stringn(x, y + i as u16, name, area_width - 1, style); buf.set_string(x + area_width as u16 - 1, y + i as u16, "…", style); } - continue; - } - - if name_width < area_width - FILE_SIZE_WIDTH { - buf.set_stringn(x, y + i as u16, name, area_width - FILE_SIZE_WIDTH, style); + // } else if file_type.is_symlink() { } else { - match name.rfind('.') { - None => { - buf.set_stringn(x, y + i as u16, name, area_width - FILE_SIZE_WIDTH, style); - } - Some(p_ind) => { - let ext_width = name[p_ind..].width(); - let file_name_width = area_width - FILE_SIZE_WIDTH - ext_width - 2; + if name_width < area_width - FILE_SIZE_WIDTH { + buf.set_stringn(x, y + i as u16, name, area_width - FILE_SIZE_WIDTH, style); + } else { + match name.rfind('.') { + None => { + buf.set_stringn( + x, + y + i as u16, + name, + area_width - FILE_SIZE_WIDTH, + style, + ); + } + Some(p_ind) => { + let ext_width = name[p_ind..].width(); + let file_name_width = area_width - FILE_SIZE_WIDTH - ext_width - 2; - buf.set_stringn(x, y + i as u16, &name[..p_ind], file_name_width, style); - buf.set_string(x + file_name_width as u16, y + i as u16, "…", style); - buf.set_string( - x + file_name_width as u16 + 1, - y + i as u16, - &name[p_ind..], - style, - ); + buf.set_stringn( + x, + y + i as u16, + &name[..p_ind], + file_name_width, + style, + ); + buf.set_string(x + file_name_width as u16, y + i as u16, "…", style); + buf.set_string( + x + file_name_width as u16 + 1, + y + i as u16, + &name[p_ind..], + style, + ); + } } } + let file_size_string = format::file_size_to_string(entry.metadata.len); + buf.set_string( + x + (area_width - FILE_SIZE_WIDTH) as u16, + y + i as u16, + file_size_string, + style, + ); } - let file_size_string = format::file_size_to_string(entry.metadata.len); - buf.set_string( - x + (area_width - FILE_SIZE_WIDTH) as u16, - y + i as u16, - file_size_string, - style, - ); } } } diff --git a/src/ui/widgets/tui_footer.rs b/src/ui/widgets/tui_footer.rs index e957590..83598a2 100644 --- a/src/ui/widgets/tui_footer.rs +++ b/src/ui/widgets/tui_footer.rs @@ -5,7 +5,7 @@ use tui::layout::Rect; use tui::style::{Color, Style}; use tui::widgets::{Paragraph, Text, Widget}; -use crate::fs::JoshutoDirEntry; +use crate::fs::{FileType, JoshutoDirEntry}; use crate::util::format; pub struct TuiFooter<'a> { @@ -51,13 +51,12 @@ impl<'a> Widget for TuiFooter<'a> { Text::raw(mimetype), ]; - if self.entry.metadata.file_type.is_symlink() { - if let Ok(path) = fs::read_link(self.entry.file_path()) { + match &self.entry.metadata.file_type { + FileType::Symlink(s) => { text.push(Text::styled(" -> ", mode_style)); - if let Some(s) = path.to_str() { - text.push(Text::styled(s.to_string(), mode_style)); - } + text.push(Text::styled(s, mode_style)); } + _ => {} } Paragraph::new(text.iter()).wrap(true).draw(area, buf); -- cgit v1.2.3