summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2022-05-03 15:21:22 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2022-05-03 15:26:44 -0400
commit7ad3eb1823556107cd412bb25d4f8e95931354e0 (patch)
treef2ae137a9b1aae2777d4d38181b36581993c6aca /src
parent9cc4f10fdb3e31723289892be5929ccd0a1c48d7 (diff)
add initial support for zoxide
Diffstat (limited to 'src')
-rw-r--r--src/commands/mod.rs1
-rw-r--r--src/commands/zoxide.rs58
-rw-r--r--src/key_command/command.rs8
-rw-r--r--src/key_command/constants.rs6
-rw-r--r--src/key_command/impl_appcommand.rs13
-rw-r--r--src/key_command/impl_appexecute.rs18
-rw-r--r--src/key_command/impl_comment.rs8
-rw-r--r--src/key_command/impl_from_str.rs7
8 files changed, 95 insertions, 24 deletions
diff --git a/src/commands/mod.rs b/src/commands/mod.rs
index d01c3e3..f585bbc 100644
--- a/src/commands/mod.rs
+++ b/src/commands/mod.rs
@@ -27,3 +27,4 @@ pub mod sub_process;
pub mod subdir_fzf;
pub mod tab_ops;
pub mod touch_file;
+pub mod zoxide;
diff --git a/src/commands/zoxide.rs b/src/commands/zoxide.rs
new file mode 100644
index 0000000..78709b0
--- /dev/null
+++ b/src/commands/zoxide.rs
@@ -0,0 +1,58 @@
+use std::path::Path;
+use std::process::{Command, Stdio};
+
+use crate::commands::change_directory;
+use crate::context::AppContext;
+use crate::error::JoshutoResult;
+use crate::ui::TuiBackend;
+
+pub fn zoxide(context: &mut AppContext, args: &str) -> JoshutoResult {
+ let cwd = std::env::current_dir()?;
+
+ let zoxide_output = Command::new("zoxide")
+ .arg("query")
+ .arg("--exclude")
+ .arg(&cwd)
+ .arg("--")
+ .arg(args)
+ .output()?;
+
+ if zoxide_output.status.success() {
+ if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) {
+ let zoxide_path = zoxide_str.trim();
+ let path = Path::new(zoxide_path);
+ context
+ .message_queue_mut()
+ .push_info(format!("z {}", zoxide_path));
+ change_directory::change_directory(context, &path)?;
+ }
+ }
+ Ok(())
+}
+
+pub fn zoxide_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult {
+ backend.terminal_drop();
+
+ let zoxide_process = Command::new("zoxide")
+ .arg("query")
+ .arg("-i")
+ .arg("--")
+ .stdin(Stdio::piped())
+ .stdout(Stdio::piped())
+ .spawn()?;
+ let zoxide_output = zoxide_process.wait_with_output()?;
+
+ backend.terminal_restore()?;
+
+ if zoxide_output.status.success() {
+ if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) {
+ let zoxide_path = zoxide_str.trim();
+ let path = Path::new(zoxide_path);
+ context
+ .message_queue_mut()
+ .push_info(format!("z {}", zoxide_path));
+ change_directory::change_directory(context, &path)?;
+ }
+ }
+ Ok(())
+}
diff --git a/src/key_command/command.rs b/src/key_command/command.rs
index 77f2316..6dae33b 100644
--- a/src/key_command/command.rs
+++ b/src/key_command/command.rs
@@ -57,12 +57,9 @@ pub enum Command {
SearchGlob(String),
SearchString(String),
SearchIncremental(String),
- SearchFzf,
SearchNext,
SearchPrev,
- SubdirFzf,
-
SelectFiles(String, SelectOption),
SetMode,
SubProcess(Vec<String>, bool),
@@ -80,4 +77,9 @@ pub enum Command {
TabSwitch(i32),
TabSwitchIndex(u32),
Help,
+
+ SearchFzf,
+ SubdirFzf,
+ Zoxide(String),
+ ZoxideInteractive,
}
diff --git a/src/key_command/constants.rs b/src/key_command/constants.rs
index 558790c..d9a4202 100644
--- a/src/key_command/constants.rs
+++ b/src/key_command/constants.rs
@@ -56,10 +56,8 @@ cmd_constants![
(CMD_SEARCH_STRING, "search"),
(CMD_SEARCH_INCREMENTAL, "search_inc"),
(CMD_SEARCH_GLOB, "search_glob"),
- (CMD_SEARCH_FZF, "search_fzf"),
(CMD_SEARCH_NEXT, "search_next"),
(CMD_SEARCH_PREV, "search_prev"),
- (CMD_SUBDIR_FZF, "subdir_fzf"),
(CMD_SELECT_FILES, "select"),
(CMD_SET_MODE, "set_mode"),
(CMD_SORT, "sort"),
@@ -73,6 +71,10 @@ cmd_constants![
(CMD_SWITCH_LINE_NUMBERS, "line_nums"),
(CMD_TOUCH_FILE, "touch"),
(CMD_HELP, "help"),
+ (CMD_SEARCH_FZF, "search_fzf"),
+ (CMD_SUBDIR_FZF, "subdir_fzf"),
+ (CMD_ZOXIDE, "z"),
+ (CMD_ZOXIDE_INTERACTIVE, "zi"),
];
pub fn complete_command(partial_command: &str) -> Vec<Pair> {
diff --git a/src/key_command/impl_appcommand.rs b/src/key_command/impl_appcommand.rs
index 6229289..c8bf2eb 100644
--- a/src/key_command/impl_appcommand.rs
+++ b/src/key_command/impl_appcommand.rs
@@ -55,27 +55,30 @@ impl AppCommand for Command {
Self::SearchString(_) => CMD_SEARCH_STRING,
Self::SearchIncremental(_) => CMD_SEARCH_INCREMENTAL,
Self::SearchGlob(_) => CMD_SEARCH_GLOB,
- Self::SearchFzf => CMD_SEARCH_FZF,
Self::SearchNext => CMD_SEARCH_NEXT,
Self::SearchPrev => CMD_SEARCH_PREV,
- Self::SubdirFzf => CMD_SUBDIR_FZF,
-
Self::SelectFiles(_, _) => CMD_SELECT_FILES,
Self::SetMode => CMD_SET_MODE,
+ Self::ShowWorkers => CMD_SHOW_WORKERS,
+
Self::Sort(_) => CMD_SORT,
Self::SortReverse => CMD_SORT_REVERSE,
Self::SubProcess(_, false) => CMD_SUBPROCESS_FOREGROUND,
Self::SubProcess(_, true) => CMD_SUBPROCESS_BACKGROUND,
- Self::ShowWorkers => CMD_SHOW_WORKERS,
+ Self::SwitchLineNums(_) => CMD_SWITCH_LINE_NUMBERS,
Self::TabSwitch(_) => CMD_TAB_SWITCH,
Self::TabSwitchIndex(_) => CMD_TAB_SWITCH_INDEX,
Self::ToggleHiddenFiles => CMD_TOGGLE_HIDDEN,
- Self::SwitchLineNums(_) => CMD_SWITCH_LINE_NUMBERS,
Self::TouchFile(_) => CMD_TOUCH_FILE,
+
+ Self::SearchFzf => CMD_SEARCH_FZF,
+ Self::SubdirFzf => CMD_SUBDIR_FZF,
+ Self::Zoxide(_) => CMD_ZOXIDE,
+ Self::ZoxideInteractive => CMD_ZOXIDE_INTERACTIVE,
}
}
}
diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs
index 8996a7c..e381770 100644
--- a/src/key_command/impl_appexecute.rs
+++ b/src/key_command/impl_appexecute.rs
@@ -80,27 +80,22 @@ impl AppExecute for Command {
// cursor will be one the selected word. And as `interactive_execute` for
// `SearchIncremental` always starts from index 0, this operation will be a no-op
Self::SearchIncremental(_) => Ok(()),
- Self::SearchFzf => search_fzf::search_fzf(context, backend),
Self::SearchNext => search::search_next(context),
Self::SearchPrev => search::search_prev(context),
- Self::SubdirFzf => subdir_fzf::subdir_fzf(context, backend),
-
Self::SelectFiles(pattern, options) => {
selection::select_files(context, pattern.as_str(), options)
}
Self::SetMode => set_mode::set_mode(context, backend),
+ Self::ShowWorkers => show_workers::show_workers(context, backend, keymap_t),
+ Self::Sort(t) => sort::set_sort(context, *t),
+ Self::SortReverse => sort::toggle_reverse(context),
Self::SubProcess(v, spawn) => {
sub_process::sub_process(context, backend, v.as_slice(), *spawn)
}
- Self::ShowWorkers => show_workers::show_workers(context, backend, keymap_t),
-
- Self::ToggleHiddenFiles => show_hidden::toggle_hidden(context),
-
Self::SwitchLineNums(d) => line_nums::switch_line_numbering(context, *d),
- Self::Sort(t) => sort::set_sort(context, *t),
- Self::SortReverse => sort::toggle_reverse(context),
+ Self::ToggleHiddenFiles => show_hidden::toggle_hidden(context),
Self::TabSwitch(i) => {
tab_ops::tab_switch(*i, context)?;
@@ -108,6 +103,11 @@ impl AppExecute for Command {
}
Self::TabSwitchIndex(i) => tab_ops::tab_switch_index(*i as usize, context),
Self::Help => help::help_loop(context, backend, keymap_t),
+
+ Self::SearchFzf => search_fzf::search_fzf(context, backend),
+ Self::SubdirFzf => subdir_fzf::subdir_fzf(context, backend),
+ Self::Zoxide(arg) => zoxide::zoxide(context, &arg),
+ Self::ZoxideInteractive => zoxide::zoxide_interactive(context, backend),
}
}
}
diff --git a/src/key_command/impl_comment.rs b/src/key_command/impl_comment.rs
index b52e3a2..dbc639a 100644
--- a/src/key_command/impl_comment.rs
+++ b/src/key_command/impl_comment.rs
@@ -72,12 +72,9 @@ impl CommandComment for Command {
Self::SearchString(_) => "Search",
Self::SearchIncremental(_) => "Search as you type",
Self::SearchGlob(_) => "Search with globbing",
- Self::SearchFzf => "Search via fzf",
Self::SearchNext => "Next search entry",
Self::SearchPrev => "Previous search entry",
- Self::SubdirFzf => "Switch to a child directory via fzf",
-
Self::SelectFiles(_, _) => "Select file",
Self::SetMode => "Set file permissions",
Self::SubProcess(_, false) => "Run a shell command",
@@ -100,6 +97,11 @@ impl CommandComment for Command {
Self::TabSwitch(_) => "Swith to the next tab",
Self::TabSwitchIndex(_) => "Swith to a given tab",
Self::Help => "Open this help page",
+
+ Self::SearchFzf => "Search via fzf",
+ Self::SubdirFzf => "Switch to a child directory via fzf",
+ Self::Zoxide(_) => "Zoxide",
+ Self::ZoxideInteractive => "Zoxide interactive",
}
}
}
diff --git a/src/key_command/impl_from_str.rs b/src/key_command/impl_from_str.rs
index ba791e2..09944a3 100644
--- a/src/key_command/impl_from_str.rs
+++ b/src/key_command/impl_from_str.rs
@@ -72,15 +72,18 @@ impl std::str::FromStr for Command {
simple_command_conversion_case!(command, CMD_RELOAD_DIRECTORY_LIST, Self::ReloadDirList);
simple_command_conversion_case!(command, CMD_RENAME_FILE_APPEND, Self::RenameFileAppend);
simple_command_conversion_case!(command, CMD_RENAME_FILE_PREPEND, Self::RenameFilePrepend);
- simple_command_conversion_case!(command, CMD_SEARCH_FZF, Self::SearchFzf);
simple_command_conversion_case!(command, CMD_SEARCH_NEXT, Self::SearchNext);
simple_command_conversion_case!(command, CMD_SEARCH_PREV, Self::SearchPrev);
- simple_command_conversion_case!(command, CMD_SUBDIR_FZF, Self::SubdirFzf);
simple_command_conversion_case!(command, CMD_SHOW_WORKERS, Self::ShowWorkers);
simple_command_conversion_case!(command, CMD_SET_MODE, Self::SetMode);
simple_command_conversion_case!(command, CMD_TOGGLE_HIDDEN, Self::ToggleHiddenFiles);
simple_command_conversion_case!(command, CMD_BULK_RENAME, Self::BulkRename);
+ simple_command_conversion_case!(command, CMD_SEARCH_FZF, Self::SearchFzf);
+ simple_command_conversion_case!(command, CMD_SUBDIR_FZF, Self::SubdirFzf);
+ simple_command_conversion_case!(command, CMD_ZOXIDE, Self::Zoxide(arg.to_string()));
+ simple_command_conversion_case!(command, CMD_ZOXIDE_INTERACTIVE, Self::ZoxideInteractive);
+
if command == CMD_QUIT {
match arg {
"--force" => Ok(Self::Quit(QuitAction::Force)),