mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Compare commits
2 Commits
63c39010b5
...
HarryPehko
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dad2eaaa28 | ||
|
|
5c3e3151a5 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -44,6 +44,7 @@ out/
|
|||||||
!doc/**/*.html
|
!doc/**/*.html
|
||||||
!doc/**/*.xml
|
!doc/**/*.xml
|
||||||
!doc/**/*.md
|
!doc/**/*.md
|
||||||
|
!doc/*.md
|
||||||
|
|
||||||
# examples directory:
|
# examples directory:
|
||||||
!examples/**/*.cpp
|
!examples/**/*.cpp
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -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)
|
- [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)
|
- [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
|
## Build using CMake
|
||||||
|
|
||||||
|
|||||||
58
doc/posix_pipe.md
Normal file
58
doc/posix_pipe.md
Normal 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);
|
||||||
|
```
|
||||||
@@ -43,7 +43,7 @@ class ScreenInteractive : public Screen {
|
|||||||
static ScreenInteractive TerminalOutput();
|
static ScreenInteractive TerminalOutput();
|
||||||
|
|
||||||
// Destructor.
|
// Destructor.
|
||||||
~ScreenInteractive();
|
~ScreenInteractive() override;
|
||||||
|
|
||||||
// Options. Must be called before Loop().
|
// Options. Must be called before Loop().
|
||||||
void TrackMouse(bool enable = true);
|
void TrackMouse(bool enable = true);
|
||||||
@@ -101,6 +101,8 @@ class ScreenInteractive : public Screen {
|
|||||||
void Draw(Component component);
|
void Draw(Component component);
|
||||||
void ResetCursorPosition();
|
void ResetCursorPosition();
|
||||||
|
|
||||||
|
void InstallPipedInputHandling();
|
||||||
|
|
||||||
void Signal(int signal);
|
void Signal(int signal);
|
||||||
|
|
||||||
void FetchTerminalEvents();
|
void FetchTerminalEvents();
|
||||||
@@ -118,6 +120,7 @@ class ScreenInteractive : public Screen {
|
|||||||
int dimx,
|
int dimx,
|
||||||
int dimy,
|
int dimy,
|
||||||
bool use_alternative_screen);
|
bool use_alternative_screen);
|
||||||
|
|
||||||
const Dimension dimension_;
|
const Dimension dimension_;
|
||||||
const bool use_alternative_screen_;
|
const bool use_alternative_screen_;
|
||||||
|
|
||||||
@@ -142,12 +145,8 @@ class ScreenInteractive : public Screen {
|
|||||||
bool force_handle_ctrl_c_ = true;
|
bool force_handle_ctrl_c_ = true;
|
||||||
bool force_handle_ctrl_z_ = true;
|
bool force_handle_ctrl_z_ = true;
|
||||||
|
|
||||||
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
|
||||||
// Piped input handling state (POSIX only)
|
// Piped input handling state (POSIX only)
|
||||||
bool handle_piped_input_ = false;
|
bool handle_piped_input_ = true;
|
||||||
bool stdin_was_redirected_ = false;
|
|
||||||
int original_stdin_fd_ = -1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// 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;
|
||||||
|
|||||||
@@ -373,22 +373,16 @@ void ScreenInteractive::TrackMouse(bool enable) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Enable or disable automatic piped input handling.
|
/// @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
|
/// for keyboard input, allowing applications to read piped data while still
|
||||||
/// receiving interactive keyboard events.
|
/// 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 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).
|
/// @note This feature is only available on POSIX systems (Linux/macOS).
|
||||||
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
|
||||||
void ScreenInteractive::HandlePipedInput(bool enable) {
|
void ScreenInteractive::HandlePipedInput(bool enable) {
|
||||||
handle_piped_input_ = 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.
|
/// @brief Add a task to the main loop.
|
||||||
/// It will be executed later, after every other scheduled tasks.
|
/// It will be executed later, after every other scheduled tasks.
|
||||||
@@ -676,47 +670,58 @@ void ScreenInteractive::Install() {
|
|||||||
// ensure it is fully applied:
|
// ensure it is fully applied:
|
||||||
Flush();
|
Flush();
|
||||||
|
|
||||||
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
// Redirect the true terminal to stdin, so that we can read keyboard input
|
||||||
// Handle piped input redirection if explicitly enabled by the application.
|
// directly from stdin, even if the input is piped from a file or another
|
||||||
// This allows applications to read data from stdin while still receiving
|
// process.
|
||||||
// keyboard input from the terminal for interactive use.
|
//
|
||||||
if (handle_piped_input_ && !stdin_was_redirected_ && !isatty(STDIN_FILENO)) {
|
// TODO: Instead of redirecting stdin, we could define the file descriptor to
|
||||||
// Save the current stdin so we can restore it later
|
// read from, and use it in the TerminalInputParser.
|
||||||
original_stdin_fd_ = dup(STDIN_FILENO);
|
InstallPipedInputHandling();
|
||||||
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;
|
quit_ = false;
|
||||||
|
|
||||||
PostAnimationTask();
|
PostAnimationTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScreenInteractive::InstallPipedInputHandling() {
|
||||||
|
#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 redirect it.
|
||||||
|
if (isatty(STDIN_FILENO)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the current stdin so we can restore it later.
|
||||||
|
int original_fd = dup(STDIN_FILENO);
|
||||||
|
if (original_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.)
|
||||||
|
// Clean up and continue without redirection
|
||||||
|
close(original_fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore the original stdin file descriptor on exit.
|
||||||
|
on_exit_functions.emplace([=] {
|
||||||
|
dup2(original_fd, STDIN_FILENO);
|
||||||
|
close(original_fd);
|
||||||
|
});
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// private
|
// private
|
||||||
void ScreenInteractive::Uninstall() {
|
void ScreenInteractive::Uninstall() {
|
||||||
ExitNow();
|
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();
|
OnExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,20 +63,24 @@ class PipedInputTest : public ::testing::Test {
|
|||||||
bool piped_stdin_setup_ = false;
|
bool piped_stdin_setup_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(PipedInputTest, DefaultBehaviorNoChange) {
|
TEST_F(PipedInputTest, DefaultBehaviorEnabled) {
|
||||||
// Test that HandlePipedInput is disabled by default
|
// Test that HandlePipedInput is enabled by default
|
||||||
|
if (!IsTtyAvailable()) {
|
||||||
|
GTEST_SKIP() << "/dev/tty not available in this environment";
|
||||||
|
}
|
||||||
|
|
||||||
auto screen = ScreenInteractive::TerminalOutput();
|
auto screen = ScreenInteractive::TerminalOutput();
|
||||||
auto component = Renderer([] { return text("test"); });
|
auto component = Renderer([] { return text("test"); });
|
||||||
|
|
||||||
SetupPipedStdin();
|
SetupPipedStdin();
|
||||||
WriteToPipedStdin("test data\n");
|
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();
|
screen.Install();
|
||||||
|
|
||||||
// Stdin should still be the pipe (isatty should return false)
|
// Stdin should be the tty
|
||||||
EXPECT_FALSE(isatty(STDIN_FILENO));
|
EXPECT_TRUE(isatty(STDIN_FILENO));
|
||||||
|
|
||||||
screen.Uninstall();
|
screen.Uninstall();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +101,7 @@ TEST_F(PipedInputTest, ExplicitlyDisabled) {
|
|||||||
screen.Uninstall();
|
screen.Uninstall();
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PipedInputTest, PipedInputDetectionAndRedirection) {
|
TEST_F(PipedInputTest, ExplicitlyEnabled) {
|
||||||
if (!IsTtyAvailable()) {
|
if (!IsTtyAvailable()) {
|
||||||
GTEST_SKIP() << "/dev/tty not available in this environment";
|
GTEST_SKIP() << "/dev/tty not available in this environment";
|
||||||
}
|
}
|
||||||
@@ -127,7 +131,6 @@ TEST_F(PipedInputTest, PipedInputDetectionAndRedirection) {
|
|||||||
TEST_F(PipedInputTest, NormalStdinUnchanged) {
|
TEST_F(PipedInputTest, NormalStdinUnchanged) {
|
||||||
// Test that normal stdin (not piped) is not affected
|
// Test that normal stdin (not piped) is not affected
|
||||||
auto screen = ScreenInteractive::TerminalOutput();
|
auto screen = ScreenInteractive::TerminalOutput();
|
||||||
screen.HandlePipedInput(true);
|
|
||||||
auto component = Renderer([] { return text("test"); });
|
auto component = Renderer([] { return text("test"); });
|
||||||
|
|
||||||
// Don't setup piped stdin - use normal stdin
|
// Don't setup piped stdin - use normal stdin
|
||||||
@@ -150,7 +153,6 @@ TEST_F(PipedInputTest, MultipleInstallUninstallCycles) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto screen = ScreenInteractive::TerminalOutput();
|
auto screen = ScreenInteractive::TerminalOutput();
|
||||||
screen.HandlePipedInput(true);
|
|
||||||
auto component = Renderer([] { return text("test"); });
|
auto component = Renderer([] { return text("test"); });
|
||||||
|
|
||||||
SetupPipedStdin();
|
SetupPipedStdin();
|
||||||
@@ -192,7 +194,6 @@ TEST_F(PipedInputTest, HandlePipedInputMethodBehavior) {
|
|||||||
// This test simulates environments like containers where /dev/tty might not exist
|
// This test simulates environments like containers where /dev/tty might not exist
|
||||||
TEST_F(PipedInputTest, GracefulFallbackWhenTtyUnavailable) {
|
TEST_F(PipedInputTest, GracefulFallbackWhenTtyUnavailable) {
|
||||||
auto screen = ScreenInteractive::TerminalOutput();
|
auto screen = ScreenInteractive::TerminalOutput();
|
||||||
screen.HandlePipedInput(true);
|
|
||||||
auto component = Renderer([] { return text("test"); });
|
auto component = Renderer([] { return text("test"); });
|
||||||
|
|
||||||
SetupPipedStdin();
|
SetupPipedStdin();
|
||||||
|
|||||||
Reference in New Issue
Block a user