From 644e24b33e95cd2f1b409f4bb6dd045ed568b574 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Thu, 26 Oct 2023 17:30:42 +0200 Subject: [PATCH] Bring clap, define rec subcommand --- Cargo.lock | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 95 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 202 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index abba36e..b83a1d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 1c9c2b5..40ef99a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index cba7986..ed997f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, + + /// Limit idle time to given number of seconds + #[arg(short, long, value_name = "SECS")] + idle_time_limit: Option, + + /// Override terminal width (columns) for recorded command + #[arg(long)] + cols: Option, + + /// Override terminal height (rows) for recorded command + #[arg(long)] + rows: Option, + + /// 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(()) }