Skip to content

Commit

Permalink
Merge pull request #17 from kumavale/build/update_nix
Browse files Browse the repository at this point in the history
update nix version
  • Loading branch information
kumavale authored Dec 31, 2024
2 parents dc627f4 + 21f497c commit 88f8624
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ features = [
libc = "0.2"

[target.'cfg(unix)'.dependencies]
nix = "0.26"
nix = { version = "0.29.0", features = ["term"] }
37 changes: 20 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
//! ```no_run
//! use getch_rs::{Getch, Key};
//!
//! fn main() {
//! let g = Getch::new();
//! let g = Getch::new();
//!
//! println!("press `q` to exit");
//! println!("press `q` to exit");
//!
//! loop {
//! match g.getch() {
//! Ok(Key::Char('q')) => break,
//! Ok(key) => println!("{:?}", key),
//! Err(e) => println!("{}", e),
//! }
//! loop {
//! match g.getch() {
//! Ok(Key::Char('q')) => break,
//! Ok(key) => println!("{:?}", key),
//! Err(e) => println!("{}", e),
//! }
//! }
//! ```
Expand Down Expand Up @@ -118,9 +116,11 @@ impl Getch {
#[cfg(not(windows))]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let stdin = std::io::stdin();

// Quering original as a separate, since `Termios` does not implement copy
let orig_term = termios::tcgetattr(0).unwrap();
let mut raw_termios = termios::tcgetattr(0).unwrap();
let orig_term = termios::tcgetattr(&stdin).unwrap();
let mut raw_termios = termios::tcgetattr(&stdin).unwrap();

// Unset canonical mode, so we get characters immediately
raw_termios.local_flags.remove(termios::LocalFlags::ICANON);
Expand All @@ -129,7 +129,7 @@ impl Getch {
// Disable local echo
raw_termios.local_flags.remove(termios::LocalFlags::ECHO);

termios::tcsetattr(0, termios::SetArg::TCSADRAIN, &raw_termios).unwrap();
termios::tcsetattr(&stdin, termios::SetArg::TCSADRAIN, &raw_termios).unwrap();

Self {
orig_term,
Expand Down Expand Up @@ -189,9 +189,10 @@ pub fn enable_echo_input() {

#[cfg(not(windows))]
{
let mut raw_termios = termios::tcgetattr(0).unwrap();
let stdin = std::io::stdin();
let mut raw_termios = termios::tcgetattr(&stdin).unwrap();
raw_termios.local_flags.insert(termios::LocalFlags::ECHO);
termios::tcsetattr(0, termios::SetArg::TCSADRAIN, &raw_termios).unwrap();
termios::tcsetattr(&stdin, termios::SetArg::TCSADRAIN, &raw_termios).unwrap();
}
}

Expand All @@ -213,9 +214,10 @@ pub fn disable_echo_input() {

#[cfg(not(windows))]
{
let mut raw_termios = termios::tcgetattr(0).unwrap();
let stdin = std::io::stdin();
let mut raw_termios = termios::tcgetattr(&stdin).unwrap();
raw_termios.local_flags.remove(termios::LocalFlags::ECHO);
termios::tcsetattr(0, termios::SetArg::TCSADRAIN, &raw_termios).unwrap();
termios::tcsetattr(&stdin, termios::SetArg::TCSADRAIN, &raw_termios).unwrap();
}
}

Expand Down Expand Up @@ -373,6 +375,7 @@ impl Drop for Getch {

#[cfg(not(windows))]
fn drop(&mut self) {
termios::tcsetattr(0, termios::SetArg::TCSADRAIN, &self.orig_term).unwrap();
let stdin = std::io::stdin();
termios::tcsetattr(&stdin, termios::SetArg::TCSADRAIN, &self.orig_term).unwrap();
}
}

0 comments on commit 88f8624

Please sign in to comment.