add some doc comments to the pty crate
Refs: https://github.com/wez/wezterm/issues/27
This commit is contained in:
1 parent
c562f35db8
commit
1d8b024f2f
4 files changed
+74
-11
No files matched your search
+10
-2
@@ -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<OsString>,
|
||||
@@ -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<S: AsRef<OsStr>>(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<S: AsRef<OsStr>>(&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<I, S>(&mut self, args: I)
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
@@ -32,6 +38,7 @@ impl CommandBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Override the value of an environmental variable
|
||||
pub fn env<K, V>(&mut self, key: K, val: V)
|
||||
where
|
||||
K: AsRef<OsStr>,
|
||||
@@ -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<u16>, Vec<u16>), Error> {
|
||||
pub(crate) fn cmdline(&self) -> Result<(Vec<u16>, Vec<u16>), Error> {
|
||||
let mut cmdline = Vec::<u16>::new();
|
||||
|
||||
let exe = Self::search_path(&self.args[0]);
|
||||
|
||||
+62
-7
@@ -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<PtySize, Error>;
|
||||
/// Obtain a readable handle; output from the slave(s) is readable
|
||||
/// via this stream.
|
||||
fn try_clone_reader(&self) -> Result<Box<std::io::Read + Send>, 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<Option<ExitStatus>>;
|
||||
/// 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<ExitStatus>;
|
||||
}
|
||||
|
||||
/// 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<Box<Child>, 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<std::process::ExitStatus> for ExitStatus {
|
||||
fn from(_status: std::process::ExitStatus) -> ExitStatus {
|
||||
// FIXME: properly fill
|
||||
ExitStatus { successful: true }
|
||||
pub fn success(&self) -> bool {
|
||||
self.successful
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::process::ExitStatus> 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<Box<PtySystem>, 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<Box<PtySystem>, 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<Self, Self::Err> {
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user