From 1d8b024f2f2677980656307c8f1cd7cdc3817637 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 19 May 2019 20:31:07 -0700 Subject: [PATCH] add some doc comments to the pty crate Refs: https://github.com/wez/wezterm/issues/27 --- pty/src/cmdbuilder.rs | 12 +++++-- pty/src/lib.rs | 69 ++++++++++++++++++++++++++++++++++---- pty/src/win/winpty/mod.rs | 2 +- pty/src/win/winpty/safe.rs | 2 +- 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/pty/src/cmdbuilder.rs b/pty/src/cmdbuilder.rs index c24086880..1a3116dd1 100644 --- a/pty/src/cmdbuilder.rs +++ b/pty/src/cmdbuilder.rs @@ -4,6 +4,8 @@ use std::ffi::{OsStr, OsString}; #[cfg(windows)] use std::os::windows::ffi::OsStrExt; +/// `CommandBuilder` is used to prepare a command to be spawned into a pty. +/// The interface is intentionally similar to that of `std::process::Command`. #[derive(Debug)] pub struct CommandBuilder { args: Vec, @@ -11,6 +13,8 @@ pub struct CommandBuilder { } impl CommandBuilder { + /// Create a new builder instance with argv[0] set to the specified + /// program. pub fn new>(program: S) -> Self { Self { args: vec![program.as_ref().to_owned()], @@ -18,10 +22,12 @@ impl CommandBuilder { } } + /// Append an argument to the current command line pub fn arg>(&mut self, arg: S) { self.args.push(arg.as_ref().to_owned()); } + /// Append a sequence of arguments to the current command line pub fn args(&mut self, args: I) where I: IntoIterator, @@ -32,6 +38,7 @@ impl CommandBuilder { } } + /// Override the value of an environmental variable pub fn env(&mut self, key: K, val: V) where K: AsRef, @@ -50,7 +57,8 @@ impl CommandBuilder { #[cfg(unix)] impl CommandBuilder { - pub fn as_command(&self) -> std::process::Command { + /// Convert the CommandBuilder to a `std::process::Command` instance. + pub(crate) fn as_command(&self) -> std::process::Command { let mut cmd = std::process::Command::new(&self.args[0]); cmd.args(&self.args[1..]); for (key, val) in &self.envs { @@ -91,7 +99,7 @@ impl CommandBuilder { exe.to_owned() } - pub fn cmdline(&self) -> Result<(Vec, Vec), Error> { + pub(crate) fn cmdline(&self) -> Result<(Vec, Vec), Error> { let mut cmdline = Vec::::new(); let exe = Self::search_path(&self.args[0]); diff --git a/pty/src/lib.rs b/pty/src/lib.rs index d15575df0..0acfd94e4 100644 --- a/pty/src/lib.rs +++ b/pty/src/lib.rs @@ -11,54 +11,88 @@ pub mod unix; #[cfg(windows)] pub mod win; +/// Represents the size of the visible display area in the pty #[derive(Debug, Clone, Copy)] pub struct PtySize { + /// The number of lines of text pub rows: u16, + /// The number of columns of text pub cols: u16, + /// The width of a cell in pixels. Note that some systems never + /// fill this value and ignore it. pub pixel_width: u16, + /// The height of a cell in pixels. Note that some systems never + /// fill this value and ignore it. pub pixel_height: u16, } +/// Represents the master/control end of the pty pub trait MasterPty: std::io::Write { /// Inform the kernel and thus the child process that the window resized. /// It will update the winsize information maintained by the kernel, /// and generate a signal for the child to notice and update its state. fn resize(&self, size: PtySize) -> Result<(), Error>; + /// Retrieves the size of the pty as known by the kernel fn get_size(&self) -> Result; + /// Obtain a readable handle; output from the slave(s) is readable + /// via this stream. fn try_clone_reader(&self) -> Result, Error>; } +/// Represents a child process spawned into the pty. +/// This handle can be used to wait for or terminate that child process. pub trait Child: std::fmt::Debug { + /// Poll the child to see if it has completed. + /// Does not block. + /// Returns None if the has not yet terminated, + /// else returns its exit status. fn try_wait(&mut self) -> IoResult>; + /// Terminate the child process fn kill(&mut self) -> IoResult<()>; + /// Blocks execution until the child process has completed, + /// yielding its exit status. fn wait(&mut self) -> IoResult; } +/// Represents the slave side of a pty. +/// Can be used to spawn processes into the pty. pub trait SlavePty { + /// Spawns the command specified by the provided CommandBuilder fn spawn_command(&self, cmd: CommandBuilder) -> Result, Error>; } +/// Represents the exit status of a child process. +/// This is rather anemic in the current version of this crate, +/// holding only an indicator of success or failure. #[derive(Debug)] pub struct ExitStatus { successful: bool, } -#[cfg(windows)] impl ExitStatus { + /// Construct an ExitStatus from a process return code pub fn with_exit_code(code: u32) -> Self { Self { successful: if code == 0 { true } else { false }, } } -} -impl From for ExitStatus { - fn from(_status: std::process::ExitStatus) -> ExitStatus { - // FIXME: properly fill - ExitStatus { successful: true } + pub fn success(&self) -> bool { + self.successful } } +impl From for ExitStatus { + fn from(status: std::process::ExitStatus) -> ExitStatus { + ExitStatus { + successful: status.success(), + } + } +} + +/// The `PtySystem` trait allows an application to work with multiple +/// possible Pty implementations at runtime. This is important on +/// Windows systems which have a variety of implementations. pub trait PtySystem { /// Create a new Pty instance with the window size set to the specified /// dimensions. Returns a (master, slave) Pty pair. The master side @@ -83,16 +117,27 @@ impl Child for std::process::Child { } } -#[allow(dead_code)] +/// `PtySystemSelection` allows selecting and constructing one of the +/// pty implementations provided by this crate. #[derive(Debug, Clone, Copy)] #[cfg_attr(feature = "serde_support", derive(Deserialize))] pub enum PtySystemSelection { + /// The Unix style pty interface Unix, + /// The Windows 10+ native Console Pty interface ConPty, + /// rprichard's WinPty interface to cygwin and msys pty. + /// This requires that winpty.dll be resolvable by the + /// embedding application. Instructions on obtaining + /// an appropriate implementation of winpty.dll can be + /// found here: + /// https://github.com/rprichard/winpty WinPty, } impl PtySystemSelection { + /// Construct an instance of PtySystem described by the enum value. + /// Windows specific enum variants result in an error. #[cfg(unix)] pub fn get(&self) -> Result, Error> { match self { @@ -100,6 +145,9 @@ impl PtySystemSelection { _ => bail!("{:?} not available on unix", self), } } + + /// Construct an instance of PtySystem described by the enum value. + /// Unix specific enum variants result in an error. #[cfg(windows)] pub fn get(&self) -> Result, Error> { match self { @@ -109,13 +157,19 @@ impl PtySystemSelection { } } + /// Returns a list of the variant names. + /// This can be useful for example to specify the list of allowable + /// options in a clap argument specification. pub fn variants() -> Vec<&'static str> { vec!["Unix", "ConPty", "WinPty"] } } +/// Parse a string into a `PtySystemSelection` value. +/// This is useful when parsing arguments or configuration files. impl std::str::FromStr for PtySystemSelection { type Err = Error; + fn from_str(s: &str) -> Result { match s.to_lowercase().as_ref() { "unix" => Ok(PtySystemSelection::Unix), @@ -131,6 +185,7 @@ impl std::str::FromStr for PtySystemSelection { } impl Default for PtySystemSelection { + /// Returns the default, system native PtySystemSelection fn default() -> PtySystemSelection { #[cfg(unix)] return PtySystemSelection::Unix; diff --git a/pty/src/win/winpty/mod.rs b/pty/src/win/winpty/mod.rs index bcff2c141..fb6090239 100644 --- a/pty/src/win/winpty/mod.rs +++ b/pty/src/win/winpty/mod.rs @@ -5,7 +5,7 @@ use crate::win::winpty::safe::{ }; use crate::{Child, MasterPty, PtySize, PtySystem, SlavePty}; use failure::{bail, Error}; -use filedescriptor::{FileDescriptor}; +use filedescriptor::FileDescriptor; use std::ffi::OsString; use std::os::windows::ffi::OsStringExt; use std::path::Path; diff --git a/pty/src/win/winpty/safe.rs b/pty/src/win/winpty/safe.rs index abb199156..14ffe6a38 100644 --- a/pty/src/win/winpty/safe.rs +++ b/pty/src/win/winpty/safe.rs @@ -8,7 +8,7 @@ use failure::{bail, ensure, format_err, Error}; use filedescriptor::{FileDescriptor, OwnedHandle}; use std::ffi::{OsStr, OsString}; use std::os::windows::ffi::{OsStrExt, OsStringExt}; -use std::os::windows::io::{FromRawHandle}; +use std::os::windows::io::FromRawHandle; use std::ptr; use winapi::shared::minwindef::DWORD; use winapi::shared::ntdef::LPCWSTR;