Include command in written header

This commit is contained in:
Marcin Kulik
2023-10-28 12:05:15 +02:00
parent e93c212533
commit 2c09257e7a
5 changed files with 34 additions and 11 deletions

View File

@@ -5,6 +5,7 @@
* Full rewrite in Rust
* rec: `--append` can be used with `--raw` now
* rec: use of `--append` and `--overwrite` together returns error now
* rec: fixed saving of custom rec command in asciicast header
## 2.4.0 (2023-10-23)

View File

@@ -13,4 +13,5 @@ pub struct Header {
pub rows: u16,
pub timestamp: u64,
pub idle_time_limit: Option<f32>,
pub command: Option<String>,
}

View File

@@ -17,6 +17,7 @@ pub struct Header {
pub height: u16,
pub timestamp: u64,
pub idle_time_limit: Option<f32>,
pub command: Option<String>,
}
pub struct Event {
@@ -164,7 +165,16 @@ impl serde::Serialize for Header {
{
use serde::ser::SerializeMap;
let len = if self.idle_time_limit.is_none() { 4 } else { 5 };
let mut len = 4;
if self.idle_time_limit.is_some() {
len += 1;
}
if self.command.is_some() {
len += 1;
}
let mut map = serializer.serialize_map(Some(len))?;
map.serialize_entry("version", &2)?;
map.serialize_entry("width", &self.width)?;
@@ -175,6 +185,10 @@ impl serde::Serialize for Header {
map.serialize_entry("idle_time_limit", &limit)?;
}
if let Some(command) = &self.command {
map.serialize_entry("command", &command)?;
}
map.end()
}
}
@@ -200,6 +214,7 @@ impl From<&Header> for super::Header {
rows: header.height,
timestamp: header.timestamp,
idle_time_limit: header.idle_time_limit,
command: header.command.clone(),
}
}
}
@@ -211,6 +226,7 @@ impl From<&super::Header> for Header {
height: header.rows,
timestamp: header.timestamp,
idle_time_limit: header.idle_time_limit,
command: header.command.clone(),
}
}
}
@@ -258,6 +274,7 @@ mod tests {
height: 24,
timestamp: 1,
idle_time_limit: None,
command: None,
};
fw.write_header(&header).unwrap();
@@ -296,6 +313,7 @@ mod tests {
height: 24,
timestamp: 1,
idle_time_limit: Some(1.5),
command: Some("/bin/bash".to_owned()),
};
fw.write_header(&header).unwrap();
@@ -304,7 +322,7 @@ mod tests {
assert_eq!(
asciicast,
"{\"version\":2,\"width\":80,\"height\":24,\"timestamp\":1,\"idle_time_limit\":1.5}\n"
"{\"version\":2,\"width\":80,\"height\":24,\"timestamp\":1,\"idle_time_limit\":1.5,\"command\":\"/bin/bash\"}\n"
);
}
}

View File

@@ -39,9 +39,9 @@ enum Commands {
#[arg(long, conflicts_with = "append")]
overwrite: bool,
/// Command to record
#[arg(short, long, default_value_t = String::from("$SHELL"))]
command: String,
/// Command to record [default: $SHELL]
#[arg(short, long)]
command: Option<String>,
/// List of env vars to save
#[arg(short, long, default_value_t = String::from("SHELL,TERM"))]
@@ -149,13 +149,12 @@ fn main() -> Result<()> {
Box::new(writer)
};
let mut recorder = recorder::Recorder::new(writer, append, stdin, idle_time_limit);
let mut recorder =
recorder::Recorder::new(writer, append, stdin, idle_time_limit, command.clone());
let command = if command == "$SHELL" {
env::var("SHELL").ok().unwrap_or("/bin/sh".to_owned())
} else {
command
};
let command = command
.or(env::var("SHELL").ok())
.unwrap_or("/bin/sh".to_owned());
pty::exec(&["/bin/sh", "-c", &command], &mut recorder)?;
}

View File

@@ -9,6 +9,7 @@ pub struct Recorder {
append: bool,
record_input: bool,
idle_time_limit: Option<f32>,
command: Option<String>,
}
impl Recorder {
@@ -17,6 +18,7 @@ impl Recorder {
append: bool,
record_input: bool,
idle_time_limit: Option<f32>,
command: Option<String>,
) -> Self {
Recorder {
writer,
@@ -24,6 +26,7 @@ impl Recorder {
append,
record_input,
idle_time_limit,
command,
}
}
@@ -47,6 +50,7 @@ impl pty::Recorder for Recorder {
rows: size.1,
timestamp,
idle_time_limit: self.idle_time_limit,
command: self.command.clone(),
};
self.writer.header(&header)