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.
This commit is contained in:
Mika Vilpas 2024-07-17 19:24:39 +03:00
parent ed6ae000d6
commit ba41a4f112
3 changed files with 9 additions and 1 deletions

View file

@ -34,4 +34,8 @@ pub struct Args {
/// Print version /// Print version
#[arg(short = 'V', long)] #[arg(short = 'V', long)]
pub version: bool, pub version: bool,
/// Set the YAZI_ID environment variable (integer)
#[arg(long)]
pub id: Option<u64>,
} }

View file

@ -21,6 +21,8 @@ pub struct Boot {
pub flavor_dir: PathBuf, pub flavor_dir: PathBuf,
pub plugin_dir: PathBuf, pub plugin_dir: PathBuf,
pub state_dir: PathBuf, pub state_dir: PathBuf,
pub yazi_id: Option<u64>,
} }
impl Boot { impl Boot {
@ -184,6 +186,7 @@ impl Default for Boot {
plugin_dir: config_dir.join("plugins"), plugin_dir: config_dir.join("plugins"),
config_dir, config_dir,
state_dir: Xdg::state_dir(), state_dir: Xdg::state_dir(),
yazi_id: ARGS.id.clone(),
} }
} }
} }

View file

@ -17,6 +17,7 @@ pub use sendable::*;
use server::*; use server::*;
pub use state::*; pub use state::*;
use stream::*; use stream::*;
use yazi_boot::BOOT;
#[cfg(unix)] #[cfg(unix)]
pub static USERS_CACHE: yazi_shared::RoCell<uzers::UsersCache> = yazi_shared::RoCell::new(); pub static USERS_CACHE: yazi_shared::RoCell<uzers::UsersCache> = yazi_shared::RoCell::new();
@ -25,7 +26,7 @@ pub fn init() {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
// Client // Client
ID.init(yazi_shared::timestamp_us()); ID.init(BOOT.yazi_id.unwrap_or(yazi_shared::timestamp_us()));
PEERS.with(Default::default); PEERS.with(Default::default);
QUEUE_TX.init(tx); QUEUE_TX.init(tx);
QUEUE_RX.init(rx); QUEUE_RX.init(rx);