feat: Improve POSIX piped input handling

Refactored the POSIX piped input handling to avoid redirecting stdin.
Instead, it now directly opens and reads from /dev/tty for keyboard input
when stdin is piped, allowing applications to process piped data while
still receiving interactive keyboard events.

This addresses the TODO comment in ScreenInteractive::InstallPipedInputHandling().
This commit is contained in:
ArthurSonzogni
2025-08-17 19:40:20 +02:00
parent 2ffbb9550e
commit d59b77e6a0
2 changed files with 18 additions and 21 deletions

View File

@@ -147,6 +147,8 @@ class ScreenInteractive : public Screen {
// Piped input handling state (POSIX only) // Piped input handling state (POSIX only)
bool handle_piped_input_ = true; bool handle_piped_input_ = true;
// File descriptor for /dev/tty, used for piped input handling.
int tty_fd_ = -1;
// The style of the cursor to restore on exit. // The style of the cursor to restore on exit.
int cursor_reset_shape_ = 1; int cursor_reset_shape_ = 1;

View File

@@ -112,13 +112,13 @@ void ftxui_on_resize(int columns, int rows) {
#else // POSIX (Linux & Mac) #else // POSIX (Linux & Mac)
int CheckStdinReady() { int CheckStdinReady(int fd) {
timeval tv = {0, 0}; // NOLINT timeval tv = {0, 0}; // NOLINT
fd_set fds; fd_set fds;
FD_ZERO(&fds); // NOLINT FD_ZERO(&fds); // NOLINT
FD_SET(STDIN_FILENO, &fds); // NOLINT FD_SET(fd, &fds); // NOLINT
select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv); // NOLINT select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT
return FD_ISSET(STDIN_FILENO, &fds); // NOLINT return FD_ISSET(fd, &fds); // NOLINT
} }
#endif #endif
@@ -684,6 +684,7 @@ void ScreenInteractive::Install() {
} }
void ScreenInteractive::InstallPipedInputHandling() { void ScreenInteractive::InstallPipedInputHandling() {
tty_fd_ = STDIN_FILENO; // Default to stdin.
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__) #if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// Handle piped input redirection if explicitly enabled by the application. // Handle piped input redirection if explicitly enabled by the application.
// This allows applications to read data from stdin while still receiving // This allows applications to read data from stdin while still receiving
@@ -692,29 +693,23 @@ void ScreenInteractive::InstallPipedInputHandling() {
return; return;
} }
// If stdin is a terminal, we don't need to redirect it. // If stdin is a terminal, we don't need to open /dev/tty.
if (isatty(STDIN_FILENO)) { if (isatty(STDIN_FILENO)) {
return; return;
} }
// Save the current stdin so we can restore it later. // Open /dev/tty for keyboard input.
int original_fd = dup(STDIN_FILENO); tty_fd_ = open("/dev/tty", O_RDONLY);
if (original_fd < 0) { if (tty_fd_ < 0) {
return;
}
// Redirect stdin to the controlling terminal for keyboard input.
if (std::freopen("/dev/tty", "r", stdin) == nullptr) {
// Failed to open /dev/tty (containers, headless systems, etc.) // Failed to open /dev/tty (containers, headless systems, etc.)
// Clean up and continue without redirection tty_fd_ = STDIN_FILENO; // Fallback to stdin.
close(original_fd);
return; return;
} }
// Restore the original stdin file descriptor on exit. // Close the /dev/tty file descriptor on exit.
on_exit_functions.emplace([=] { on_exit_functions.emplace([this] {
dup2(original_fd, STDIN_FILENO); close(tty_fd_);
close(original_fd); tty_fd_ = -1;
}); });
#endif #endif
} }
@@ -1152,7 +1147,7 @@ void ScreenInteractive::FetchTerminalEvents() {
internal_->terminal_input_parser.Add(out[i]); internal_->terminal_input_parser.Add(out[i]);
} }
#else // POSIX (Linux & Mac) #else // POSIX (Linux & Mac)
if (!CheckStdinReady()) { if (!CheckStdinReady(tty_fd_)) {
const auto timeout = const auto timeout =
std::chrono::steady_clock::now() - internal_->last_char_time; std::chrono::steady_clock::now() - internal_->last_char_time;
const size_t timeout_ms = const size_t timeout_ms =
@@ -1164,7 +1159,7 @@ void ScreenInteractive::FetchTerminalEvents() {
// Read chars from the terminal. // Read chars from the terminal.
std::array<char, 128> out{}; std::array<char, 128> out{};
size_t l = read(fileno(stdin), out.data(), out.size()); size_t l = read(tty_fd_, out.data(), out.size());
// Convert the chars to events. // Convert the chars to events.
for (size_t i = 0; i < l; ++i) { for (size_t i = 0; i < l; ++i) {