Skip to content

Commit be00881

Browse files
authored
feat: allow initializing input when opening it with commands like rename, create, find, filter, etc. (#2578)
1 parent fd007ab commit be00881

File tree

11 files changed

+52
-60
lines changed

11 files changed

+52
-60
lines changed

yazi-core/src/mgr/commands/create.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,23 @@ impl Mgr {
2222
#[yazi_codegen::command]
2323
pub fn create(&self, opt: Opt) {
2424
let cwd = self.cwd().to_owned();
25+
let mut input = InputProxy::show(InputCfg::create(opt.dir));
26+
2527
tokio::spawn(async move {
26-
let mut result = InputProxy::show(InputCfg::create(opt.dir));
27-
let Some(Ok(name)) = result.recv().await else {
28-
return Ok(());
29-
};
28+
let Some(Ok(name)) = input.recv().await else { return };
3029
if name.is_empty() {
31-
return Ok(());
30+
return;
3231
}
3332

3433
let new = cwd.join(&name);
3534
if !opt.force
3635
&& maybe_exists(&new).await
3736
&& !ConfirmProxy::show(ConfirmCfg::overwrite(&new)).await
3837
{
39-
return Ok(());
38+
return;
4039
}
4140

42-
Self::create_do(new, opt.dir || name.ends_with('/') || name.ends_with('\\')).await
41+
_ = Self::create_do(new, opt.dir || name.ends_with('/') || name.ends_with('\\')).await;
4342
});
4443
}
4544

yazi-core/src/mgr/commands/open.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use yazi_config::{YAZI, popup::PickCfg};
66
use yazi_fs::File;
77
use yazi_macro::emit;
88
use yazi_plugin::isolate;
9-
use yazi_proxy::{MgrProxy, TasksProxy, options::OpenDoOpt};
9+
use yazi_proxy::{MgrProxy, PickProxy, TasksProxy, options::OpenDoOpt};
1010
use yazi_shared::{MIME_DIR, event::{CmdCow, EventQuit}, url::Url};
1111

1212
use crate::{mgr::Mgr, tab::Folder, tasks::Tasks};
@@ -95,12 +95,10 @@ impl Mgr {
9595
return;
9696
}
9797

98+
let pick = PickProxy::show(PickCfg::open(openers.iter().map(|o| o.desc()).collect()));
9899
let urls = [opt.hovered].into_iter().chain(targets.into_iter().map(|(u, _)| u)).collect();
99100
tokio::spawn(async move {
100-
let result =
101-
yazi_proxy::PickProxy::show(PickCfg::open(openers.iter().map(|o| o.desc()).collect()));
102-
103-
if let Ok(choice) = result.await {
101+
if let Ok(choice) = pick.await {
104102
TasksProxy::open_with(Cow::Borrowed(openers[choice]), opt.cwd, urls);
105103
}
106104
});

yazi-core/src/mgr/commands/remove.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ impl Mgr {
4141
return self.remove_do(opt, tasks);
4242
}
4343

44-
tokio::spawn(async move {
45-
let result = ConfirmProxy::show(if opt.permanently {
46-
ConfirmCfg::delete(&opt.targets)
47-
} else {
48-
ConfirmCfg::trash(&opt.targets)
49-
});
44+
let confirm = ConfirmProxy::show(if opt.permanently {
45+
ConfirmCfg::delete(&opt.targets)
46+
} else {
47+
ConfirmCfg::trash(&opt.targets)
48+
});
5049

51-
if result.await {
50+
tokio::spawn(async move {
51+
if confirm.await {
5252
MgrProxy::remove_do(opt.targets, opt.permanently);
5353
}
5454
});

yazi-core/src/mgr/commands/rename.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ impl Mgr {
5353

5454
let old = hovered.url_owned();
5555
let tab = self.tabs.active().id;
56-
tokio::spawn(async move {
57-
let mut result = InputProxy::show(InputCfg::rename().with_value(name).with_cursor(cursor));
58-
let Some(Ok(name)) = result.recv().await else {
59-
return;
60-
};
56+
let mut input = InputProxy::show(InputCfg::rename().with_value(name).with_cursor(cursor));
6157

58+
tokio::spawn(async move {
59+
let Some(Ok(name)) = input.recv().await else { return };
6260
if name.is_empty() {
6361
return;
6462
}

yazi-core/src/tab/commands/cd.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ impl Tab {
7777
}
7878

7979
fn cd_interactive(&mut self) {
80-
tokio::spawn(async move {
81-
let rx = InputProxy::show(InputCfg::cd());
80+
let input = InputProxy::show(InputCfg::cd());
8281

83-
let rx = Debounce::new(UnboundedReceiverStream::new(rx), Duration::from_millis(50));
82+
tokio::spawn(async move {
83+
let rx = Debounce::new(UnboundedReceiverStream::new(input), Duration::from_millis(50));
8484
pin!(rx);
8585

8686
while let Some(result) = rx.next().await {

yazi-core/src/tab/commands/filter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ impl From<CmdCow> for Opt {
3030
impl Tab {
3131
#[yazi_codegen::command]
3232
pub fn filter(&mut self, opt: Opt) {
33-
tokio::spawn(async move {
34-
let rx = InputProxy::show(InputCfg::filter());
33+
let input = InputProxy::show(InputCfg::filter());
3534

36-
let rx = Debounce::new(UnboundedReceiverStream::new(rx), Duration::from_millis(50));
35+
tokio::spawn(async move {
36+
let rx = Debounce::new(UnboundedReceiverStream::new(input), Duration::from_millis(50));
3737
pin!(rx);
3838

3939
while let Some(result) = rx.next().await {

yazi-core/src/tab/commands/find.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ impl From<CmdCow> for Opt {
2525
impl Tab {
2626
#[yazi_codegen::command]
2727
pub fn find(&mut self, opt: Opt) {
28-
tokio::spawn(async move {
29-
let rx = InputProxy::show(InputCfg::find(opt.prev));
28+
let input = InputProxy::show(InputCfg::find(opt.prev));
3029

31-
let rx = Debounce::new(UnboundedReceiverStream::new(rx), Duration::from_millis(50));
30+
tokio::spawn(async move {
31+
let rx = Debounce::new(UnboundedReceiverStream::new(input), Duration::from_millis(50));
3232
pin!(rx);
3333

3434
while let Some(Ok(s)) | Some(Err(InputError::Typed(s))) = rx.next().await {

yazi-core/src/tab/commands/search.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{borrow::Cow, mem, time::Duration};
22

3-
use anyhow::bail;
43
use tokio::pin;
54
use tokio_stream::{StreamExt, wrappers::UnboundedReceiverStream};
65
use tracing::error;
@@ -13,22 +12,18 @@ use crate::tab::Tab;
1312

1413
impl Tab {
1514
pub fn search(&mut self, opt: impl TryInto<SearchOpt>) {
16-
let Ok(mut opt) = opt.try_into() else {
15+
let Ok(mut opt): Result<SearchOpt, _> = opt.try_into() else {
1716
return AppProxy::notify_error("Invalid `search` option", "Failed to parse search option");
1817
};
1918

20-
if opt.via == SearchOptVia::None {
21-
return self.search_stop();
22-
}
23-
2419
if let Some(handle) = self.search.take() {
2520
handle.abort();
2621
}
2722

28-
tokio::spawn(async move {
29-
let mut input =
30-
InputProxy::show(InputCfg::search(&opt.via.to_string()).with_value(opt.subject));
23+
let mut input =
24+
InputProxy::show(InputCfg::search(opt.via.as_ref()).with_value(opt.subject.to_owned()));
3125

26+
tokio::spawn(async move {
3227
if let Some(Ok(subject)) = input.recv().await {
3328
opt.subject = Cow::Owned(subject);
3429
TabProxy::search_do(opt);
@@ -68,7 +63,6 @@ impl Tab {
6863
subject: opt.subject.into_owned(),
6964
args: opt.args,
7065
}),
71-
SearchOptVia::None => bail!("Invalid `via` option for `search` command"),
7266
}?;
7367

7468
let rx = UnboundedReceiverStream::new(rx).chunks_timeout(5000, Duration::from_millis(500));

yazi-core/src/tab/commands/shell.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@ impl Tab {
5454

5555
let cwd = opt.cwd.take().unwrap_or_else(|| self.cwd().clone());
5656
let selected = self.hovered_and_selected().cloned().collect();
57+
58+
let input = opt.interactive.then(|| {
59+
InputProxy::show(
60+
InputCfg::shell(opt.block).with_value(opt.run.to_owned()).with_cursor(opt.cursor),
61+
)
62+
});
63+
5764
tokio::spawn(async move {
58-
if opt.interactive {
59-
let mut result =
60-
InputProxy::show(InputCfg::shell(opt.block).with_value(opt.run).with_cursor(opt.cursor));
61-
match result.recv().await {
65+
if let Some(mut rx) = input {
66+
match rx.recv().await {
6267
Some(Ok(e)) => opt.run = Cow::Owned(e),
6368
_ => return,
6469
}

yazi-proxy/src/options/search.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, fmt::Display};
1+
use std::borrow::Cow;
22

33
use yazi_shared::event::CmdCow;
44

@@ -35,31 +35,27 @@ impl TryFrom<CmdCow> for SearchOpt {
3535
// Via
3636
#[derive(PartialEq, Eq)]
3737
pub enum SearchOptVia {
38-
// TODO: remove `None` in the future
39-
None,
4038
Rg,
41-
Fd,
4239
Rga,
40+
Fd,
4341
}
4442

4543
impl From<&str> for SearchOptVia {
4644
fn from(value: &str) -> Self {
4745
match value {
4846
"rg" => Self::Rg,
49-
"fd" => Self::Fd,
5047
"rga" => Self::Rga,
51-
_ => Self::None,
48+
_ => Self::Fd,
5249
}
5350
}
5451
}
5552

56-
impl Display for SearchOptVia {
57-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58-
f.write_str(match self {
53+
impl AsRef<str> for SearchOptVia {
54+
fn as_ref(&self) -> &str {
55+
match self {
5956
Self::Rg => "rg",
60-
Self::Fd => "fd",
6157
Self::Rga => "rga",
62-
Self::None => "none",
63-
})
58+
Self::Fd => "fd",
59+
}
6460
}
6561
}

yazi-proxy/src/tab.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ impl TabProxy {
2525
pub fn search_do(opt: SearchOpt) {
2626
emit!(Call(
2727
// TODO: use second positional argument instead of `args` parameter
28-
Cmd::args("mgr:search_do", &[opt.subject]).with("via", opt.via).with("args", opt.args_raw)
28+
Cmd::args("mgr:search_do", &[opt.subject])
29+
.with("via", opt.via.as_ref())
30+
.with("args", opt.args_raw)
2931
));
3032
}
3133
}

0 commit comments

Comments
 (0)