From a6612eda350fe44c81b1f78f11c91e393ce7ded4 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sun, 24 Aug 2025 05:08:54 +0200 Subject: [PATCH] Fix errors. --- include/ftxui/component/receiver.hpp | 2 +- include/ftxui/util/warn_windows_macro.hpp | 6 +-- src/ftxui/component/screen_interactive.cpp | 8 ++-- .../screen_interactive_piped_input_test.cpp | 48 ++++++++++--------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/include/ftxui/component/receiver.hpp b/include/ftxui/component/receiver.hpp index 3ed16fcb..e9015607 100644 --- a/include/ftxui/component/receiver.hpp +++ b/include/ftxui/component/receiver.hpp @@ -4,7 +4,6 @@ #ifndef FTXUI_COMPONENT_RECEIVER_HPP_ #define FTXUI_COMPONENT_RECEIVER_HPP_ -#include #include // for copy, max #include // for atomic, __atomic_base #include // for condition_variable @@ -12,6 +11,7 @@ #include // for mutex, unique_lock #include // for queue #include // for move +#include "ftxui/util/warn_windows_macro.hpp" namespace ftxui { diff --git a/include/ftxui/util/warn_windows_macro.hpp b/include/ftxui/util/warn_windows_macro.hpp index 922f305a..24e8a8ff 100644 --- a/include/ftxui/util/warn_windows_macro.hpp +++ b/include/ftxui/util/warn_windows_macro.hpp @@ -2,8 +2,8 @@ // Use of this source code is governed by the MIT license that can be found in // the LICENSE file. -#ifndef FTXUI_UTIL_WARN_WINDOWS_MACRO_H_ -#define FTXUI_UTIL_WARN_WINDOWS_MACRO_H_ +#ifndef FTXUI_UTIL_WARN_WINDOWS_MACRO_HPP_ +#define FTXUI_UTIL_WARN_WINDOWS_MACRO_HPP_ #ifdef min #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 . To fix this, add '#define NOMINMAX' before including , or pass '/DNOMINMAX' as a compiler flag." #endif -#endif // FTXUI_UTIL_WARN_WINDOWS_MACRO_H_ +#endif // FTXUI_UTIL_WARN_WINDOWS_MACRO_HPP_ diff --git a/src/ftxui/component/screen_interactive.cpp b/src/ftxui/component/screen_interactive.cpp index ea443c5c..5bc33d3c 100644 --- a/src/ftxui/component/screen_interactive.cpp +++ b/src/ftxui/component/screen_interactive.cpp @@ -115,7 +115,7 @@ void ftxui_on_resize(int columns, int rows) { int CheckStdinReady(int fd) { timeval tv = {0, 0}; // NOLINT fd_set fds; - FD_ZERO(&fds); // NOLINT + FD_ZERO(&fds); // NOLINT FD_SET(fd, &fds); // NOLINT select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT return FD_ISSET(fd, &fds); // NOLINT @@ -679,7 +679,7 @@ void ScreenInteractive::Install() { } void ScreenInteractive::InstallPipedInputHandling() { - tty_fd_ = fileno(stdin); // NOLINT + tty_fd_ = STDIN_FILENO; #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 @@ -689,7 +689,7 @@ void ScreenInteractive::InstallPipedInputHandling() { } // If stdin is a terminal, we don't need to open /dev/tty. - if (isatty(fileno(stdin))) { + if (isatty(STDIN_FILENO)) { return; } @@ -697,7 +697,7 @@ void ScreenInteractive::InstallPipedInputHandling() { 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. + tty_fd_ = STDIN_FILENO; // Fallback to stdin. return; } diff --git a/src/ftxui/component/screen_interactive_piped_input_test.cpp b/src/ftxui/component/screen_interactive_piped_input_test.cpp index 643f8bb8..e0061bca 100644 --- a/src/ftxui/component/screen_interactive_piped_input_test.cpp +++ b/src/ftxui/component/screen_interactive_piped_input_test.cpp @@ -1,11 +1,11 @@ // Copyright 2025 Arthur Sonzogni. All rights reserved. // Use of this source code is governed by the MIT license that can be found in // the LICENSE file. -#include -#include #include -#include +#include #include +#include +#include #include "ftxui/component/component.hpp" #include "ftxui/component/screen_interactive.hpp" @@ -47,7 +47,7 @@ class PipedInputTest : public ::testing::Test { void WriteToPipedStdin(const std::string& data) { if (piped_stdin_setup_) { 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"); screen.Install(); - + // Stdin should still be the pipe since feature is disabled EXPECT_FALSE(isatty(STDIN_FILENO)); - + screen.Uninstall(); } @@ -107,7 +107,7 @@ TEST_F(PipedInputTest, ExplicitlyEnabled) { } auto screen = ScreenInteractive::TerminalOutput(); - screen.HandlePipedInput(true); // Explicitly enable + screen.HandlePipedInput(true); // Explicitly enable auto component = Renderer([] { return text("test"); }); SetupPipedStdin(); @@ -117,12 +117,12 @@ TEST_F(PipedInputTest, ExplicitlyEnabled) { EXPECT_FALSE(isatty(STDIN_FILENO)); screen.Install(); - + // After install with piped input handling: stdin should be redirected to tty EXPECT_TRUE(isatty(STDIN_FILENO)); - + screen.Uninstall(); - + // After uninstall: stdin should be restored to original state // Note: This will be the pipe we set up, so it should be non-tty EXPECT_FALSE(isatty(STDIN_FILENO)); @@ -137,12 +137,12 @@ TEST_F(PipedInputTest, NormalStdinUnchanged) { bool original_isatty = isatty(STDIN_FILENO); screen.Install(); - + // Stdin should remain unchanged EXPECT_EQ(original_isatty, isatty(STDIN_FILENO)); - + screen.Uninstall(); - + // Stdin should still be unchanged EXPECT_EQ(original_isatty, isatty(STDIN_FILENO)); } @@ -173,12 +173,12 @@ TEST_F(PipedInputTest, MultipleInstallUninstallCycles) { TEST_F(PipedInputTest, HandlePipedInputMethodBehavior) { auto screen = ScreenInteractive::TerminalOutput(); - + // Test method can be called multiple times screen.HandlePipedInput(true); screen.HandlePipedInput(false); screen.HandlePipedInput(true); - + // Should be enabled after last call SetupPipedStdin(); WriteToPipedStdin("test data\n"); @@ -191,7 +191,8 @@ TEST_F(PipedInputTest, HandlePipedInputMethodBehavior) { } // 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) { auto screen = ScreenInteractive::TerminalOutput(); auto component = Renderer([] { return text("test"); }); @@ -199,22 +200,23 @@ TEST_F(PipedInputTest, GracefulFallbackWhenTtyUnavailable) { SetupPipedStdin(); WriteToPipedStdin("test data\n"); - // This test doesn't directly mock /dev/tty unavailability since that's hard to do - // in a unit test environment, but the code path handles freopen() failure gracefully + // This test doesn't directly mock /dev/tty unavailability since that's hard + // to do in a unit test environment, but the code path handles freopen() + // failure gracefully screen.Install(); - + // The behavior depends on whether /dev/tty is available // If available, stdin gets redirected; if not, it remains piped // Both behaviors are correct - + screen.Uninstall(); - + // 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 ftxui -#endif // !defined(_WIN32) && !defined(__EMSCRIPTEN__) \ No newline at end of file +#endif // !defined(_WIN32) && !defined(__EMSCRIPTEN__)