mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
refactor: quit command (#484)
This commit is contained in:
parent
c3a8bdc3b6
commit
d7d000c213
7 changed files with 45 additions and 34 deletions
|
|
@ -1,10 +1,9 @@
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
|
||||||
use tokio::fs;
|
|
||||||
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, 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};
|
||||||
|
|
||||||
|
|
@ -96,9 +95,9 @@ impl Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn quit_with_selected(selected: &[&File]) -> bool {
|
fn quit_with_selected(selected: &[&File]) -> bool {
|
||||||
let Some(p) = ARGS.chooser_file.clone() else {
|
if ARGS.chooser_file.is_none() {
|
||||||
return false;
|
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());
|
||||||
|
|
@ -106,10 +105,7 @@ impl Manager {
|
||||||
s
|
s
|
||||||
});
|
});
|
||||||
|
|
||||||
tokio::spawn(async move {
|
emit!(Quit(EventQuit { selected: Some(paths), ..Default::default() }));
|
||||||
fs::write(p, paths.as_encoded_bytes()).await.ok();
|
|
||||||
emit!(Quit(false));
|
|
||||||
});
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use yazi_config::popup::InputCfg;
|
use yazi_config::popup::InputCfg;
|
||||||
use yazi_shared::{emit, event::Exec};
|
use yazi_shared::{emit, event::{EventQuit, Exec}};
|
||||||
|
|
||||||
use crate::{input::Input, manager::Manager, tasks::Tasks};
|
use crate::{input::Input, manager::Manager, tasks::Tasks};
|
||||||
|
|
||||||
|
|
@ -16,11 +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 tasks = tasks.len();
|
let tasks = tasks.len();
|
||||||
if tasks == 0 {
|
if tasks == 0 {
|
||||||
emit!(Quit(opt.no_cwd_file));
|
emit!(Quit(opt));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -28,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(opt.no_cwd_file));
|
emit!(Quit(opt));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,8 @@ impl App {
|
||||||
Event::Key(key) => app.dispatch_key(key),
|
Event::Key(key) => app.dispatch_key(key),
|
||||||
Event::Resize => app.resize()?,
|
Event::Resize => app.resize()?,
|
||||||
Event::Paste(str) => app.dispatch_paste(str),
|
Event::Paste(str) => app.dispatch_paste(str),
|
||||||
Event::Quit(no_cwd_file) => {
|
Event::Quit(opt) => {
|
||||||
app.quit(no_cwd_file)?;
|
app.quit(opt)?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,33 @@
|
||||||
|
use std::ffi::OsString;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use yazi_config::ARGS;
|
use yazi_config::ARGS;
|
||||||
use yazi_shared::term::Term;
|
use yazi_shared::{event::EventQuit, term::Term};
|
||||||
|
|
||||||
use crate::app::App;
|
use crate::app::App;
|
||||||
|
|
||||||
pub struct Opt {
|
|
||||||
no_cwd_file: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<bool> for Opt {
|
|
||||||
fn from(no_cwd_file: bool) -> Self { Self { no_cwd_file } }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub(crate) fn quit(&mut self, opt: impl Into<Opt>) -> Result<()> {
|
pub(crate) fn quit(&mut self, opt: EventQuit) -> Result<()> {
|
||||||
let opt = opt.into() as Opt;
|
if !opt.no_cwd_file {
|
||||||
|
self.cwd_to_file();
|
||||||
if let Some(p) = ARGS.cwd_file.as_ref().filter(|_| !opt.no_cwd_file) {
|
}
|
||||||
let cwd = self.cx.manager.cwd().as_os_str();
|
if let Some(selected) = opt.selected {
|
||||||
std::fs::write(p, cwd.as_encoded_bytes()).ok();
|
self.selected_to_file(selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
Term::goodbye(|| false);
|
Term::goodbye(|| false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cwd_to_file(&self) {
|
||||||
|
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 selected_to_file(&self, selected: OsString) {
|
||||||
|
if let Some(p) = &ARGS.chooser_file {
|
||||||
|
std::fs::write(p, selected.as_encoded_bytes()).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,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(false)).is_err() {
|
if tx.send(Event::Quit(Default::default())).is_err() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
use std::ffi::OsString;
|
||||||
|
|
||||||
use crossterm::event::KeyEvent;
|
use crossterm::event::KeyEvent;
|
||||||
use tokio::sync::{mpsc::UnboundedSender, oneshot};
|
use tokio::sync::{mpsc::UnboundedSender, oneshot};
|
||||||
|
|
||||||
|
|
@ -13,7 +15,13 @@ pub enum Event {
|
||||||
Key(KeyEvent),
|
Key(KeyEvent),
|
||||||
Resize,
|
Resize,
|
||||||
Paste(String),
|
Paste(String),
|
||||||
Quit(bool), // no-cwd-file
|
Quit(EventQuit),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct EventQuit {
|
||||||
|
pub no_cwd_file: bool,
|
||||||
|
pub selected: Option<OsString>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
|
|
@ -31,13 +39,12 @@ impl Event {
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! emit {
|
macro_rules! emit {
|
||||||
(Quit($no_cwd_file:expr)) => {
|
(Quit($opt:expr)) => {
|
||||||
$crate::event::Event::Quit($no_cwd_file).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();
|
||||||
};
|
};
|
||||||
|
|
||||||
($event:ident) => {
|
($event:ident) => {
|
||||||
$crate::event::Event::$event.emit();
|
$crate::event::Event::$event.emit();
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ impl Term {
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
disable_raw_mode().ok();
|
disable_raw_mode().ok();
|
||||||
|
|
||||||
std::process::exit(f() as i32);
|
std::process::exit(f() as i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue