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

Added a function that reads a key with cursor hidden #202

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions src/term.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading