feat: add interactive cd

This commit is contained in:
XYenon 2023-08-09 15:56:20 +08:00
parent d198f142e2
commit 598d63ccb9
3 changed files with 22 additions and 3 deletions

View file

@ -1,4 +1,4 @@
use core::{emit, files::FilesSort, input::InputMode}; use core::{files::FilesSort, input::InputMode};
use std::path::PathBuf; use std::path::PathBuf;
use config::{keymap::{Control, Exec, Key, KeymapLayer}, manager::SortBy, KEYMAP}; use config::{keymap::{Control, Exec, Key, KeymapLayer}, manager::SortBy, KEYMAP};
@ -67,8 +67,8 @@ impl Executor {
"forward" => cx.manager.active_mut().forward(), "forward" => cx.manager.active_mut().forward(),
"cd" => { "cd" => {
let path = exec.args.get(0).map(PathBuf::from).unwrap_or_default(); let path = exec.args.get(0).map(PathBuf::from).unwrap_or_default();
emit!(Cd(path)); let interactive = exec.named.contains_key("interactive");
false cx.manager.active_mut().cd_interactive(interactive, path)
} }
// Selection // Selection

View file

@ -97,6 +97,7 @@ keymap = [
{ on = [ "g", "c" ], exec = "cd ~/.config" }, { on = [ "g", "c" ], exec = "cd ~/.config" },
{ on = [ "g", "d" ], exec = "cd ~/Downloads" }, { on = [ "g", "d" ], exec = "cd ~/Downloads" },
{ on = [ "g", "t" ], exec = "cd /tmp" }, { on = [ "g", "t" ], exec = "cd /tmp" },
{ on = [ "g", "<Space>" ], exec = "cd --interactive" },
] ]
[tasks] [tasks]

View file

@ -74,6 +74,24 @@ impl Tab {
true true
} }
pub fn cd_interactive(&mut self, interactive: bool, target: PathBuf) -> bool {
if interactive {
tokio::spawn(async move {
let result = emit!(Input(InputOpt::top("Path:").with_value(target.to_string_lossy())));
if let Ok(path) = result.await {
let path = PathBuf::from(path);
emit!(Cd(path));
}
Ok::<(), Error>(())
});
}
else {
emit!(Cd(target));
}
false
}
pub async fn cd(&mut self, mut target: PathBuf) -> bool { pub async fn cd(&mut self, mut target: PathBuf) -> bool {
let file = if let Ok(f) = File::from(&target).await { let file = if let Ok(f) = File::from(&target).await {
f f