Set ASCIINEMA_SESSION env var for session child process

This commit is contained in:
Marcin Kulik
2025-08-29 16:07:29 +02:00
parent e425874d08
commit ca7bf62232
3 changed files with 22 additions and 4 deletions

View File

@@ -33,6 +33,8 @@ pub enum Commands {
///
/// Press <ctrl+d> or type 'exit' to end the recording session.
/// Press <ctrl+\> 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 <ctrl+d> or type 'exit' to end the streaming session.
/// Press <ctrl+\> 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 <ctrl+d> or type 'exit' to end the session.
/// Press <ctrl+\> 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<String>,
#[arg(hide = true)]
pub env: Vec<String>,
}
#[derive(Debug, Args)]

View File

@@ -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<String>) -> Vec<String> {
vec!["/bin/sh".to_owned(), "-c".to_owned(), command]
}
fn build_exec_extra_env(relay_id: Option<&String>) -> HashMap<String, String> {
fn build_exec_extra_env(vars: &[String], relay_id: Option<&String>) -> HashMap<String, String> {
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());

View File

@@ -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()