use anyhow::{Context, Result};
use clap::Parser;
use scriptty::{Engine, parse_file};
use std::io::Write;
#[derive(Parser, Debug)]
#[command(
name = "scriptty",
about = "Run a scriptty script against an interactive terminal program",
version
)]
struct Args {
#[arg(short, long)]
script: String,
#[arg(short, long)]
command: String,
#[arg(trailing_var_arg = true)]
args: Vec<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
let args = Args::parse();
let events = parse_file(&args.script)
.with_context(|| format!("Failed to parse script file: {}", args.script))?;
let mut engine = Engine::spawn(&args.command, &args.args).context("Failed to spawn engine")?;
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
clear_screen()?;
engine
.execute(events)
.await
.context("Failed to execute script")?;
Ok(())
}
fn clear_screen() -> Result<()> {
print!("\x1B[2J\x1B[1;1H");
std::io::stdout().flush()?;
Ok(())
}