mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Compare commits
7 Commits
HarryPehko
...
0cd3bdcdab
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cd3bdcdab | ||
|
|
21b24a1b78 | ||
|
|
bfd07ba309 | ||
|
|
d20b84f720 | ||
|
|
0dde21f09e | ||
|
|
2d50269730 | ||
|
|
df2ddcc588 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -44,7 +44,6 @@ out/
|
||||
!doc/**/*.html
|
||||
!doc/**/*.xml
|
||||
!doc/**/*.md
|
||||
!doc/*.md
|
||||
|
||||
# examples directory:
|
||||
!examples/**/*.cpp
|
||||
|
||||
@@ -169,13 +169,15 @@ ftxui_cc_library(
|
||||
"src/ftxui/component/util.cpp",
|
||||
"src/ftxui/component/window.cpp",
|
||||
|
||||
|
||||
# Private header from ftxui:dom.
|
||||
"src/ftxui/dom/node_decorator.hpp",
|
||||
|
||||
# Private header from ftxui:screen.
|
||||
"src/ftxui/screen/string_internal.hpp",
|
||||
"src/ftxui/screen/util.hpp",
|
||||
|
||||
# Private header.
|
||||
"include/ftxui/util/warn_windows_macro.hpp",
|
||||
],
|
||||
hdrs = [
|
||||
"include/ftxui/component/animation.hpp",
|
||||
|
||||
@@ -29,6 +29,8 @@ Next
|
||||
### Component
|
||||
- Fix ScreenInteractive::FixedSize screen stomps on the preceding terminal
|
||||
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)
|
||||
|
||||
@@ -378,8 +378,6 @@ 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)
|
||||
- [Consu](https://github.com/cpp-best-practices/game_jam/blob/main/Jam1_April_2022/consu.md)
|
||||
|
||||
|
||||
|
||||
## Build using CMake
|
||||
|
||||
It is **highly** recommended to use CMake FetchContent to depend on FTXUI so you may specify which commit you would like to depend on.
|
||||
|
||||
@@ -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);
|
||||
```
|
||||
@@ -17,10 +17,12 @@ add_subdirectory(dom)
|
||||
if (EMSCRIPTEN)
|
||||
get_property(EXAMPLES GLOBAL PROPERTY FTXUI::EXAMPLES)
|
||||
foreach(file
|
||||
"index.css"
|
||||
"index.html"
|
||||
"index.mjs"
|
||||
"index.css"
|
||||
"run_webassembly.py")
|
||||
"run_webassembly.py"
|
||||
"sw.js"
|
||||
)
|
||||
configure_file(${file} ${file})
|
||||
endforeach(file)
|
||||
endif()
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#ifndef FTXUI_COMPONENT_RECEIVER_HPP_
|
||||
#define FTXUI_COMPONENT_RECEIVER_HPP_
|
||||
|
||||
#include <ftxui/util/warn_windows_macro.h>
|
||||
#include <ftxui/util/warn_windows_macro.hpp>
|
||||
#include <algorithm> // for copy, max
|
||||
#include <atomic> // for atomic, __atomic_base
|
||||
#include <condition_variable> // for condition_variable
|
||||
|
||||
@@ -43,11 +43,10 @@ class ScreenInteractive : public Screen {
|
||||
static ScreenInteractive TerminalOutput();
|
||||
|
||||
// Destructor.
|
||||
~ScreenInteractive() override;
|
||||
~ScreenInteractive();
|
||||
|
||||
// Options. Must be called before Loop().
|
||||
void TrackMouse(bool enable = true);
|
||||
void HandlePipedInput(bool enable = true);
|
||||
|
||||
// Return the currently active screen, nullptr if none.
|
||||
static ScreenInteractive* Active();
|
||||
@@ -101,8 +100,6 @@ class ScreenInteractive : public Screen {
|
||||
void Draw(Component component);
|
||||
void ResetCursorPosition();
|
||||
|
||||
void InstallPipedInputHandling();
|
||||
|
||||
void Signal(int signal);
|
||||
|
||||
void FetchTerminalEvents();
|
||||
@@ -120,7 +117,6 @@ class ScreenInteractive : public Screen {
|
||||
int dimx,
|
||||
int dimy,
|
||||
bool use_alternative_screen);
|
||||
|
||||
const Dimension dimension_;
|
||||
const bool use_alternative_screen_;
|
||||
|
||||
@@ -145,9 +141,6 @@ class ScreenInteractive : public Screen {
|
||||
bool force_handle_ctrl_c_ = true;
|
||||
bool force_handle_ctrl_z_ = true;
|
||||
|
||||
// Piped input handling state (POSIX only)
|
||||
bool handle_piped_input_ = true;
|
||||
|
||||
// The style of the cursor to restore on exit.
|
||||
int cursor_reset_shape_ = 1;
|
||||
|
||||
|
||||
@@ -372,18 +372,6 @@ void ScreenInteractive::TrackMouse(bool enable) {
|
||||
track_mouse_ = enable;
|
||||
}
|
||||
|
||||
/// @brief Enable or disable automatic piped input handling.
|
||||
/// When enabled, FTXUI will detect piped input and redirect stdin from /dev/tty
|
||||
/// for keyboard input, allowing applications to read piped data while still
|
||||
/// receiving interactive keyboard events.
|
||||
/// @param enable Whether to enable piped input handling. Default is true.
|
||||
/// @note This must be called before Loop().
|
||||
/// @note This feature is enabled by default.
|
||||
/// @note This feature is only available on POSIX systems (Linux/macOS).
|
||||
void ScreenInteractive::HandlePipedInput(bool enable) {
|
||||
handle_piped_input_ = enable;
|
||||
}
|
||||
|
||||
/// @brief Add a task to the main loop.
|
||||
/// It will be executed later, after every other scheduled tasks.
|
||||
void ScreenInteractive::Post(Task task) {
|
||||
@@ -670,55 +658,11 @@ void ScreenInteractive::Install() {
|
||||
// ensure it is fully applied:
|
||||
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;
|
||||
|
||||
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
|
||||
void ScreenInteractive::Uninstall() {
|
||||
ExitNow();
|
||||
|
||||
@@ -1,220 +0,0 @@
|
||||
// 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 <gtest/gtest.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <cstdio>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "ftxui/component/component.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
namespace {
|
||||
|
||||
// Test fixture for piped input functionality
|
||||
class PipedInputTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
// Save original stdin for restoration
|
||||
original_stdin_ = dup(STDIN_FILENO);
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
// Restore original stdin
|
||||
if (original_stdin_ >= 0) {
|
||||
dup2(original_stdin_, STDIN_FILENO);
|
||||
close(original_stdin_);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a pipe and redirect stdin to read from it
|
||||
void SetupPipedStdin() {
|
||||
if (pipe(pipe_fds_) == 0) {
|
||||
dup2(pipe_fds_[0], STDIN_FILENO);
|
||||
close(pipe_fds_[0]);
|
||||
// Keep write end open for writing test data
|
||||
piped_stdin_setup_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Write test data to the piped stdin
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// Check if /dev/tty is available (not available in some CI environments)
|
||||
bool IsTtyAvailable() {
|
||||
struct stat st;
|
||||
return stat("/dev/tty", &st) == 0;
|
||||
}
|
||||
|
||||
private:
|
||||
int original_stdin_ = -1;
|
||||
int pipe_fds_[2] = {-1, -1};
|
||||
bool piped_stdin_setup_ = false;
|
||||
};
|
||||
|
||||
TEST_F(PipedInputTest, DefaultBehaviorEnabled) {
|
||||
// Test that HandlePipedInput is enabled by default
|
||||
if (!IsTtyAvailable()) {
|
||||
GTEST_SKIP() << "/dev/tty not available in this environment";
|
||||
}
|
||||
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
auto component = Renderer([] { return text("test"); });
|
||||
|
||||
SetupPipedStdin();
|
||||
WriteToPipedStdin("test data\n");
|
||||
|
||||
// Install should redirect stdin since HandlePipedInput is on by default
|
||||
screen.Install();
|
||||
|
||||
// Stdin should be the tty
|
||||
EXPECT_TRUE(isatty(STDIN_FILENO));
|
||||
|
||||
screen.Uninstall();
|
||||
}
|
||||
|
||||
TEST_F(PipedInputTest, ExplicitlyDisabled) {
|
||||
// Test that explicitly disabling works
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
screen.HandlePipedInput(false);
|
||||
auto component = Renderer([] { return text("test"); });
|
||||
|
||||
SetupPipedStdin();
|
||||
WriteToPipedStdin("test data\n");
|
||||
|
||||
screen.Install();
|
||||
|
||||
// Stdin should still be the pipe since feature is disabled
|
||||
EXPECT_FALSE(isatty(STDIN_FILENO));
|
||||
|
||||
screen.Uninstall();
|
||||
}
|
||||
|
||||
TEST_F(PipedInputTest, ExplicitlyEnabled) {
|
||||
if (!IsTtyAvailable()) {
|
||||
GTEST_SKIP() << "/dev/tty not available in this environment";
|
||||
}
|
||||
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
screen.HandlePipedInput(true); // Explicitly enable
|
||||
auto component = Renderer([] { return text("test"); });
|
||||
|
||||
SetupPipedStdin();
|
||||
WriteToPipedStdin("test data\n");
|
||||
|
||||
// Before install: stdin should be piped
|
||||
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));
|
||||
}
|
||||
|
||||
TEST_F(PipedInputTest, NormalStdinUnchanged) {
|
||||
// Test that normal stdin (not piped) is not affected
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
auto component = Renderer([] { return text("test"); });
|
||||
|
||||
// Don't setup piped stdin - use normal stdin
|
||||
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));
|
||||
}
|
||||
|
||||
TEST_F(PipedInputTest, MultipleInstallUninstallCycles) {
|
||||
if (!IsTtyAvailable()) {
|
||||
GTEST_SKIP() << "/dev/tty not available in this environment";
|
||||
}
|
||||
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
auto component = Renderer([] { return text("test"); });
|
||||
|
||||
SetupPipedStdin();
|
||||
WriteToPipedStdin("test data\n");
|
||||
|
||||
// First cycle
|
||||
screen.Install();
|
||||
EXPECT_TRUE(isatty(STDIN_FILENO));
|
||||
screen.Uninstall();
|
||||
EXPECT_FALSE(isatty(STDIN_FILENO));
|
||||
|
||||
// Second cycle should work the same
|
||||
screen.Install();
|
||||
EXPECT_TRUE(isatty(STDIN_FILENO));
|
||||
screen.Uninstall();
|
||||
EXPECT_FALSE(isatty(STDIN_FILENO));
|
||||
}
|
||||
|
||||
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");
|
||||
|
||||
if (IsTtyAvailable()) {
|
||||
screen.Install();
|
||||
EXPECT_TRUE(isatty(STDIN_FILENO));
|
||||
screen.Uninstall();
|
||||
}
|
||||
}
|
||||
|
||||
// Test the graceful fallback when /dev/tty is not available
|
||||
// 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"); });
|
||||
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
#endif // !defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
||||
@@ -33,6 +33,20 @@ Decorator flexDirection(Direction direction) {
|
||||
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>
|
||||
class SliderBase : public SliderOption<T>, public ComponentBase {
|
||||
public:
|
||||
@@ -47,59 +61,15 @@ class SliderBase : public SliderOption<T>, public ComponentBase {
|
||||
flexDirection(this->direction) | reflect(gauge_box_) | gauge_color;
|
||||
}
|
||||
|
||||
void OnLeft() {
|
||||
switch (this->direction) {
|
||||
case Direction::Right:
|
||||
this->value() -= this->increment();
|
||||
break;
|
||||
case Direction::Left:
|
||||
this->value() += this->increment();
|
||||
break;
|
||||
case Direction::Up:
|
||||
case Direction::Down:
|
||||
break;
|
||||
void OnDirection(Direction pressed) {
|
||||
if (pressed == this->direction) {
|
||||
this->value() += this->increment();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void OnRight() {
|
||||
switch (this->direction) {
|
||||
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;
|
||||
if (pressed == Opposite(this->direction)) {
|
||||
this->value() -= this->increment();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,16 +80,16 @@ class SliderBase : public SliderOption<T>, public ComponentBase {
|
||||
|
||||
T old_value = this->value();
|
||||
if (event == Event::ArrowLeft || event == Event::Character('h')) {
|
||||
OnLeft();
|
||||
OnDirection(Direction::Left);
|
||||
}
|
||||
if (event == Event::ArrowRight || event == Event::Character('l')) {
|
||||
OnRight();
|
||||
OnDirection(Direction::Right);
|
||||
}
|
||||
if (event == Event::ArrowUp || event == Event::Character('k')) {
|
||||
OnDown();
|
||||
OnDirection(Direction::Up);
|
||||
}
|
||||
if (event == Event::ArrowDown || event == Event::Character('j')) {
|
||||
OnUp();
|
||||
OnDirection(Direction::Down);
|
||||
}
|
||||
|
||||
this->value() = std::max(this->min(), std::min(this->max(), this->value()));
|
||||
|
||||
@@ -58,11 +58,39 @@ void ComputeShrinkHard(std::vector<Element>* elements,
|
||||
|
||||
element.size = element.min_size + added_space;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Called when the size allowed is lower than the requested size, and the
|
||||
// shrinkable element can not absorbe the (negative) extra_space. This assign
|
||||
// zero to shrinkable elements and distribute the remaining (negative)
|
||||
// extra_space toward the other non shrinkable elements.
|
||||
void ComputeShrinkHardHack(std::vector<Element>* elements,
|
||||
int extra_space,
|
||||
int size)
|
||||
{
|
||||
for (Element& element : *elements)
|
||||
{
|
||||
if (element.flex_shrink != 0)
|
||||
{
|
||||
element.size = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto it = elements->rbegin(); it != elements->rend(); ++it)
|
||||
{
|
||||
Element& element = *it;
|
||||
|
||||
auto remove = std::min(element.min_size, -extra_space);
|
||||
element.size = element.min_size - remove;
|
||||
extra_space += remove;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void Compute(std::vector<Element>* elements, int target_size) {
|
||||
void Compute(std::vector<Element>* elements, int target_size, bool hack) {
|
||||
int size = 0;
|
||||
int flex_grow_sum = 0;
|
||||
int flex_shrink_sum = 0;
|
||||
@@ -84,8 +112,14 @@ void Compute(std::vector<Element>* elements, int target_size) {
|
||||
ComputeShrinkEasy(elements, extra_space, flex_shrink_sum);
|
||||
|
||||
} else {
|
||||
ComputeShrinkHard(elements, extra_space + flex_shrink_size,
|
||||
size - flex_shrink_size);
|
||||
if (hack)
|
||||
{
|
||||
ComputeShrinkHardHack(elements, extra_space + flex_shrink_size, size - flex_shrink_size);
|
||||
}
|
||||
else
|
||||
{
|
||||
ComputeShrinkHard(elements, extra_space + flex_shrink_size, size - flex_shrink_size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ struct Element {
|
||||
int size = 0;
|
||||
};
|
||||
|
||||
void Compute(std::vector<Element>* elements, int target_size);
|
||||
void Compute(std::vector<Element>* elements, int target_size, bool hack=false);
|
||||
} // namespace ftxui::box_helper
|
||||
|
||||
#endif /* end of include guard: FTXUI_DOM_BOX_HELPER_HPP */
|
||||
|
||||
@@ -46,6 +46,12 @@ class GridBox : public Node {
|
||||
line.push_back(filler());
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto& line : lines_) {
|
||||
for (const auto &element : line) {
|
||||
children_.push_back( element );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
@@ -109,7 +115,7 @@ class GridBox : public Node {
|
||||
|
||||
const int target_size_x = box.x_max - box.x_min + 1;
|
||||
const int target_size_y = box.y_max - box.y_min + 1;
|
||||
box_helper::Compute(&elements_x, target_size_x);
|
||||
box_helper::Compute(&elements_x, target_size_x, true);
|
||||
box_helper::Compute(&elements_y, target_size_y);
|
||||
|
||||
Box box_y = box;
|
||||
|
||||
@@ -216,13 +216,13 @@ Element Table::Render() {
|
||||
|
||||
// Line
|
||||
if ((x + y) % 2 == 1) {
|
||||
it = std::move(it) | flex;
|
||||
it = std::move(it)| flex; //it = std::move(it); // | flex;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Cells
|
||||
if ((x % 2) == 1 && (y % 2) == 1) {
|
||||
it = std::move(it) | flex_shrink;
|
||||
// it = std::move(it) | flex_shrink; //it = std::move(it) | flex_shrink;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user