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

read Esc without delay on non-windows #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 13 additions & 1 deletion input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ struct Input {
friend std::ostream& operator<<(std::ostream& os, const Input& inp);

private:
// Function to set stdin non-blocking on Unix-like systems
#ifndef _WIN32
static void set_non_blocking(bool enable) {
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, enable ? flags | O_NONBLOCK : flags & ~O_NONBLOCK);
}
#endif

using reader_fn = char (*)();
static Input read_helper(reader_fn get_char) {
char byte = get_char();
Expand Down Expand Up @@ -185,6 +193,7 @@ struct Input {
break;
case SpecKey::Esc: {
#ifndef _WIN32
set_non_blocking(true);
char next_byte = get_char();

if (next_byte == 79 || next_byte == 91) {
Expand Down Expand Up @@ -216,8 +225,11 @@ struct Input {
// ignore_byte = get_char();
break;
}
break;
} else {
input = Input(SpecKey::Esc);
}
set_non_blocking(false);
break;
#endif
input = Input(SpecKey::Esc);
}
Expand Down
Loading