use super::pty_core::pty_types::{Controlled, ControlledChild, Controller, PtyCommand};
use portable_pty::{PtySize, native_pty_system};
pub fn create_pty_pair(pty_size: PtySize) -> miette::Result<(Controller, Controlled)> {
let pty_system = native_pty_system();
let pty_pair = pty_system
.openpty(pty_size)
.map_err(|e| miette::miette!("Failed to open PTY: {}", e))?;
Ok((pty_pair.master, pty_pair.slave))
}
pub fn spawn_command_in_pty(
controlled: &Controlled,
command: PtyCommand,
) -> miette::Result<ControlledChild> {
controlled
.spawn_command(command)
.map_err(|e| miette::miette!("Failed to spawn command: {}", e))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_pty_pair() {
let pty_size = PtySize::default();
let result = create_pty_pair(pty_size);
assert!(result.is_ok());
}
#[test]
fn test_create_pty_pair_with_custom_size() {
let pty_size = PtySize {
rows: 30,
cols: 100,
pixel_width: 0,
pixel_height: 0,
};
let result = create_pty_pair(pty_size);
assert!(result.is_ok());
}
#[test]
fn test_spawn_command_in_pty() {
let pty_size = PtySize::default();
let (_controller, controlled) = create_pty_pair(pty_size).unwrap();
let mut command = PtyCommand::new("echo");
command.arg("test");
let result = spawn_command_in_pty(&controlled, command);
assert!(result.is_ok());
}
}