This commit is contained in:
sxyazi 2024-08-21 23:15:28 +08:00
parent 219a17eb94
commit 666adc7c2f
No known key found for this signature in database
5 changed files with 51 additions and 65 deletions

View file

@ -196,7 +196,7 @@ keymap = [
keymap = [
{ on = "<C-c>", run = "close", desc = "Cancel input" },
{ on = "<Enter>", run = "close --submit", desc = "Submit the input" },
{ on = "<Enter>", run = "close --submit", desc = "Submit input" },
{ on = "<Esc>", run = "escape", desc = "Go back the normal mode, or cancel input" },
{ on = "<C-[>", run = "escape", desc = "Go back the normal mode, or cancel input" },
@ -264,27 +264,23 @@ keymap = [
[confirm]
keymap = [
{ on = [ "<Esc>" ], run = "close", desc = "Cancel the confirm" },
{ on = [ "<Enter>" ], run = "close --submit", desc = "Submit the confirm" },
{ on = "<Esc>", run = "close", desc = "Cancel the confirm" },
{ on = "<C-[>", run = "close", desc = "Cancel the confirm" },
{ on = "<C-c>", run = "close", desc = "Cancel the confirm" },
{ on = "<Enter>", run = "close --submit", desc = "Submit the confirm" },
{ on = [ "n" ], run = "close", desc = "Cancel the confirm" },
{ on = [ "N" ], run = "close", desc = "Cancel the confirm" },
{ on = [ "y" ], run = "close --submit", desc = "Submit the confirm" },
{ on = [ "Y" ], run = "close --submit", desc = "Submit the confirm" },
{ on = "n", run = "close", desc = "Cancel the confirm" },
{ on = "y", run = "close --submit", desc = "Submit the confirm" },
{ on = [ "k" ], run = "arrow -1", desc = "Move cursor up" },
{ on = [ "j" ], run = "arrow 1", desc = "Move cursor down" },
{ on = "k", run = "arrow -1", desc = "Move cursor up" },
{ on = "j", run = "arrow 1", desc = "Move cursor down" },
{ on = [ "K" ], run = "arrow -5", desc = "Move cursor up 5 lines" },
{ on = [ "J" ], run = "arrow 5", desc = "Move cursor down 5 lines" },
{ on = "<Up>", run = "arrow -1", desc = "Move cursor up" },
{ on = "<Down>", run = "arrow 1", desc = "Move cursor down" },
{ on = [ "<Up>" ], run = "arrow -1", desc = "Move cursor up" },
{ on = [ "<Down>" ], run = "arrow 1", desc = "Move cursor down" },
{ on = [ "<S-Up>" ], run = "arrow -5", desc = "Move cursor up 5 lines" },
{ on = [ "<S-Down>" ], run = "arrow 5", desc = "Move cursor down 5 lines" },
{ on = [ "~" ], run = "help", desc = "Open help" }
# Help
{ on = "~", run = "help", desc = "Open help" },
{ on = "<F1>", run = "help", desc = "Open help" },
]
[completion]

View file

@ -179,14 +179,14 @@ delete_origin = "top-center"
delete_offset = [ 0, 5, 70, 20 ]
# overwrite
overwrite_title = "Are you sure?"
overwrite_content = "Will overwrite existing file: {url}"
overwrite_title = "Overwrite file?"
overwrite_content = "Will overwrite the following file:\n{url}"
overwrite_origin = "top-center"
overwrite_offset = [ 0, 2, 50, 20 ]
# quit
quit_title = "Are you sure you want to quit?"
quit_content = "The following tasks are running:\n"
quit_title = "Quit?"
quit_content = "The following task is still running, are you sure you want to quit?\n{tasks}"
quit_origin = "top-center"
quit_offset = [ 0, 2, 50, 20 ]

View file

@ -29,7 +29,6 @@ pub struct ConfirmCfg {
}
impl InputCfg {
#[inline]
pub fn cd() -> Self {
Self {
title: INPUT.cd_title.to_owned(),
@ -39,7 +38,6 @@ impl InputCfg {
}
}
#[inline]
pub fn create() -> Self {
Self {
title: INPUT.create_title.to_owned(),
@ -48,7 +46,6 @@ impl InputCfg {
}
}
#[inline]
pub fn rename() -> Self {
Self {
title: INPUT.rename_title.to_owned(),
@ -57,7 +54,6 @@ impl InputCfg {
}
}
#[inline]
pub fn filter() -> Self {
Self {
title: INPUT.filter_title.to_owned(),
@ -67,7 +63,6 @@ impl InputCfg {
}
}
#[inline]
pub fn find(prev: bool) -> Self {
Self {
title: INPUT.find_title[prev as usize].to_owned(),
@ -77,7 +72,6 @@ impl InputCfg {
}
}
#[inline]
pub fn search(name: &str) -> Self {
Self {
title: INPUT.search_title.replace("{n}", name),
@ -86,7 +80,6 @@ impl InputCfg {
}
}
#[inline]
pub fn shell(block: bool) -> Self {
Self {
title: INPUT.shell_title[block as usize].to_owned(),
@ -110,25 +103,22 @@ impl InputCfg {
}
impl ConfirmCfg {
#[inline]
pub fn delete(targets: &[yazi_shared::fs::Url]) -> Self {
pub fn trash(urls: &[yazi_shared::fs::Url]) -> Self {
Self {
title: CONFIRM.delete_title.replace("{n}", &targets.len().to_string()),
position: Position::new(CONFIRM.delete_origin, CONFIRM.delete_offset),
content: targets.iter().map(|t| t.to_string()).collect::<Vec<_>>().join("\n"),
}
}
#[inline]
pub fn trash(targets: &[yazi_shared::fs::Url]) -> Self {
Self {
title: CONFIRM.trash_title.replace("{n}", &targets.len().to_string()),
title: Self::replace_number(&CONFIRM.trash_title, urls.len(), usize::MAX),
position: Position::new(CONFIRM.trash_origin, CONFIRM.trash_offset),
content: targets.iter().map(|t| t.to_string()).collect::<Vec<_>>().join("\n"),
content: urls.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n"),
}
}
pub fn delete(urls: &[yazi_shared::fs::Url]) -> Self {
Self {
title: Self::replace_number(&CONFIRM.delete_title, urls.len(), usize::MAX),
position: Position::new(CONFIRM.delete_origin, CONFIRM.delete_offset),
content: urls.iter().map(ToString::to_string).collect::<Vec<_>>().join("\n"),
}
}
#[inline]
pub fn overwrite(url: &Url) -> Self {
Self {
title: CONFIRM.overwrite_title.to_owned(),
@ -137,19 +127,18 @@ impl ConfirmCfg {
}
}
#[inline]
pub fn quit(ongoing_task_names: Vec<String>) -> Self {
let n = ongoing_task_names.len();
let mut message = CONFIRM.quit_content.replace("{n}", &n.to_string());
message.push_str(&ongoing_task_names.join("\n"));
pub fn quit(tasks: Vec<String>) -> Self {
Self {
title: CONFIRM.quit_title.to_owned(),
content: message,
title: Self::replace_number(&CONFIRM.quit_title, tasks.len(), 10),
content: CONFIRM.quit_content.replace("{tasks}", &tasks.join("\n")),
position: Position::new(CONFIRM.quit_origin, CONFIRM.quit_offset),
}
}
fn replace_number(tpl: &str, n: usize, max: usize) -> String {
let s = tpl.replace("{s}", if n > 1 { "s" } else { "" });
s.replace("{n}", &if n > max { format!("{max}+") } else { n.to_string() })
}
}
impl SelectCfg {
@ -158,7 +147,6 @@ impl SelectCfg {
SELECT.open_offset.height.min(SELECT.border().saturating_add(len as u16))
}
#[inline]
pub fn open(items: Vec<String>) -> Self {
let max_height = Self::max_height(items.len());
Self {

View file

@ -18,27 +18,21 @@ impl From<Cmd> for Opt {
fn from(c: Cmd) -> Self { Self { no_cwd_file: c.bool("no-cwd-file") } }
}
// async fn recv(result: &mut impl Future<Output = anyhow::Result<bool>>) ->
// anyhow::Result<bool> { result
//}
impl Manager {
pub fn quit(&self, opt: impl Into<Opt>, tasks: &Tasks) {
let opt = EventQuit { no_cwd_file: opt.into().no_cwd_file, ..Default::default() };
let ongoing = tasks.ongoing().clone();
let left = ongoing.lock().len();
let left: Vec<String> = ongoing.lock().values().take(11).map(|t| t.name.clone()).collect();
if left == 0 {
if left.is_empty() {
emit!(Quit(opt));
return;
}
tokio::spawn(async move {
let mut i = 0;
let result = ConfirmProxy::show(ConfirmCfg::quit(
ongoing.lock().values().map(|t| t.name.clone()).collect(),
));
let mut rx = ConfirmProxy::show_rx(ConfirmCfg::quit(left));
loop {
select! {
_ = time::sleep(Duration::from_millis(100)) => {
@ -49,10 +43,16 @@ impl Manager {
return;
}
}
b = &mut rx => {
if b.unwrap_or(false) {
emit!(Quit(opt));
}
return;
}
}
}
if result.await {
if rx.await.unwrap_or(false) {
emit!(Quit(opt));
}
});

View file

@ -6,9 +6,11 @@ pub struct ConfirmProxy;
impl ConfirmProxy {
#[inline]
pub async fn show(cfg: ConfirmCfg) -> bool {
pub async fn show(cfg: ConfirmCfg) -> bool { Self::show_rx(cfg).await.unwrap_or(false) }
pub fn show_rx(cfg: ConfirmCfg) -> oneshot::Receiver<bool> {
let (tx, rx) = oneshot::channel();
emit!(Call(Cmd::new("show").with_any("tx", tx).with_any("cfg", cfg), Layer::Confirm));
rx.await.unwrap_or(false)
rx
}
}