From 666adc7c2fe87b1990c82ad69f5fcabf4057e4ed Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 21 Aug 2024 23:15:28 +0800 Subject: [PATCH] Refactor --- yazi-config/preset/keymap.toml | 32 ++++++++--------- yazi-config/preset/yazi.toml | 8 ++--- yazi-config/src/popup/options.rs | 50 ++++++++++---------------- yazi-core/src/manager/commands/quit.rs | 20 +++++------ yazi-proxy/src/confirm.rs | 6 ++-- 5 files changed, 51 insertions(+), 65 deletions(-) diff --git a/yazi-config/preset/keymap.toml b/yazi-config/preset/keymap.toml index 9d9d484b..7c08299e 100644 --- a/yazi-config/preset/keymap.toml +++ b/yazi-config/preset/keymap.toml @@ -196,7 +196,7 @@ keymap = [ keymap = [ { on = "", run = "close", desc = "Cancel input" }, - { on = "", run = "close --submit", desc = "Submit the input" }, + { on = "", run = "close --submit", desc = "Submit input" }, { on = "", run = "escape", desc = "Go back the normal mode, or cancel input" }, { on = "", run = "escape", desc = "Go back the normal mode, or cancel input" }, @@ -264,27 +264,23 @@ keymap = [ [confirm] keymap = [ - { on = [ "" ], run = "close", desc = "Cancel the confirm" }, - { on = [ "" ], run = "close --submit", desc = "Submit the confirm" }, + { on = "", run = "close", desc = "Cancel the confirm" }, + { on = "", run = "close", desc = "Cancel the confirm" }, + { on = "", run = "close", desc = "Cancel the confirm" }, + { on = "", 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 = "", run = "arrow -1", desc = "Move cursor up" }, + { on = "", run = "arrow 1", desc = "Move cursor down" }, - { on = [ "" ], run = "arrow -1", desc = "Move cursor up" }, - { on = [ "" ], run = "arrow 1", desc = "Move cursor down" }, - - { on = [ "" ], run = "arrow -5", desc = "Move cursor up 5 lines" }, - { on = [ "" ], run = "arrow 5", desc = "Move cursor down 5 lines" }, - - { on = [ "~" ], run = "help", desc = "Open help" } + # Help + { on = "~", run = "help", desc = "Open help" }, + { on = "", run = "help", desc = "Open help" }, ] [completion] diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 545550cd..9c1e99f3 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -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 ] diff --git a/yazi-config/src/popup/options.rs b/yazi-config/src/popup/options.rs index dbab4c67..d3cd6590 100644 --- a/yazi-config/src/popup/options.rs +++ b/yazi-config/src/popup/options.rs @@ -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::>().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::>().join("\n"), + content: urls.iter().map(ToString::to_string).collect::>().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::>().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) -> 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) -> 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) -> Self { let max_height = Self::max_height(items.len()); Self { diff --git a/yazi-core/src/manager/commands/quit.rs b/yazi-core/src/manager/commands/quit.rs index 7cec3022..fbbdb929 100644 --- a/yazi-core/src/manager/commands/quit.rs +++ b/yazi-core/src/manager/commands/quit.rs @@ -18,27 +18,21 @@ impl From for Opt { fn from(c: Cmd) -> Self { Self { no_cwd_file: c.bool("no-cwd-file") } } } -// async fn recv(result: &mut impl Future>) -> -// anyhow::Result { result -//} - impl Manager { pub fn quit(&self, opt: impl Into, 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 = 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)); } }); diff --git a/yazi-proxy/src/confirm.rs b/yazi-proxy/src/confirm.rs index 3605cea0..3e27da9e 100644 --- a/yazi-proxy/src/confirm.rs +++ b/yazi-proxy/src/confirm.rs @@ -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 { 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 } }