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