3 Commits

Author SHA1 Message Date
ArthurSonzogni
dad2eaaa28 Tweak implementation and documentation. 2025-08-17 19:19:06 +02:00
ArthurSonzogni
5c3e3151a5 Update doc 2025-08-17 17:21:24 +02:00
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
7 changed files with 93 additions and 67 deletions

View File

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

View File

@@ -27,16 +27,8 @@ Next
- Remove dependency on 'pthread'. - Remove dependency on 'pthread'.
### Component ### 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 - Fix ScreenInteractive::FixedSize screen stomps on the preceding terminal
output. Thanks @zozowell in #1064. output. Thanks @zozowell in #1064.
- Fix vertical `ftxui::Slider`. The "up" key was previously decreasing the
value. Thanks @its-pablo in #1093 for reporting the issue.
6.1.9 (2025-05-07) 6.1.9 (2025-05-07)

View File

@@ -17,12 +17,10 @@ add_subdirectory(dom)
if (EMSCRIPTEN) if (EMSCRIPTEN)
get_property(EXAMPLES GLOBAL PROPERTY FTXUI::EXAMPLES) get_property(EXAMPLES GLOBAL PROPERTY FTXUI::EXAMPLES)
foreach(file foreach(file
"index.css"
"index.html" "index.html"
"index.mjs" "index.mjs"
"run_webassembly.py" "index.css"
"sw.js" "run_webassembly.py")
)
configure_file(${file} ${file}) configure_file(${file} ${file})
endforeach(file) endforeach(file)
endif() endif()

View File

@@ -4,7 +4,7 @@
#ifndef FTXUI_COMPONENT_RECEIVER_HPP_ #ifndef FTXUI_COMPONENT_RECEIVER_HPP_
#define FTXUI_COMPONENT_RECEIVER_HPP_ #define FTXUI_COMPONENT_RECEIVER_HPP_
#include <ftxui/util/warn_windows_macro.hpp> #include <ftxui/util/warn_windows_macro.h>
#include <algorithm> // for copy, max #include <algorithm> // for copy, max
#include <atomic> // for atomic, __atomic_base #include <atomic> // for atomic, __atomic_base
#include <condition_variable> // for condition_variable #include <condition_variable> // for condition_variable

View File

