summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-04-17 21:29:10 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-04-17 21:33:47 -0400
commit280e4b741e86ae9637a169345dbd14cf344f69c6 (patch)
treeeb02ff1482d0af99a8b2b0da53132c9153bcbd9e
parent40e0305aff57be040fbc0b2914a34ae10bc2d83f (diff)
code cleanup
-rw-r--r--src/commands/cursor_move.rs74
-rw-r--r--src/commands/file_ops.rs15
-rw-r--r--src/commands/parent_cursor_move.rs11
-rw-r--r--src/commands/rename_file.rs12
-rw-r--r--src/commands/sub_process.rs10
-rw-r--r--src/context/worker_context.rs1
-rw-r--r--src/event/app_event.rs56
-rw-r--r--src/fs/dirlist.rs24
-rw-r--r--src/history.rs2
-rw-r--r--src/ui/views/tui_folder_view.rs6
-rw-r--r--src/ui/views/tui_textfield.rs6
-rw-r--r--src/util/style.rs13
12 files changed, 105 insertions, 125 deletions
diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs
index 60d4fd5..5757b47 100644
--- a/src/commands/cursor_move.rs
+++ b/src/commands/cursor_move.rs
@@ -55,12 +55,11 @@ pub fn cursor_move(context: &mut AppContext, new_index: usize) {
}
pub fn up(context: &mut AppContext, u: usize) -> JoshutoResult {
- let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- Some(curr_list) => curr_list
- .get_index()
- .map(|idx| if idx > u { idx - u } else { 0 }),
- None => None,
- };
+ let movement = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .curr_list_ref()
+ .and_then(|list| list.get_index().map(|idx| idx.saturating_sub(u)));
if let Some(s) = movement {
cursor_move(context, s);
@@ -69,10 +68,12 @@ pub fn up(context: &mut AppContext, u: usize) -> JoshutoResult {
}
pub fn down(context: &mut AppContext, u: usize) -> JoshutoResult {
- let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- Some(curr_list) => curr_list.get_index().map(|idx| idx + u),
- None => None,
- };
+ let movement = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .curr_list_ref()
+ .and_then(|list| list.get_index().map(|idx| idx.saturating_add(u)));
+
if let Some(s) = movement {
cursor_move(context, s);
}
@@ -80,17 +81,18 @@ pub fn down(context: &mut AppContext, u: usize) -> JoshutoResult {
}
pub fn home(context: &mut AppContext) -> JoshutoResult {
- let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- Some(curr_list) => {
+ let movement = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .curr_list_ref()
+ .and_then(|curr_list| {
let len = curr_list.len();
if len == 0 {
None
} else {
Some(0)
}
- }
- None => None,
- };
+ });
if let Some(s) = movement {
cursor_move(context, s);
@@ -99,17 +101,18 @@ pub fn home(context: &mut AppContext) -> JoshutoResult {
}
pub fn end(context: &mut AppContext) -> JoshutoResult {
- let movement: Option<usize> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- Some(curr_list) => {
+ let movement = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .curr_list_ref()
+ .and_then(|curr_list| {
let len = curr_list.len();
if len == 0 {
None
} else {
Some(len - 1)
}
- }
- None => None,
- };
+ });
if let Some(s) = movement {
cursor_move(context, s);
@@ -143,14 +146,11 @@ pub fn page_up(
let page_size = get_page_size(context, backend).unwrap_or(10) as f64 * proportion;
let page_size = page_size as usize;
- let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- Some(curr_list) => {
- curr_list
- .get_index()
- .map(|idx| if idx > page_size { idx - page_size } else { 0 })
- }
- None => None,
- };
+ let movement = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .curr_list_ref()
+ .and_then(|list| list.get_index().map(|idx| idx.saturating_sub(page_size)));
if let Some(s) = movement {
cursor_move(context, s);
@@ -166,19 +166,11 @@ pub fn page_down(
let page_size = get_page_size(context, backend).unwrap_or(10) as f64 * proportion;
let page_size = page_size as usize;
- let movement = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- Some(curr_list) => {
- let dir_len = curr_list.len();
- curr_list.get_index().map(|idx| {
- if idx + page_size > dir_len - 1 {
- dir_len - 1
- } else {
- idx + page_size
- }
- })
- }
- None => None,
- };
+ let movement = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .curr_list_ref()
+ .and_then(|list| list.get_index().map(|idx| idx.saturating_add(page_size)));
if let Some(s) = movement {
cursor_move(context, s);
diff --git a/src/commands/file_ops.rs b/src/commands/file_ops.rs
index 708b642..e0976cf 100644
--- a/src/commands/file_ops.rs
+++ b/src/commands/file_ops.rs
@@ -68,9 +68,12 @@ pub fn copy_filename_without_extension(context: &mut AppContext) -> JoshutoResul
.curr_tab_ref()
.curr_list_ref()
.and_then(|c| c.curr_entry_ref())
- .map(|entry| match entry.file_name().rsplit_once('.') {
- Some((name, _)) => name.to_string(),
- _ => entry.file_name().to_string(),
+ .map(|entry| {
+ entry
+ .file_name()
+ .rsplit_once('.')
+ .map(|(name, _)| name.to_string())
+ .unwrap_or_else(|| entry.file_name().to_string())
});
if let Some(file_name) = entry_file_name {
@@ -101,10 +104,8 @@ pub fn copy_dirpath(context: &mut AppContext) -> JoshutoResult {
.curr_list_ref()
.map(|dirlist| dirlist.file_path());
- if let Some(pathbuf) = opt_entry {
- if let Some(dir) = pathbuf.to_str().map(String::from) {
- copy_string_to_buffer(dir)?
- }
+ if let Some(s) = opt_entry.and_then(|p| p.to_str().map(String::from)) {
+ copy_string_to_buffer(s)?
};
Ok(())
}
diff --git a/src/commands/parent_cursor_move.rs b/src/commands/parent_cursor_move.rs
index febd391..b6c26fb 100644
--- a/src/commands/parent_cursor_move.rs
+++ b/src/commands/parent_cursor_move.rs
@@ -32,12 +32,11 @@ pub fn parent_cursor_move(context: &mut AppContext, new_index: usize) -> Joshuto
}
pub fn parent_up(context: &mut AppContext, u: usize) -> JoshutoResult {
- let movement = match context.tab_context_ref().curr_tab_ref().parent_list_ref() {
- Some(list) => list
- .get_index()
- .map(|idx| if idx > u { idx - u } else { 0 }),
- None => None,
- };
+ let movement = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .parent_list_ref()
+ .and_then(|list| list.get_index().map(|idx| idx.saturating_sub(u)));
if let Some(s) = movement {
parent_cursor_move(context, s)?;
diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs
index 981cfcf..fa4a23d 100644
--- a/src/commands/rename_file.rs
+++ b/src/commands/rename_file.rs
@@ -101,13 +101,11 @@ pub fn rename_file_prepend(
backend: &mut TuiBackend,
keymap_t: &AppKeyMapping,
) -> JoshutoResult {
- let mut file_name: Option<String> = None;
-
- if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
- file_name = curr_list
- .curr_entry_ref()
- .map(|s| s.file_name().to_string());
- }
+ let file_name = context
+ .tab_context_ref()
+ .curr_tab_ref()
+ .curr_list_ref()
+ .and_then(|list| list.curr_entry_ref().map(|s| s.file_name().to_string()));
if let Some(file_name) = file_name {
_rename_file_prepend(context, backend, keymap_t, file_name)?;
diff --git a/src/commands/sub_process.rs b/src/commands/sub_process.rs
index 08629ab..db699dd 100644
--- a/src/commands/sub_process.rs
+++ b/src/commands/sub_process.rs
@@ -16,10 +16,12 @@ fn execute_sub_process(
"%s" => {
if let Some(curr_list) = context.tab_context_ref().curr_tab_ref().curr_list_ref() {
let mut i = 0;
- for entry in curr_list.iter_selected().map(|e| e.file_name()) {
- command.arg(entry);
- i += 1;
- }
+ curr_list.iter_selected()
+ .map(|e| e.file_name())
+ .for_each(|file_name| {
+ command.arg(file_name);
+ i += 1;
+ });
if i == 0 {
if let Some(entry) = curr_list.curr_entry_ref() {
command.arg(entry.file_name());
diff --git a/src/context/worker_context.rs b/src/context/worker_context.rs
index d8aea55..fdd76fa 100644
--- a/src/context/worker_context.rs
+++ b/src/context/worker_context.rs
@@ -7,6 +7,7 @@ use crate::event::AppEvent;
use crate::io::{IoWorkerObserver, IoWorkerProgress, IoWorkerThread};
pub struct WorkerContext {
+ // forks of applications
child_pool: HashMap<u32, thread::JoinHandle<()>>,
// to send info
event_tx: mpsc::Sender<AppEvent>,
diff --git a/src/event/app_event.rs b/src/event/app_event.rs
index aa39f00..fd232f6 100644
--- a/src/event/app_event.rs
+++ b/src/event/app_event.rs
@@ -16,18 +16,24 @@ use crate::preview::preview_file::FilePreview;
#[derive(Debug)]
pub enum AppEvent {
+ // User input events
Termion(Event),
+ // background IO worker events
IoWorkerCreate,
IoWorkerProgress(IoWorkerProgress),
IoWorkerResult(io::Result<IoWorkerProgress>),
+ // forked process events
ChildProcessComplete(u32),
+ // preview thread events
PreviewDir(io::Result<JoshutoDirList>),
PreviewFile(path::PathBuf, io::Result<FilePreview>),
+ // terminal size change events
Signal(i32),
+ // filesystem change events
Filesystem(notify::Event),
}
@@ -44,13 +50,30 @@ pub struct Events {
impl Events {
pub fn new() -> Self {
- Events::with_config()
+ Self::default()
}
- pub fn with_config() -> Self {
+ // We need a next() and a flush() so we don't continuously consume
+ // input from the console. Sometimes, other applications need to
+ // read terminal inputs while joshuto is in the background
+ pub fn next(&self) -> Result<AppEvent, mpsc::RecvError> {
+ let event = self.event_rx.recv()?;
+ Ok(event)
+ }
+
+ pub fn flush(&self) {
+ let _ = self.input_tx.send(());
+ }
+}
+
+impl std::default::Default for Events {
+ fn default() -> Self {
let (input_tx, input_rx) = mpsc::sync_channel(1);
let (event_tx, event_rx) = mpsc::channel();
+ // edge case that starts off the input thread
+ let _ = input_tx.send(());
+
// signal thread
let event_tx2 = event_tx.clone();
let _ = thread::spawn(move || {
@@ -69,25 +92,10 @@ impl Events {
let _ = thread::spawn(move || {
let stdin = io::stdin();
let mut events = stdin.events();
- match events.next() {
- Some(event) => match event {
- Ok(event) => {
- if let Err(e) = event_tx2.send(AppEvent::Termion(event)) {
- eprintln!("Input thread send err: {:#?}", e);
- return;
- }
- }
- Err(_) => return,
- },
- None => return,
- }
while input_rx.recv().is_ok() {
if let Some(Ok(event)) = events.next() {
- if let Err(e) = event_tx2.send(AppEvent::Termion(event)) {
- eprintln!("Input thread send err: {:#?}", e);
- return;
- }
+ let _ = event_tx2.send(AppEvent::Termion(event));
}
}
});
@@ -98,16 +106,4 @@ impl Events {
input_tx,
}
}
-
- // We need a next() and a flush() so we don't continuously consume
- // input from the console. Sometimes, other applications need to
- // read terminal inputs while joshuto is in the background
- pub fn next(&self) -> Result<AppEvent, mpsc::RecvError> {
- let event = self.event_rx.recv()?;
- Ok(event)
- }
-
- pub fn flush(&self) {
- let _ = self.input_tx.send(());
- }
}
diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs
index 719ca71..33ee7c9 100644
--- a/src/fs/dirlist.rs
+++ b/src/fs/dirlist.rs
@@ -1,5 +1,5 @@
-use std::path;
use std::slice::{Iter, IterMut};
+use std::{io, path};
use crate::config::option::DisplayOption;
use crate::context::UiContext;
@@ -36,15 +36,14 @@ impl JoshutoDirList {
}
}
- pub fn from_path(path: path::PathBuf, options: &DisplayOption) -> std::io::Result<Self> {
+ pub fn from_path(path: path::PathBuf, options: &DisplayOption) -> io::Result<Self> {
let filter_func = options.filter_func();
- let mut contents = read_directory(path.as_path(), filter_func, options)?;
-
let sort_options = options.sort_options_ref();
+
+ let mut contents = read_directory(path.as_path(), filter_func, options)?;
contents.sort_by(|f1, f2| sort_options.compare(f1, f2));
let index = if contents.is_empty() { None } else { Some(0) };
-
let metadata = JoshutoMetadata::from(&path)?;
Ok(Self {
@@ -125,13 +124,10 @@ impl JoshutoDirList {
pub fn modified(&self) -> bool {
let metadata = std::fs::symlink_metadata(self.file_path());
- match metadata {
- Ok(m) => match m.modified() {
- Ok(s) => s > self.metadata.modified(),
- _ => false,
- },
- _ => false,
- }
+ metadata
+ .and_then(|m| m.modified())
+ .map(|m| m > self.metadata.modified())
+ .unwrap_or(false)
}
pub fn depreciate(&mut self) {
@@ -142,8 +138,8 @@ impl JoshutoDirList {
self._need_update || self.modified()
}
- pub fn file_path(&self) -> &path::PathBuf {
- &self.path
+ pub fn file_path(&self) -> &path::Path {
+ self.path.as_path()
}
pub fn any_selected(&self) -> bool {
diff --git a/src/history.rs b/src/history.rs
index 9e0c558..80afa37 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -134,7 +134,7 @@ pub fn create_dirlist_with_history(
contents.sort_by(|f1, f2| sort_options.compare(f1, f2));
let contents_len = contents.len();
- let index: Option<usize> = if contents_len == 0 {
+ let index = if contents_len == 0 {
None
} else {
match history.get(path) {
diff --git a/src/ui/views/tui_folder_view.rs b/src/ui/views/tui_folder_view.rs
index 1be9367..966a48e 100644
--- a/src/ui/views/tui_folder_view.rs
+++ b/src/ui/views/tui_folder_view.rs
@@ -172,11 +172,7 @@ impl<'a> Widget for TuiFolderView<'a> {
// render tabs
if self.context.tab_context_ref().len() > 1 {
- let topbar_width = if area.width > TAB_VIEW_WIDTH {
- area.width - TAB_VIEW_WIDTH
- } else {
- 0
- };
+ let topbar_width = area.width.saturating_sub(TAB_VIEW_WIDTH);
let rect = Rect {
x: topbar_width,
diff --git a/src/ui/views/tui_textfield.rs b/src/ui/views/tui_textfield.rs
index b2372d4..997e664 100644
--- a/src/ui/views/tui_textfield.rs
+++ b/src/ui/views/tui_textfield.rs
@@ -172,11 +172,7 @@ impl<'a> TuiTextField<'a> {
Key::Home => line_buffer.move_home(),
Key::End => line_buffer.move_end(),
Key::Up => {
- curr_history_index = if curr_history_index > 0 {
- curr_history_index - 1
- } else {
- 0
- };
+ curr_history_index = curr_history_index.saturating_sub(1);
line_buffer.move_home();
line_buffer.kill_line();
if let Some(s) = context
diff --git a/src/util/style.rs b/src/util/style.rs
index 4670792..4312425 100644
--- a/src/util/style.rs
+++ b/src/util/style.rs
@@ -48,14 +48,17 @@ fn file_style(entry: &JoshutoDirEntry) -> Style {
.bg(THEME_T.executable.bg)
.add_modifier(THEME_T.executable.modifier)
} else {
- match entry
+ entry
.file_path()
.extension()
.and_then(|s| s.to_str())
.and_then(|s| THEME_T.ext.get(s))
- {
- Some(t) => Style::default().fg(t.fg).bg(t.bg).add_modifier(t.modifier),
- None => regular_style,
- }
+ .map(|theme| {
+ Style::default()
+ .fg(theme.fg)
+ .bg(theme.bg)
+ .add_modifier(theme.modifier)
+ })
+ .unwrap_or(regular_style)
}
}