From 3fc1c86fcf43610fb9ba06bc31cab774dcdf5485 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 19 Jun 2024 17:04:50 +0800 Subject: [PATCH] Simplify the code --- yazi-config/preset/keymap.toml | 10 ++++++--- yazi-config/preset/yazi.toml | 21 +++++++++---------- yazi-config/src/popup/confirm.rs | 5 ++--- yazi-config/src/popup/options.rs | 14 ++++++------- yazi-core/src/confirm/commands/arrow.rs | 16 +++++++------- yazi-core/src/confirm/commands/show.rs | 6 ++++-- yazi-core/src/confirm/confirm.rs | 28 +++++-------------------- yazi-core/src/manager/commands/quit.rs | 2 +- yazi-fm/src/confirm/confirm.rs | 12 +++++------ yazi-plugin/src/utils/layer.rs | 2 +- yazi-proxy/src/confirm.rs | 7 ------- 11 files changed, 50 insertions(+), 73 deletions(-) diff --git a/yazi-config/preset/keymap.toml b/yazi-config/preset/keymap.toml index 72cfddf0..9d9d484b 100644 --- a/yazi-config/preset/keymap.toml +++ b/yazi-config/preset/keymap.toml @@ -264,9 +264,13 @@ keymap = [ [confirm] keymap = [ - { on = [ "" ], run = "close", desc = "Cancel the confirm" }, - { on = [ "n" ], run = "close", desc = "Cancel the confirm" }, - { on = [ "y" ], run = "close --submit", desc = "Submit 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 = [ "k" ], run = "arrow -1", desc = "Move cursor up" }, { on = [ "j" ], run = "arrow 1", desc = "Move cursor down" }, diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index 68832364..97a728a2 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -169,27 +169,26 @@ shell_offset = [ 0, 2, 50, 3 ] [confirm] # trash -trash_title = "Move {n} selected file{s} to trash?" +trash_title = "Trash {n} selected file{s}?" trash_origin = "top-center" trash_offset = [ 0, 5, 70, 20 ] # delete -delete_title = "Delete {n} selected file{s} permanently?" +delete_title = "Permanently delete {n} selected file{s}?" 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 ] +overwrite_title = "Are you sure?" +overwrite_content = "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 ] - +quit_title = "Are you sure you want to quit?" +quit_content = "The following tasks are running:\n" +quit_origin = "top-center" +quit_offset = [ 0, 2, 50, 20 ] [select] open_title = "Open with:" diff --git a/yazi-config/src/popup/confirm.rs b/yazi-config/src/popup/confirm.rs index c199d715..b55686f7 100644 --- a/yazi-config/src/popup/confirm.rs +++ b/yazi-config/src/popup/confirm.rs @@ -18,13 +18,13 @@ pub struct Confirm { // overwrite pub overwrite_title: String, - pub overwrite_message: String, + pub overwrite_content: String, pub overwrite_origin: Origin, pub overwrite_offset: Offset, // quit pub quit_title: String, - pub quit_message: String, + pub quit_content: String, pub quit_origin: Origin, pub quit_offset: Offset, } @@ -43,6 +43,5 @@ impl FromStr for Confirm { } impl Confirm { - #[inline] pub const fn border(&self) -> u16 { 2 } } diff --git a/yazi-config/src/popup/options.rs b/yazi-config/src/popup/options.rs index 34b7407f..51dc2353 100644 --- a/yazi-config/src/popup/options.rs +++ b/yazi-config/src/popup/options.rs @@ -22,7 +22,7 @@ pub struct SelectCfg { #[derive(Default)] pub struct ConfirmCfg { pub title: String, - pub message: String, + pub content: String, pub position: Position, } @@ -113,7 +113,7 @@ impl ConfirmCfg { Self { title: CONFIRM.delete_title.replace("{n}", &targets.len().to_string()), position: Position::new(CONFIRM.delete_origin, CONFIRM.delete_offset), - message: targets.iter().map(|t| t.to_string()).collect::>().join("\n"), + content: targets.iter().map(|t| t.to_string()).collect::>().join("\n"), } } @@ -122,7 +122,7 @@ impl ConfirmCfg { Self { title: CONFIRM.trash_title.replace("{n}", &targets.len().to_string()), position: Position::new(CONFIRM.trash_origin, CONFIRM.trash_offset), - message: targets.iter().map(|t| t.to_string()).collect::>().join("\n"), + content: targets.iter().map(|t| t.to_string()).collect::>().join("\n"), } } @@ -130,7 +130,7 @@ impl ConfirmCfg { pub fn overwrite(file: &str) -> Self { Self { title: CONFIRM.overwrite_title.to_owned(), - message: CONFIRM.overwrite_message.replace("{file}", file), + content: CONFIRM.overwrite_content.replace("{file}", file), position: Position::new(CONFIRM.overwrite_origin, CONFIRM.overwrite_offset), } } @@ -138,13 +138,13 @@ impl ConfirmCfg { #[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()); + let mut message = CONFIRM.quit_content.replace("{n}", &n.to_string()); message.push_str(&ongoing_task_names.join("\n")); Self { - title: CONFIRM.quit_title.to_owned(), - message, + title: CONFIRM.quit_title.to_owned(), + content: message, position: Position::new(CONFIRM.quit_origin, CONFIRM.quit_offset), } } diff --git a/yazi-core/src/confirm/commands/arrow.rs b/yazi-core/src/confirm/commands/arrow.rs index dcc4d801..23a913f2 100644 --- a/yazi-core/src/confirm/commands/arrow.rs +++ b/yazi-core/src/confirm/commands/arrow.rs @@ -12,23 +12,21 @@ impl From for Opt { impl Confirm { fn next(&mut self, step: usize) { - let len = self.message_num_lines(); - if len == 0 { + if self.lines == 0 { return; } - let old = self.vertical_scroll; - self.vertical_scroll = (self.vertical_scroll + step).min(len - 1); + let old = self.offset; + self.offset = (self.offset + step).min(self.lines - 1); - render!(old != self.vertical_scroll); + render!(old != self.offset); } fn prev(&mut self, step: usize) { - let old = self.vertical_scroll; + let old = self.offset; + self.offset -= step.min(self.offset); - self.vertical_scroll -= step.min(self.vertical_scroll); - - render!(old != self.vertical_scroll); + render!(old != self.offset); } pub fn arrow(&mut self, opt: impl Into) { diff --git a/yazi-core/src/confirm/commands/show.rs b/yazi-core/src/confirm/commands/show.rs index 3f124271..36dcb036 100644 --- a/yazi-core/src/confirm/commands/show.rs +++ b/yazi-core/src/confirm/commands/show.rs @@ -25,10 +25,12 @@ impl Confirm { self.close(false); self.title = opt.cfg.title; - self.set_message(&opt.cfg.message); - self.vertical_scroll = 0; + self.content = opt.cfg.content; + self.lines = self.content.lines().count(); + self.offset = 0; self.position = opt.cfg.position; + self.callback = Some(opt.tx); self.visible = true; render!(); diff --git a/yazi-core/src/confirm/confirm.rs b/yazi-core/src/confirm/confirm.rs index 22b3ab11..43b1774b 100644 --- a/yazi-core/src/confirm/confirm.rs +++ b/yazi-core/src/confirm/confirm.rs @@ -4,31 +4,13 @@ use yazi_config::popup::Position; #[derive(Default)] pub struct Confirm { - pub(super) title: String, - message: String, - message_num_lines: usize, + pub title: String, + pub content: String, + pub lines: usize, + pub offset: usize, pub position: Position, - pub vertical_scroll: usize, pub(super) callback: Option>>, - - pub visible: bool, -} - -impl Confirm { - #[inline] - pub fn set_message(&mut self, message: &str) { - self.message = message.to_string(); - self.message_num_lines = self.message.split('\n').count(); - } - - #[inline] - pub fn message(&self) -> String { self.message.clone() } - - #[inline] - pub fn message_num_lines(&self) -> usize { self.message_num_lines } - - #[inline] - pub fn title(&self) -> String { self.title.clone() } + pub visible: bool, } diff --git a/yazi-core/src/manager/commands/quit.rs b/yazi-core/src/manager/commands/quit.rs index 221b9837..c05a691e 100644 --- a/yazi-core/src/manager/commands/quit.rs +++ b/yazi-core/src/manager/commands/quit.rs @@ -1,4 +1,4 @@ -use std::{future::Future, time::Duration}; +use std::time::Duration; use tokio::{select, time}; use yazi_config::popup::ConfirmCfg; diff --git a/yazi-fm/src/confirm/confirm.rs b/yazi-fm/src/confirm/confirm.rs index ecfdfdae..5cbd9d17 100644 --- a/yazi-fm/src/confirm/confirm.rs +++ b/yazi-fm/src/confirm/confirm.rs @@ -20,7 +20,7 @@ impl<'a> Widget for Confirm<'a> { Block::bordered() .border_type(BorderType::Rounded) .border_style(THEME.input.border) - .title(Line::styled(&confirm.title(), THEME.input.title)) + .title(Line::styled(&confirm.title, THEME.input.title)) .render(area, buf); let popup_layout = @@ -39,17 +39,17 @@ impl<'a> Widget for Confirm<'a> { .vertical_margin(1) .split(popup_layout[1]); - Paragraph::new(confirm.message().split('\n').map(Line::from).collect::>()) + Paragraph::new(confirm.content.lines().map(Line::from).collect::>()) .block(Block::bordered().border_type(BorderType::Rounded).border_style(THEME.input.border)) - .scroll((confirm.vertical_scroll as u16, 0)) + .scroll((confirm.offset as u16, 0)) .wrap(Wrap { trim: false }) .render(popup_layout[0], buf); const BORDER_SIZE: usize = 2; - if confirm.message_num_lines() > popup_layout[0].as_size().height as usize - BORDER_SIZE { + if confirm.lines > popup_layout[0].as_size().height as usize - BORDER_SIZE { let mut scrollbar_state = - ScrollbarState::new(confirm.message().split('\n').collect::>().len()) - .position(confirm.vertical_scroll); + ScrollbarState::new(confirm.content.lines().collect::>().len()) + .position(confirm.offset); Scrollbar::new(ScrollbarOrientation::VerticalRight).render( popup_layout[0].inner(&Margin { vertical: 1, horizontal: 0 }), diff --git a/yazi-plugin/src/utils/layer.rs b/yazi-plugin/src/utils/layer.rs index 3ba0944e..66945c3b 100644 --- a/yazi-plugin/src/utils/layer.rs +++ b/yazi-plugin/src/utils/layer.rs @@ -91,7 +91,7 @@ impl Utils { lua.create_async_function(|lua, t: Table| async move { let result = ConfirmProxy::show(ConfirmCfg { title: t.raw_get("title")?, - message: t.raw_get("message")?, + content: t.raw_get("content")?, position: Position::try_from(t.raw_get::<_, Table>("position")?)?.into(), }); diff --git a/yazi-proxy/src/confirm.rs b/yazi-proxy/src/confirm.rs index 068e9f01..eaf2925e 100644 --- a/yazi-proxy/src/confirm.rs +++ b/yazi-proxy/src/confirm.rs @@ -11,11 +11,4 @@ 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 - } }