2 Commits

Author SHA1 Message Date
Harri Pehkonen
63c39010b5 Merge 143b24c6a5 into f3448f49f1 2025-08-17 12:16:30 +00:00
Arthur Sonzogni
f3448f49f1 Improve example style (#1097)
Some checks are pending
Build / Bazel, ${{ matrix.cxx }}, ${{ matrix.os }} (cl, cl, windows-latest) (push) Waiting to run
Build / Bazel, ${{ matrix.cxx }}, ${{ matrix.os }} (clang, clang++, macos-latest) (push) Waiting to run
Build / Bazel, ${{ matrix.cxx }}, ${{ matrix.os }} (clang, clang++, ubuntu-latest) (push) Waiting to run
Build / Bazel, ${{ matrix.cxx }}, ${{ matrix.os }} (gcc, g++, macos-latest) (push) Waiting to run
Build / Bazel, ${{ matrix.cxx }}, ${{ matrix.os }} (gcc, g++, ubuntu-latest) (push) Waiting to run
Build / CMake, ${{ matrix.compiler }}, ${{ matrix.os }} (cl, Windows MSVC, windows-latest) (push) Waiting to run
Build / CMake, ${{ matrix.compiler }}, ${{ matrix.os }} (gcc, Linux GCC, ubuntu-latest) (push) Waiting to run
Build / CMake, ${{ matrix.compiler }}, ${{ matrix.os }} (llvm, llvm-cov gcov, Linux Clang, ubuntu-latest) (push) Waiting to run
Build / CMake, ${{ matrix.compiler }}, ${{ matrix.os }} (llvm, llvm-cov gcov, MacOS clang, macos-latest) (push) Waiting to run
Build / Test modules (llvm, ubuntu-latest) (push) Waiting to run
Documentation / documentation (push) Waiting to run
Based uppon @yurenchen000 suggestion.

Fixed:https://github.com/ArthurSonzogni/FTXUI/issues/1090
2025-08-17 13:53:19 +02:00
9 changed files with 235 additions and 144 deletions

1
.gitignore vendored
View File

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

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) - [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

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

@@ -1,15 +1,19 @@
@import url(https://fonts.googleapis.com/css?family=Khula:700); @import url(https://fonts.googleapis.com/css?family=Khula:700);
html {
--toc-width: 250px;
}
body { body {
background-color:#EEE; background-color: #EEE;
padding:0px; padding: 0px;
margin:0px; margin: 0px;
font-family: Khula, Helvetica, sans-serif; font-family: Khula, Helvetica, sans-serif;
font-size: 130%; font-size: 130%;
} }
.page { .page {
max-width:1300px; max-width: 1300px;
margin: auto; margin: auto;
padding: 10px; padding: 10px;
} }
@@ -20,7 +24,7 @@ a {
margin: 0 -.25rem; margin: 0 -.25rem;
padding: 0 .25rem; padding: 0 .25rem;
transition: color .3s ease-in-out, transition: color .3s ease-in-out,
box-shadow .3s ease-in-out; box-shadow .3s ease-in-out;
} }
a:hover { a:hover {
@@ -30,45 +34,48 @@ a:hover {
h1 { h1 {
text-decoration: underline; text-decoration: underline;
width:100%; width: 100%;
background-color: rgba(100,100,255,0.5); background-color: rgba(100, 100, 255, 0.5);
padding: 10px; padding: 10px;
margin: 0; margin: 0;
} }
#selectExample { #selectExample {
flex:1; flex: 1;
} }
#selectExample, #selectExample option { #selectExample,
#selectExample option {
font-size: 16px; font-size: 16px;
font-family: sans-serif; font-family: sans-serif;
font-weight: 700; font-weight: 700;
line-height: 1.3; line-height: 1.3;
border:0px; border: 0px;
background-color: #bbb; background-color: #bbb;
color:black; color: black;
} }
#selectExample:focus { #selectExample:focus {
outline:none; outline: none;
} }
#terminal { #terminal {
width:100%; width: 100%;
height 500px; height 500px;
height: calc(clamp(200px, 100vh - 300px, 900px)); height: calc(clamp(200px, 100vh - 300px, 900px));
overflow: hidden; overflow: hidden;
border:none; border: none;
background-color:black; padding: 10px;
margin: 10px;
} }
#terminalContainer { #terminalContainer {
overflow: hidden; overflow: hidden;
border-radius: 10px; border-radius: 10px;
box-shadow: 0px 2px 10px 0px rgba(0,0,0,0.75), box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.75),
0px 2px 80px 0px rgba(0,0,0,0.50); 0px 2px 80px 0px rgba(0, 0, 0, 0.50);
background-color: black;
} }
.fakeButtons { .fakeButtons {
@@ -76,7 +83,7 @@ h1 {
width: 10px; width: 10px;
border-radius: 50%; border-radius: 50%;
border: 1px solid #000; border: 1px solid #000;
margin:6px; margin: 6px;
background-color: #ff3b47; background-color: #ff3b47;
border-color: #9d252b; border-color: #9d252b;
display: inline-block; display: inline-block;
@@ -95,13 +102,79 @@ h1 {
} }
.fakeMenu { .fakeMenu {
display:flex; display: flex;
flex-direction: row; flex-direction: row;
width:100%; width: 100%;
box-sizing: border-box; box-sizing: border-box;
height: 25px; height: 25px;
background-color: #bbb; background-color: #bbb;
color:black; color: black;
margin: 0 auto; margin: 0 auto;
overflow: hidden; overflow: hidden;
} }
.toc-container {
position: fixed;
left: 0;
top: 0;
bottom: 0;
width: var(--toc-width);
background: white;
padding: 0;
overflow-y: auto;
overflow-x: hidden;
scrollbar-width: thin;
}
.toc-title {
font-weight: bold;
margin-bottom: 5px;
font-size: 0.9em;
color: #555;
position: sticky;
transition: position 1.0s ease-in-out;
top: 0;
z-index: 1;
padding: 20px;
margin: 0;
border-bottom: 1px solid #ddd;
/* Gradient background for the title */
background-color: #f0f0f0;
}
.toc-item {
padding: 3px 8px;
margin: 0;
cursor: pointer;
font-size: 0.85em;
border-radius: 3px;
transition: background 0.2s;
}
.toc-item:hover {
background: #f0f0f0;
}
.toc-item.selected {
background: #e0e0e0;
font-weight: bold;
}
@media (max-width: 1024px) {
.toc-container {
display: none;
}
.page {
margin-left: 0;
}
}
@media (min-width: 1025px) {
.page {
margin-left: calc(var(--toc-width) + 20px);
}
}

View File

@@ -9,13 +9,18 @@
<script type="module" src="index.mjs"></script> <script type="module" src="index.mjs"></script>
</head> </head>
<body> <body>
<div class="toc-container">
<div class="toc-list"></div>
</div>
<script id="example_script"></script> <script id="example_script"></script>
<div class="page"> <div class="page">
<p> <p>
<a href="https://github.com/ArthurSonzogni/FTXUI">FTXUI</a> is a simple <a href="https://github.com/ArthurSonzogni/FTXUI">FTXUI</a> is a simple
functional C++ library for terminal user interface. <br/> functional C++ library for terminal user interface. <br/>
This showcases the: <a href="https://github.com/ArthurSonzogni/FTXUI/tree/master/examples">./example/</a> folder. <br/> This showcases the: <a
href="https://github.com/ArthurSonzogni/FTXUI/tree/master/examples">./example/</a>
folder. See <a id="source">source</a>.
</p> </p>
<div id="terminalContainer"> <div id="terminalContainer">

View File

@@ -92,6 +92,69 @@ window.Module = {
}, },
}; };
const source = document.querySelector("#source");
source.href = "https://github.com/ArthurSonzogni/FTXUI/blob/main/examples/" + example + ".cpp";
const words = example.split('/') const words = example.split('/')
words[1] = "ftxui_example_" + words[1] + ".js" words[1] = "ftxui_example_" + words[1] + ".js"
document.querySelector("#example_script").src = words.join('/'); document.querySelector("#example_script").src = words.join('/');
// Table of Contents (TOC) for quick navigation.
// Get select element
const selectEl = document.querySelector('select#selectExample');
if (!selectEl) {
console.error('select#selectExample not found');
} else {
// Get TOC container
const tocContainer = document.querySelector('.toc-container');
const tocList = tocContainer.querySelector('.toc-list');
// Group options by directory
const groupedOptions = Array.from(selectEl.options).reduce((acc, option) => {
const [dir, file] = option.text.split('/');
if (!acc[dir]) {
acc[dir] = [];
}
acc[dir].push({ option, file });
return acc;
}, {});
// Generate TOC items
for (const dir in groupedOptions) {
const dirContainer = document.createElement('div');
const dirHeader = document.createElement('div');
dirHeader.textContent = dir;
dirHeader.className = 'toc-title';
dirContainer.appendChild(dirHeader);
groupedOptions[dir].forEach(({ option, file }) => {
const tocItem = document.createElement('div');
tocItem.textContent = file;
tocItem.className = 'toc-item';
if (selectEl.options[selectEl.selectedIndex].value === option.value) {
tocItem.classList.add('selected');
}
// Click handler
tocItem.addEventListener('click', () => {
for(let i=0; i<selectEl.options.length; ++i) {
if (selectEl.options[i].value == option.value) {
selectEl.selectedIndex = i;
break;
}
}
history.pushState({}, "", "?file=" + option.value);
location.reload();
});
dirContainer.appendChild(tocItem);
});
tocList.appendChild(dirContainer);
}
}''

