Remove the --filename option for rec and session commands

Most of it can be done by using variable interpolation in a shell e.g.:

  asciinema rec "${HOSTNAME}-${USER}-$(date +%s)-$$.cast"
This commit is contained in:
Marcin Kulik
2025-05-04 21:41:59 +02:00
parent 0dee9c5c6f
commit 2115303df2
6 changed files with 4 additions and 187 deletions

121
Cargo.lock generated
View File

@@ -26,21 +26,6 @@ dependencies = [
"memchr",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "anstream"
version = "0.6.18"
@@ -104,13 +89,11 @@ dependencies = [
"anyhow",
"avt",
"axum",
"chrono",
"clap",
"clap_complete",
"clap_mangen",
"config",
"futures-util",
"hostname",
"mime_guess",
"nix 0.30.1",
"reqwest",
@@ -361,20 +344,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "chrono"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "clang-sys"
version = "1.8.1"
@@ -888,17 +857,6 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "hostname"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a56f203cd1c76362b69e3863fd987520ac36cf70a8c92627449b2f64a8cf7d65"
dependencies = [
"cfg-if",
"libc",
"windows-link",
]
[[package]]
name = "html5ever"
version = "0.29.1"
@@ -1015,30 +973,6 @@ dependencies = [
"tracing",
]
[[package]]
name = "iana-time-zone"
version = "0.1.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "icu_collections"
version = "1.5.0"
@@ -1460,15 +1394,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "numtoa"
version = "0.1.0"
@@ -3016,41 +2941,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-core"
version = "0.61.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings 0.4.0",
]
[[package]]
name = "windows-implement"
version = "0.60.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.1"
@@ -3064,7 +2954,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4286ad90ddb45071efd1a66dfa43eb02dd0dfbae1545ad6cc3c51cf34d7e8ba3"
dependencies = [
"windows-result",
"windows-strings 0.3.1",
"windows-strings",
"windows-targets 0.53.0",
]
@@ -3086,15 +2976,6 @@ dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-sys"
version = "0.52.0"

View File

@@ -44,8 +44,6 @@ tokio-tungstenite = { version = "0.26.2", features = ["rustls-tls-native-roots"]
rustls = { version = "0.23.26", default-features = false, features = ["aws_lc_rs"] }
sha2 = "0.10.8"
tokio-util = "0.7.10"
chrono = "0.4.38"
hostname = "0.4.1"
[build-dependencies]
clap = { version = "4.5.37", features = ["derive"] }

View File

@@ -78,10 +78,6 @@ pub struct Record {
#[arg(long, conflicts_with = "append")]
pub overwrite: bool,
/// Filename template, used when recording to a directory
#[arg(long, value_name = "TEMPLATE")]
pub filename: Option<String>,
/// Title of the recording
#[arg(short, long)]
pub title: Option<String>,
@@ -195,10 +191,6 @@ pub struct Session {
#[arg(long, conflicts_with = "append")]
pub overwrite: bool,
/// Filename template, used when recording to a directory
#[arg(long, value_name = "TEMPLATE")]
pub filename: Option<String>,
/// Title of the recording
#[arg(short, long)]
pub title: Option<String>,

View File

@@ -3,12 +3,10 @@ use std::env;
use std::fs::{self, OpenOptions};
use std::io::LineWriter;
use std::net::TcpListener;
use std::path::{Path, PathBuf};
use std::process;
use std::path::Path;
use std::time::Duration;
use anyhow::{anyhow, bail, Context, Result};
use chrono::Local;
use tokio::runtime::Runtime;
use tokio::time;
use tokio_util::sync::CancellationToken;
@@ -47,13 +45,8 @@ impl cli::Session {
let term_version = self.get_term_version()?;
let env = capture_env(self.rec_env.take(), cmd_config);
let path = self
let file_writer = self
.output_file
.take()
.map(|path| self.ensure_filename(path, cmd_config))
.transpose()?;
let file_writer = path
.as_ref()
.map(|path| {
self.get_file_writer(
@@ -100,7 +93,7 @@ impl cli::Session {
status::info!("asciinema session started");
if let Some(path) = path {
if let Some(path) = self.output_file.as_ref() {
status::info!("Recording to {}", path);
}
@@ -185,44 +178,6 @@ impl cli::Session {
Ok(())
}
fn ensure_filename(&mut self, path_: String, config: &config::Session) -> Result<String> {
let mut path = PathBuf::from(&path_);
if path.exists() && fs::metadata(&path)?.is_dir() {
let mut tpl = self.filename.clone().unwrap_or(config.filename.clone());
if tpl.contains("{pid}") {
let pid = process::id().to_string();
tpl = tpl.replace("{pid}", &pid);
}
if tpl.contains("{user}") {
let user = env::var("USER").ok().unwrap_or("unknown".to_owned());
tpl = tpl.replace("{user}", &user);
}
if tpl.contains("{hostname}") {
let hostname = hostname::get()
.ok()
.and_then(|h| h.into_string().ok())
.unwrap_or("unknown".to_owned());
tpl = tpl.replace("{hostname}", &hostname);
}
let filename = Local::now().format(&tpl).to_string();
path.push(Path::new(&filename));
if let Some(dir) = path.parent() {
fs::create_dir_all(dir)?;
}
Ok(path.to_string_lossy().to_string())
} else {
Ok(path_)
}
}
fn get_file_writer<N: Notifier + 'static>(
&self,
path: &str,

View File

@@ -9,7 +9,6 @@ use serde::Deserialize;
use uuid::Uuid;
const DEFAULT_SERVER_URL: &str = "https://asciinema.org";
const DEFAULT_REC_FILENAME: &str = "%Y-%m-%d-%H-%M-%S-{pid}.cast";
const INSTALL_ID_FILENAME: &str = "install-id";
pub type Key = Option<Vec<u8>>;
@@ -41,7 +40,6 @@ pub struct Cmd {
#[allow(unused)]
pub struct Session {
pub command: Option<String>,
pub filename: String,
pub rec_input: bool,
pub rec_env: Option<String>,
pub idle_time_limit: Option<f64>,
@@ -54,7 +52,6 @@ pub struct Session {
#[allow(unused)]
pub struct Rec {
pub command: Option<String>,
pub filename: String,
pub rec_input: bool,
pub rec_env: Option<String>,
pub idle_time_limit: Option<f64>,
@@ -96,11 +93,9 @@ impl Config {
let mut config = config::Config::builder()
.set_default("server.url", None::<Option<String>>)?
.set_default("cmd.rec.rec_input", false)?
.set_default("cmd.rec.filename", DEFAULT_REC_FILENAME)?
.set_default("cmd.play.speed", None::<Option<f64>>)?
.set_default("cmd.stream.rec_input", false)?
.set_default("cmd.session.rec_input", false)?
.set_default("cmd.session.filename", DEFAULT_REC_FILENAME)?
.set_default("notifications.enabled", true)?
.add_source(config::File::with_name("/etc/asciinema/config.toml").required(false))
.add_source(
@@ -154,7 +149,6 @@ impl Config {
pub fn cmd_rec(&self) -> Session {
Session {
command: self.cmd.rec.command.clone(),
filename: self.cmd.rec.filename.clone(),
rec_input: self.cmd.rec.rec_input,
rec_env: self.cmd.rec.rec_env.clone(),
idle_time_limit: self.cmd.rec.idle_time_limit,
@@ -167,7 +161,6 @@ impl Config {
pub fn cmd_stream(&self) -> Session {
Session {
command: self.cmd.stream.command.clone(),
filename: "".to_string(),
rec_input: self.cmd.stream.rec_input,
rec_env: self.cmd.stream.rec_env.clone(),
idle_time_limit: None,

View File

@@ -44,7 +44,6 @@ fn main() -> anyhow::Result<()> {
output_format: cmd.output_format,
overwrite: cmd.overwrite,
command: cmd.command,
filename: cmd.filename,
rec_env: cmd.rec_env,
title: cmd.title,
idle_time_limit: cmd.idle_time_limit,
@@ -66,7 +65,6 @@ fn main() -> anyhow::Result<()> {
output_format: None,
overwrite: false,
command: stream.command,
filename: None,
rec_env: stream.rec_env,
title: None,
idle_time_limit: None,