Skip to content

Commit

Permalink
Return Ctrl+C instead of SIGINT (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeevab committed Jan 9, 2024
1 parent 75ed017 commit b1e432a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/kb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ pub enum Key {
PageUp,
PageDown,
Char(char),
CtrlC,
}
10 changes: 9 additions & 1 deletion src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,15 @@ impl Term {
if !self.is_tty {
Ok(Key::Unknown)
} else {
read_single_key()
read_single_key(false)
}
}

pub fn read_key_raw(&self) -> io::Result<Key> {
if !self.is_tty {
Ok(Key::Unknown)
} else {
read_single_key(true)
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/unix_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn read_single_key_impl(fd: i32) -> Result<Key, io::Error> {
}
}

pub fn read_single_key() -> io::Result<Key> {
pub fn read_single_key(ctrlc_key: bool) -> io::Result<Key> {
let tty_f;
let fd = unsafe {
if libc::isatty(libc::STDIN_FILENO) == 1 {
Expand All @@ -321,8 +321,12 @@ pub fn read_single_key() -> io::Result<Key> {
// if the user hit ^C we want to signal SIGINT to outselves.
if let Err(ref err) = rv {
if err.kind() == io::ErrorKind::Interrupted {
unsafe {
libc::raise(libc::SIGINT);
if !ctrlc_key {
unsafe {
libc::raise(libc::SIGINT);
}
} else {
return Ok(Key::CtrlC);
}
}
}
Expand Down

0 comments on commit b1e432a

Please sign in to comment.