fix: register and ignore the SIGINT signal explicitly

This commit is contained in:
sxyazi 2024-11-12 08:24:28 +08:00
parent fd8871d878
commit 4ff4038b41
No known key found for this signature in database
2 changed files with 26 additions and 22 deletions

View file

@ -10,8 +10,9 @@ use yazi_shared::{RoCell, env_exists};
use crate::{Adapter, Dimension}; use crate::{Adapter, Dimension};
#[allow(clippy::type_complexity)] type Cmd = Option<(PathBuf, Rect)>;
static DEMON: RoCell<Option<UnboundedSender<Option<(PathBuf, Rect)>>>> = RoCell::new();
static DEMON: RoCell<Option<UnboundedSender<Cmd>>> = RoCell::new();
pub(super) struct Ueberzug; pub(super) struct Ueberzug;
@ -34,7 +35,7 @@ impl Ueberzug {
child = Self::create_demon(adapter).ok(); child = Self::create_demon(adapter).ok();
} }
if let Some(c) = &mut child { if let Some(c) = &mut child {
Self::send_command(c, cmd).await.ok(); Self::send_command(adapter, c, cmd).await.ok();
} }
} }
}); });
@ -108,30 +109,28 @@ impl Ueberzug {
rect rect
} }
async fn send_command(child: &mut Child, cmd: Option<(PathBuf, Rect)>) -> Result<()> { async fn send_command(adapter: Adapter, child: &mut Child, cmd: Cmd) -> Result<()> {
let stdin = child.stdin.as_mut().unwrap(); let s = if let Some((path, rect)) = cmd {
if let Some((path, rect)) = cmd {
debug!("ueberzugpp rect before adjustment: {:?}", rect); debug!("ueberzugpp rect before adjustment: {:?}", rect);
let rect = Self::adjust_rect(rect); let rect = Self::adjust_rect(rect);
debug!("ueberzugpp rect after adjustment: {:?}", rect); debug!("ueberzugpp rect after adjustment: {:?}", rect);
let s = format!( format!(
r#"{{"action":"add","identifier":"yazi","x":{},"y":{},"max_width":{},"max_height":{},"path":"{}"}}{}"#, r#"{{"action":"add","identifier":"yazi","x":{},"y":{},"max_width":{},"max_height":{},"path":"{}"}}{}"#,
rect.x, rect.x,
rect.y, rect.y,
rect.width, rect.width,
rect.height, rect.height,
path.to_string_lossy(), path.to_string_lossy(),
"\n" '\n'
); )
debug!("ueberzugpp command: {}", s);
stdin.write_all(s.as_bytes()).await?;
} else { } else {
debug!("ueberzugpp command: remove"); format!(r#"{{"action":"remove","identifier":"yazi"}}{}"#, '\n')
stdin };
.write_all(format!(r#"{{"action":"remove","identifier":"yazi"}}{}"#, "\n").as_bytes())
.await?; debug!("`ueberzugpp layer -so {adapter}` command: {s}");
} child.stdin.as_mut().unwrap().write_all(s.as_bytes()).await?;
Ok(()) Ok(())
} }
} }

View file

@ -26,12 +26,13 @@ impl Signals {
#[cfg(unix)] #[cfg(unix)]
#[inline] #[inline]
fn handle_sys(n: libc::c_int) -> bool { fn handle_sys(n: libc::c_int) -> bool {
use libc::{SIGCONT, SIGHUP, SIGQUIT, SIGSTOP, SIGTERM, SIGTSTP}; use libc::{SIGCONT, SIGHUP, SIGINT, SIGQUIT, SIGSTOP, SIGTERM, SIGTSTP};
use tracing::error; use tracing::error;
use yazi_proxy::{AppProxy, HIDER}; use yazi_proxy::{AppProxy, HIDER};
match n { match n {
SIGHUP | SIGTERM | SIGQUIT => { SIGINT => { /* ignored */ }
SIGQUIT | SIGHUP | SIGTERM => {
Event::Quit(Default::default()).emit(); Event::Quit(Default::default()).emit();
return false; return false;
} }
@ -73,13 +74,17 @@ impl Signals {
fn spawn(mut rx: mpsc::UnboundedReceiver<(bool, Option<oneshot::Sender<()>>)>) -> Result<()> { fn spawn(mut rx: mpsc::UnboundedReceiver<(bool, Option<oneshot::Sender<()>>)>) -> Result<()> {
#[cfg(unix)] #[cfg(unix)]
use libc::{SIGCONT, SIGHUP, SIGQUIT, SIGTERM, SIGTSTP}; use libc::{SIGCONT, SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGTSTP};
#[cfg(unix)] #[cfg(unix)]
let mut sys = signal_hook_tokio::Signals::new([ let mut sys = signal_hook_tokio::Signals::new([
// Terminating signals // Interrupt signals (Ctrl-C, Ctrl-\)
SIGHUP, SIGTERM, SIGQUIT, // SIGINT, SIGQUIT, //
// Job control signals // Hangup signal (Terminal closed)
SIGHUP, //
// Termination signal (kill)
SIGTERM, //
// Job control signals (Ctrl-Z, fg/bg)
SIGTSTP, SIGCONT, SIGTSTP, SIGCONT,
])?; ])?;
#[cfg(windows)] #[cfg(windows)]