Bring clap, define rec subcommand

This commit is contained in:
Marcin Kulik
2023-10-26 17:30:42 +02:00
parent 8575bf34da
commit 644e24b33e
3 changed files with 202 additions and 7 deletions

113
Cargo.lock generated
View File

@@ -2,6 +2,54 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
[[package]]
name = "anstyle-parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.75"
@@ -13,6 +61,7 @@ name = "asciinema"
version = "3.0.0-alpha.1"
dependencies = [
"anyhow",
"clap",
"mio",
"nix",
"serde",
@@ -39,6 +88,52 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "errno"
version = "0.3.5"
@@ -55,6 +150,12 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5"
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "itoa"
version = "1.0.9"
@@ -203,6 +304,12 @@ dependencies = [
"serde",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "2.0.38"
@@ -245,6 +352,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View File

@@ -17,6 +17,7 @@ mio = { version ="0.8", features = ["os-poll", "os-ext"] }
termion = "2.0.1"
serde = { version = "1.0.189", features = ["derive"] }
serde_json = "1.0.107"
clap = { version = "4.4.7", features = ["derive"] }
[dev-dependencies]
tempfile = "3.8.0"

View File

@@ -1,16 +1,97 @@
mod asciicast;
mod pty;
mod recorder;
use anyhow::{anyhow, Result};
use std::env;
use anyhow::Result;
use clap::{Args, Parser, Subcommand, ValueEnum};
#[derive(Debug, Parser)]
#[command(name = "asciinema")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand)]
enum Commands {
/// Record terminal session
#[command(name = "rec")]
Record {
filename: String,
/// Enable input recording
#[arg(long)]
stdin: bool,
/// Append to existing asciicast file
#[arg(long)]
append: bool,
/// Save raw output only
#[arg(long)]
raw: bool,
/// Overwrite target file if it already exists
#[arg(long)]
overwrite: bool,
/// Command to record
#[arg(short, long, default_value_t = String::from("$SHELL"))]
command: String,
/// List of env vars to save
#[arg(short, long, default_value_t = String::from("SHELL,TERM"))]
env: String,
/// Title of the recording
#[arg(short, long)]
title: Option<String>,
/// Limit idle time to given number of seconds
#[arg(short, long, value_name = "SECS")]
idle_time_limit: Option<f64>,
/// Override terminal width (columns) for recorded command
#[arg(long)]
cols: Option<u16>,
/// Override terminal height (rows) for recorded command
#[arg(long)]
rows: Option<u16>,
/// Quiet mode - suppress all notices/warnings
#[arg(short, long)]
quiet: bool,
},
}
fn main() -> Result<()> {
let path = env::args()
.nth(1)
.ok_or(anyhow!("output filename missing"))?;
let cli = Cli::parse();
let mut recorder = recorder::new(path, recorder::Format::Asciicast, false, true)?;
pty::exec(&["/bin/bash"], &mut recorder)?;
match cli.command {
Commands::Record {
filename,
stdin,
append,
raw,
overwrite,
command,
env,
title,
idle_time_limit,
cols,
rows,
quiet,
} => {
let format = if raw {
recorder::Format::Raw
} else {
recorder::Format::Asciicast
};
let mut recorder = recorder::new(filename, format, append, stdin)?;
pty::exec(&["/bin/bash"], &mut recorder)?;
}
}
Ok(())
}