View File

@@ -43,7 +43,7 @@ class ScreenInteractive : public Screen {
static ScreenInteractive TerminalOutput(); static ScreenInteractive TerminalOutput();
// Destructor. // Destructor.
~ScreenInteractive() override; ~ScreenInteractive();
// Options. Must be called before Loop(). // Options. Must be called before Loop().
void TrackMouse(bool enable = true); void TrackMouse(bool enable = true);
@@ -101,8 +101,6 @@ 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();
@@ -120,7 +118,6 @@ 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_;
@@ -145,8 +142,12 @@ 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_ = true; 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. // The style of the cursor to restore on exit.
int cursor_reset_shape_ = 1; int cursor_reset_shape_ = 1;

View File

@@ -373,16 +373,22 @@ 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 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 /// 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. Default is true. /// @param enable Whether to enable piped input handling
/// @note This must be called before Loop(). /// @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). /// @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.
@@ -670,58 +676,47 @@ void ScreenInteractive::Install() {
// ensure it is fully applied: // ensure it is fully applied:
Flush(); Flush();
// Redirect the true terminal to stdin, so that we can read keyboard input #if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// directly from stdin, even if the input is piped from a file or another // Handle piped input redirection if explicitly enabled by the application.
// process. // This allows applications to read data from stdin while still receiving
// // keyboard input from the terminal for interactive use.
// TODO: Instead of redirecting stdin, we could define the file descriptor to if (handle_piped_input_ && !stdin_was_redirected_ && !isatty(STDIN_FILENO)) {
// read from, and use it in the TerminalInputParser. // Save the current stdin so we can restore it later
InstallPipedInputHandling(); 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; 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();
} }

View File

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