Fix errors.

This commit is contained in:
ArthurSonzogni
2025-08-24 05:08:54 +02:00
parent 2b876a8712
commit a6612eda35
4 changed files with 33 additions and 31 deletions

View File

@@ -4,7 +4,6 @@
#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 <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
@@ -12,6 +11,7 @@
#include <mutex> // for mutex, unique_lock #include <mutex> // for mutex, unique_lock
#include <queue> // for queue #include <queue> // for queue
#include <utility> // for move #include <utility> // for move
#include "ftxui/util/warn_windows_macro.hpp"
namespace ftxui { namespace ftxui {

View File

@@ -2,8 +2,8 @@
// Use of this source code is governed by the MIT license that can be found in // Use of this source code is governed by the MIT license that can be found in
// the LICENSE file. // the LICENSE file.
#ifndef FTXUI_UTIL_WARN_WINDOWS_MACRO_H_ #ifndef FTXUI_UTIL_WARN_WINDOWS_MACRO_HPP_
#define FTXUI_UTIL_WARN_WINDOWS_MACRO_H_ #define FTXUI_UTIL_WARN_WINDOWS_MACRO_HPP_
#ifdef min #ifdef min
#error \ #error \
@@ -15,4 +15,4 @@
"The macro 'max' is defined, which conflicts with the standard C++ library and FTXUI. This is often caused by including <windows.h>. To fix this, add '#define NOMINMAX' before including <windows.h>, or pass '/DNOMINMAX' as a compiler flag." "The macro 'max' is defined, which conflicts with the standard C++ library and FTXUI. This is often caused by including <windows.h>. To fix this, add '#define NOMINMAX' before including <windows.h>, or pass '/DNOMINMAX' as a compiler flag."
#endif #endif
#endif // FTXUI_UTIL_WARN_WINDOWS_MACRO_H_ #endif // FTXUI_UTIL_WARN_WINDOWS_MACRO_HPP_

View File

@@ -115,7 +115,7 @@ void ftxui_on_resize(int columns, int rows) {
int CheckStdinReady(int fd) { int CheckStdinReady(int fd) {
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(fd, &fds); // NOLINT
select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT
return FD_ISSET(fd, &fds); // NOLINT return FD_ISSET(fd, &fds); // NOLINT
@@ -679,7 +679,7 @@ void ScreenInteractive::Install() {
} }
void ScreenInteractive::InstallPipedInputHandling() { void ScreenInteractive::InstallPipedInputHandling() {
tty_fd_ = fileno(stdin); // NOLINT tty_fd_ = STDIN_FILENO;
#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
@@ -689,7 +689,7 @@ void ScreenInteractive::InstallPipedInputHandling() {
} }
// If stdin is a terminal, we don't need to open /dev/tty. // If stdin is a terminal, we don't need to open /dev/tty.
if (isatty(fileno(stdin))) { if (isatty(STDIN_FILENO)) {
return; return;
} }
@@ -697,7 +697,7 @@ void ScreenInteractive::InstallPipedInputHandling() {
tty_fd_ = open("/dev/tty", O_RDONLY); tty_fd_ = open("/dev/tty", O_RDONLY);
if (tty_fd_ < 0) { if (tty_fd_ < 0) {
// 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. tty_fd_ = STDIN_FILENO; // Fallback to stdin.
return; return;
} }

View File

@@ -1,11 +1,11 @@
// Copyright 2025 Arthur Sonzogni. All rights reserved. // Copyright 2025 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in // Use of this source code is governed by the MIT license that can be found in
// the LICENSE file. // the LICENSE file.
#include <gtest/gtest.h>
#include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <cstdio> #include <gtest/gtest.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h>
#include <cstdio>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/component/screen_interactive.hpp" #include "ftxui/component/screen_interactive.hpp"
@@ -47,7 +47,7 @@ class PipedInputTest : public ::testing::Test {
void WriteToPipedStdin(const std::string& data) { void WriteToPipedStdin(const std::string& data) {
if (piped_stdin_setup_) { if (piped_stdin_setup_) {
write(pipe_fds_[1], data.c_str(), data.length()); write(pipe_fds_[1], data.c_str(), data.length());
close(pipe_fds_[1]); // Close write end to signal EOF close(pipe_fds_[1]); // Close write end to signal EOF
} }
} }
@@ -94,10 +94,10 @@ TEST_F(PipedInputTest, ExplicitlyDisabled) {
WriteToPipedStdin("test data\n"); WriteToPipedStdin("test data\n");
screen.Install(); screen.Install();
// Stdin should still be the pipe since feature is disabled // Stdin should still be the pipe since feature is disabled
EXPECT_FALSE(isatty(STDIN_FILENO)); EXPECT_FALSE(isatty(STDIN_FILENO));
screen.Uninstall(); screen.Uninstall();
} }
@@ -107,7 +107,7 @@ TEST_F(PipedInputTest, ExplicitlyEnabled) {
} }
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
screen.HandlePipedInput(true); // Explicitly enable screen.HandlePipedInput(true); // Explicitly enable
auto component = Renderer([] { return text("test"); }); auto component = Renderer([] { return text("test"); });
SetupPipedStdin(); SetupPipedStdin();
@@ -117,12 +117,12 @@ TEST_F(PipedInputTest, ExplicitlyEnabled) {
EXPECT_FALSE(isatty(STDIN_FILENO)); EXPECT_FALSE(isatty(STDIN_FILENO));
screen.Install(); screen.Install();
// After install with piped input handling: stdin should be redirected to tty // After install with piped input handling: stdin should be redirected to tty
EXPECT_TRUE(isatty(STDIN_FILENO)); EXPECT_TRUE(isatty(STDIN_FILENO));
screen.Uninstall(); screen.Uninstall();
// After uninstall: stdin should be restored to original state // After uninstall: stdin should be restored to original state
// Note: This will be the pipe we set up, so it should be non-tty // Note: This will be the pipe we set up, so it should be non-tty
EXPECT_FALSE(isatty(STDIN_FILENO)); EXPECT_FALSE(isatty(STDIN_FILENO));
@@ -137,12 +137,12 @@ TEST_F(PipedInputTest, NormalStdinUnchanged) {
bool original_isatty = isatty(STDIN_FILENO); bool original_isatty = isatty(STDIN_FILENO);
screen.Install(); screen.Install();
// Stdin should remain unchanged // Stdin should remain unchanged
EXPECT_EQ(original_isatty, isatty(STDIN_FILENO)); EXPECT_EQ(original_isatty, isatty(STDIN_FILENO));
screen.Uninstall(); screen.Uninstall();
// Stdin should still be unchanged // Stdin should still be unchanged
EXPECT_EQ(original_isatty, isatty(STDIN_FILENO)); EXPECT_EQ(original_isatty, isatty(STDIN_FILENO));
} }
@@ -173,12 +173,12 @@ TEST_F(PipedInputTest, MultipleInstallUninstallCycles) {
TEST_F(PipedInputTest, HandlePipedInputMethodBehavior) { TEST_F(PipedInputTest, HandlePipedInputMethodBehavior) {
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();
// Test method can be called multiple times // Test method can be called multiple times
screen.HandlePipedInput(true); screen.HandlePipedInput(true);
screen.HandlePipedInput(false); screen.HandlePipedInput(false);
screen.HandlePipedInput(true); screen.HandlePipedInput(true);
// Should be enabled after last call // Should be enabled after last call
SetupPipedStdin(); SetupPipedStdin();
WriteToPipedStdin("test data\n"); WriteToPipedStdin("test data\n");
@@ -191,7 +191,8 @@ TEST_F(PipedInputTest, HandlePipedInputMethodBehavior) {
} }
// Test the graceful fallback when /dev/tty is not available // Test the graceful fallback when /dev/tty is not available
// 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();
auto component = Renderer([] { return text("test"); }); auto component = Renderer([] { return text("test"); });
@@ -199,22 +200,23 @@ TEST_F(PipedInputTest, GracefulFallbackWhenTtyUnavailable) {
SetupPipedStdin(); SetupPipedStdin();
WriteToPipedStdin("test data\n"); WriteToPipedStdin("test data\n");
// This test doesn't directly mock /dev/tty unavailability since that's hard to do // This test doesn't directly mock /dev/tty unavailability since that's hard
// in a unit test environment, but the code path handles freopen() failure gracefully // to do in a unit test environment, but the code path handles freopen()
// failure gracefully
screen.Install(); screen.Install();
// The behavior depends on whether /dev/tty is available // The behavior depends on whether /dev/tty is available
// If available, stdin gets redirected; if not, it remains piped // If available, stdin gets redirected; if not, it remains piped
// Both behaviors are correct // Both behaviors are correct
screen.Uninstall(); screen.Uninstall();
// After uninstall, stdin should be restored // After uninstall, stdin should be restored
EXPECT_FALSE(isatty(STDIN_FILENO)); // Should still be our test pipe EXPECT_FALSE(isatty(STDIN_FILENO)); // Should still be our test pipe
} }
} // namespace } // namespace
} // namespace ftxui } // namespace ftxui
#endif // !defined(_WIN32) && !defined(__EMSCRIPTEN__) #endif // !defined(_WIN32) && !defined(__EMSCRIPTEN__)