From bf09447eb22f1d02314bb12507b5d69e1bb68263 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 24 Feb 2024 13:53:15 +0100 Subject: [PATCH] Upgrade nix to 0.28 --- Cargo.toml | 2 +- src/config.rs | 14 +++++++------- src/tty/unix.rs | 44 ++++++++++++++++++++------------------------ 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 58b99eb2c..581105ccc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ regex = { version = "1.5.5", optional = true } rustyline-derive = { version = "0.10.0", optional = true, path = "rustyline-derive" } [target.'cfg(unix)'.dependencies] -nix = { version = "0.27", default-features = false, features = ["fs", "ioctl", "poll", "signal", "term"] } +nix = { version = "0.28", default-features = false, features = ["fs", "ioctl", "poll", "signal", "term"] } utf8parse = "0.2" skim = { version = "0.10", optional = true, default-features = false } signal-hook = { version = "0.3", optional = true, default-features = false } diff --git a/src/config.rs b/src/config.rs index 5a531d3f3..bb6ec65ae 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,7 +15,7 @@ pub struct Config { completion_prompt_limit: usize, /// Duration (milliseconds) Rustyline will wait for a character when /// reading an ambiguous key sequence. - keyseq_timeout: i32, + keyseq_timeout: Option, /// Emacs or Vi mode edit_mode: EditMode, /// If true, each nonblank line returned by `readline` will be @@ -108,7 +108,7 @@ impl Config { /// /// By default, no timeout (-1) or 500ms if `EditMode::Vi` is activated. #[must_use] - pub fn keyseq_timeout(&self) -> i32 { + pub fn keyseq_timeout(&self) -> Option { self.keyseq_timeout } @@ -217,7 +217,7 @@ impl Default for Config { history_ignore_space: false, completion_type: CompletionType::Circular, // TODO Validate completion_prompt_limit: 100, - keyseq_timeout: -1, + keyseq_timeout: None, edit_mode: EditMode::Emacs, auto_add_history: false, bell_style: BellStyle::default(), @@ -383,7 +383,7 @@ impl Builder { /// After seeing an ESC key, wait at most `keyseq_timeout_ms` for another /// byte. #[must_use] - pub fn keyseq_timeout(mut self, keyseq_timeout_ms: i32) -> Self { + pub fn keyseq_timeout(mut self, keyseq_timeout_ms: Option) -> Self { self.set_keyseq_timeout(keyseq_timeout_ms); self } @@ -526,7 +526,7 @@ pub trait Configurer { } /// Timeout for ambiguous key sequences in milliseconds. - fn set_keyseq_timeout(&mut self, keyseq_timeout_ms: i32) { + fn set_keyseq_timeout(&mut self, keyseq_timeout_ms: Option) { self.config_mut().keyseq_timeout = keyseq_timeout_ms; } @@ -534,8 +534,8 @@ pub trait Configurer { fn set_edit_mode(&mut self, edit_mode: EditMode) { self.config_mut().edit_mode = edit_mode; match edit_mode { - EditMode::Emacs => self.set_keyseq_timeout(-1), // no timeout - EditMode::Vi => self.set_keyseq_timeout(500), + EditMode::Emacs => self.set_keyseq_timeout(None), // no timeout + EditMode::Vi => self.set_keyseq_timeout(Some(500)), } } diff --git a/src/tty/unix.rs b/src/tty/unix.rs index b4bdd3877..c0c8a0d24 100644 --- a/src/tty/unix.rs +++ b/src/tty/unix.rs @@ -15,7 +15,7 @@ use std::sync::{Arc, Mutex}; use log::{debug, warn}; use nix::errno::Errno; -use nix::poll::{self, PollFlags}; +use nix::poll::{self, PollFlags, PollTimeout}; use nix::sys::select::{self, FdSet}; #[cfg(not(feature = "termios"))] use nix::sys::termios::Termios; @@ -190,7 +190,7 @@ type PipeWriter = (Arc>, SyncSender); /// Console input reader pub struct PosixRawReader { tty_in: BufReader, - timeout_ms: i32, + timeout_ms: PollTimeout, parser: Parser, key_map: PosixKeyMap, // external print reader @@ -255,7 +255,7 @@ impl PosixRawReader { }; Self { tty_in, - timeout_ms: config.keyseq_timeout(), + timeout_ms: config.keyseq_timeout().into(), parser: Parser::new(), key_map, pipe_reader, @@ -298,8 +298,8 @@ impl PosixRawReader { if !allow_recurse { return Ok(E::ESC); } - let timeout = if self.timeout_ms < 0 { - 100 + let timeout = if self.timeout_ms.is_none() { + 100u8.into() } else { self.timeout_ms }; @@ -702,12 +702,12 @@ impl PosixRawReader { }) } - fn poll(&mut self, timeout_ms: i32) -> Result { + fn poll(&mut self, timeout_ms: PollTimeout) -> Result { let n = self.tty_in.buffer().len(); if n > 0 { return Ok(n as i32); } - let mut fds = [poll::PollFd::new(self, PollFlags::POLLIN)]; + let mut fds = [poll::PollFd::new(self.as_fd(), PollFlags::POLLIN)]; let r = poll::poll(&mut fds, timeout_ms); match r { Ok(n) => Ok(n), @@ -736,11 +736,11 @@ impl PosixRawReader { .map(|fd| unsafe { BorrowedFd::borrow_raw(fd) }); loop { let mut readfds = FdSet::new(); - if let Some(ref sigwinch_pipe) = sigwinch_pipe { + if let Some(sigwinch_pipe) = sigwinch_pipe { readfds.insert(sigwinch_pipe); } - readfds.insert(&tty_in); - if let Some(ref pipe_reader) = pipe_reader { + readfds.insert(tty_in); + if let Some(pipe_reader) = pipe_reader { readfds.insert(pipe_reader); } if let Err(err) = select::select(None, Some(&mut readfds), None, None, None) { @@ -752,10 +752,10 @@ impl PosixRawReader { continue; } }; - if sigwinch_pipe.map_or(false, |fd| readfds.contains(&fd)) { + if sigwinch_pipe.map_or(false, |fd| readfds.contains(fd)) { self.tty_in.get_ref().sigwinch()?; return Err(ReadlineError::WindowResized); - } else if readfds.contains(&tty_in) { + } else if readfds.contains(tty_in) { // prefer user input over external print return self.next_key(single_esc_abort).map(Event::KeyPress); } else if let Some(ref pipe_reader) = self.pipe_reader { @@ -794,8 +794,8 @@ impl RawReader for PosixRawReader { if !self.tty_in.buffer().is_empty() { debug!(target: "rustyline", "read buffer {:?}", self.tty_in.buffer()); } - let timeout_ms = if single_esc_abort && self.timeout_ms == -1 { - 0 + let timeout_ms = if single_esc_abort && self.timeout_ms.is_none() { + PollTimeout::ZERO } else { self.timeout_ms }; @@ -1119,14 +1119,14 @@ impl Renderer for PosixRenderer { } fn move_cursor_at_leftmost(&mut self, rdr: &mut PosixRawReader) -> Result<()> { - if rdr.poll(0)? != 0 { + if rdr.poll(PollTimeout::ZERO)? != 0 { debug!(target: "rustyline", "cannot request cursor location"); return Ok(()); } /* Report cursor location */ self.write_and_flush("\x1b[6n")?; /* Read the response: ESC [ rows ; cols R */ - if rdr.poll(100)? == 0 + if rdr.poll(PollTimeout::from(100u8))? == 0 || rdr.next_char()? != '\x1b' || rdr.next_char()? != '[' || read_digits_until(rdr, ';')?.is_none() @@ -1163,7 +1163,7 @@ fn read_digits_until(rdr: &mut PosixRawReader, sep: char) -> Result> fn write_all(fd: RawFd, buf: &str) -> nix::Result<()> { let mut bytes = buf.as_bytes(); while !bytes.is_empty() { - match write(fd, bytes) { + match write(unsafe { BorrowedFd::borrow_raw(fd) }, bytes) { Ok(0) => return Err(Errno::EIO), Ok(n) => bytes = &bytes[n..], Err(Errno::EINTR) => {} @@ -1194,7 +1194,7 @@ fn set_cursor_visibility(fd: RawFd, visible: bool) -> Result