@@ -147,8 +147,6 @@ 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 fd) { int CheckStdinReady() {
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(fd, &fds); // NOLINT FD_SET(STDIN_FILENO, &fds); // NOLINT
select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv); // NOLINT
return FD_ISSET(fd, &fds); // NOLINT return FD_ISSET(STDIN_FILENO, &fds); // NOLINT
} }
#endif #endif
@@ -539,8 +539,6 @@ void ScreenInteractive::Install() {
// https://github.com/ArthurSonzogni/FTXUI/issues/846 // https://github.com/ArthurSonzogni/FTXUI/issues/846
Flush(); Flush();
InstallPipedInputHandling();
// After uninstalling the new configuration, flush it to the terminal to // After uninstalling the new configuration, flush it to the terminal to
// ensure it is fully applied: // ensure it is fully applied:
on_exit_functions.emplace([] { Flush(); }); on_exit_functions.emplace([] { Flush(); });
@@ -606,10 +604,9 @@ void ScreenInteractive::Install() {
} }
struct termios terminal; // NOLINT struct termios terminal; // NOLINT
tcgetattr(tty_fd_, &terminal); tcgetattr(STDIN_FILENO, &terminal);
on_exit_functions.emplace([terminal = terminal, tty_fd_ = tty_fd_] { on_exit_functions.emplace(
tcsetattr(tty_fd_, TCSANOW, &terminal); [=] { tcsetattr(STDIN_FILENO, TCSANOW, &terminal); });
});
// Enabling raw terminal input mode // Enabling raw terminal input mode
terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition
@@ -637,7 +634,7 @@ void ScreenInteractive::Install() {
// read. // read.
terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read. terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read.
tcsetattr(tty_fd_, TCSANOW, &terminal); tcsetattr(STDIN_FILENO, TCSANOW, &terminal);
#endif #endif
@@ -673,13 +670,20 @@ 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
// directly from stdin, even if the input is piped from a file or another
// process.
//
// TODO: Instead of redirecting stdin, we could define the file descriptor to
// read from, and use it in the TerminalInputParser.
InstallPipedInputHandling();
quit_ = false; quit_ = false;
PostAnimationTask(); PostAnimationTask();
} }
void ScreenInteractive::InstallPipedInputHandling() { void ScreenInteractive::InstallPipedInputHandling() {
tty_fd_ = fileno(stdin); // NOLINT
#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
@@ -688,23 +692,29 @@ void ScreenInteractive::InstallPipedInputHandling() {
return; return;
} }
// If stdin is a terminal, we don't need to open /dev/tty. // If stdin is a terminal, we don't need to redirect it.
if (isatty(fileno(stdin))) { if (isatty(STDIN_FILENO)) {
return; return;
} }
// Open /dev/tty for keyboard input. // Save the current stdin so we can restore it later.
tty_fd_ = open("/dev/tty", O_RDONLY); int original_fd = dup(STDIN_FILENO);
if (tty_fd_ < 0) { 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.) // Failed to open /dev/tty (containers, headless systems, etc.)
tty_fd_ = fileno(stdin); // Fallback to stdin. // Clean up and continue without redirection
close(original_fd);
return; return;
} }
// Close the /dev/tty file descriptor on exit. // Restore the original stdin file descriptor on exit.
on_exit_functions.emplace([this] { on_exit_functions.emplace([=] {
close(tty_fd_); dup2(original_fd, STDIN_FILENO);
tty_fd_ = -1; close(original_fd);
}); });
#endif #endif
} }
@@ -1142,7 +1152,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(tty_fd_)) { if (!CheckStdinReady()) {
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 =
@@ -1154,7 +1164,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(tty_fd_, out.data(), out.size()); size_t l = read(fileno(stdin), 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) {

View File

@@ -33,20 +33,6 @@ Decorator flexDirection(Direction direction) {
return xflex; // NOT_REACHED() return xflex; // NOT_REACHED()
} }
Direction Opposite(Direction d) {
switch (d) {
case Direction::Up:
return Direction::Down;
case Direction::Down:
return Direction::Up;
case Direction::Left:
return Direction::Right;
case Direction::Right:
return Direction::Left;
}
return d; // NOT_REACHED()
}
template <class T> template <class T>
class SliderBase : public SliderOption<T>, public ComponentBase { class SliderBase : public SliderOption<T>, public ComponentBase {
public: public:
@@ -61,15 +47,59 @@ class SliderBase : public SliderOption<T>, public ComponentBase {
flexDirection(this->direction) | reflect(gauge_box_) | gauge_color; flexDirection(this->direction) | reflect(gauge_box_) | gauge_color;
} }
void OnDirection(Direction pressed) { void OnLeft() {
if (pressed == this->direction) { switch (this->direction) {
this->value() += this->increment(); case Direction::Right:
return; this->value() -= this->increment();
break;
case Direction::Left:
this->value() += this->increment();
break;
case Direction::Up:
case Direction::Down:
break;
} }
}
if (pressed == Opposite(this->direction)) { void OnRight() {
this->value() -= this->increment(); switch (this->direction) {
return; case Direction::Right:
this->value() += this->increment();
break;
case Direction::Left:
this->value() -= this->increment();
break;
case Direction::Up:
case Direction::Down:
break;
}
}
void OnUp() {
switch (this->direction) {
case Direction::Up:
this->value() -= this->increment();
break;
case Direction::Down:
this->value() += this->increment();
break;
case Direction::Left:
case Direction::Right:
break;
}
}
void OnDown() {
switch (this->direction) {
case Direction::Down:
this->value() += this->increment();
break;
case Direction::Up:
this->value() -= this->increment();
break;
case Direction::Left:
case Direction::Right:
break;
} }
} }
@@ -80,16 +110,16 @@ class SliderBase : public SliderOption<T>, public ComponentBase {
T old_value = this->value(); T old_value = this->value();
if (event == Event::ArrowLeft || event == Event::Character('h')) { if (event == Event::ArrowLeft || event == Event::Character('h')) {
OnDirection(Direction::Left); OnLeft();
} }
if (event == Event::ArrowRight || event == Event::Character('l')) { if (event == Event::ArrowRight || event == Event::Character('l')) {
OnDirection(Direction::Right); OnRight();
} }
if (event == Event::ArrowUp || event == Event::Character('k')) { if (event == Event::ArrowUp || event == Event::Character('k')) {
OnDirection(Direction::Up); OnDown();
} }
if (event == Event::ArrowDown || event == Event::Character('j')) { if (event == Event::ArrowDown || event == Event::Character('j')) {
OnDirection(Direction::Down); OnUp();
} }
this->value() = std::max(this->min(), std::min(this->max(), this->value())); this->value() = std::max(this->min(), std::min(this->max(), this->value()));