1 Commits

Author SHA1 Message Date
Harri Pehkonen
143b24c6a5 Add opt-in piped input support for POSIX systems
Enables applications to read piped data while maintaining interactive
keyboard input by redirecting stdin to /dev/tty when explicitly enabled.
2025-08-17 14:08:51 +02:00
8 changed files with 84 additions and 134 deletions

1
.gitignore vendored
View File

@@ -44,7 +44,6 @@ out/
!doc/**/*.html
!doc/**/*.xml
!doc/**/*.md
!doc/*.md
# examples directory:
!examples/**/*.cpp

View File

@@ -169,15 +169,13 @@ ftxui_cc_library(
"src/ftxui/component/util.cpp",
"src/ftxui/component/window.cpp",
# Private header from ftxui:dom.
"src/ftxui/dom/node_decorator.hpp",
# Private header from ftxui:screen.
"src/ftxui/screen/string_internal.hpp",
"src/ftxui/screen/util.hpp",
# Private header.
"include/ftxui/util/warn_windows_macro.hpp",
],
hdrs = [
"include/ftxui/component/animation.hpp",

View File

@@ -27,12 +27,6 @@ Next
- Remove dependency on 'pthread'.
### Component
- Feature: POSIX Piped Input Handling.
- Allows FTXUI applications to read data from stdin (when piped) while still receiving keyboard input from the terminal.
- Enabled by default.
- Can be disabled using `ScreenInteractive::HandlePipedInput(false)`.
- Only available on Linux and macOS.
Thanks @HarryPehkonen for PR #1094.
- Fix ScreenInteractive::FixedSize screen stomps on the preceding terminal
output. Thanks @zozowell in #1064.

View File

@@ -378,7 +378,21 @@ Several games using the FTXUI have been made during the Game Jam:
- [smoothlife](https://github.com/cpp-best-practices/game_jam/blob/main/Jam1_April_2022/smoothlife.md)
- [Consu](https://github.com/cpp-best-practices/game_jam/blob/main/Jam1_April_2022/consu.md)
## Advanced Usage
### Piped Input Support
If your application reads from stdin (piped data) and also needs interactive keyboard input:
```cpp
auto screen = ScreenInteractive::Fullscreen();
screen.HandlePipedInput(true); // Enable before Loop()
screen.Loop(component);
```
This allows commands like `cat data.txt | your_app` to work with full keyboard interaction.
**Note:** This feature is only available on POSIX systems (Linux/macOS). On Windows, the method call is a no-op.
## Build using CMake

View File

@@ -1,58 +0,0 @@
# POSIX Piped Input in FTXUI
> [!WARNING]
> This feature works only on Linux and macOS. It is not supported on
> Windows and WebAssembly.
## What is a POSIX Pipe?
A POSIX pipe is a way for two separate programs to communicate. One program sends its output directly as input to another program. Think of it like a one-way tube for data.
**Example:**
Imagine you want to list files and then filter them interactively.
- `ls`: Lists files.
- `interactive_grep`: An FTXUI application that filters text and lets you type.
You can connect them with a pipe (`|`):
```bash
ls -l | interactive_grep
```
Here's what happens:
1. `ls -l` lists files with details.
2. The `|` sends this list directly to `interactive_grep`.
3. `interactive_grep` receives the list and displays it. Because it's an FTXUI app, you can then type to filter the list, even though it received initial data from `ls`.
## How FTXUI Handles Piped Input
Now that you understand what a POSIX pipe is, let's look at how FTXUI uses them.
FTXUI lets your application read data from other programs (like from a pipe) while still allowing you to use your keyboard for interaction. This is useful for interactive command-line tools that process data.
Normally, FTXUI applications receive all input from `stdin`. However, when FTXUI detects that `stdin` is connected to the output of a pipe (meaning data is being piped into your application), it automatically switches to reading interactive keyboard input from `/dev/tty`. This ensures that your application can still receive user input even while processing piped data.
This feature is **turned on by default**.
If your FTXUI application needs to read piped data and also respond to keyboard input, you typically don't need to do anything special:
```cpp
auto screen = ScreenInteractive::Fullscreen();
// screen.HandlePipedInput(true); // This is enabled by default
screen.Loop(component);
```
## Turning Off Piped Input
If you don't need this feature, or if it conflicts with your custom input handling, you can turn it off.
To disable it, call `HandlePipedInput(false)` before starting your application's main loop:
```cpp
auto screen = ScreenInteractive::Fullscreen();
screen.HandlePipedInput(false); // Turn off piped input handling
screen.Loop(component);
```

View File

@@ -43,7 +43,7 @@ class ScreenInteractive : public Screen {
static ScreenInteractive TerminalOutput();
// Destructor.
~ScreenInteractive() override;
~ScreenInteractive();
// Options. Must be called before Loop().
void TrackMouse(bool enable = true);
@@ -101,8 +101,6 @@ class ScreenInteractive : public Screen {
void Draw(Component component);
void ResetCursorPosition();
void InstallPipedInputHandling();
void Signal(int signal);
void FetchTerminalEvents();
@@ -120,7 +118,6 @@ class ScreenInteractive : public Screen {
int dimx,
int dimy,
bool use_alternative_screen);
const Dimension dimension_;
const bool use_alternative_screen_;
@@ -145,10 +142,12 @@ class ScreenInteractive : public Screen {
bool force_handle_ctrl_c_ = true;
bool force_handle_ctrl_z_ = true;
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// Piped input handling state (POSIX only)
bool handle_piped_input_ = true;
// File descriptor for /dev/tty, used for piped input handling.
int tty_fd_ = -1;
bool handle_piped_input_ = false;
bool stdin_was_redirected_ = false;
int original_stdin_fd_ = -1;
#endif
// The style of the cursor to restore on exit.
int cursor_reset_shape_ = 1;

View File

@@ -112,13 +112,13 @@ void ftxui_on_resize(int columns, int rows) {
#else // POSIX (Linux & Mac)
int CheckStdinReady(int fd) {
int CheckStdinReady() {
timeval tv = {0, 0}; // NOLINT
fd_set fds;
FD_ZERO(&fds); // NOLINT
FD_SET(fd, &fds); // NOLINT
select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT
return FD_ISSET(fd, &fds); // NOLINT
FD_SET(STDIN_FILENO, &fds); // NOLINT
select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv); // NOLINT
return FD_ISSET(STDIN_FILENO, &fds); // NOLINT
}
#endif
@@ -373,16 +373,22 @@ void ScreenInteractive::TrackMouse(bool enable) {
}
/// @brief Enable or disable automatic piped input handling.
/// When enabled, FTXUI will detect piped input and redirect stdin from /dev/tty
/// When enabled, FTXUI will detect piped input and redirect stdin to /dev/tty
/// for keyboard input, allowing applications to read piped data while still
/// receiving interactive keyboard events.
/// @param enable Whether to enable piped input handling. Default is true.
/// @param enable Whether to enable piped input handling
/// @note This must be called before Loop().
/// @note This feature is enabled by default.
/// @note This feature is disabled by default for backward compatibility.
/// @note This feature is only available on POSIX systems (Linux/macOS).
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
void ScreenInteractive::HandlePipedInput(bool enable) {
handle_piped_input_ = enable;
}
#else
void ScreenInteractive::HandlePipedInput(bool /*enable*/) {
// This feature is not supported on this platform.
}
#endif
/// @brief Add a task to the main loop.
/// It will be executed later, after every other scheduled tasks.
@@ -539,8 +545,6 @@ void ScreenInteractive::Install() {
// https://github.com/ArthurSonzogni/FTXUI/issues/846
Flush();
InstallPipedInputHandling();
// After uninstalling the new configuration, flush it to the terminal to
// ensure it is fully applied:
on_exit_functions.emplace([] { Flush(); });
@@ -606,10 +610,9 @@ void ScreenInteractive::Install() {
}
struct termios terminal; // NOLINT
tcgetattr(tty_fd_, &terminal);
on_exit_functions.emplace([terminal = terminal, tty_fd_ = tty_fd_] {
tcsetattr(tty_fd_, TCSANOW, &terminal);
});
tcgetattr(STDIN_FILENO, &terminal);
on_exit_functions.emplace(
[=] { tcsetattr(STDIN_FILENO, TCSANOW, &terminal); });
// Enabling raw terminal input mode
terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition
@@ -637,7 +640,7 @@ void ScreenInteractive::Install() {
// read.
terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read.
tcsetattr(tty_fd_, TCSANOW, &terminal);
tcsetattr(STDIN_FILENO, TCSANOW, &terminal);
#endif
@@ -673,45 +676,47 @@ void ScreenInteractive::Install() {
// ensure it is fully applied:
Flush();
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// Handle piped input redirection if explicitly enabled by the application.
// This allows applications to read data from stdin while still receiving
// keyboard input from the terminal for interactive use.
if (handle_piped_input_ && !stdin_was_redirected_ && !isatty(STDIN_FILENO)) {
// Save the current stdin so we can restore it later
original_stdin_fd_ = dup(STDIN_FILENO);
if (original_stdin_fd_ >= 0) {
// Redirect stdin to the controlling terminal for keyboard input
if (freopen("/dev/tty", "r", stdin) != nullptr) {
stdin_was_redirected_ = true;
} else {
// Failed to open /dev/tty (containers, headless systems, etc.)
// Clean up and continue without redirection
close(original_stdin_fd_);
original_stdin_fd_ = -1;
}
}
// If dup() failed, we silently continue without redirection
}
#endif
quit_ = false;
PostAnimationTask();
}
void ScreenInteractive::InstallPipedInputHandling() {
tty_fd_ = fileno(stdin); // NOLINT
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// Handle piped input redirection if explicitly enabled by the application.
// This allows applications to read data from stdin while still receiving
// keyboard input from the terminal for interactive use.
if (!handle_piped_input_) {
return;
}
// If stdin is a terminal, we don't need to open /dev/tty.
if (isatty(fileno(stdin))) {
return;
}
// Open /dev/tty for keyboard input.
tty_fd_ = open("/dev/tty", O_RDONLY);
if (tty_fd_ < 0) {
// Failed to open /dev/tty (containers, headless systems, etc.)
tty_fd_ = fileno(stdin); // Fallback to stdin.
return;
}
// Close the /dev/tty file descriptor on exit.
on_exit_functions.emplace([this] {
close(tty_fd_);
tty_fd_ = -1;
});
#endif
}
// private
void ScreenInteractive::Uninstall() {
ExitNow();
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// Restore stdin to its original state if we redirected it
if (stdin_was_redirected_ && original_stdin_fd_ >= 0) {
dup2(original_stdin_fd_, STDIN_FILENO);
close(original_stdin_fd_);
original_stdin_fd_ = -1;
stdin_was_redirected_ = false;
}
#endif
OnExit();
}
@@ -1142,7 +1147,7 @@ void ScreenInteractive::FetchTerminalEvents() {
internal_->terminal_input_parser.Add(out[i]);
}
#else // POSIX (Linux & Mac)
if (!CheckStdinReady(tty_fd_)) {
if (!CheckStdinReady()) {
const auto timeout =
std::chrono::steady_clock::now() - internal_->last_char_time;
const size_t timeout_ms =
@@ -1154,7 +1159,7 @@ void ScreenInteractive::FetchTerminalEvents() {
// Read chars from the terminal.
std::array<char, 128> out{};
size_t l = read(tty_fd_, out.data(), out.size());
size_t l = read(fileno(stdin), out.data(), out.size());
// Convert the chars to events.
for (size_t i = 0; i < l; ++i) {

View File

@@ -63,24 +63,20 @@ class PipedInputTest : public ::testing::Test {
bool piped_stdin_setup_ = false;
};
TEST_F(PipedInputTest, DefaultBehaviorEnabled) {
// Test that HandlePipedInput is enabled by default
if (!IsTtyAvailable()) {
GTEST_SKIP() << "/dev/tty not available in this environment";
}
TEST_F(PipedInputTest, DefaultBehaviorNoChange) {
// Test that HandlePipedInput is disabled by default
auto screen = ScreenInteractive::TerminalOutput();
auto component = Renderer([] { return text("test"); });
SetupPipedStdin();
WriteToPipedStdin("test data\n");
// Install should redirect stdin since HandlePipedInput is on by default
// Install should not redirect stdin since HandlePipedInput not called
screen.Install();
// Stdin should be the tty
EXPECT_TRUE(isatty(STDIN_FILENO));
// Stdin should still be the pipe (isatty should return false)
EXPECT_FALSE(isatty(STDIN_FILENO));
screen.Uninstall();
}
@@ -101,7 +97,7 @@ TEST_F(PipedInputTest, ExplicitlyDisabled) {
screen.Uninstall();
}
TEST_F(PipedInputTest, ExplicitlyEnabled) {
TEST_F(PipedInputTest, PipedInputDetectionAndRedirection) {
if (!IsTtyAvailable()) {
GTEST_SKIP() << "/dev/tty not available in this environment";
}
@@ -131,6 +127,7 @@ TEST_F(PipedInputTest, ExplicitlyEnabled) {
TEST_F(PipedInputTest, NormalStdinUnchanged) {
// Test that normal stdin (not piped) is not affected
auto screen = ScreenInteractive::TerminalOutput();
screen.HandlePipedInput(true);
auto component = Renderer([] { return text("test"); });
// Don't setup piped stdin - use normal stdin
@@ -153,6 +150,7 @@ TEST_F(PipedInputTest, MultipleInstallUninstallCycles) {
}
auto screen = ScreenInteractive::TerminalOutput();
screen.HandlePipedInput(true);
auto component = Renderer([] { return text("test"); });
SetupPipedStdin();
@@ -194,6 +192,7 @@ TEST_F(PipedInputTest, HandlePipedInputMethodBehavior) {
// This test simulates environments like containers where /dev/tty might not exist
TEST_F(PipedInputTest, GracefulFallbackWhenTtyUnavailable) {
auto screen = ScreenInteractive::TerminalOutput();
screen.HandlePipedInput(true);
auto component = Renderer([] { return text("test"); });
SetupPipedStdin();