From ba41a4f112af7dffec923d7d638c621cec24ccd3 Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Wed, 17 Jul 2024 19:24:39 +0300 Subject: [PATCH] feat: allow setting YAZI_ID as a command line argument As discussed in https://github.com/sxyazi/yazi/issues/989#issuecomment-2231468495, this will allow applications embedding yazi to set the YAZI_ID environment variable to a known value. Afterwards, the application can use this id with `ya pub` to publish messages to the specific yazi instance. --- yazi-boot/src/args.rs | 4 ++++ yazi-boot/src/boot.rs | 3 +++ yazi-dds/src/lib.rs | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/yazi-boot/src/args.rs b/yazi-boot/src/args.rs index 07e8c12f..1732b92c 100644 --- a/yazi-boot/src/args.rs +++ b/yazi-boot/src/args.rs @@ -34,4 +34,8 @@ pub struct Args { /// Print version #[arg(short = 'V', long)] pub version: bool, + + /// Set the YAZI_ID environment variable (integer) + #[arg(long)] + pub id: Option, } diff --git a/yazi-boot/src/boot.rs b/yazi-boot/src/boot.rs index 9f412674..5016cfb1 100644 --- a/yazi-boot/src/boot.rs +++ b/yazi-boot/src/boot.rs @@ -21,6 +21,8 @@ pub struct Boot { pub flavor_dir: PathBuf, pub plugin_dir: PathBuf, pub state_dir: PathBuf, + + pub yazi_id: Option, } impl Boot { @@ -184,6 +186,7 @@ impl Default for Boot { plugin_dir: config_dir.join("plugins"), config_dir, state_dir: Xdg::state_dir(), + yazi_id: ARGS.id.clone(), } } } diff --git a/yazi-dds/src/lib.rs b/yazi-dds/src/lib.rs index 6a50bcde..90f4d022 100644 --- a/yazi-dds/src/lib.rs +++ b/yazi-dds/src/lib.rs @@ -17,6 +17,7 @@ pub use sendable::*; use server::*; pub use state::*; use stream::*; +use yazi_boot::BOOT; #[cfg(unix)] pub static USERS_CACHE: yazi_shared::RoCell = yazi_shared::RoCell::new(); @@ -25,7 +26,7 @@ pub fn init() { let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); // Client - ID.init(yazi_shared::timestamp_us()); + ID.init(BOOT.yazi_id.unwrap_or(yazi_shared::timestamp_us())); PEERS.with(Default::default); QUEUE_TX.init(tx); QUEUE_RX.init(rx);