use crate::command::{Context, ScripttyCommand};
use crate::parser::parse_quoted_string;
use anyhow::Result;
use async_trait::async_trait;
use std::time::Duration;
use tokio::time::sleep;
pub struct SendInput {
pub data: Vec<u8>,
}
impl SendInput {
pub const NAME: &'static str = "send";
pub fn new(text: impl Into<String>) -> Self {
Self {
data: text.into().into_bytes(),
}
}
}
#[async_trait(?Send)]
impl ScripttyCommand for SendInput {
fn name(&self) -> &'static str {
Self::NAME
}
fn parse(args: &str) -> Result<Self> {
Ok(Self::new(parse_quoted_string(args)?))
}
async fn execute(&self, ctx: &mut Context) -> Result<()> {
ctx.write_to_pty(&self.data)?;
sleep(Duration::from_millis(50)).await;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command::ScripttyCommand;
#[test]
fn test_parse() {
let cmd = SendInput::parse(r#""hello""#).unwrap();
assert_eq!(cmd.data, b"hello");
}
#[test]
fn test_data_content() {
let cmd = SendInput::new("cmd");
assert_eq!(cmd.data, b"cmd");
}
}