mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
Simplify the code
This commit is contained in:
parent
373d452451
commit
3fc1c86fcf
11 changed files with 50 additions and 73 deletions
|
|
@ -264,9 +264,13 @@ keymap = [
|
|||
|
||||
[confirm]
|
||||
keymap = [
|
||||
{ on = [ "<Esc>" ], run = "close", desc = "Cancel the confirm" },
|
||||
{ on = [ "n" ], run = "close", desc = "Cancel the confirm" },
|
||||
{ on = [ "y" ], run = "close --submit", desc = "Submit the confirm" },
|
||||
{ on = [ "<Esc>" ], 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 = [ "k" ], run = "arrow -1", desc = "Move cursor up" },
|
||||
{ on = [ "j" ], run = "arrow 1", desc = "Move cursor down" },
|
||||
|
|
|
|||
|
|
@ -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:"
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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::<Vec<_>>().join("\n"),
|
||||
content: targets.iter().map(|t| t.to_string()).collect::<Vec<_>>().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::<Vec<_>>().join("\n"),
|
||||
content: targets.iter().map(|t| t.to_string()).collect::<Vec<_>>().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<String>) -> 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),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,23 +12,21 @@ impl From<Cmd> 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<Opt>) {
|
||||
|
|
|
|||
|
|
@ -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!();
|
||||
|
|
|
|||
|
|
@ -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<Sender<Result<bool>>>,
|
||||
|
||||
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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::{future::Future, time::Duration};
|
||||
use std::time::Duration;
|
||||
|
||||
use tokio::{select, time};
|
||||
use yazi_config::popup::ConfirmCfg;
|
||||
|
|
|
|||
|
|
@ -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::<Vec<Line>>())
|
||||
Paragraph::new(confirm.content.lines().map(Line::from).collect::<Vec<Line>>())
|
||||
.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::<Vec<&str>>().len())
|
||||
.position(confirm.vertical_scroll);
|
||||
ScrollbarState::new(confirm.content.lines().collect::<Vec<&str>>().len())
|
||||
.position(confirm.offset);
|
||||
|
||||
Scrollbar::new(ScrollbarOrientation::VerticalRight).render(
|
||||
popup_layout[0].inner(&Margin { vertical: 1, horizontal: 0 }),
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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<bool> {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
emit!(Call(Cmd::new("show").with_any("tx", tx).with_any("cfg", cfg), Layer::Confirm));
|
||||
rx
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue