Skip to content

Commit

Permalink
Added a function that reads a key with cursor hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Jan 23, 2024
1 parent de2f15a commit ba35c34
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/term.rs
Expand Up @@ -292,15 +292,25 @@ impl Term {
if !self.is_tty {
Ok(Key::Unknown)
} else {
read_single_key(false)
read_single_key(false, false)
}
}

/// Like [`read_key`](Self::read_key) but disables the cursor.
pub fn read_key_no_cursor(&self) -> io::Result<Key> {
if !self.is_tty {
Ok(Key::Unknown)
} else {
read_single_key(false, true)
}
}

/// Like [`read_key`](Self::read_key) but it can catch `CtrlC`.
pub fn read_key_raw(&self) -> io::Result<Key> {
if !self.is_tty {
Ok(Key::Unknown)
} else {
read_single_key(true)
read_single_key(true, false)
}
}

Expand Down
13 changes: 12 additions & 1 deletion src/unix_term.rs
Expand Up @@ -294,7 +294,7 @@ fn read_single_key_impl(fd: i32) -> Result<Key, io::Error> {
}
}

pub fn read_single_key(ctrlc_key: bool) -> io::Result<Key> {
pub fn read_single_key(ctrlc_key: bool, hide_cursor: bool) -> io::Result<Key> {
let tty_f;
let fd = unsafe {
if libc::isatty(libc::STDIN_FILENO) == 1 {
Expand All @@ -314,8 +314,19 @@ pub fn read_single_key(ctrlc_key: bool) -> io::Result<Key> {
unsafe { libc::cfmakeraw(&mut termios) };
termios.c_oflag = original.c_oflag;
c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSADRAIN, &termios) })?;

if hide_cursor {
unsafe {
libc::write(fd, b"\x1b[?25l".as_ptr() as *const _, 6);
}
}
let rv: io::Result<Key> = read_single_key_impl(fd);
c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSADRAIN, &original) })?;
if hide_cursor {
unsafe {
libc::write(fd, b"\x1b[?25h".as_ptr() as *const _, 6);
}
}

// if the user hit ^C we want to signal SIGINT to outselves.
if let Err(ref err) = rv {
Expand Down
2 changes: 1 addition & 1 deletion src/wasm_term.rs
Expand Up @@ -39,7 +39,7 @@ pub fn read_secure() -> io::Result<String> {
))
}

pub fn read_single_key(_ctrlc_key: bool) -> io::Result<Key> {
pub fn read_single_key(_ctrlc_key: bool, _hide_cursor: bool) -> io::Result<Key> {
Err(io::Error::new(
io::ErrorKind::Other,
"unsupported operation",
Expand Down
4 changes: 2 additions & 2 deletions src/windows_term/mod.rs
Expand Up @@ -354,7 +354,7 @@ pub fn key_from_key_code(code: VIRTUAL_KEY) -> Key {
pub fn read_secure() -> io::Result<String> {
let mut rv = String::new();
loop {
match read_single_key(false)? {
match read_single_key(false, false)? {
Key::Enter => {
break;
}
Expand All @@ -373,7 +373,7 @@ pub fn read_secure() -> io::Result<String> {
Ok(rv)
}

pub fn read_single_key(_ctrlc_key: bool) -> io::Result<Key> {
pub fn read_single_key(_ctrlc_key: bool, _hide_cursor: bool) -> io::Result<Key> {
let key_event = read_key_event()?;

let unicode_char = unsafe { key_event.uChar.UnicodeChar };
Expand Down

0 comments on commit ba35c34

Please sign in to comment.