summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAzad <49314270+Akmadan23@users.noreply.github.com>2023-11-04 15:32:08 +0100
committerGitHub <noreply@github.com>2023-11-04 10:32:08 -0400
commitee50d175d8c14cc7420e922864e6c5289c2785f3 (patch)
treef051ddedc3c86bd24f0e3a3f6480a88c1cc1af52
parent50a8fa70e1870266897973c87886e306f47e99bf (diff)
feat: add `focus_on_create` option (#448)
* feat: add `focus_on_create` option * Replace `PathBuf` with `Path` * Minor clippy fix
-rw-r--r--config/joshuto.toml1
-rw-r--r--docs/configuration/joshuto.toml.md7
-rw-r--r--src/commands/cursor_move.rs19
-rw-r--r--src/commands/new_directory.rs6
-rw-r--r--src/commands/touch_file.rs6
-rw-r--r--src/config/clean/app/config.rs2
-rw-r--r--src/config/raw/app/config.rs2
-rw-r--r--src/fs/dirlist.rs10
8 files changed, 52 insertions, 1 deletions
diff --git a/config/joshuto.toml b/config/joshuto.toml
index 455210d..0543241 100644
--- a/config/joshuto.toml
+++ b/config/joshuto.toml
@@ -1,5 +1,6 @@
numbered_command = false
+focus_on_create = true
use_trash = true
watch_files = true
xdg_open = false
diff --git a/docs/configuration/joshuto.toml.md b/docs/configuration/joshuto.toml.md
index ee88c33..1fc6e6e 100644
--- a/docs/configuration/joshuto.toml.md
+++ b/docs/configuration/joshuto.toml.md
@@ -20,6 +20,13 @@ use_trash = true
# Watch for filesystem changes and update directory listings accordingly
watch_files = true
+# If true the cursor will focus newly created files or directories with `:touch` or `:mkdir`
+# Even if true, the behavior can be avoided prefixing the new file/dir with "./"
+# E.g.:
+# - `:mkdir a` moves the cursor to the new directory `a`
+# - `:mkdir ./b` keeps the cursor where it was
+focus_on_create = true
+
# The maximum file size to show a preview for
max_preview_size = 2097152 # 2MB
diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs
index 1f6e5ff..2d7688f 100644
--- a/src/commands/cursor_move.rs
+++ b/src/commands/cursor_move.rs
@@ -1,5 +1,7 @@
+use std::path;
+
use crate::context::AppContext;
-use crate::error::AppResult;
+use crate::error::{AppError, AppErrorKind, AppResult};
use crate::ui::AppBackend;
pub fn lazy_load_directory_size(context: &mut AppContext) {
@@ -54,6 +56,21 @@ pub fn cursor_move(context: &mut AppContext, new_index: usize) {
}
}
+pub fn to_path(context: &mut AppContext, path: &path::Path) -> AppResult {
+ // This error should never happen
+ let err = || AppError::new(AppErrorKind::UnknownError, String::from("Unexpected error"));
+ let ui_context = context.ui_context_ref().clone();
+ let display_options = context.config_ref().display_options_ref().clone();
+ if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() {
+ if let path::Component::Normal(name) = path.components().next().ok_or_else(err)? {
+ let index = curr_list.get_index_from_name(name.to_str().ok_or_else(err)?);
+ curr_list.set_index(index, &ui_context, &display_options);
+ }
+ }
+
+ Ok(())
+}
+
pub fn up(context: &mut AppContext, u: usize) -> AppResult {
let movement = context
.tab_context_ref()
diff --git a/src/commands/new_directory.rs b/src/commands/new_directory.rs
index ec9efd4..32b248e 100644
--- a/src/commands/new_directory.rs
+++ b/src/commands/new_directory.rs
@@ -1,5 +1,6 @@
use std::path;
+use crate::commands::cursor_move;
use crate::context::AppContext;
use crate::error::AppResult;
use crate::history::DirectoryHistory;
@@ -13,5 +14,10 @@ pub fn new_directory(context: &mut AppContext, p: &path::Path) -> AppResult {
tab.history_mut()
.reload(&curr_path, &options, &tab_options)?;
}
+
+ if context.config_ref().focus_on_create {
+ cursor_move::to_path(context, p)?;
+ }
+
Ok(())
}
diff --git a/src/commands/touch_file.rs b/src/commands/touch_file.rs
index 27249b1..f36beff 100644
--- a/src/commands/touch_file.rs
+++ b/src/commands/touch_file.rs
@@ -4,6 +4,7 @@ use std::time::SystemTime;
use filetime::FileTime;
+use crate::commands::cursor_move;
use crate::context::AppContext;
use crate::error::AppResult;
use crate::history::create_dirlist_with_history;
@@ -58,5 +59,10 @@ pub fn touch_file(context: &mut AppContext, arg: &str) -> AppResult {
create_dirlist_with_history(history, path.as_path(), &options, &tab_options)?;
history.insert(path, new_dirlist);
}
+
+ if context.config_ref().focus_on_create {
+ cursor_move::to_path(context, path::Path::new(arg))?;
+ }
+
Ok(())
}
diff --git a/src/config/clean/app/config.rs b/src/config/clean/app/config.rs
index 292078d..fb8ceda 100644
--- a/src/config/clean/app/config.rs
+++ b/src/config/clean/app/config.rs
@@ -16,6 +16,7 @@ pub struct AppConfig {
pub xdg_open: bool,
pub xdg_open_fork: bool,
pub watch_files: bool,
+ pub focus_on_create: bool,
pub cmd_aliases: HashMap<String, String>,
pub _display_options: DisplayOption,
pub _preview_options: PreviewOption,
@@ -78,6 +79,7 @@ impl From<AppConfigRaw> for AppConfig {
xdg_open_fork: raw.xdg_open_fork,
watch_files: raw.watch_files,
cmd_aliases: raw.cmd_aliases,
+ focus_on_create: raw.focus_on_create,
_display_options: DisplayOption::from(raw.display_options),
_preview_options: PreviewOption::from(raw.preview_options),
_search_options: SearchOption::from(raw.search_options),
diff --git a/src/config/raw/app/config.rs b/src/config/raw/app/config.rs
index be358bc..feac4f2 100644
--- a/src/config/raw/app/config.rs
+++ b/src/config/raw/app/config.rs
@@ -26,6 +26,8 @@ pub struct AppConfigRaw {
pub xdg_open_fork: bool,
#[serde(default = "default_true")]
pub watch_files: bool,
+ #[serde(default = "default_true")]
+ pub focus_on_create: bool,
#[serde(default)]
pub cmd_aliases: HashMap<String, String>,
#[serde(default, rename = "display")]
diff --git a/src/fs/dirlist.rs b/src/fs/dirlist.rs
index 72b809b..2acf7c1 100644
--- a/src/fs/dirlist.rs
+++ b/src/fs/dirlist.rs
@@ -69,6 +69,16 @@ impl JoshutoDirList {
self.index
}
+ pub fn get_index_from_name(&self, name: &str) -> Option<usize> {
+ for (index, entry) in self.iter().enumerate() {
+ if name == entry.file_name() {
+ return Some(index);
+ }
+ }
+
+ None
+ }
+
pub fn get_visual_mode_anchor_index(&self) -> Option<usize> {
self.visual_mode_anchor_index
}