diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-12-13 12:42:53 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-12-13 12:42:53 -0500 |
commit | 2e5cdd07c2d8c18bad4099c82e36af64dc473a64 (patch) | |
tree | a9d7dee0f5a603cc96d6415a4549436a0f1818e0 /src/ui | |
parent | 468e607c5d1cd5d554ae356fd3c8e78425840347 (diff) |
code cleanup and fix path not showing when too long
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/widgets/tui_dirlist_detailed.rs | 8 | ||||
-rw-r--r-- | src/ui/widgets/tui_menu.rs | 26 | ||||
-rw-r--r-- | src/ui/widgets/tui_tab.rs | 4 | ||||
-rw-r--r-- | src/ui/widgets/tui_textfield.rs | 9 | ||||
-rw-r--r-- | src/ui/widgets/tui_topbar.rs | 43 | ||||
-rw-r--r-- | src/ui/widgets/tui_worker.rs | 62 |
6 files changed, 78 insertions, 74 deletions
diff --git a/src/ui/widgets/tui_dirlist_detailed.rs b/src/ui/widgets/tui_dirlist_detailed.rs index 70330c9..4bb63ce 100644 --- a/src/ui/widgets/tui_dirlist_detailed.rs +++ b/src/ui/widgets/tui_dirlist_detailed.rs @@ -9,6 +9,8 @@ use crate::util::format; const FILE_SIZE_WIDTH: usize = 8; +const ELLIPSIS: &str = "…"; + pub struct TuiDirListDetailed<'a> { dirlist: &'a JoshutoDirList, } @@ -63,7 +65,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> { 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); + buf.set_string(x + area_width as u16 - 1, y + i as u16, ELLIPSIS, style); } } FileType::Symlink(_) => { @@ -72,7 +74,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> { 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); + buf.set_string(x + area_width as u16 - 1, y + i as u16, ELLIPSIS, style); } } FileType::File => { @@ -108,7 +110,7 @@ impl<'a> Widget for TuiDirListDetailed<'a> { buf.set_string( x + file_name_width as u16, y + i as u16, - "…", + ELLIPSIS, style, ); diff --git a/src/ui/widgets/tui_menu.rs b/src/ui/widgets/tui_menu.rs index f7c9080..d8fc148 100644 --- a/src/ui/widgets/tui_menu.rs +++ b/src/ui/widgets/tui_menu.rs @@ -28,19 +28,19 @@ impl ToString for Key { match *self { Key::Char(c) => format!("{}", c), Key::Ctrl(c) => format!("ctrl+{}", c), - Key::Left => format!("arrow_left"), - Key::Right => format!("arrow_right"), - Key::Up => format!("arrow_up"), - Key::Down => format!("arrow_down"), - Key::Backspace => format!("backspace"), - Key::Home => format!("home"), - Key::End => format!("end"), - Key::PageUp => format!("page_up"), - Key::PageDown => format!("page_down"), - Key::BackTab => format!("backtab"), - Key::Insert => format!("insert"), - Key::Delete => format!("delete"), - Key::Esc => format!("escape"), + Key::Left => "arrow_left".to_string(), + Key::Right => "arrow_right".to_string(), + Key::Up => "arrow_up".to_string(), + Key::Down => "arrow_down".to_string(), + Key::Backspace => "backspace".to_string(), + Key::Home => "home".to_string(), + Key::End => "end".to_string(), + Key::PageUp => "page_up".to_string(), + Key::PageDown => "page_down".to_string(), + Key::BackTab => "backtab".to_string(), + Key::Insert => "insert".to_string(), + Key::Delete => "delete".to_string(), + Key::Esc => "escape".to_string(), Key::F(i) => format!("f{}", i), k => format!("{:?}", k), } diff --git a/src/ui/widgets/tui_tab.rs b/src/ui/widgets/tui_tab.rs index 73b4d1d..d3e27f6 100644 --- a/src/ui/widgets/tui_tab.rs +++ b/src/ui/widgets/tui_tab.rs @@ -29,10 +29,10 @@ impl<'a> Widget for TuiTabBar<'a> { } else { area.width as usize - str1.len() }; - if space_avail >= self.name.len() { + if space_avail >= self.name.width() { self.name } else { - "" + "…" } }; let text = Spans::from(vec![ diff --git a/src/ui/widgets/tui_textfield.rs b/src/ui/widgets/tui_textfield.rs index 07f38b8..c70c079 100644 --- a/src/ui/widgets/tui_textfield.rs +++ b/src/ui/widgets/tui_textfield.rs @@ -40,15 +40,6 @@ pub struct TuiTextField<'a> { } impl<'a> TuiTextField<'a> { - pub fn new() -> Self { - Self { - _prompt: "", - _prefix: "", - _suffix: "", - _menu_items: None, - } - } - pub fn menu_items<I>(&mut self, items: I) -> &mut Self where I: Iterator<Item = &'a str>, diff --git a/src/ui/widgets/tui_topbar.rs b/src/ui/widgets/tui_topbar.rs index eb9b3a0..be35c7f 100644 --- a/src/ui/widgets/tui_topbar.rs +++ b/src/ui/widgets/tui_topbar.rs @@ -4,7 +4,7 @@ use tui::buffer::Buffer; use tui::layout::Rect; use tui::style::{Color, Modifier, Style}; use tui::text::{Span, Spans}; -use tui::widgets::{Paragraph, Widget, Wrap}; +use tui::widgets::{Paragraph, Widget}; use crate::{HOSTNAME, USERNAME}; @@ -28,18 +28,37 @@ impl<'a> Widget for TuiTopBar<'a> { .fg(Color::LightBlue) .add_modifier(Modifier::BOLD); - let curr_path_str = self.path.to_string_lossy(); + let mut ellipses = None; + let mut curr_path_str = self.path.to_string_lossy(); - let text = Spans::from(vec![ - Span::styled(USERNAME.as_str(), username_style), - Span::styled("@", username_style), - Span::styled(HOSTNAME.as_str(), username_style), - Span::styled(" ", username_style), - Span::styled(curr_path_str, path_style), - ]); + if curr_path_str.len() > area.width as usize { + match self.path.file_name() { + Some(s) => { + curr_path_str = s.to_string_lossy(); + ellipses = Some(Span::styled("…", path_style)); + } + None => {} + } + } - Paragraph::new(text) - .wrap(Wrap { trim: true }) - .render(area, buf); + let text = match ellipses { + Some(s) => Spans::from(vec![ + Span::styled(USERNAME.as_str(), username_style), + Span::styled("@", username_style), + Span::styled(HOSTNAME.as_str(), username_style), + Span::styled(" ", username_style), + s, + Span::styled(curr_path_str, path_style), + ]), + None => Spans::from(vec![ + Span::styled(USERNAME.as_str(), username_style), + Span::styled("@", username_style), + Span::styled(HOSTNAME.as_str(), username_style), + Span::styled(" ", username_style), + Span::styled(curr_path_str, path_style), + ]), + }; + + Paragraph::new(text).render(area, buf); } } diff --git a/src/ui/widgets/tui_worker.rs b/src/ui/widgets/tui_worker.rs index 8707e5d..68abee0 100644 --- a/src/ui/widgets/tui_worker.rs +++ b/src/ui/widgets/tui_worker.rs @@ -1,7 +1,6 @@ use tui::buffer::Buffer; use tui::layout::Rect; use tui::style::{Color, Modifier, Style}; -use tui::text::Span; use tui::widgets::{Paragraph, Widget, Wrap}; use crate::context::JoshutoContext; @@ -34,53 +33,46 @@ impl<'a> Widget for TuiWorker<'a> { TuiTopBar::new(curr_tab.pwd()).render(rect, buf); match self.context.worker_ref() { - Some(io_obs) => match io_obs.progress.as_ref() { - Some(progress) => { - let msg = match progress.kind() { - FileOp::Copy => format!( - "Copying ({}/{}) {:?} -> {:?}", - progress.index() + 1, - progress.len(), - io_obs.src_path(), - io_obs.dest_path() - ), - FileOp::Cut => format!( - "Moving ({}/{}) {:?} -> {:?}", - progress.index() + 1, - progress.len(), - io_obs.src_path(), - io_obs.dest_path() - ), + Some(io_obs) => { + if let Some(progress) = io_obs.progress.as_ref() { + let op_str = match progress.kind() { + FileOp::Copy => "Copying", + FileOp::Cut => "Moving", }; + let msg = format!( + "{} ({}/{}) {:?} -> {:?}", + op_str, + progress.index() + 1, + progress.len(), + io_obs.src_path(), + io_obs.dest_path() + ); let style = Style::default(); buf.set_stringn(0, 2, msg, area.width as usize, style); + let style = Style::default() .fg(Color::Yellow) .add_modifier(Modifier::BOLD); buf.set_stringn(0, 4, "Queue:", area.width as usize, style); + let style = Style::default(); for (i, worker) in self.context.worker_iter().enumerate() { - let msg = match worker.kind() { - FileOp::Copy => format!( - "{:02} Copy {} items {:?} -> {:?}", - i + 1, - worker.paths.len(), - worker.paths[0].parent().unwrap(), - worker.dest - ), - FileOp::Cut => format!( - "{:02} Moving {} items {:?} -> {:?}", - i + 1, - worker.paths.len(), - worker.paths[0].parent().unwrap(), - worker.dest - ), + let op_str = match worker.kind() { + FileOp::Copy => "Copy", + FileOp::Cut => "Cut", }; + let msg = format!( + "{:02} {} {} items {:?} -> {:?}", + i + 1, + op_str, + worker.paths.len(), + worker.paths[0].parent().unwrap(), + worker.dest + ); buf.set_stringn(0, (4 + i + 2) as u16, msg, area.width as usize, style); } } - _ => {} - }, + } _ => { let style = Style::default(); buf.set_stringn(0, 2, "No operations running", area.width as usize, style); |