From 78c3aee6971d1e3f17c4db98397a23009c409077 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 28 Jan 2018 15:35:16 -0800 Subject: [PATCH] Add support for responding to escape sequences --- src/term/mod.rs | 27 +++++++++++++++++++++++---- src/xwin.rs | 6 ++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/term/mod.rs b/src/term/mod.rs index 7c6124fbe..4c936654f 100644 --- a/src/term/mod.rs +++ b/src/term/mod.rs @@ -318,6 +318,12 @@ pub struct TerminalState { /// If true then the terminal state has changed state_changed: bool, + + /// Some parsing operations may yield responses that need + /// to be returned to the client. They are collected here + /// and this is used as the result of the advance_bytes() + /// method. + answerback: Option>, } impl TerminalState { @@ -337,6 +343,7 @@ impl TerminalState { cursor_x: 0, cursor_y: 0, state_changed: true, + answerback: None, } } @@ -435,6 +442,12 @@ impl TerminalState { pub fn cursor_pos(&self) -> (usize, usize) { (self.cursor_x, self.cursor_y) } + + fn push_answerback(&mut self, buf: &[u8]) { + let mut result = self.answerback.take().unwrap_or_else(Vec::new); + result.extend_from_slice(buf); + self.answerback = Some(result) + } } pub struct Terminal { @@ -466,12 +479,15 @@ impl Terminal { } } - /// Feed the terminal parser a slice of bytes of input - pub fn advance_bytes>(&mut self, bytes: B) { + /// Feed the terminal parser a slice of bytes of input. + /// The return value is an optional sequence of bytes which should + /// we sent back to the client. + pub fn advance_bytes>(&mut self, bytes: B) -> Option> { let bytes = bytes.as_ref(); for b in bytes.iter() { self.parser.advance(&mut self.state, *b); } + self.answerback.take() } } @@ -594,10 +610,13 @@ impl vte::Perform for TerminalState { CSIAction::SetDecPrivateMode(DecPrivateMode::ApplicationCursorKeys, _on) => {} CSIAction::SetDecPrivateMode(DecPrivateMode::BrackedPaste, _on) => {} CSIAction::DeviceStatusReport => { - // TODO: should emit "CSI 0 n" + // "OK" + self.push_answerback(b"\x1b[0n"); } CSIAction::ReportCursorPosition => { - // TODO: should emit "CSI r ; c R" + let row = self.cursor_y + 1; + let col = self.cursor_x + 1; + self.push_answerback(format!("\x1b[{};{}R", row, col).as_bytes()); } } } diff --git a/src/xwin.rs b/src/xwin.rs index 703fa59b8..194bc415b 100644 --- a/src/xwin.rs +++ b/src/xwin.rs @@ -1,7 +1,7 @@ use failure::Error; use font::{Font, ftwrap}; use pty::MasterPty; -use std::io::Read; +use std::io::{Read, Write}; use std::mem; use std::process::Child; use std::slice; @@ -408,7 +408,9 @@ impl<'a> TerminalWindow<'a> { loop { match self.pty.read(&mut buf) { Ok(size) => { - self.terminal.advance_bytes(&buf[0..size]); + if let Some(answer) = self.terminal.advance_bytes(&buf[0..size]) { + self.pty.write(&answer).ok(); // discard error + } self.need_paint = true; if size < BUFSIZE { // If we had a short read then there is no more