From f5902708a6e597064482edfc076248a67882b7c5 Mon Sep 17 00:00:00 2001 From: Jiayi Zhao Date: Tue, 15 Dec 2020 11:20:35 -0500 Subject: folder view now always prefix file names with a space - remove horizontal margins in folder view - move copy/cut methods into separate functions - change some fields to private --- src/ui/widgets/tui_dirlist.rs | 36 ++++++++++++------------ src/ui/widgets/tui_dirlist_detailed.rs | 51 +++++++++++++++++----------------- src/ui/widgets/tui_footer.rs | 18 +++++------- 3 files changed, 51 insertions(+), 54 deletions(-) (limited to 'src/ui/widgets') diff --git a/src/ui/widgets/tui_dirlist.rs b/src/ui/widgets/tui_dirlist.rs index afa3258..b19aad7 100644 --- a/src/ui/widgets/tui_dirlist.rs +++ b/src/ui/widgets/tui_dirlist.rs @@ -28,8 +28,7 @@ impl<'a> Widget for TuiDirList<'a> { let x = area.left(); let y = area.top(); - let dir_len = self.dirlist.contents.len(); - if dir_len == 0 { + if self.dirlist.contents.len() == 0 { let style = Style::default().bg(Color::Red).fg(Color::White); buf.set_stringn(x, y, "empty", area.width as usize, style); return; @@ -39,8 +38,10 @@ impl<'a> Widget for TuiDirList<'a> { let skip_dist = curr_index / area.height as usize * area.height as usize; let screen_index = curr_index % area.height as usize; - let area_width = area.width as usize - 1; - let space_fill = " ".repeat(area_width); + let drawing_width = area.width as usize - 2; + let space_fill = " ".repeat(drawing_width + 1); + + let x_start = x + 1; for (i, entry) in self .dirlist @@ -60,46 +61,45 @@ impl<'a> Widget for TuiDirList<'a> { entry.get_style() }; - let file_type = &entry.metadata.file_type; - match file_type { + match entry.metadata.file_type() { FileType::Directory => { - if name_width <= area_width { - buf.set_stringn(x, y + i as u16, name, area_width, style); + if name_width <= drawing_width { + buf.set_stringn(x_start, y + i as u16, name, drawing_width, style); } else { - 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); + buf.set_stringn(x_start, y + i as u16, name, drawing_width - 1, style); + buf.set_string(x_start + drawing_width as u16 - 1, y + i as u16, "…", style); } } _ => { - if name_width < area_width { - buf.set_stringn(x, y + i as u16, name, area_width, style); + if name_width < drawing_width { + buf.set_stringn(x_start, y + i as u16, name, drawing_width, style); } else { match name.rfind('.') { None => { - buf.set_stringn(x, y + i as u16, name, area_width, style); + buf.set_stringn(x_start, y + i as u16, name, drawing_width, style); } Some(p_ind) => { let ext_width = name[p_ind..].width(); - let file_name_width = area_width - ext_width - 1; + let file_name_width = drawing_width - ext_width - 1; buf.set_stringn( - x, + x_start, y + i as u16, &name[..p_ind], file_name_width, style, ); buf.set_string( - x + file_name_width as u16, + x_start + file_name_width as u16, y + i as u16, "…", style, ); buf.set_stringn( - x + file_name_width as u16 + 1, + x_start + file_name_width as u16 + 1, y + i as u16, &name[p_ind..], - area_width - file_name_width, + drawing_width - file_name_width, style, ); } diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs index b63ebdb..422793a 100644 --- a/src/ui/widgets/tui_dirlist_detailed.rs +++ b/src/ui/widgets/tui_dirlist_detailed.rs @@ -42,8 +42,10 @@ impl<'a> Widget for TuiDirListDetailed<'a> { let skip_dist = curr_index / area.height as usize * area.height as usize; let screen_index = curr_index % area.height as usize; - let area_width = area.width as usize; - let space_fill = " ".repeat(area_width); + let drawing_width = area.width as usize; + let space_fill = " ".repeat(drawing_width); + + let x_start = x + 1; for (i, entry) in self .dirlist @@ -63,67 +65,66 @@ impl<'a> Widget for TuiDirListDetailed<'a> { buf.set_string(x, y + i as u16, space_fill.as_str(), style); - let file_type = &entry.metadata.file_type; - match file_type { + match entry.metadata.file_type() { FileType::Directory => { - if name_width <= area_width { - buf.set_string(x, y + i as u16, name, style); + if name_width <= drawing_width { + buf.set_string(x_start, y + i as u16, name, style); } else { - 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, ELLIPSIS, style); + buf.set_stringn(x_start, y + i as u16, name, drawing_width - 1, style); + buf.set_string(x_start + drawing_width as u16 - 1, y + i as u16, ELLIPSIS, style); } } FileType::Symlink(_) => { - if name_width < area_width - 4 { - buf.set_string(x, y + i as u16, name, style); - buf.set_string(x + area_width as u16 - 4, y + i as u16, "->", style); + if name_width < drawing_width - 4 { + buf.set_string(x_start, y + i as u16, name, style); + buf.set_string(x_start + drawing_width as u16 - 4, y + i as u16, "->", style); } else { - 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, ELLIPSIS, style); + buf.set_stringn(x_start, y + i as u16, name, drawing_width - 1, style); + buf.set_string(x_start + drawing_width as u16 - 1, y + i as u16, ELLIPSIS, style); } } FileType::File => { - if name_width < area_width - FILE_SIZE_WIDTH { - buf.set_stringn(x, y + i as u16, name, area_width - FILE_SIZE_WIDTH, style); + if name_width < drawing_width - FILE_SIZE_WIDTH { + buf.set_stringn(x_start, y + i as u16, name, drawing_width - FILE_SIZE_WIDTH, style); } else { match name.rfind('.') { None => { buf.set_stringn( - x, + x_start, y + i as u16, name, - area_width - FILE_SIZE_WIDTH, + drawing_width - FILE_SIZE_WIDTH, style, ); } Some(p_ind) => { let ext_width = name[p_ind..].width(); let file_name_width = - if ext_width > area_width - FILE_SIZE_WIDTH - 2 { + if ext_width > drawing_width - FILE_SIZE_WIDTH - 2 { 0 } else { - area_width - FILE_SIZE_WIDTH - ext_width - 2 + drawing_width - FILE_SIZE_WIDTH - ext_width - 2 }; buf.set_stringn( - x, + x_start, y + i as u16, &name[..p_ind], file_name_width, style, ); buf.set_string( - x + file_name_width as u16, + x_start + file_name_width as u16, y + i as u16, ELLIPSIS, style, ); let file_ext_width = - area_width - file_name_width - FILE_SIZE_WIDTH - 2; + drawing_width - file_name_width - FILE_SIZE_WIDTH - 2; buf.set_stringn( - x + file_name_width as u16 + 1, + x_start + file_name_width as u16 + 1, y + i as u16, &name[p_ind..], file_ext_width, @@ -132,9 +133,9 @@ impl<'a> Widget for TuiDirListDetailed<'a> { } } } - let file_size_string = format::file_size_to_string(entry.metadata.len); + let file_size_string = format::file_size_to_string(entry.metadata.len()); buf.set_string( - x + (area_width - FILE_SIZE_WIDTH) as u16, + x_start + (drawing_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 35189dc..603d5a6 100644 --- a/src/ui/widgets/tui_footer.rs +++ b/src/ui/widgets/tui_footer.rs @@ -23,16 +23,12 @@ impl<'a> Widget for TuiFooter<'a> { match self.list.index { Some(i) if i < self.list.contents.len() => { let entry = &self.list.contents[i]; - let mode = entry.metadata.permissions.mode(); - let mode = format::mode_to_string(mode); let mode_style = Style::default().fg(Color::Cyan); + let mode_str = format::mode_to_string(entry.metadata.permissions_ref().mode()); - let mtime = entry.metadata.modified; - let mtime = format::mtime_to_string(mtime); - - let size = entry.metadata.len; - let size = format::file_size_to_string(size); + let mtime_str = format::mtime_to_string(entry.metadata.modified()); + let size_str = format::file_size_to_string(entry.metadata.len()); #[cfg(unix)] let mimetype = match entry.metadata.mimetype.as_ref() { @@ -41,20 +37,20 @@ impl<'a> Widget for TuiFooter<'a> { }; let mut text = vec![ - Span::styled(mode, mode_style), + Span::styled(mode_str, mode_style), Span::raw(" "), Span::raw(format!("{}/{}", i + 1, self.list.contents.len())), Span::raw(" "), - Span::raw(mtime), + Span::raw(mtime_str), Span::raw(" "), - Span::raw(size), + Span::raw(size_str), #[cfg(unix)] Span::raw(" "), #[cfg(unix)] Span::raw(mimetype), ]; - if let FileType::Symlink(s) = &entry.metadata.file_type { + if let FileType::Symlink(s) = entry.metadata.file_type() { text.push(Span::styled(" -> ", mode_style)); text.push(Span::styled(s, mode_style)); } -- cgit v1.2.3