From ca7bf622320803f92bc2a8f2e76d156f3d804514 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Fri, 29 Aug 2025 16:07:29 +0200 Subject: [PATCH] Set ASCIINEMA_SESSION env var for session child process --- src/cli.rs | 9 +++++++++ src/cmd/session.rs | 15 +++++++++++---- src/main.rs | 2 ++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index da8ec83..08d9567 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -33,6 +33,8 @@ pub enum Commands { /// /// Press or type 'exit' to end the recording session. /// Press to pause/resume capture of the session. + /// + /// During the session, the ASCIINEMA_SESSION environment variable is set to a unique session ID. #[clap(visible_alias = "rec", about = "Record a terminal session", long_about)] Record(Record), @@ -42,6 +44,8 @@ pub enum Commands { /// /// Press or type 'exit' to end the streaming session. /// Press to pause/resume capture of the session. + /// + /// During the session, the ASCIINEMA_SESSION environment variable is set to a unique session ID. #[clap(about = "Stream a terminal session", long_about)] Stream(Stream), @@ -51,6 +55,8 @@ pub enum Commands { /// /// Press or type 'exit' to end the session. /// Press to pause/resume capture of the session. + /// + /// During the session, the ASCIINEMA_SESSION environment variable is set to a unique session ID. #[clap(about = "Record and stream a terminal session", long_about)] Session(Session), @@ -416,6 +422,9 @@ pub struct Session { /// Specify a custom asciinema server URL for streaming to self-hosted servers. The URL should be the base URL of the server (e.g. https://asciinema.example.com). Can also be set via environment variable ASCIINEMA_SERVER_URL or config file option server.url. If no server URL is configured via this option, environment variable, or config file, you will be prompted to choose one (defaulting to asciinema.org), which will be saved as a default. #[arg(long, value_name = "URL", help = "asciinema server URL", long_help)] pub server_url: Option, + + #[arg(hide = true)] + pub env: Vec, } #[derive(Debug, Args)] diff --git a/src/cmd/session.rs b/src/cmd/session.rs index 19f61c6..a678d5f 100644 --- a/src/cmd/session.rs +++ b/src/cmd/session.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, HashSet}; use std::env; use std::path::{Path, PathBuf}; -use std::process::ExitCode; +use std::process::{self, ExitCode}; use std::time::{Duration, SystemTime}; use anyhow::{anyhow, bail, Context, Result}; @@ -122,7 +122,7 @@ impl cli::Session { } let command = &build_exec_command(command.as_ref().cloned()); - let extra_env = &build_exec_extra_env(relay_id.as_ref()); + let extra_env = &build_exec_extra_env(&self.env, relay_id.as_ref()); let exit_status = { let mut tty = self.get_tty(true).await?; @@ -486,10 +486,17 @@ fn build_exec_command(command: Option) -> Vec { vec!["/bin/sh".to_owned(), "-c".to_owned(), command] } -fn build_exec_extra_env(relay_id: Option<&String>) -> HashMap { +fn build_exec_extra_env(vars: &[String], relay_id: Option<&String>) -> HashMap { let mut env = HashMap::new(); - env.insert("ASCIINEMA_REC".to_owned(), "1".to_owned()); + for var in vars { + if let Some((name, value)) = var.split_once('=') { + env.insert(name.to_owned(), value.to_owned()); + } + } + + let session_id = format!("{:x}", hash::fnv1a_128(process::id().to_string())); + env.insert("ASCIINEMA_SESSION".to_owned(), session_id); if let Some(id) = relay_id { env.insert("ASCIINEMA_RELAY_ID".to_owned(), id.clone()); diff --git a/src/main.rs b/src/main.rs index 1c129fd..7b1bf24 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,6 +58,7 @@ fn main() -> ExitCode { return_: cmd.return_, log_file: cmd.log_file, server_url: None, + env: vec!["ASCIINEMA_REC=1".to_owned()], }; cmd.run().report() @@ -81,6 +82,7 @@ fn main() -> ExitCode { return_: cmd.return_, log_file: cmd.log_file, server_url: cmd.server_url, + env: Vec::new(), }; cmd.run().report()