Skip to content

Commit 62ac224

Browse files
committed
fix: disable passthrough when the user launches Yazi in Neovim inside tmux (#2014)
1 parent b8b3ab9 commit 62ac224

9 files changed

Lines changed: 66 additions & 30 deletions

File tree

scripts/publish.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
cargo publish -p yazi-macro
2+
sleep 30
3+
cargo publish -p yazi-codegen
4+
sleep 30
15
cargo publish -p yazi-shared
26
sleep 30
7+
cargo publish -p yazi-fs
8+
sleep 30
39
cargo publish -p yazi-config
410
sleep 30
511
cargo publish -p yazi-proxy
612
sleep 30
7-
cargo publish -p yazi-fs
8-
sleep 30
913
cargo publish -p yazi-adapter
1014
sleep 30
1115
cargo publish -p yazi-boot

yazi-adapter/src/adapter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl Adapter {
9292
protocols.retain(|p| *p == Self::Iip);
9393
if env_exists("ZELLIJ_SESSION_NAME") {
9494
protocols.retain(|p| *p == Self::Sixel);
95-
} else if *TMUX {
95+
} else if *TMUX != 0 {
9696
protocols.retain(|p| *p != Self::KgpOld);
9797
}
9898
if let Some(p) = protocols.first() {

yazi-adapter/src/brand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use tracing::warn;
22
use yazi_shared::env_exists;
33

4-
use crate::Mux;
4+
use crate::{Mux, NVIM};
55

66
#[derive(Clone, Copy, Debug)]
77
pub enum Brand {
@@ -27,7 +27,7 @@ impl Brand {
2727
pub fn from_env() -> Option<Self> {
2828
use Brand as B;
2929

30-
if env_exists("NVIM_LOG_FILE") && env_exists("NVIM") {
30+
if *NVIM {
3131
return Some(Self::Neovim);
3232
}
3333

yazi-adapter/src/emulator.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Emulator {
8282

8383
// I really don't want to add this,
8484
// But tmux and ConPTY sometimes cause the cursor position to get out of sync.
85-
if *TMUX || cfg!(windows) {
85+
if *TMUX != 0 || cfg!(windows) {
8686
execute!(buf, SavePosition, MoveTo(x, y), Show)?;
8787
execute!(buf, MoveTo(x, y), Show)?;
8888
execute!(buf, MoveTo(x, y), Show)?;
@@ -92,7 +92,7 @@ impl Emulator {
9292
}
9393

9494
let result = cb(&mut buf);
95-
if *TMUX || cfg!(windows) {
95+
if *TMUX != 0 || cfg!(windows) {
9696
queue!(buf, Hide, RestorePosition)?;
9797
} else {
9898
queue!(buf, RestorePosition)?;
@@ -136,9 +136,9 @@ impl Emulator {
136136

137137
execute!(
138138
LineWriter::new(stderr()),
139-
Print("\x1b[16t"), // Request cell size
140-
Print("\x1b]11;?\x07"), // Request background color
141-
Print(Mux::csi("\x1b[0c")), // Request device attributes
139+
Print("\x1b[16t"), // Request cell size
140+
Print("\x1b]11;?\x07"), // Request background color
141+
Print("\x1b[0c"), // Request device attributes
142142
)?;
143143

144144
let resp = futures::executor::block_on(Self::read_until_da1());

yazi-adapter/src/lib.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,30 @@ pub static ADAPTOR: RoCell<Adapter> = RoCell::new();
1313
static SHOWN: SyncCell<Option<ratatui::layout::Rect>> = SyncCell::new(None);
1414

1515
// Tmux support
16-
pub static TMUX: RoCell<bool> = RoCell::new();
16+
pub static TMUX: RoCell<u8> = RoCell::new();
1717
static ESCAPE: RoCell<&'static str> = RoCell::new();
1818
static START: RoCell<&'static str> = RoCell::new();
1919
static CLOSE: RoCell<&'static str> = RoCell::new();
2020

2121
// WSL support
2222
pub static WSL: RoCell<bool> = RoCell::new();
2323

24+
// Neovim support
25+
pub static NVIM: RoCell<bool> = RoCell::new();
26+
2427
pub fn init() -> anyhow::Result<()> {
2528
// Tmux support
26-
TMUX.init(env_exists("TMUX_PANE") && env_exists("TMUX"));
27-
ESCAPE.init(if *TMUX { "\x1b\x1b" } else { "\x1b" });
28-
START.init(if *TMUX { "\x1bPtmux;\x1b\x1b" } else { "\x1b" });
29-
CLOSE.init(if *TMUX { "\x1b\\" } else { "" });
30-
31-
if *TMUX {
32-
_ = std::process::Command::new("tmux")
33-
.args(["set", "-p", "allow-passthrough", "all"])
34-
.stdin(std::process::Stdio::null())
35-
.stdout(std::process::Stdio::null())
36-
.stderr(std::process::Stdio::null())
37-
.status();
38-
}
29+
TMUX.init(Mux::tmux_passthrough());
30+
ESCAPE.init(if *TMUX == 2 { "\x1b\x1b" } else { "\x1b" });
31+
START.init(if *TMUX == 2 { "\x1bPtmux;\x1b\x1b" } else { "\x1b" });
32+
CLOSE.init(if *TMUX == 2 { "\x1b\\" } else { "" });
3933

4034
// WSL support
4135
WSL.init(in_wsl());
4236

37+
// Neovim support
38+
NVIM.init(env_exists("NVIM_LOG_FILE") && env_exists("NVIM"));
39+
4340
EMULATOR.init(Emulator::detect());
4441
yazi_config::init_flavor(EMULATOR.light)?;
4542

yazi-adapter/src/mux.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
use crate::{CLOSE, ESCAPE, START, TMUX};
1+
use tracing::error;
2+
use yazi_shared::env_exists;
3+
4+
use crate::{CLOSE, ESCAPE, NVIM, START, TMUX};
25

36
pub struct Mux;
47

58
impl Mux {
69
pub fn csi(s: &str) -> std::borrow::Cow<str> {
7-
if *TMUX {
10+
if *TMUX == 2 && !*NVIM {
811
std::borrow::Cow::Owned(format!(
912
"{}{}{}",
1013
*START,
@@ -16,6 +19,34 @@ impl Mux {
1619
}
1720
}
1821

22+
pub fn tmux_passthrough() -> u8 {
23+
if !env_exists("TMUX_PANE") || !env_exists("TMUX") {
24+
return 0;
25+
}
26+
27+
let child = std::process::Command::new("tmux")
28+
.args(["set", "-p", "allow-passthrough", "all"])
29+
.stdin(std::process::Stdio::null())
30+
.stdout(std::process::Stdio::null())
31+
.stderr(std::process::Stdio::piped())
32+
.spawn();
33+
34+
match child.and_then(|c| c.wait_with_output()) {
35+
Ok(output) if output.status.success() => return 2,
36+
Ok(output) => {
37+
error!(
38+
"Running `tmux set -p allow-passthrough all` failed: {:?}, {}",
39+
output.status,
40+
String::from_utf8_lossy(&output.stderr)
41+
);
42+
}
43+
Err(err) => {
44+
error!("Failed to spawn `tmux set -p allow-passthrough all`: {err}");
45+
}
46+
}
47+
1
48+
}
49+
1950
pub fn tmux_sixel_flag() -> &'static str {
2051
let stdout = std::process::Command::new("tmux")
2152
.args(["-LwU0dju1is5", "-f/dev/null", "start", ";", "display", "-p", "#{sixel_support}"])
@@ -33,7 +64,7 @@ impl Mux {
3364

3465
pub(super) fn term_program() -> (Option<String>, Option<String>) {
3566
let (mut term, mut program) = (None, None);
36-
if !*TMUX {
67+
if *TMUX == 0 {
3768
return (term, program);
3869
}
3970
let Ok(output) = std::process::Command::new("tmux").arg("show-environment").output() else {

yazi-boot/src/actions/debug.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ impl Actions {
4141
writeln!(s, "\nWSL")?;
4242
writeln!(s, " WSL: {:?}", *yazi_adapter::WSL)?;
4343

44+
writeln!(s, "\nNVIM")?;
45+
writeln!(s, " NVIM : {:?}", *yazi_adapter::NVIM)?;
46+
writeln!(s, " Neovim version: {}", Self::process_output("nvim", "--version"))?;
47+
4448
writeln!(s, "\nVariables")?;
4549
writeln!(s, " SHELL : {:?}", env::var_os("SHELL"))?;
4650
writeln!(s, " EDITOR : {:?}", env::var_os("EDITOR"))?;
@@ -68,7 +72,7 @@ impl Actions {
6872
)?;
6973

7074
writeln!(s, "\nMultiplexers")?;
71-
writeln!(s, " TMUX : {:?}", *yazi_adapter::TMUX)?;
75+
writeln!(s, " TMUX : {}", *yazi_adapter::TMUX)?;
7276
writeln!(s, " tmux version : {}", Self::process_output("tmux", "-V"))?;
7377
writeln!(s, " tmux build flags : enable-sixel={}", Mux::tmux_sixel_flag())?;
7478
writeln!(s, " ZELLIJ_SESSION_NAME: {:?}", env::var_os("ZELLIJ_SESSION_NAME"))?;

yazi-codegen/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ proc-macro = true
1313

1414
[dependencies]
1515
# External dependencies
16-
syn = "2.0.90"
16+
syn = { version = "2.0.90", features = [ "full" ] }
1717
quote = "1.0.37"

yazi-fs/src/fns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ async fn _copy_with_progress(from: PathBuf, to: PathBuf, cha: Cha) -> io::Result
258258
tokio::task::spawn_blocking(move || {
259259
let mut reader = std::fs::File::open(from)?;
260260
let mut writer = std::fs::OpenOptions::new()
261-
.mode(cha.mode)
261+
.mode(cha.mode as u32) // Do not remove `as u32`, https://github.com/termux/termux-packages/pull/22481
262262
.write(true)
263263
.create(true)
264264
.truncate(true)

0 commit comments

Comments
 (0)