Skip to content

Commit

Permalink
feat: add focus_on_create option (#448)
Browse files Browse the repository at this point in the history
* feat: add `focus_on_create` option

* Replace `PathBuf` with `Path`

* Minor clippy fix
  • Loading branch information
Akmadan23 authored Nov 4, 2023
1 parent 50a8fa7 commit ee50d17
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/joshuto.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
numbered_command = false

focus_on_create = true
use_trash = true
watch_files = true
xdg_open = false
Expand Down
7 changes: 7 additions & 0 deletions docs/configuration/joshuto.toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 18 additions & 1 deletion src/commands/cursor_move.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions src/commands/new_directory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path;

use crate::commands::cursor_move;
use crate::context::AppContext;
use crate::error::AppResult;
use crate::history::DirectoryHistory;
Expand All @@ -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(())
}
6 changes: 6 additions & 0 deletions src/commands/touch_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
2 changes: 2 additions & 0 deletions src/config/clean/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions src/config/raw/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
10 changes: 10 additions & 0 deletions src/fs/dirlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit ee50d17

Please sign in to comment.