Skip to content

Commit

Permalink
Merge pull request #589 from gwenn/buf_read
Browse files Browse the repository at this point in the history
Use a BufReader on unix platform
  • Loading branch information
gwenn authored Jan 2, 2022
2 parents 870c5fb + e32f147 commit 0f5c499
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ regex = { version = "1.5.4", optional = true }
nix = "0.23"
utf8parse = "0.2"
skim = { version = "0.9", optional = true }
buf_redux = "0.8"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["consoleapi", "handleapi", "minwindef", "processenv", "std", "winbase", "wincon", "winuser"] }
Expand Down
12 changes: 10 additions & 2 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::os::unix::io::{AsRawFd, RawFd};
use std::sync;
use std::sync::atomic::{AtomicBool, Ordering};

use buf_redux::BufReader;
use log::{debug, warn};
use nix::poll::{self, PollFlags};
use nix::sys::signal;
Expand Down Expand Up @@ -149,7 +150,7 @@ impl Read for StdinRaw {

/// Console input reader
pub struct PosixRawReader {
stdin: StdinRaw,
stdin: BufReader<StdinRaw>,
timeout_ms: i32,
buf: [u8; 1],
parser: Parser,
Expand Down Expand Up @@ -191,7 +192,7 @@ const RXVT_CTRL_SHIFT: char = '@';
impl PosixRawReader {
fn new(config: &Config, key_map: PosixKeyMap) -> Self {
Self {
stdin: StdinRaw {},
stdin: BufReader::with_capacity(1024, StdinRaw {}),
timeout_ms: config.keyseq_timeout(),
buf: [0; 1],
parser: Parser::new(),
Expand Down Expand Up @@ -644,6 +645,10 @@ impl PosixRawReader {
}

fn poll(&mut self, timeout_ms: i32) -> ::nix::Result<i32> {
let n = self.stdin.buf_len();
if n > 0 {
return Ok(n as i32);
}
let mut fds = [poll::PollFd::new(STDIN_FILENO, PollFlags::POLLIN)];
let r = poll::poll(&mut fds, timeout_ms);
match r {
Expand All @@ -666,6 +671,9 @@ impl RawReader for PosixRawReader {

let mut key = KeyEvent::new(c, M::NONE);
if key == E::ESC {
if self.stdin.buf_len() > 0 {
debug!(target: "rustyline", "read buffer {:?}", self.stdin.buffer());
}
let timeout_ms = if single_esc_abort && self.timeout_ms == -1 {
0
} else {
Expand Down

0 comments on commit 0f5c499

Please sign in to comment.