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
b3aead3466
commit
b6df93f797
5 changed files with 30 additions and 41 deletions
|
|
@ -3,7 +3,7 @@ use std::ffi::OsString;
|
|||
use tracing::error;
|
||||
use yazi_config::{popup::SelectCfg, ARGS, OPEN};
|
||||
use yazi_plugin::isolate;
|
||||
use yazi_shared::{emit, event::{Exec, QuitAction}, fs::{File, Url}, Layer, MIME_DIR};
|
||||
use yazi_shared::{emit, event::{EventQuit, Exec}, fs::{File, Url}, Layer, MIME_DIR};
|
||||
|
||||
use crate::{manager::Manager, select::Select, tasks::Tasks};
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ impl Manager {
|
|||
let selected = self.selected();
|
||||
if selected.is_empty() {
|
||||
return;
|
||||
} else if Self::quit_with_chooser(&selected) {
|
||||
} else if Self::quit_with_selected(&selected) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -94,8 +94,10 @@ impl Manager {
|
|||
});
|
||||
}
|
||||
|
||||
fn quit_with_chooser(selected: &[&File]) -> bool {
|
||||
let mut quit_actions = vec![QuitAction::CwdToFile];
|
||||
fn quit_with_selected(selected: &[&File]) -> bool {
|
||||
if ARGS.chooser_file.is_none() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let paths = selected.iter().fold(OsString::new(), |mut s, &f| {
|
||||
s.push(f.url.as_os_str());
|
||||
|
|
@ -103,18 +105,7 @@ impl Manager {
|
|||
s
|
||||
});
|
||||
|
||||
if ARGS.chooser_file.is_some() {
|
||||
quit_actions.push(QuitAction::SelectToFile(paths.clone()));
|
||||
};
|
||||
|
||||
if quit_actions.len() > 1 {
|
||||
tokio::spawn(async move {
|
||||
emit!(Quit(quit_actions));
|
||||
});
|
||||
|
||||
emit!(Quit(EventQuit { selected: Some(paths), ..Default::default() }));
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use yazi_config::popup::InputCfg;
|
||||
use yazi_shared::{emit, event::{Exec, QuitAction}};
|
||||
use yazi_shared::{emit, event::{EventQuit, Exec}};
|
||||
|
||||
use crate::{input::Input, manager::Manager, tasks::Tasks};
|
||||
|
||||
|
|
@ -16,12 +16,11 @@ impl From<&Exec> for Opt {
|
|||
|
||||
impl Manager {
|
||||
pub fn quit(&self, opt: impl Into<Opt>, tasks: &Tasks) {
|
||||
let opt = opt.into() as Opt;
|
||||
let actions = if opt.no_cwd_file { vec![] } else { vec![QuitAction::CwdToFile] };
|
||||
let opt = EventQuit { no_cwd_file: opt.into().no_cwd_file, ..Default::default() };
|
||||
|
||||
let tasks = tasks.len();
|
||||
if tasks == 0 {
|
||||
emit!(Quit(actions));
|
||||
emit!(Quit(opt));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +28,7 @@ impl Manager {
|
|||
let mut result = Input::_show(InputCfg::quit(tasks));
|
||||
if let Some(Ok(choice)) = result.recv().await {
|
||||
if choice == "y" || choice == "Y" {
|
||||
emit!(Quit(actions));
|
||||
emit!(Quit(opt));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,31 +2,31 @@ use std::ffi::OsString;
|
|||
|
||||
use anyhow::Result;
|
||||
use yazi_config::ARGS;
|
||||
use yazi_shared::{event::QuitAction, term::Term};
|
||||
use yazi_shared::{event::EventQuit, term::Term};
|
||||
|
||||
use crate::app::App;
|
||||
|
||||
impl App {
|
||||
pub(crate) fn quit(&mut self, actions: Vec<QuitAction>) -> Result<()> {
|
||||
for action in actions {
|
||||
match action {
|
||||
QuitAction::CwdToFile => self.cwd_to_file(),
|
||||
QuitAction::SelectToFile(selected) => self.select_to_file(selected),
|
||||
pub(crate) fn quit(&mut self, opt: EventQuit) -> Result<()> {
|
||||
if !opt.no_cwd_file {
|
||||
self.cwd_to_file();
|
||||
}
|
||||
if let Some(selected) = opt.selected {
|
||||
self.selected_to_file(selected);
|
||||
}
|
||||
|
||||
Term::goodbye(|| false);
|
||||
}
|
||||
|
||||
fn cwd_to_file(&self) {
|
||||
if let Some(p) = ARGS.cwd_file.as_ref() {
|
||||
if let Some(p) = &ARGS.cwd_file {
|
||||
let cwd = self.cx.manager.cwd().as_os_str();
|
||||
std::fs::write(p, cwd.as_encoded_bytes()).ok();
|
||||
}
|
||||
}
|
||||
|
||||
fn select_to_file(&self, selected: OsString) {
|
||||
if let Some(p) = ARGS.chooser_file.clone() {
|
||||
fn selected_to_file(&self, selected: OsString) {
|
||||
if let Some(p) = &ARGS.chooser_file {
|
||||
std::fs::write(p, selected.as_encoded_bytes()).ok();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,7 +48,6 @@ impl Signals {
|
|||
fn spawn_system_task(&self) -> Result<JoinHandle<()>> {
|
||||
use libc::{SIGCONT, SIGHUP, SIGINT, SIGQUIT, SIGTERM};
|
||||
use yazi_scheduler::Scheduler;
|
||||
use yazi_shared::event::QuitAction;
|
||||
|
||||
let tx = self.tx.clone();
|
||||
let mut signals = signal_hook_tokio::Signals::new([
|
||||
|
|
@ -62,7 +61,7 @@ impl Signals {
|
|||
while let Some(signal) = signals.next().await {
|
||||
match signal {
|
||||
SIGHUP | SIGTERM | SIGQUIT | SIGINT => {
|
||||
if tx.send(Event::Quit(vec![QuitAction::CwdToFile])).is_err() {
|
||||
if tx.send(Event::Quit(Default::default())).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,13 +15,13 @@ pub enum Event {
|
|||
Key(KeyEvent),
|
||||
Resize(u16, u16),
|
||||
Paste(String),
|
||||
Quit(Vec<QuitAction>),
|
||||
Quit(EventQuit),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum QuitAction {
|
||||
CwdToFile,
|
||||
SelectToFile(OsString),
|
||||
#[derive(Debug, Default)]
|
||||
pub struct EventQuit {
|
||||
pub no_cwd_file: bool,
|
||||
pub selected: Option<OsString>,
|
||||
}
|
||||
|
||||
impl Event {
|
||||
|
|
@ -39,8 +39,8 @@ impl Event {
|
|||
|
||||
#[macro_export]
|
||||
macro_rules! emit {
|
||||
(Quit($actions:expr)) => {
|
||||
$crate::event::Event::Quit($actions).emit();
|
||||
(Quit($opt:expr)) => {
|
||||
$crate::event::Event::Quit($opt).emit();
|
||||
};
|
||||
(Call($exec:expr, $layer:expr)) => {
|
||||
$crate::event::Event::Call($exec, $layer).emit();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue