diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-12-06 15:12:41 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-12-06 15:12:41 -0500 |
commit | 9daeedfc29e12664539ec972ad0180c5728e161a (patch) | |
tree | 0e9fbd07fac2102b623ef009446c48d179d4b6c6 /src | |
parent | 0e3ee5aa31368a1501e24beb33ae1020c0ac332e (diff) |
add arrow indicating symlinks
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/mod.rs | 2 | ||||
-rw-r--r-- | src/ui/widgets/tui_dirlist.rs | 76 | ||||
-rw-r--r-- | src/ui/widgets/tui_dirlist_detailed.rs | 127 | ||||
-rw-r--r-- | src/util/format.rs | 5 |
4 files changed, 121 insertions, 89 deletions
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 3367c73..18b7579 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -6,8 +6,8 @@ pub mod delete_files; pub mod file_ops; pub mod new_directory; pub mod open_file; -pub mod parent_directory; pub mod parent_cursor_move; +pub mod parent_directory; pub mod quit; pub mod reload; pub mod rename_file; diff --git a/src/ui/widgets/tui_dirlist.rs b/src/ui/widgets/tui_dirlist.rs index 32ded90..60b7374 100644 --- a/src/ui/widgets/tui_dirlist.rs +++ b/src/ui/widgets/tui_dirlist.rs @@ -4,7 +4,7 @@ use tui::style::{Color, Modifier, Style}; use tui::widgets::Widget; use unicode_width::UnicodeWidthStr; -use crate::fs::JoshutoDirList; +use crate::fs::{FileType, JoshutoDirList}; pub struct TuiDirList<'a> { dirlist: &'a JoshutoDirList, @@ -37,12 +37,7 @@ impl<'a> Widget for TuiDirList<'a> { let curr_index = self.dirlist.index.unwrap(); let skip_dist = curr_index / area.height as usize * area.height as usize; - - let screen_index = if skip_dist > 0 { - curr_index % skip_dist - } else { - curr_index - }; + let screen_index = curr_index % area.height as usize; let area_width = area.width as usize - 1; for (i, entry) in self @@ -62,36 +57,49 @@ impl<'a> Widget for TuiDirList<'a> { }; 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); - } 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); - } - } else if name_width < area_width { - buf.set_stringn(x, y + i as u16, name, area_width, style); - } else { - match name.rfind('.') { - None => { + match file_type { + FileType::Directory => { + if name_width <= area_width { buf.set_stringn(x, y + i as u16, name, area_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); } - Some(0) => { - let file_name_width = area_width; - buf.set_stringn(x, y + i as u16, &name, file_name_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_stringn( + x + file_name_width as u16 + 1, + y + i as u16, + &name[p_ind..], + area_width - file_name_width, + style, + ); + } + } } } } diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs index 142757e..c76e198 100644 --- a/src/ui/widgets/tui_dirlist_detailed.rs +++ b/src/ui/widgets/tui_dirlist_detailed.rs @@ -4,7 +4,7 @@ use tui::style::{Color, Modifier, Style}; use tui::widgets::Widget; use unicode_width::UnicodeWidthStr; -use crate::fs::JoshutoDirList; +use crate::fs::{FileType, JoshutoDirList}; use crate::util::format; const FILE_SIZE_WIDTH: usize = 8; @@ -27,7 +27,6 @@ impl<'a> Widget for TuiDirListDetailed<'a> { let x = area.left(); let y = area.top(); - let curr_index = match self.dirlist.index { Some(i) => i, None => { @@ -38,12 +37,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> { }; let skip_dist = curr_index / area.height as usize * area.height as usize; - - let screen_index = if skip_dist > 0 { - curr_index % skip_dist - } else { - curr_index - }; + let screen_index = curr_index % area.height as usize; let area_width = area.width as usize; for (i, entry) in self @@ -63,57 +57,82 @@ impl<'a> Widget for TuiDirListDetailed<'a> { }; 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); - } 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); + match file_type { + FileType::Directory => { + if name_width <= area_width { + buf.set_string(x, 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, "…", style); + } } - // TODO: print out symlink path - // } else if file_type.is_symlink() { - } else { - 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; + FileType::Symlink(p) => { + 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); + } 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); + } + } + 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); + } 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 = + if ext_width > area_width - FILE_SIZE_WIDTH - 2 { + 0 + } else { + 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, + ); + + let file_ext_width = + area_width - file_name_width - FILE_SIZE_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 + file_name_width as u16 + 1, + y + i as u16, + &name[p_ind..], + file_ext_width, + 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/util/format.rs b/src/util/format.rs index e690513..19d670c 100644 --- a/src/util/format.rs +++ b/src/util/format.rs @@ -1,5 +1,8 @@ use std::time; +use tui::buffer::Buffer; +use tui::style::Style; + use super::unix; pub fn file_size_to_string(file_size: u64) -> String { @@ -32,3 +35,5 @@ pub fn mtime_to_string(mtime: time::SystemTime) -> String { let datetime: chrono::DateTime<chrono::offset::Utc> = mtime.into(); datetime.format(MTIME_FORMATTING).to_string() } + +pub fn write_str_to_buf(buf: &mut Buffer, s: &str, width: usize, x: u16, y: u16, style: Style) {} |