1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-22 22:42:48 +03:00

proof of concept with having mio drive the pty reads

This commit is contained in:
Wez Furlong 2018-01-26 09:00:40 -08:00
parent 0ecda48f01
commit ab74b4c731
3 changed files with 73 additions and 10 deletions

View File

@ -8,6 +8,7 @@ failure = "0.1.1"
freetype = "0.3.0"
harfbuzz-sys = "0.1.15"
libc = "0.2.36"
mio = "0.6.12"
resize = "0.3.0"
servo-fontconfig = "0.4.0"
unicode-segmentation = "1.2.0"

View File

@ -10,6 +10,7 @@ extern crate freetype;
extern crate resize;
extern crate vte;
extern crate libc;
extern crate mio;
#[macro_use]
pub mod log;
@ -343,17 +344,48 @@ fn ptyrun() -> Result<(), Error> {
let (mut master, slave) = pty::openpty(24, 80)?;
let child = slave.spawn_command(cmd)?;
let mut child = slave.spawn_command(cmd)?;
eprintln!("spawned: {:?}", child);
loop {
let mut buf = [0;256];
use mio::{Events, Poll, PollOpt, Ready, Token};
let poll = Poll::new()?;
poll.register(
&master,
Token(0),
Ready::readable(),
PollOpt::edge(),
)?;
match master.read(&mut buf) {
Ok(size) => println!("[ls] {}", std::str::from_utf8(&buf[0..size]).unwrap()),
Err(err) => {
eprintln!("[ls:err] {:?}", err);
break
let mut events = Events::with_capacity(8);
loop {
println!("polling");
poll.poll(&mut events, None)?;
match child.try_wait() {
Ok(Some(status)) => {
println!("child exited: {}", status);
break;
}
Ok(None) => println!("child still running"),
Err(e) => {
println!("failed to wait for child: {}", e);
break;
}
}
for event in &events {
if event.token() == Token(0) && event.readiness().is_readable() {
println!("readable, doing read!");
let mut buf = [0; 256];
match master.read(&mut buf) {
Ok(size) => println!("[ls] {}", std::str::from_utf8(&buf[0..size]).unwrap()),
Err(err) => {
eprintln!("[ls:err] {:?}", err);
break;
}
}
}
}
}
@ -363,5 +395,5 @@ fn ptyrun() -> Result<(), Error> {
fn main() {
ptyrun().unwrap();
// run().unwrap();
// run().unwrap();
}

View File

@ -2,10 +2,13 @@
use failure::Error;
use libc::{self, winsize};
use mio::{Poll, PollOpt, Ready, Token};
use mio::event::Evented;
use mio::unix::EventedFd;
use std::io;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio, Child};
use std::process::{Child, Command, Stdio};
use std::ptr;
/// Represents the master end of a pty.
@ -229,3 +232,30 @@ impl io::Read for MasterPty {
}
}
}
/// Glue for working with mio
impl Evented for MasterPty {
fn register(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> io::Result<()> {
EventedFd(&self.fd).register(poll, token, interest, opts)
}
fn reregister(
&self,
poll: &Poll,
token: Token,
interest: Ready,
opts: PollOpt,
) -> io::Result<()> {
EventedFd(&self.fd).reregister(poll, token, interest, opts)
}
fn deregister(&self, poll: &Poll) -> io::Result<()> {
EventedFd(&self.fd).deregister(poll)
}
}