Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a BufReader on unix platform #589

Merged
merged 1 commit into from
Jan 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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