Skip to content

Commit

Permalink
Merge branch 'rustix'
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Apr 20, 2023
2 parents 1d64ef6 + 8d5cc4a commit b5d2654
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gix-prompt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ thiserror = "1.0.32"
parking_lot = "0.12.1"

[target.'cfg(unix)'.dependencies]
nix = { version = "0.26.1", default-features = false, features = ["term"] }
rustix = { version = "0.37.13", features = ["termios"] }

[dev-dependencies]
gix-testtools = { path = "../tests/tools"}
Expand Down
2 changes: 1 addition & 1 deletion gix-prompt/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum Error {
TtyIo(#[from] std::io::Error),
#[cfg(unix)]
#[error("Failed to obtain or set terminal configuration")]
TerminalConfiguration(#[from] nix::errno::Errno),
TerminalConfiguration(#[from] rustix::io::Errno),
}

/// The way the user is prompted.
Expand Down
62 changes: 46 additions & 16 deletions gix-prompt/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ pub const TTY_PATH: &str = "/dev/tty";
#[cfg(unix)]
pub(crate) mod imp {
use std::{
io::{BufRead, Write},
os::unix::io::{AsRawFd, RawFd},
fs::File,
io,
io::{BufRead, Read, Write},
};

use nix::sys::{termios, termios::Termios};
use parking_lot::{const_mutex, lock_api::MutexGuard, Mutex, RawMutex};
use rustix::termios::{self, Termios};

use crate::{unix::TTY_PATH, Error, Mode, Options};

Expand All @@ -21,8 +22,10 @@ pub(crate) mod imp {
Mode::Disable => Err(Error::Disabled),
Mode::Hidden => {
let state = TERM_STATE.lock();
let mut in_out = std::fs::OpenOptions::new().write(true).read(true).open(TTY_PATH)?;
let restore = save_term_state_and_disable_echo(state, in_out.as_raw_fd())?;
let mut in_out = save_term_state_and_disable_echo(
state,
std::fs::OpenOptions::new().write(true).read(true).open(TTY_PATH)?,
)?;
in_out.write_all(prompt.as_bytes())?;

let mut buf_read = std::io::BufReader::with_capacity(64, in_out);
Expand All @@ -33,7 +36,7 @@ pub(crate) mod imp {
if out.ends_with('\r') {
out.pop();
}
restore.now()?;
buf_read.into_inner().restore_term_state()?;
Ok(out)
}
Mode::Visible => {
Expand All @@ -52,41 +55,68 @@ pub(crate) mod imp {

struct RestoreTerminalStateOnDrop<'a> {
state: TermiosGuard<'a>,
fd: RawFd,
fd: File,
}

impl<'a> Read for RestoreTerminalStateOnDrop<'a> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.fd.read(buf)
}

fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
self.fd.read_vectored(bufs)
}
}

impl<'a> Write for RestoreTerminalStateOnDrop<'a> {
#[inline(always)]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.fd.write(buf)
}

#[inline(always)]
fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
self.fd.write_vectored(bufs)
}

#[inline(always)]
fn flush(&mut self) -> io::Result<()> {
self.fd.flush()
}
}

impl<'a> RestoreTerminalStateOnDrop<'a> {
fn now(mut self) -> Result<(), Error> {
fn restore_term_state(mut self) -> Result<(), Error> {
let state = self.state.take().expect("BUG: we exist only if something is saved");
termios::tcsetattr(self.fd, termios::SetArg::TCSAFLUSH, &state)?;
termios::tcsetattr(&self.fd, termios::OptionalActions::Flush, &state)?;
Ok(())
}
}

impl<'a> Drop for RestoreTerminalStateOnDrop<'a> {
fn drop(&mut self) {
if let Some(state) = self.state.take() {
termios::tcsetattr(self.fd, termios::SetArg::TCSAFLUSH, &state).ok();
termios::tcsetattr(&self.fd, termios::OptionalActions::Flush, &state).ok();
}
}
}

fn save_term_state_and_disable_echo(
mut state: TermiosGuard<'_>,
fd: RawFd,
fd: File,
) -> Result<RestoreTerminalStateOnDrop<'_>, Error> {
assert!(
state.is_none(),
"BUG: recursive calls are not possible and we restore afterwards"
);

let prev = termios::tcgetattr(fd)?;
let mut new = prev.clone();
let prev = termios::tcgetattr(&fd)?;
let mut new = prev;
*state = prev.into();

new.local_flags &= !termios::LocalFlags::ECHO;
new.local_flags |= termios::LocalFlags::ECHONL;
termios::tcsetattr(fd, termios::SetArg::TCSAFLUSH, &new)?;
new.c_lflag &= !termios::ECHO;
new.c_lflag |= termios::ECHONL;
termios::tcsetattr(&fd, termios::OptionalActions::Flush, &new)?;

Ok(RestoreTerminalStateOnDrop { fd, state })
}
Expand Down

0 comments on commit b5d2654

Please sign in to comment.