[][src]Trait tokio_pty_process::PtyMaster

pub trait PtyMaster {
    fn ptsname(&self) -> Poll<OsString, Error>;
fn resize(&self, rows: c_ushort, cols: c_ushort) -> Poll<(), Error>;
fn winsize(&self) -> Poll<(c_ushort, c_ushort), Error>; }

Trait containing generalized methods for PTYs

Required methods

fn ptsname(&self) -> Poll<OsString, Error>

Return the full pathname of the slave device counterpart

Example

extern crate tokio;
extern crate tokio_pty_process;

use std::ffi::OsString;
use tokio::prelude::*;
use tokio_pty_process::{AsyncPtyMaster, PtyMaster};

struct PtsName<T: PtyMaster>(T);

impl<T: PtyMaster> Future for PtsName<T> {
    type Item = OsString;
    type Error = std::io::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0.ptsname()
    }
}

fn main() {
    let master = AsyncPtyMaster::open().expect("Could not open the PTY");

    let ptsname = PtsName(master).wait().expect("Could not get the ptsname");

    println!("PTS name: {}", ptsname.to_string_lossy());
}

fn resize(&self, rows: c_ushort, cols: c_ushort) -> Poll<(), Error>

Resize the PTY

Example

extern crate tokio;
extern crate tokio_pty_process;
extern crate libc;

use tokio_pty_process::{AsyncPtyMaster, PtyMaster, CommandExt};
use tokio::prelude::*;
use std::ffi::OsString;
use libc::c_ushort;
struct Resize<T: PtyMaster> {
    pty: T,
    rows: c_ushort,
    cols: c_ushort,
}

impl<T: PtyMaster> Future for Resize<T> {
    type Item = ();
    type Error = std::io::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.pty.resize(self.rows, self.cols)
    }
}

fn main() {
    let master = AsyncPtyMaster::open().expect("Could not open the PTY");

    // On macos, it's only possible to resize a PTY with a child spawned
    // On it, so let's just do that:
    #[cfg(target_os="macos")]
    let mut child = std::process::Command::new("cat")
        .spawn_pty_async(&master)
        .expect("Could not spawn child");

    Resize {
        pty: master,
        cols: 80,
        rows: 50,
    }
    .wait()
    .expect("Could not resize the PTY");

    #[cfg(target_os="macos")]
    child.kill().expect("Could not kill child");
}

fn winsize(&self) -> Poll<(c_ushort, c_ushort), Error>

Get the PTY size

Example

extern crate tokio;
extern crate tokio_pty_process;
extern crate libc;

use tokio_pty_process::{AsyncPtyMaster, PtyMaster, CommandExt};
use tokio::prelude::*;
use std::ffi::OsString;
use libc::c_ushort;

struct GetSize<'a, T: PtyMaster> (&'a T);
impl<'a, T: PtyMaster> Future for GetSize<'a, T> {
    type Item = (c_ushort, c_ushort);
    type Error = std::io::Error;
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.0.winsize()
    }
}

fn main() {
    let master = AsyncPtyMaster::open().expect("Could not open the PTY");

    // On macos, it's only possible to resize a PTY with a child spawned
    // On it, so let's just do that:
    #[cfg(target_os="macos")]
    let mut child = std::process::Command::new("cat")
        .spawn_pty_async(&master)
        .expect("Could not spawn child");

    let (rows, cols) = GetSize(&master)
        .wait()
        .expect("Could not get PTY size");

    #[cfg(target_os="macos")]
    child.kill().expect("Could not kill child");
}
Loading content...

Implementors

impl<T: AsAsyncPtyFd> PtyMaster for T[src]

Loading content...