summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2024-03-10 22:58:45 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2024-03-10 22:58:45 -0400
commit93d2a18674e6c9c929aeb08c489488fd7b185f04 (patch)
tree6029f6a8b51612a814609b56c1f97ec6ada0e099
parent9a185e3c2437e41ee08ef3d6f2e2616981e046e8 (diff)
refactor code to reduce clones
- move a lot of methods into functions - printing icons is moved to rendering section
-rw-r--r--config/joshuto.toml2
-rw-r--r--src/commands/change_directory.rs26
-rw-r--r--src/commands/delete_files.rs14
-rw-r--r--src/commands/new_directory.rs12
-rw-r--r--src/commands/reload.rs122
-rw-r--r--src/commands/rename_file.rs15
-rw-r--r--src/commands/tab_ops.rs119
-rw-r--r--src/commands/touch_file.rs16
-rw-r--r--src/context/app_context.rs20
-rw-r--r--src/context/tab_context.rs5
-rw-r--r--src/event/process_event.rs33
-rw-r--r--src/fs/dirlist.rs4
-rw-r--r--src/fs/entry.rs63
-rw-r--r--src/history.rs130
-rw-r--r--src/preview/preview_dir.rs3
-rw-r--r--src/run.rs18
-rw-r--r--src/tab/tab_struct.rs20
-rw-r--r--src/ui/views/tui_folder_view.rs6
-rw-r--r--src/ui/views/tui_hsplit_view.rs20
-rw-r--r--src/ui/widgets/tui_dirlist.rs84
-rw-r--r--src/ui/widgets/tui_dirlist_detailed.rs33
21 files changed, 399 insertions, 366 deletions
diff --git a/config/joshuto.toml b/config/joshuto.toml
index f5fcaf5..7e37573 100644
--- a/config/joshuto.toml
+++ b/config/joshuto.toml
@@ -21,7 +21,7 @@ column_ratio = [1, 4, 4]
scroll_offset = 6
show_borders = true
show_hidden = false
-show_icons = true
+show_icons = false
# none, absolute, relative
line_number_style = "none"
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs
index 220ca59..6ac2575 100644
--- a/src/commands/change_directory.rs
+++ b/src/commands/change_directory.rs
@@ -3,7 +3,7 @@ use std::path;
use crate::commands::reload;
use crate::context::AppContext;
use crate::error::AppResult;
-use crate::history::DirectoryHistory;
+use crate::history::{generate_entries_to_root, DirectoryHistory};
use crate::util::cwd;
// ChangeDirectory command
@@ -28,25 +28,19 @@ pub fn change_directory(context: &mut AppContext, mut path: &path::Path) -> AppR
};
cd(new_cwd.as_path(), context)?;
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- let ui_context = context.ui_context_ref().clone();
- let tab_options = context
- .tab_context_ref()
- .curr_tab_ref()
- .option_ref()
- .clone();
+ let dirlists = generate_entries_to_root(
+ new_cwd.as_path(),
+ context.config_ref(),
+ context.tab_context_ref().curr_tab_ref().history_ref(),
+ context.ui_context_ref(),
+ context.config_ref().display_options_ref(),
+ context.tab_context_ref().curr_tab_ref().option_ref(),
+ )?;
context
.tab_context_mut()
.curr_tab_mut()
.history_mut()
- .populate_to_root(
- new_cwd.as_path(),
- &config,
- &ui_context,
- &options,
- &tab_options,
- )?;
+ .insert_entries(dirlists);
Ok(())
}
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs
index 755edb8..7076e80 100644
--- a/src/commands/delete_files.rs
+++ b/src/commands/delete_files.rs
@@ -5,11 +5,12 @@ use termion::event::Key;
use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};
-use crate::history::DirectoryHistory;
use crate::io::{FileOperation, FileOperationOptions, IoWorkerThread};
use crate::ui::widgets::TuiPrompt;
use crate::ui::AppBackend;
+use super::tab_ops;
+
fn prompt(context: &mut AppContext, backend: &mut AppBackend, paths_len: usize) -> bool {
let ch = {
let prompt_str = format!("Delete {} files? (Y/n)", paths_len);
@@ -92,14 +93,7 @@ pub fn delete_selected_files(
delete_files(context, paths, background, permanently)?;
}
- let curr_tab = context.tab_context_ref().curr_tab_ref();
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- let curr_path = curr_tab.cwd().to_path_buf();
- for (_, tab) in context.tab_context_mut().iter_mut() {
- let tab_options = tab.option_ref().clone();
- tab.history_mut()
- .reload(&curr_path, &config, &options, &tab_options)?;
- }
+ let curr_path = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();
+ tab_ops::reload_all_tabs(context, curr_path.as_path())?;
Ok(())
}
diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs
index d4bd68d..1d6e2e8 100644
--- a/src/commands/new_directory.rs
+++ b/src/commands/new_directory.rs
@@ -3,18 +3,14 @@ use std::path;
use crate::commands::cursor_move;
use crate::context::AppContext;
use crate::error::AppResult;
-use crate::history::DirectoryHistory;
+
+use super::tab_ops;
pub fn new_directory(context: &mut AppContext, p: &path::Path) -> AppResult {
std::fs::create_dir_all(p)?;
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
+
let curr_path = context.tab_context_ref().curr_tab_ref().cwd().to_path_buf();
- for (_, tab) in context.tab_context_mut().iter_mut() {
- let tab_options = tab.option_ref().clone();
- tab.history_mut()
- .reload(&curr_path, &config, &options, &tab_options)?;
- }
+ tab_ops::reload_all_tabs(context, curr_path.as_path())?;
if context.config_ref().focus_on_create {
cursor_move::to_path(context, p)?;
diff --git a/src/commands/reload.rs b/src/commands/reload.rs
index bee7754..4134272 100644
--- a/src/commands/reload.rs
+++ b/src/commands/reload.rs
@@ -1,55 +1,44 @@
use crate::context::AppContext;
use crate::error::AppResult;
-use crate::history::create_dirlist_with_history;
+use crate::history::{create_dirlist_with_history, DirectoryHistory};
use uuid::Uuid;
// reload only if we have a queued reload
pub fn soft_reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> {
- let mut paths = Vec::with_capacity(3);
+ let mut dirlists = Vec::with_capacity(3);
if let Some(curr_tab) = context.tab_context_ref().tab_ref(id) {
- if let Some(curr_list) = curr_tab.curr_list_ref() {
- if curr_list.need_update() {
- paths.push(curr_list.file_path().to_path_buf());
- }
- }
- if let Some(curr_list) = curr_tab.parent_list_ref() {
- if curr_list.need_update() {
- paths.push(curr_list.file_path().to_path_buf());
- }
- }
- if let Some(curr_list) = curr_tab.child_list_ref() {
- if curr_list.need_update() {
- paths.push(curr_list.file_path().to_path_buf());
- }
- }
- }
-
- if !paths.is_empty() {
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- let tab_options = context
- .tab_context_ref()
- .curr_tab_ref()
- .option_ref()
- .clone();
- if let Some(history) = context
- .tab_context_mut()
- .tab_mut(id)
- .map(|t| t.history_mut())
+ let config = context.config_ref();
+ let display_options = context.config_ref().display_options_ref();
+ let tab_options = context.tab_context_ref().curr_tab_ref().option_ref();
+ let history = curr_tab.history_ref();
+ for curr_list in [
+ curr_tab.parent_list_ref(),
+ curr_tab.curr_list_ref(),
+ curr_tab.child_list_ref(),
+ ]
+ .into_iter()
+ .flatten()
{
- for path in paths {
+ if curr_list.need_update() {
let new_dirlist = create_dirlist_with_history(
history,
- path.as_path(),
- &config,
- &options,
- &tab_options,
+ curr_list.file_path(),
+ display_options,
+ tab_options,
)?;
- history.insert(path, new_dirlist);
+ dirlists.push(new_dirlist);
}
}
}
+
+ if let Some(history) = context
+ .tab_context_mut()
+ .tab_mut(id)
+ .map(|t| t.history_mut())
+ {
+ history.insert_entries(dirlists);
+ }
Ok(())
}
@@ -59,43 +48,36 @@ pub fn soft_reload_curr_tab(context: &mut AppContext) -> std::io::Result<()> {
}
pub fn reload(context: &mut AppContext, id: &Uuid) -> std::io::Result<()> {
- let mut paths = Vec::with_capacity(3);
+ let mut dirlists = Vec::with_capacity(3);
if let Some(curr_tab) = context.tab_context_ref().tab_ref(id) {
- if let Some(curr_list) = curr_tab.curr_list_ref() {
- paths.push(curr_list.file_path().to_path_buf());
- }
- if let Some(curr_list) = curr_tab.parent_list_ref() {
- paths.push(curr_list.file_path().to_path_buf());
- }
- if let Some(curr_list) = curr_tab.child_list_ref() {
- paths.push(curr_list.file_path().to_path_buf());
+ let config = context.config_ref();
+ let display_options = context.config_ref().display_options_ref();
+ let tab_options = context.tab_context_ref().curr_tab_ref().option_ref();
+ let history = curr_tab.history_ref();
+ for curr_list in [
+ curr_tab.parent_list_ref(),
+ curr_tab.curr_list_ref(),
+ curr_tab.child_list_ref(),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ let new_dirlist = create_dirlist_with_history(
+ history,
+ curr_list.file_path(),
+ display_options,
+ tab_options,
+ )?;
+ dirlists.push(new_dirlist);
}
}
- if !paths.is_empty() {
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- let tab_options = context
- .tab_context_ref()
- .curr_tab_ref()
- .option_ref()
- .clone();
- if let Some(history) = context
- .tab_context_mut()
- .tab_mut(id)
- .map(|t| t.history_mut())
- {
- for path in paths {
- let new_dirlist = create_dirlist_with_history(
- history,
- path.as_path(),
- &config,
- &options,
- &tab_options,
- )?;
- history.insert(path, new_dirlist);
- }
- }
+ if let Some(history) = context
+ .tab_context_mut()
+ .tab_mut(id)
+ .map(|t| t.history_mut())
+ {
+ history.insert_entries(dirlists);
}
context
.message_queue_mut()
diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs
index 45ad9fe..3cecc38 100644
--- a/src/commands/rename_file.rs
+++ b/src/commands/rename_file.rs
@@ -27,16 +27,13 @@ pub fn _rename_file(
.map(|lst| lst.file_path().to_path_buf());
if let Some(path) = path {
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- let tab_options = context
- .tab_context_ref()
- .curr_tab_ref()
- .option_ref()
- .clone();
+ let new_dirlist = {
+ let display_options = context.config_ref().display_options_ref();
+ let tab_options = context.tab_context_ref().curr_tab_ref().option_ref();
+ let history = context.tab_context_ref().curr_tab_ref().history_ref();
+ create_dirlist_with_history(history, path.as_path(), display_options, tab_options)?
+ };
let history = context.tab_context_mut().curr_tab_mut().history_mut();
- let new_dirlist =
- create_dirlist_with_history(history, path.as_path(), &config, &options, &tab_options)?;
history.insert(path, new_dirlist);
}
Ok(())
diff --git a/src/commands/tab_ops.rs b/src/commands/tab_ops.rs
index cbba359..f1adfe2 100644
--- a/src/commands/tab_ops.rs
+++ b/src/commands/tab_ops.rs
@@ -1,11 +1,15 @@
-use std::path;
+use std::collections::HashMap;
+use std::path::Path;
+use std::{io, path};
use uuid::Uuid;
use crate::config::clean::app::display::new_tab::NewTabMode;
use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};
-use crate::history::DirectoryHistory;
+use crate::history::{
+ create_dirlist_with_history, generate_entries_to_root, DirectoryHistory, JoshutoHistory,
+};
use crate::tab::{JoshutoTab, TabHomePage};
use crate::util::{cwd, unix};
@@ -35,39 +39,46 @@ fn _tab_switch(new_index: usize, context: &mut AppContext) -> std::io::Result<()
None => None,
};
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- let tab_options = context
- .tab_context_ref()
- .curr_tab_ref()
- .option_ref()
- .clone();
+ let display_options = context.config_ref().display_options_ref();
+ let tab_options = context.tab_context_ref().curr_tab_ref().option_ref();
- let history = context.tab_context_mut().curr_tab_mut().history_mut();
- if history
- .create_or_soft_update(cwd.as_path(), &config, &options, &tab_options)
- .is_err()
- {
- history.remove(cwd.as_path());
- }
+ let history = context.tab_context_ref().curr_tab_ref().history_ref();
- if let Some(cwd_parent) = cwd.parent() {
- if history
- .create_or_soft_update(cwd_parent, &config, &options, &tab_options)
- .is_err()
- {
- history.remove(cwd_parent);
+ let mut dirlists = Vec::with_capacity(3);
+ for curr_path in [
+ Some(cwd.as_path().to_path_buf()),
+ cwd.parent().map(|p| p.to_path_buf()),
+ entry_path,
+ ]
+ .into_iter()
+ .flatten()
+ {
+ match history.get(&curr_path) {
+ Some(list) => {
+ if list.need_update() {
+ let dirlist = create_dirlist_with_history(
+ history,
+ cwd.as_path(),
+ display_options,
+ tab_options,
+ )?;
+ dirlists.push(dirlist);
+ }
+ }
+ None => {
+ let dirlist = create_dirlist_with_history(
+ history,
+ cwd.as_path(),
+ display_options,
+ tab_options,
+ )?;
+ dirlists.push(dirlist);
+ }
}
}
- if let Some(file_path) = entry_path {
- if history
- .create_or_soft_update(file_path.as_path(), &config, &options, &tab_options)
- .is_err()
- {
- history.remove(file_path.as_path());
- }
- }
+ let history = context.tab_context_mut().curr_tab_mut().history_mut();
+ history.insert_entries(dirlists);
Ok(())
}
@@ -140,12 +151,28 @@ pub fn new_tab(context: &mut AppContext, mode: &NewTabMode) -> AppResult {
}?;
if new_tab_path.exists() && new_tab_path.is_dir() {
let id = Uuid::new_v4();
- let tab = JoshutoTab::new(
- new_tab_path,
+ let mut new_tab_history = JoshutoHistory::new();
+ let tab_display_options = context
+ .config_ref()
+ .display_options_ref()
+ .default_tab_display_option
+ .clone();
+ let dirlists = generate_entries_to_root(
+ new_tab_path.as_path(),
context.config_ref(),
+ &new_tab_history,
context.ui_context_ref(),
context.config_ref().display_options_ref(),
+ &tab_display_options,
)?;
+ new_tab_history.insert_entries(dirlists);
+
+ let tab_display_options = context
+ .config_ref()
+ .display_options_ref()
+ .default_tab_display_option
+ .clone();
+ let tab = JoshutoTab::new(new_tab_path, new_tab_history, tab_display_options)?;
context.tab_context_mut().insert_tab(id, tab);
let new_index = context.tab_context_ref().len() - 1;
context.tab_context_mut().index = new_index;
@@ -179,3 +206,31 @@ pub fn close_tab(context: &mut AppContext) -> AppResult {
_tab_switch(tab_index, context)?;
Ok(())
}
+
+pub fn reload_all_tabs(context: &mut AppContext, curr_path: &Path) -> io::Result<()> {
+ let mut map = HashMap::new();
+ {
+ let display_options = context.config_ref().display_options_ref();
+
+ for (id, tab) in context.tab_context_ref().iter() {
+ let tab_options = tab.option_ref();
+ let history = tab.history_ref();
+ let dirlist =
+ create_dirlist_with_history(history, curr_path, display_options, tab_options)?;
+ map.insert(*id, dirlist);
+ }
+ }
+
+ for (id, dirlist) in map {
+ if let Some(tab) = context.tab_context_mut().tab_mut(&id) {
+ tab.history_mut().insert(curr_path.to_path_buf(), dirlist);
+ }
+ }
+ Ok(())
+}
+
+pub fn remove_entry_from_all_tabs(context: &mut AppContext, curr_path: &Path) {
+ for (_, tab) in context.tab_context_mut().iter_mut() {
+ tab.history_mut().remove(curr_path);
+ }
+}
diff --git a/src/commands/touch_file.rs b/src/commands/touch_file.rs
index 41ba1c3..5fbf039 100644
--- a/src/commands/touch_file.rs
+++ b/src/commands/touch_file.rs
@@ -48,16 +48,14 @@ pub fn touch_file(context: &mut AppContext, arg: &str) -> AppResult {
.map(|lst| lst.file_path().to_path_buf());
if let Some(path) = path {
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- let tab_options = context
- .tab_context_ref()
- .curr_tab_ref()
- .option_ref()
- .clone();
+ let new_dirlist = {
+ let options = context.config_ref().display_options_ref();
+ let tab_options = context.tab_context_ref().curr_tab_ref().option_ref();
+ let history = context.tab_context_ref().curr_tab_ref().history_ref();
+
+ create_dirlist_with_history(history, path.as_path(), options, tab_options)?
+ };
let history = context.tab_context_mut().curr_tab_mut().history_mut();
- let new_dirlist =
- create_dirlist_with_history(history, path.as_path(), &config, &options, &tab_options)?;
history.insert(path, new_dirlist);
}
diff --git a/src/context/app_context.rs b/src/context/app_context.rs
index 4b7f8b7..db8e17a 100644
--- a/src/context/app_context.rs
+++ b/src/context/app_context.rs
@@ -23,27 +23,27 @@ pub struct AppContext {
// args from the command line
pub args: Args,
// app config
- config: AppConfig,
+ pub config: AppConfig,
// context related to tabs
- tab_context: TabContext,
+ pub tab_context: TabContext,
// context related to local file state
- local_state: Option<LocalStateContext>,
+ pub local_state: Option<LocalStateContext>,
// context related to searching
- search_context: Option<MatchContext>,
+ pub search_context: Option<MatchContext>,
// message queue for displaying messages
- message_queue: MessageQueue,
+ pub message_queue: MessageQueue,
// context related to io workers
- worker_context: WorkerContext,
+ pub worker_context: WorkerContext,
// context related to previews
pub preview_context: PreviewContext,
// context related to command line
- commandline_context: CommandLineContext,
+ pub commandline_context: CommandLineContext,
// user interface context; data which is input to both, the UI rendering and the app state
- ui_context: UiContext,
+ pub ui_context: UiContext,
// filesystem watcher to inform about changes in shown directories
- watcher: notify::RecommendedWatcher,
+ pub watcher: notify::RecommendedWatcher,
// list of watched paths; seems not to be possible to get them from a notify::Watcher
- watched_paths: HashSet<path::PathBuf>,
+ pub watched_paths: HashSet<path::PathBuf>,
// the stdout of the last `shell` command
pub last_stdout: Option<String>,
}
diff --git a/src/context/tab_context.rs b/src/context/tab_context.rs
index 1df7295..35ee9ed 100644
--- a/src/context/tab_context.rs
+++ b/src/context/tab_context.rs
@@ -1,4 +1,4 @@
-use std::collections::hash_map::IterMut;
+use std::collections::hash_map::{Iter, IterMut};
use std::collections::HashMap;
use uuid::Uuid;
@@ -66,6 +66,9 @@ impl TabContext {
tab
}
+ pub fn iter(&self) -> Iter<Uuid, JoshutoTab> {
+ self.tabs.iter()
+ }
pub fn iter_mut(&mut self) -> IterMut<Uuid, JoshutoTab> {
self.tabs.iter_mut()
}
diff --git a/src/event/process_event.rs b/src/event/process_event.rs
index 91b8930..b8e1cff 100644
--- a/src/event/process_event.rs
+++ b/src/event/process_event.rs
@@ -7,6 +7,7 @@ use signal_hook::consts::signal;
use termion::event::{Event, Key, MouseButton, MouseEvent};
use uuid::Uuid;
+use crate::commands::tab_ops;
use crate::commands::{cursor_move, parent_cursor_move, reload};
use crate::config::clean::keymap::AppKeyMapping;
use crate::config::clean::keymap::KeyMapping;
@@ -15,7 +16,6 @@ use crate::error::AppResult;
use crate::event::AppEvent;
use crate::event::PreviewData;
use crate::fs::JoshutoDirList;
-use crate::history::DirectoryHistory;
use crate::io::FileOperationProgress;
use crate::key_command::{AppExecute, Command, CommandKeybind};
use crate::preview::preview_dir::PreviewDirState;
@@ -94,24 +94,19 @@ pub fn process_worker_progress(context: &mut AppContext, res: FileOperationProgr
pub fn process_finished_worker(context: &mut AppContext, res: AppResult<FileOperationProgress>) {
let worker_context = context.worker_context_mut();
let observer = worker_context.remove_worker().unwrap();
- let config = context.config_ref().clone();
- let options = context.config_ref().display_options_ref().clone();
- for (_, tab) in context.tab_context_mut().iter_mut() {
- let tab_options = tab.option_ref().clone();
- if observer.dest_path().exists() {
- let _ = tab
- .history_mut()
- .reload(observer.dest_path(), &config, &options, &tab_options);
- } else {
- tab.history_mut().remove(observer.dest_path());
- }
- if observer.src_path().exists() {
- let _ = tab
- .history_mut()
- .reload(observer.src_path(), &config, &options, &tab_options);
- } else {
- tab.history_mut().remove(observer.src_path());
- }
+
+ let observer_path = observer.dest_path();
+ if observer_path.exists() {
+ let _ = tab_ops::reload_all_tabs(context, observer_path);
+ } else {
+ tab_ops::remove_entry_from_all_tabs(context, observer_path);
+ }
+
+ let observer_path = observer.src_path();
+ if observer_path.exists() {
+ let _ = tab_ops::reload_all_tabs(context, observer_path);
+ } else {
+ tab_ops::remove_entry_from_all_tabs(context, observer_path);
}
observer.join();
diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs
index 97affac..de13440 100644
--- a/src/fs/dirlist.rs
+++ b/src/fs/dirlist.rs
@@ -44,13 +44,11 @@ impl JoshutoDirList {
pub fn from_path(
path: path::PathBuf,
- config: &AppConfig,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<Self> {
let filter_func = options.filter_func();
- let mut contents =
- read_directory(path.as_path(), filter_func, config, options, tab_options)?;
+ let mut contents = read_directory(path.as_path(), filter_func, options, tab_options)?;
contents.sort_by(|f1, f2| tab_options.sort_options_ref().compare(f1, f2));
diff --git a/src/fs/entry.rs b/src/fs/entry.rs
index d0940c3..9a4b557 100644
--- a/src/fs/entry.rs
+++ b/src/fs/entry.rs
@@ -10,10 +10,9 @@ use crate::ICONS_T;
#[derive(Clone, Debug)]
pub struct JoshutoDirEntry {
- name: String,
- ext: Option<String>,
- label: String,
- path: path::PathBuf,
+ pub name: String,
+ pub ext: Option<String>,
+ pub path: path::PathBuf,
pub metadata: JoshutoMetadata,
/// Directly selected by the user, _not_ by a current visual mode selection
permanent_selected: bool,
@@ -26,7 +25,6 @@ impl JoshutoDirEntry {
pub fn from(
direntry: &walkdir::DirEntry,
base: &path::Path,
- config: &AppConfig,
options: &DisplayOption,
) -> io::Result<Self> {
let path = direntry.path().to_path_buf();
@@ -42,13 +40,7 @@ impl JoshutoDirEntry {
.path()
.extension()
.and_then(|s| s.to_str())
- .map(|s| {
- if config.case_sensitive_ext {
- s.to_string()
- } else {
- s.to_lowercase()
- }
- });
+ .map(|s| s.to_string());
let mut metadata = JoshutoMetadata::from(&path)?;
@@ -58,20 +50,9 @@ impl JoshutoDirEntry {
}
}
- #[cfg(feature = "devicons")]
- let label = if options.show_icons() {
- create_icon_label(name.as_str(), &ext, config, &metadata)
- } else {
- name.clone()
- };
-
- #[cfg(not(feature = "devicons"))]
- let label = name.clone();
-
Ok(Self {
name,
ext,
- label,
path,
metadata,
permanent_selected: false,
@@ -88,10 +69,6 @@ impl JoshutoDirEntry {
self.ext.as_deref()
}
- pub fn label(&self) -> &str {
- self.label.as_str()
- }
-
pub fn file_path(&self) -> &path::Path {
self.path.as_path()
}
@@ -152,38 +129,6 @@ impl std::cmp::Ord for JoshutoDirEntry {
}
}
-#[cfg(feature = "devicons")]
-fn create_icon_label(
- name: &str,
- ext: &Option<String>,
- config: &AppConfig,
- metadata: &JoshutoMetadata,
-) -> String {
- let label = {
- let icon = match metadata.file_type() {
- FileType::Directory => ICONS_T
- .directory_exact
- .get(name)
- .cloned()
- .unwrap_or(ICONS_T.default_dir.clone()),
- _ => ICONS_T.file_exact.get(name).cloned().unwrap_or(match ext {
- Some(ext) => {
- let icon = if config.case_sensitive_ext {
- ICONS_T.ext.get(ext)
- } else {
- ICONS_T.ext.get(&ext.to_lowercase())
- };
-
- icon.unwrap_or(&ICONS_T.default_file).to_string()
- }
- None => ICONS_T.default_file.clone(),
- }),
- };
- format!("{} {}", icon, name)
- };
- label
-}
-
fn get_directory_size(path: &path::Path) -> io::Result<usize> {
fs::read_dir(path).map(|s| s.count())
}
diff --git a/src/history.rs b/src/history.rs
index 0035d88..5302828 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -12,32 +12,16 @@ use crate::context::UiContext;
use crate::fs::{JoshutoDirEntry, JoshutoDirList, JoshutoMetadata};
pub trait DirectoryHistory {
- fn populate_to_root(
- &mut self,
- path: &Path,
- config: &AppConfig,
- ui_context: &UiContext,
- options: &DisplayOption,
- tab_options: &TabDisplayOption,
- ) -> io::Result<()>;
+ fn insert_entries(&mut self, entries: Vec<JoshutoDirList>);
fn create_or_soft_update(
&mut self,
path: &Path,
- config: &AppConfig,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()>;
fn create_or_reload(
&mut self,
path: &Path,
- config: &AppConfig,
- options: &DisplayOption,
- tab_options: &TabDisplayOption,
- ) -> io::Result<()>;
- fn reload(
- &mut self,
- path: &Path,
- config: &AppConfig,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()>;
@@ -49,53 +33,15 @@ pub trait DirectoryHistory {
pub type JoshutoHistory = HashMap<PathBuf, JoshutoDirList>;
impl DirectoryHistory for JoshutoHistory {
- fn populate_to_root(
- &mut self,
- path: &Path,
- config: &AppConfig,
- ui_context: &UiContext,
- options: &DisplayOption,
- tab_options: &TabDisplayOption,
- ) -> io::Result<()> {
- let mut dirlists = Vec::new();
-
- let mut prev: Option<&Path> = None;
- for curr in path.ancestors() {
- if self.contains_key(curr) {
- let mut new_dirlist =
- create_dirlist_with_history(self, curr, config, options, tab_options)?;
- if let Some(ancestor) = prev.as_ref() {
- if let Some(i) = get_index_of_value(&new_dirlist.contents, ancestor) {
- new_dirlist.set_index(Some(i), ui_context, options);
- }
- }
- dirlists.push(new_dirlist);
- } else {
- let mut new_dirlist = JoshutoDirList::from_path(
- curr.to_path_buf().clone(),
- config,
- options,
- tab_options,
- )?;
- if let Some(ancestor) = prev.as_ref() {
- if let Some(i) = get_index_of_value(&new_dirlist.contents, ancestor) {
- new_dirlist.set_index(Some(i), ui_context, options);
- }
- }
- dirlists.push(new_dirlist);
- }
- prev = Some(curr);
- }
- for dirlist in dirlists {
+ fn insert_entries(&mut self, entries: Vec<JoshutoDirList>) {
+ for dirlist in entries {
self.insert(dirlist.file_path().to_path_buf(), dirlist);
}
- Ok(())
}
fn create_or_soft_update(
&mut self,
path: &Path,
- config: &AppConfig,
options: &DisplayOption,
tab_options: &TabDisplayOption,
) -> io::Result<()> {
@@ -106,9 +52,9 @@ impl DirectoryHistory for JoshutoHistory {
};
if need_update {
let dirlist = if contains_key {
- create_dirlist_with_history(self, path, config, options, tab_options)?
+ create_dirlist_with_history(self, path, options, tab_options)?
} else {
-