diff options
-rw-r--r-- | src/commands/key_command.rs | 16 | ||||
-rw-r--r-- | src/commands/selection.rs | 8 | ||||
-rw-r--r-- | src/commands/set_mode.rs | 4 | ||||
-rw-r--r-- | src/commands/show_workers.rs | 3 | ||||
-rw-r--r-- | src/context/local_state.rs | 2 | ||||
-rw-r--r-- | src/fs/entry.rs | 21 | ||||
-rw-r--r-- | src/history.rs | 4 | ||||
-rw-r--r-- | src/io/io_observer.rs | 38 | ||||
-rw-r--r-- | src/io/io_worker.rs | 12 | ||||
-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 | ||||
-rw-r--r-- | src/util/devicons.rs | 4 | ||||
-rw-r--r-- | src/util/event.rs | 46 |
17 files changed, 140 insertions, 170 deletions
diff --git a/src/commands/key_command.rs b/src/commands/key_command.rs index 1c32470..967bf2d 100644 --- a/src/commands/key_command.rs +++ b/src/commands/key_command.rs @@ -276,14 +276,12 @@ impl KeyCommand { )), }, }, - "tab_switch" => match arg { - arg => match arg.parse::<i32>() { - Ok(s) => Ok(Self::TabSwitch(s)), - Err(e) => Err(JoshutoError::new( - JoshutoErrorKind::IOInvalidData, - format!("{}: {}", command, e.to_string()), - )), - }, + "tab_switch" => match arg.parse::<i32>() { + Ok(s) => Ok(Self::TabSwitch(s)), + Err(e) => Err(JoshutoError::new( + JoshutoErrorKind::IOInvalidData, + format!("{}: {}", command, e.to_string()), + )), }, "toggle_hidden" => Ok(Self::ToggleHiddenFiles), inp => Err(JoshutoError::new( @@ -348,7 +346,7 @@ impl JoshutoRunnable for KeyCommand { Self::ToggleHiddenFiles => show_hidden::toggle_hidden(context), - Self::Sort(t) => sort::set_sort(context, t.clone()), + Self::Sort(t) => sort::set_sort(context, *t), Self::SortReverse => sort::toggle_reverse(context), Self::TabSwitch(i) => { diff --git a/src/commands/selection.rs b/src/commands/selection.rs index 7e9a636..b184474 100644 --- a/src/commands/selection.rs +++ b/src/commands/selection.rs @@ -15,11 +15,9 @@ pub fn select_files(context: &mut JoshutoContext, toggle: bool, all: bool) -> Jo s.set_selected(!s.is_selected()); cursor_move::down(context, 1)?; } - } else { - if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() { - for curr in &mut curr_list.contents { - curr.set_selected(!curr.is_selected()); - } + } else if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() { + for curr in &mut curr_list.contents { + curr.set_selected(!curr.is_selected()); } } } else if !all { diff --git a/src/commands/set_mode.rs b/src/commands/set_mode.rs index ebe5a09..978bb24 100644 --- a/src/commands/set_mode.rs +++ b/src/commands/set_mode.rs @@ -56,8 +56,8 @@ pub fn set_mode(context: &mut JoshutoContext, backend: &mut TuiBackend) -> Joshu }; if let Some(s) = user_input { - if s.starts_with(PREFIX) { - let s = &s[PREFIX.len()..]; + if let Some(stripped) = s.strip_prefix(PREFIX) { + let s = stripped; let mode = str_to_mode(s); let entry = context diff --git a/src/commands/show_workers.rs b/src/commands/show_workers.rs index 04e6b84..1ddb74c 100644 --- a/src/commands/show_workers.rs +++ b/src/commands/show_workers.rs @@ -1,12 +1,9 @@ -use crate::commands::KeyCommand; use crate::context::JoshutoContext; use crate::error::JoshutoResult; use crate::ui::views::TuiWorkerView; use crate::ui::TuiBackend; -use super::JoshutoRunnable; - pub fn show_workers(context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { let view = TuiWorkerView::new(); view.display(context, backend); diff --git a/src/context/local_state.rs b/src/context/local_state.rs index c48660f..71ca8f4 100644 --- a/src/context/local_state.rs +++ b/src/context/local_state.rs @@ -20,7 +20,7 @@ impl LocalStateContext { self.file_op = operation; } - pub fn set_paths<'a, I>(&mut self, vals: I) + pub fn set_paths<I>(&mut self, vals: I) where I: Iterator<Item = path::PathBuf>, { diff --git a/src/fs/entry.rs b/src/fs/entry.rs index 4e06c0c..e6e1ae6 100644 --- a/src/fs/entry.rs +++ b/src/fs/entry.rs @@ -1,6 +1,6 @@ use std::{fs, path}; -use tui::style::{Modifier, Style}; +use tui::style::Style; use crate::fs::{FileType, JoshutoMetadata}; use crate::util::devicons::*; @@ -98,25 +98,6 @@ impl JoshutoDirEntry { self.selected = selected; } - pub fn get_modifier(&self) -> Modifier { - let filetype = &self.metadata.file_type; - - match filetype { - FileType::Directory => THEME_T.directory.modifier, - FileType::Symlink(_) => THEME_T.link.modifier, - _ => match self.file_path().extension() { - None => Modifier::empty(), - Some(os_str) => match os_str.to_str() { - None => Modifier::empty(), - Some(s) => match THEME_T.ext.get(s) { - None => Modifier::empty(), - Some(t) => t.modifier, - }, - }, - }, - } - } - pub fn get_style(&self) -> Style { let metadata = &self.metadata; let filetype = &metadata.file_type; diff --git a/src/history.rs b/src/history.rs index fb425d4..b458f32 100644 --- a/src/history.rs +++ b/src/history.rs @@ -90,7 +90,7 @@ impl DirectoryHistory for JoshutoHistory { match self.entry(path.to_path_buf()) { Entry::Occupied(mut entry) => { let dirlist = entry.get_mut(); - if let Err(_) = dirlist.reload_contents(sort_option) { + if dirlist.reload_contents(sort_option).is_err() { entry.remove_entry(); } } @@ -105,7 +105,7 @@ impl DirectoryHistory for JoshutoHistory { fn reload(&mut self, path: &Path, sort_option: &sort::SortOption) -> std::io::Result<()> { if let Entry::Occupied(mut entry) = self.entry(path.to_path_buf()) { let dirlist = entry.get_mut(); - if let Err(_) = dirlist.reload_contents(sort_option) { + if dirlist.reload_contents(sort_option).is_err() { entry.remove_entry(); } } diff --git a/src/io/io_observer.rs b/src/io/io_observer.rs index 0648fbd..532b6f5 100644 --- a/src/io/io_observer.rs +++ b/src/io/io_observer.rs @@ -25,10 +25,7 @@ impl IOWorkerObserver { } pub fn join(self) -> bool { - match self.handle.join() { - Ok(_) => true, - _ => false, - } + matches!(self.handle.join(), Ok(_)) } pub fn set_progress(&mut self, progress: IOWorkerProgress) { self.progress = Some(progress); @@ -38,26 +35,19 @@ impl IOWorkerObserver { None => {} Some(progress) => { let size_str = format::file_size_to_string(progress.processed()); - match progress.kind() { - FileOp::Cut => { - let msg = format!( - "moving ({}/{}) {} completed", - progress.index() + 1, - progress.len(), - size_str - ); - self.msg = msg; - } - FileOp::Copy => { - let msg = format!( - "copying ({}/{}) {} completed", - progress.index() + 1, - progress.len(), - size_str - ); - self.msg = msg; - } - } + let op_str = match progress.kind() { + FileOp::Cut => "Moving", + FileOp::Copy => "Copying", + }; + + let msg = format!( + "{} ({}/{}) {} completed", + op_str, + progress.index() + 1, + progress.len(), + size_str + ); + self.msg = msg; } } } diff --git a/src/io/io_worker.rs b/src/io/io_worker.rs index f44a2e6..58347ee 100644 --- a/src/io/io_worker.rs +++ b/src/io/io_worker.rs @@ -160,7 +160,8 @@ impl IOWorkerThread { } Ok(()) } else if file_type.is_file() { - progress._processed += fs::copy(src, dest_buf)?; + let processed = progress.processed() + fs::copy(src, dest_buf)?; + progress.set_processed(processed); Ok(()) } else if file_type.is_symlink() { let link_path = fs::read_link(src)?; @@ -208,7 +209,8 @@ impl IOWorkerThread { if file_type.is_dir() { match fs::rename(src, dest_buf.as_path()) { Ok(_) => { - progress._processed += metadata.len(); + let processed = progress.processed() + metadata.len(); + progress.set_processed(processed); } Err(_) => { fs::create_dir(dest_buf.as_path())?; @@ -228,13 +230,15 @@ impl IOWorkerThread { if fs::rename(src, dest_buf.as_path()).is_err() { fs::copy(src, dest_buf.as_path())?; fs::remove_file(src)?; - progress._processed += metadata.len(); + let processed = progress.processed() + metadata.len(); + progress.set_processed(processed); } } else if file_type.is_symlink() { let link_path = fs::read_link(src)?; std::os::unix::fs::symlink(link_path, dest_buf)?; fs::remove_file(src)?; - progress._processed += metadata.len(); + let processed = progress.processed() + metadata.len(); + progress.set_processed(processed); } Ok(()) } 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); diff --git a/src/util/devicons.rs b/src/util/devicons.rs index ec0a5f2..823de44 100644 --- a/src/util/devicons.rs +++ b/src/util/devicons.rs @@ -1,7 +1,7 @@ use phf::phf_map; -pub const DEFAULT_DIR: &'static str = ""; -pub const DEFAULT_FILE: &'static str = ""; +pub const DEFAULT_DIR: &str = ""; +pub const DEFAULT_FILE: &str = ""; // all those glyphs will show as weird squares if you don't have the correct patched font // My advice is to use NerdFonts which can be found here: diff --git a/src/util/event.rs b/src/util/event.rs index f019b80..3d89503 100644 --- a/src/util/event.rs +++ b/src/util/event.rs @@ -41,36 +41,34 @@ impl Events { let (input_tx, input_rx) = mpsc::sync_channel(1); let (event_tx, event_rx) = mpsc::channel(); - { - let event_tx = event_tx.clone(); - thread::spawn(move || { - let stdin = io::stdin(); - let mut keys = stdin.keys(); - match keys.next() { - Some(key) => match key { - Ok(key) => { - if let Err(e) = event_tx.send(Event::Input(key)) { - eprintln!("Input thread send err: {:#?}", e); - return; - } + let event_tx2 = event_tx.clone(); + thread::spawn(move || { + let stdin = io::stdin(); + let mut keys = stdin.keys(); + match keys.next() { + Some(key) => match key { + Ok(key) => { + if let Err(e) = event_tx2.send(Event::Input(key)) { + eprintln!("Input thread send err: {:#?}", e); + return; } - _ => return, - }, + } _ => return, - } + }, + _ => return, + } - while let Ok(_) = input_rx.recv() { - if let Some(key) = keys.next() { - if let Ok(key) = key { - if let Err(e) = event_tx.send(Event::Input(key)) { - eprintln!("Input thread send err: {:#?}", e); - return; - } + while input_rx.recv().is_ok() { + if let Some(key) = keys.next() { + if let Ok(key) = key { + if let Err(e) = event_tx2.send(Event::Input(key)) { + eprintln!("Input thread send err: {:#?}", e); + return; } } } - }) - }; + } + }); Events { event_tx, |