From 373d452451e0389e02b17d461f6390fe966aefb0 Mon Sep 17 00:00:00 2001 From: Chris Schade Date: Mon, 17 Jun 2024 09:24:59 +0200 Subject: [PATCH] Working through replacing quit/overwrite Input with Confirm popup --- yazi-config/preset/yazi.toml | 27 ++++++++------- yazi-config/src/popup/confirm.rs | 12 +++++++ yazi-config/src/popup/input.rs | 10 ------ yazi-config/src/popup/options.rs | 42 +++++++++++++----------- yazi-core/src/manager/commands/create.rs | 8 ++--- yazi-core/src/manager/commands/quit.rs | 26 ++++++++------- yazi-proxy/src/confirm.rs | 7 ++++ 7 files changed, 75 insertions(+), 57 deletions(-) diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 0440e8b8..68832364 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -167,27 +167,30 @@ shell_title = [ "Shell:", "Shell (block):" ] shell_origin = "top-center" shell_offset = [ 0, 2, 50, 3 ] -# overwrite -overwrite_title = "Overwrite an existing file? (y/N)" -overwrite_origin = "top-center" -overwrite_offset = [ 0, 2, 50, 3 ] - -# quit -quit_title = "{n} task{s} running, sure to quit? (y/N)" -quit_origin = "top-center" -quit_offset = [ 0, 2, 50, 3 ] - [confirm] # trash -trash_title = "Move {n} selected file{s} to trash? (y/N)" +trash_title = "Move {n} selected file{s} to trash?" trash_origin = "top-center" trash_offset = [ 0, 5, 70, 20 ] # delete -delete_title = "Delete {n} selected file{s} permanently? (y/N)" +delete_title = "Delete {n} selected file{s} permanently?" delete_origin = "top-center" delete_offset = [ 0, 5, 70, 20 ] +# overwrite +overwrite_title = "Are you sure?" +overwrite_message = "Overwrite existing file: {file}?" +overwrite_origin = "top-center" +overwrite_offset = [ 0, 2, 50, 20 ] + +# quit +quit_title = "Are you sure you want to quit?" +quit_message = "The following tasks are running:\n" +quit_origin = "top-center" +quit_offset = [ 0, 2, 50, 20 ] + + [select] open_title = "Open with:" open_origin = "hovered" diff --git a/yazi-config/src/popup/confirm.rs b/yazi-config/src/popup/confirm.rs index 682608e7..c199d715 100644 --- a/yazi-config/src/popup/confirm.rs +++ b/yazi-config/src/popup/confirm.rs @@ -15,6 +15,18 @@ pub struct Confirm { pub delete_title: String, pub delete_origin: Origin, pub delete_offset: Offset, + + // overwrite + pub overwrite_title: String, + pub overwrite_message: String, + pub overwrite_origin: Origin, + pub overwrite_offset: Offset, + + // quit + pub quit_title: String, + pub quit_message: String, + pub quit_origin: Origin, + pub quit_offset: Offset, } impl FromStr for Confirm { diff --git a/yazi-config/src/popup/input.rs b/yazi-config/src/popup/input.rs index 29bbd57e..633066b1 100644 --- a/yazi-config/src/popup/input.rs +++ b/yazi-config/src/popup/input.rs @@ -42,16 +42,6 @@ pub struct Input { pub shell_title: [String; 2], pub shell_origin: Origin, pub shell_offset: Offset, - - // overwrite - pub overwrite_title: String, - pub overwrite_origin: Origin, - pub overwrite_offset: Offset, - - // quit - pub quit_title: String, - pub quit_origin: Origin, - pub quit_offset: Offset, } impl Input { diff --git a/yazi-config/src/popup/options.rs b/yazi-config/src/popup/options.rs index d15e7bfa..34b7407f 100644 --- a/yazi-config/src/popup/options.rs +++ b/yazi-config/src/popup/options.rs @@ -94,25 +94,6 @@ impl InputCfg { } } - #[inline] - pub fn overwrite() -> Self { - Self { - title: INPUT.overwrite_title.to_owned(), - position: Position::new(INPUT.overwrite_origin, INPUT.overwrite_offset), - ..Default::default() - } - } - - #[inline] - pub fn quit(n: usize) -> Self { - let title = INPUT.quit_title.replace("{n}", &n.to_string()); - Self { - title: title.replace("{s}", if n > 1 { "s" } else { "" }), - position: Position::new(INPUT.quit_origin, INPUT.quit_offset), - ..Default::default() - } - } - #[inline] pub fn with_value(mut self, value: impl Into) -> Self { self.value = value.into(); @@ -144,6 +125,29 @@ impl ConfirmCfg { message: targets.iter().map(|t| t.to_string()).collect::>().join("\n"), } } + + #[inline] + pub fn overwrite(file: &str) -> Self { + Self { + title: CONFIRM.overwrite_title.to_owned(), + message: CONFIRM.overwrite_message.replace("{file}", file), + position: Position::new(CONFIRM.overwrite_origin, CONFIRM.overwrite_offset), + } + } + + #[inline] + pub fn quit(ongoing_task_names: Vec) -> Self { + let n = ongoing_task_names.len(); + let mut message = CONFIRM.quit_message.replace("{n}", &n.to_string()); + + message.push_str(&ongoing_task_names.join("\n")); + + Self { + title: CONFIRM.quit_title.to_owned(), + message, + position: Position::new(CONFIRM.quit_origin, CONFIRM.quit_offset), + } + } } impl SelectCfg { diff --git a/yazi-core/src/manager/commands/create.rs b/yazi-core/src/manager/commands/create.rs index a9e223ce..16fc114a 100644 --- a/yazi-core/src/manager/commands/create.rs +++ b/yazi-core/src/manager/commands/create.rs @@ -2,8 +2,8 @@ use std::collections::HashMap; use anyhow::Result; use tokio::fs; -use yazi_config::popup::InputCfg; -use yazi_proxy::{InputProxy, TabProxy, WATCHER}; +use yazi_config::popup::{ConfirmCfg, InputCfg}; +use yazi_proxy::{ConfirmProxy, InputProxy, TabProxy, WATCHER}; use yazi_shared::{event::Cmd, fs::{maybe_exists, ok_or_not_found, symlink_realpath, File, FilesOp, Url}}; use crate::manager::Manager; @@ -32,8 +32,8 @@ impl Manager { let new = cwd.join(&name); if !opt.force && maybe_exists(&new).await { - match InputProxy::show(InputCfg::overwrite()).recv().await { - Some(Ok(c)) if c == "y" || c == "Y" => (), + match ConfirmProxy::show(ConfirmCfg::overwrite(&new.to_string())).await { + Ok(c) if c => (), _ => return Ok(()), } } diff --git a/yazi-core/src/manager/commands/quit.rs b/yazi-core/src/manager/commands/quit.rs index d2691f2f..221b9837 100644 --- a/yazi-core/src/manager/commands/quit.rs +++ b/yazi-core/src/manager/commands/quit.rs @@ -1,8 +1,8 @@ -use std::time::Duration; +use std::{future::Future, time::Duration}; use tokio::{select, time}; -use yazi_config::popup::InputCfg; -use yazi_proxy::InputProxy; +use yazi_config::popup::ConfirmCfg; +use yazi_proxy::ConfirmProxy; use yazi_shared::{emit, event::{Cmd, EventQuit}}; use crate::{manager::Manager, tasks::Tasks}; @@ -18,6 +18,10 @@ 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() }; @@ -32,7 +36,9 @@ impl Manager { tokio::spawn(async move { let mut i = 0; - let mut result = InputProxy::show(InputCfg::quit(left)); + let result = ConfirmProxy::show(ConfirmCfg::quit( + ongoing.lock().values().map(|t| t.name.clone()).collect(), + )); loop { select! { _ = time::sleep(Duration::from_millis(100)) => { @@ -43,17 +49,13 @@ impl Manager { return; } } - choice = result.recv() => { - if matches!(choice, Some(Ok(s)) if s == "y" || s == "Y") { - emit!(Quit(opt)); - } - return; - } } } - if let Some(Ok(choice)) = result.recv().await { - if choice == "y" || choice == "Y" { + // recv(&mut result); + + if let Ok(choice) = result.await { + if choice { emit!(Quit(opt)); } } diff --git a/yazi-proxy/src/confirm.rs b/yazi-proxy/src/confirm.rs index eaf2925e..068e9f01 100644 --- a/yazi-proxy/src/confirm.rs +++ b/yazi-proxy/src/confirm.rs @@ -11,4 +11,11 @@ impl ConfirmProxy { emit!(Call(Cmd::new("show").with_any("tx", tx).with_any("cfg", cfg), Layer::Confirm)); rx.await? } + + #[inline] + pub fn show_sync(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 + } }