7 Commits

Author SHA1 Message Date
ArthurSonzogni
e85d655b4d Fix stdin references. 2025-08-17 20:21:20 +02:00
ArthurSonzogni
4b0d0984a5 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().
2025-08-17 19:48:54 +02:00
ArthurSonzogni
b0ba518d85 docs: Update CHANGELOG.md for POSIX Piped Input Handling
Added an entry for the POSIX Piped Input Handling feature, including
details about its functionality, default state, and how to disable it.
Also attributed @HarryPehkonen for their contribution in PR #1094.
2025-08-17 19:32:28 +02:00
ArthurSonzogni
be39aed592 Tweak implementation and documentation. 2025-08-17 19:24:26 +02:00
ArthurSonzogni
587e7620f1 Update doc 2025-08-17 19:24:26 +02:00
Harri Pehkonen
def5c6d50c 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 19:24:26 +02:00
ArthurSonzogni
0dde21f09e Fix Bazel build. 2025-08-17 19:23:53 +02:00
8 changed files with 134 additions and 84 deletions

1
.gitignore vendored
View File

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

View File

@@ -169,13 +169,15 @@ 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,6 +27,12 @@ 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,21 +378,7 @@ 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

58
doc/posix_pipe.md Normal file
View File

@@ -0,0 +1,58 @@
# 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();
~ScreenInteractive() override;
// Options. Must be called before Loop().
void TrackMouse(bool enable = true);
@@ -101,6 +101,8 @@ class ScreenInteractive : public Screen {
void Draw(Component component);
void ResetCursorPosition();
void InstallPipedInputHandling();
void Signal(int signal);
void FetchTerminalEvents();
@@ -118,6 +120,7 @@ class ScreenInteractive : public Screen {
int dimx,
int dimy,
bool use_alternative_screen);
const Dimension dimension_;
const bool use_alternative_screen_;
@@ -142,12 +145,10 @@ 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_ = false;
bool stdin_was_redirected_ = false;
int original_stdin_fd_ = -1;
#endif
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.
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 CheckStdinReady(int fd) {
timeval tv = {0, 0}; // NOLINT
fd_set fds;
FD_ZERO(&fds); // NOLINT
FD_SET(STDIN_FILENO, &fds); // NOLINT
select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv); // NOLINT
return FD_ISSET(STDIN_FILENO, &fds); // NOLINT
FD_SET(fd, &fds); // NOLINT
select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT
return FD_ISSET(fd, &fds); // NOLINT
}
#endif
@@ -373,22 +373,16 @@ void ScreenInteractive::TrackMouse(bool enable) {
}
/// @brief Enable or disable automatic piped input handling.
/// When enabled, FTXUI will detect piped input and redirect stdin to /dev/tty
/// When enabled, FTXUI will detect piped input and redirect stdin from /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
/// @param enable Whether to enable piped input handling. Default is true.
/// @note This must be called before Loop().
/// @note This feature is disabled by default for backward compatibility.
/// @note This feature is enabled by default.
/// @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.
@@ -545,6 +539,8 @@ 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(); });
@@ -610,9 +606,10 @@ void ScreenInteractive::Install() {
}
struct termios terminal; // NOLINT
tcgetattr(STDIN_FILENO, &terminal);
on_exit_functions.emplace(
[=] { tcsetattr(STDIN_FILENO, TCSANOW, &terminal); });
tcgetattr(tty_fd_, &terminal);
on_exit_functions.emplace([terminal = terminal, tty_fd_ = tty_fd_] {
tcsetattr(tty_fd_, TCSANOW, &terminal);
});
// Enabling raw terminal input mode
terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition
@@ -640,7 +637,7 @@ void ScreenInteractive::Install() {
// read.
terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read.
tcsetattr(STDIN_FILENO, TCSANOW, &terminal);
tcsetattr(tty_fd_, TCSANOW, &terminal);
#endif
@@ -676,47 +673,45 @@ 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();
}
@@ -1147,7 +1142,7 @@ void ScreenInteractive::FetchTerminalEvents() {
internal_->terminal_input_parser.Add(out[i]);
}
#else // POSIX (Linux & Mac)
if (!CheckStdinReady()) {
if (!CheckStdinReady(tty_fd_)) {
const auto timeout =
std::chrono::steady_clock::now() - internal_->last_char_time;
const size_t timeout_ms =
@@ -1159,7 +1154,7 @@ void ScreenInteractive::FetchTerminalEvents() {
// Read chars from the terminal.
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.
for (size_t i = 0; i < l; ++i) {

View File

@@ -63,19 +63,23 @@ class PipedInputTest : public ::testing::Test {
bool piped_stdin_setup_ = false;
};
TEST_F(PipedInputTest, DefaultBehaviorNoChange) {
// Test that HandlePipedInput is disabled by default
TEST_F(PipedInputTest, DefaultBehaviorEnabled) {
// Test that HandlePipedInput is enabled by default
if (!IsTtyAvailable()) {
GTEST_SKIP() << "/dev/tty not available in this environment";
}
auto screen = ScreenInteractive::TerminalOutput();
auto component = Renderer([] { return text("test"); });
SetupPipedStdin();
WriteToPipedStdin("test data\n");
// Install should not redirect stdin since HandlePipedInput not called
// Install should redirect stdin since HandlePipedInput is on by default
screen.Install();
// Stdin should still be the pipe (isatty should return false)
EXPECT_FALSE(isatty(STDIN_FILENO));
// Stdin should be the tty
EXPECT_TRUE(isatty(STDIN_FILENO));
screen.Uninstall();
}
@@ -97,7 +101,7 @@ TEST_F(PipedInputTest, ExplicitlyDisabled) {
screen.Uninstall();
}
TEST_F(PipedInputTest, PipedInputDetectionAndRedirection) {
TEST_F(PipedInputTest, ExplicitlyEnabled) {
if (!IsTtyAvailable()) {
GTEST_SKIP() << "/dev/tty not available in this environment";
}
@@ -127,7 +131,6 @@ TEST_F(PipedInputTest, PipedInputDetectionAndRedirection) {
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
@@ -150,7 +153,6 @@ TEST_F(PipedInputTest, MultipleInstallUninstallCycles) {
}
auto screen = ScreenInteractive::TerminalOutput();
screen.HandlePipedInput(true);
auto component = Renderer([] { return text("test"); });
SetupPipedStdin();
@@ -192,7 +194,6 @@ 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();