mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Compare commits
5 Commits
f48da3c208
...
HarryPehko
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dad2eaaa28 | ||
|
|
5c3e3151a5 | ||
|
|
143b24c6a5 | ||
|
|
40e1fac3d4 | ||
|
|
8ef18ab647 |
2
.bazelrc
2
.bazelrc
@@ -1,3 +1,5 @@
|
|||||||
|
common --enable_bzlmod
|
||||||
|
|
||||||
build --features=layering_check
|
build --features=layering_check
|
||||||
build --enable_bzlmod
|
build --enable_bzlmod
|
||||||
|
|
||||||
|
|||||||
@@ -2,3 +2,6 @@
|
|||||||
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
|
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
|
||||||
BasedOnStyle: Chromium
|
BasedOnStyle: Chromium
|
||||||
Standard: Cpp11
|
Standard: Cpp11
|
||||||
|
|
||||||
|
InsertBraces: true
|
||||||
|
InsertNewlineAtEOF: true
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -44,6 +44,7 @@ out/
|
|||||||
!doc/**/*.html
|
!doc/**/*.html
|
||||||
!doc/**/*.xml
|
!doc/**/*.xml
|
||||||
!doc/**/*.md
|
!doc/**/*.md
|
||||||
|
!doc/*.md
|
||||||
|
|
||||||
# examples directory:
|
# examples directory:
|
||||||
!examples/**/*.cpp
|
!examples/**/*.cpp
|
||||||
|
|||||||
@@ -158,11 +158,18 @@ ftxui_cc_library(
|
|||||||
"src/ftxui/component/resizable_split.cpp",
|
"src/ftxui/component/resizable_split.cpp",
|
||||||
"src/ftxui/component/screen_interactive.cpp",
|
"src/ftxui/component/screen_interactive.cpp",
|
||||||
"src/ftxui/component/slider.cpp",
|
"src/ftxui/component/slider.cpp",
|
||||||
|
"src/ftxui/component/task.cpp",
|
||||||
|
"src/ftxui/component/task_internal.hpp",
|
||||||
|
"src/ftxui/component/task_queue.cpp",
|
||||||
|
"src/ftxui/component/task_queue.hpp",
|
||||||
|
"src/ftxui/component/task_runner.cpp",
|
||||||
|
"src/ftxui/component/task_runner.hpp",
|
||||||
"src/ftxui/component/terminal_input_parser.cpp",
|
"src/ftxui/component/terminal_input_parser.cpp",
|
||||||
"src/ftxui/component/terminal_input_parser.hpp",
|
"src/ftxui/component/terminal_input_parser.hpp",
|
||||||
"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",
|
||||||
|
|
||||||
@@ -205,7 +212,6 @@ cc_test(
|
|||||||
"src/ftxui/component/menu_test.cpp",
|
"src/ftxui/component/menu_test.cpp",
|
||||||
"src/ftxui/component/modal_test.cpp",
|
"src/ftxui/component/modal_test.cpp",
|
||||||
"src/ftxui/component/radiobox_test.cpp",
|
"src/ftxui/component/radiobox_test.cpp",
|
||||||
"src/ftxui/component/receiver_test.cpp",
|
|
||||||
"src/ftxui/component/resizable_split_test.cpp",
|
"src/ftxui/component/resizable_split_test.cpp",
|
||||||
"src/ftxui/component/slider_test.cpp",
|
"src/ftxui/component/slider_test.cpp",
|
||||||
"src/ftxui/component/terminal_input_parser_test.cpp",
|
"src/ftxui/component/terminal_input_parser_test.cpp",
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ Next
|
|||||||
import ftxui.util;
|
import ftxui.util;
|
||||||
```
|
```
|
||||||
Thanks @mikomikotaishi for PR #1015.
|
Thanks @mikomikotaishi for PR #1015.
|
||||||
|
- Remove dependency on 'pthread'.
|
||||||
|
|
||||||
### Component
|
### Component
|
||||||
- Fix ScreenInteractive::FixedSize screen stomps on the preceding terminal
|
- Fix ScreenInteractive::FixedSize screen stomps on the preceding terminal
|
||||||
|
|||||||
@@ -144,16 +144,16 @@ add_library(component
|
|||||||
src/ftxui/component/resizable_split.cpp
|
src/ftxui/component/resizable_split.cpp
|
||||||
src/ftxui/component/screen_interactive.cpp
|
src/ftxui/component/screen_interactive.cpp
|
||||||
src/ftxui/component/slider.cpp
|
src/ftxui/component/slider.cpp
|
||||||
|
src/ftxui/component/task.cpp
|
||||||
|
src/ftxui/component/task_internal.hpp
|
||||||
|
src/ftxui/component/task_queue.cpp
|
||||||
|
src/ftxui/component/task_queue.hpp
|
||||||
|
src/ftxui/component/task_runner.cpp
|
||||||
|
src/ftxui/component/task_runner.hpp
|
||||||
src/ftxui/component/terminal_input_parser.cpp
|
src/ftxui/component/terminal_input_parser.cpp
|
||||||
src/ftxui/component/terminal_input_parser.hpp
|
src/ftxui/component/terminal_input_parser.hpp
|
||||||
src/ftxui/component/util.cpp
|
src/ftxui/component/util.cpp
|
||||||
src/ftxui/component/window.cpp
|
src/ftxui/component/window.cpp
|
||||||
src/ftxui/core/task.cpp
|
|
||||||
src/ftxui/core/task.hpp
|
|
||||||
src/ftxui/core/task_queue.cpp
|
|
||||||
src/ftxui/core/task_queue.hpp
|
|
||||||
src/ftxui/core/task_runner.cpp
|
|
||||||
src/ftxui/core/task_runner.hpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(dom PUBLIC screen)
|
target_link_libraries(dom PUBLIC screen)
|
||||||
|
|||||||
@@ -378,6 +378,8 @@ 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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Build using CMake
|
## 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.
|
It is **highly** recommended to use CMake FetchContent to depend on FTXUI so you may specify which commit you would like to depend on.
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ add_executable(ftxui-tests
|
|||||||
src/ftxui/component/resizable_split_test.cpp
|
src/ftxui/component/resizable_split_test.cpp
|
||||||
src/ftxui/component/screen_interactive_test.cpp
|
src/ftxui/component/screen_interactive_test.cpp
|
||||||
src/ftxui/component/slider_test.cpp
|
src/ftxui/component/slider_test.cpp
|
||||||
|
src/ftxui/component/task_test.cpp
|
||||||
src/ftxui/component/terminal_input_parser_test.cpp
|
src/ftxui/component/terminal_input_parser_test.cpp
|
||||||
src/ftxui/component/toggle_test.cpp
|
src/ftxui/component/toggle_test.cpp
|
||||||
src/ftxui/core/task_test.cpp
|
|
||||||
src/ftxui/dom/blink_test.cpp
|
src/ftxui/dom/blink_test.cpp
|
||||||
src/ftxui/dom/bold_test.cpp
|
src/ftxui/dom/bold_test.cpp
|
||||||
src/ftxui/dom/border_test.cpp
|
src/ftxui/dom/border_test.cpp
|
||||||
|
|||||||
58
doc/posix_pipe.md
Normal file
58
doc/posix_pipe.md
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
# 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);
|
||||||
|
```
|
||||||
@@ -133,8 +133,9 @@ int main() {
|
|||||||
float dy = 50.f;
|
float dy = 50.f;
|
||||||
ys[x] = int(dy + 20 * cos(dx * 0.14) + 10 * sin(dx * 0.42));
|
ys[x] = int(dy + 20 * cos(dx * 0.14) + 10 * sin(dx * 0.42));
|
||||||
}
|
}
|
||||||
for (int x = 1; x < 99; x++)
|
for (int x = 1; x < 99; x++) {
|
||||||
c.DrawPointLine(x, ys[x], x + 1, ys[x + 1]);
|
c.DrawPointLine(x, ys[x], x + 1, ys[x + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
return canvas(std::move(c));
|
return canvas(std::move(c));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -82,10 +82,12 @@ int main() {
|
|||||||
size(WIDTH, EQUAL, dimx) | size(HEIGHT, EQUAL, dimy) |
|
size(WIDTH, EQUAL, dimx) | size(HEIGHT, EQUAL, dimy) |
|
||||||
bgcolor(Color::HSV(index * 25, 255, 255)) |
|
bgcolor(Color::HSV(index * 25, 255, 255)) |
|
||||||
color(Color::Black);
|
color(Color::Black);
|
||||||
if (element_xflex_grow)
|
if (element_xflex_grow) {
|
||||||
element = element | xflex_grow;
|
element = element | xflex_grow;
|
||||||
if (element_yflex_grow)
|
}
|
||||||
|
if (element_yflex_grow) {
|
||||||
element = element | yflex_grow;
|
element = element | yflex_grow;
|
||||||
|
}
|
||||||
return element;
|
return element;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -119,10 +121,12 @@ int main() {
|
|||||||
|
|
||||||
group = group | notflex;
|
group = group | notflex;
|
||||||
|
|
||||||
if (!group_xflex_grow)
|
if (!group_xflex_grow) {
|
||||||
group = hbox(group, filler());
|
group = hbox(group, filler());
|
||||||
if (!group_yflex_grow)
|
}
|
||||||
|
if (!group_yflex_grow) {
|
||||||
group = vbox(group, filler());
|
group = vbox(group, filler());
|
||||||
|
}
|
||||||
|
|
||||||
group = group | flex;
|
group = group | flex;
|
||||||
return group;
|
return group;
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
// Copyright 2020 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 <stddef.h> // for size_t
|
#include <stddef.h> // for size_t
|
||||||
#include <array> // for array
|
#include <array> // for array
|
||||||
#include <atomic> // for atomic
|
#include <atomic> // for atomic
|
||||||
#include <chrono> // for operator""s, chrono_literals
|
#include <chrono> // for operator""s, chrono_literals
|
||||||
#include <cmath> // for sin
|
#include <cmath> // for sin
|
||||||
#include <ftxui/component/loop.hpp>
|
#include <ftxui/component/loop.hpp>
|
||||||
#include <functional> // for ref, reference_wrapper, function
|
#include <functional> // for ref, reference_wrapper, function
|
||||||
#include <memory> // for allocator, shared_ptr, __shared_ptr_access
|
#include <memory> // for allocator, shared_ptr, __shared_ptr_access
|
||||||
@@ -514,7 +514,7 @@ int main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Loop loop(&screen, main_renderer);
|
Loop loop(&screen, main_renderer);
|
||||||
while(!loop.HasQuitted()) {
|
while (!loop.HasQuitted()) {
|
||||||
// Update the state of the application.
|
// Update the state of the application.
|
||||||
shift++;
|
shift++;
|
||||||
|
|
||||||
@@ -525,7 +525,7 @@ int main() {
|
|||||||
loop.RunOnce();
|
loop.RunOnce();
|
||||||
|
|
||||||
// Sleep for a short duration to control the frame rate (60 FPS).
|
// Sleep for a short duration to control the frame rate (60 FPS).
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000/60));
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000 / 60));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -22,10 +22,12 @@ MenuEntryOption Colored(ftxui::Color c) {
|
|||||||
option.transform = [c](EntryState state) {
|
option.transform = [c](EntryState state) {
|
||||||
state.label = (state.active ? "> " : " ") + state.label;
|
state.label = (state.active ? "> " : " ") + state.label;
|
||||||
Element e = text(state.label) | color(c);
|
Element e = text(state.label) | color(c);
|
||||||
if (state.focused)
|
if (state.focused) {
|
||||||
e = e | inverted;
|
e = e | inverted;
|
||||||
if (state.active)
|
}
|
||||||
|
if (state.active) {
|
||||||
e = e | bold;
|
e = e | bold;
|
||||||
|
}
|
||||||
return e;
|
return e;
|
||||||
};
|
};
|
||||||
return option;
|
return option;
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ int main() {
|
|||||||
std::vector<std::string> entries;
|
std::vector<std::string> entries;
|
||||||
int selected = 0;
|
int selected = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 30; ++i)
|
for (int i = 0; i < 30; ++i) {
|
||||||
entries.push_back("Entry " + std::to_string(i));
|
entries.push_back("Entry " + std::to_string(i));
|
||||||
|
}
|
||||||
auto radiobox = Menu(&entries, &selected);
|
auto radiobox = Menu(&entries, &selected);
|
||||||
auto renderer = Renderer(radiobox, [&] {
|
auto renderer = Renderer(radiobox, [&] {
|
||||||
return radiobox->Render() | vscroll_indicator | frame |
|
return radiobox->Render() | vscroll_indicator | frame |
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ int main() {
|
|||||||
std::vector<std::string> entries;
|
std::vector<std::string> entries;
|
||||||
int selected = 0;
|
int selected = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 100; ++i)
|
for (int i = 0; i < 100; ++i) {
|
||||||
entries.push_back(std::to_string(i));
|
entries.push_back(std::to_string(i));
|
||||||
|
}
|
||||||
auto radiobox = Menu(&entries, &selected, MenuOption::Horizontal());
|
auto radiobox = Menu(&entries, &selected, MenuOption::Horizontal());
|
||||||
auto renderer = Renderer(
|
auto renderer = Renderer(
|
||||||
radiobox, [&] { return radiobox->Render() | hscroll_indicator | frame; });
|
radiobox, [&] { return radiobox->Render() | hscroll_indicator | frame; });
|
||||||
|
|||||||
@@ -116,10 +116,12 @@ Component VMenu1(std::vector<std::string>* entries, int* selected) {
|
|||||||
option.entries_option.transform = [](EntryState state) {
|
option.entries_option.transform = [](EntryState state) {
|
||||||
state.label = (state.active ? "> " : " ") + state.label;
|
state.label = (state.active ? "> " : " ") + state.label;
|
||||||
Element e = text(state.label);
|
Element e = text(state.label);
|
||||||
if (state.focused)
|
if (state.focused) {
|
||||||
e = e | bgcolor(Color::Blue);
|
e = e | bgcolor(Color::Blue);
|
||||||
if (state.active)
|
}
|
||||||
|
if (state.active) {
|
||||||
e = e | bold;
|
e = e | bold;
|
||||||
|
}
|
||||||
return e;
|
return e;
|
||||||
};
|
};
|
||||||
return Menu(entries, selected, option);
|
return Menu(entries, selected, option);
|
||||||
@@ -130,10 +132,12 @@ Component VMenu2(std::vector<std::string>* entries, int* selected) {
|
|||||||
option.entries_option.transform = [](EntryState state) {
|
option.entries_option.transform = [](EntryState state) {
|
||||||
state.label += (state.active ? " <" : " ");
|
state.label += (state.active ? " <" : " ");
|
||||||
Element e = hbox(filler(), text(state.label));
|
Element e = hbox(filler(), text(state.label));
|
||||||
if (state.focused)
|
if (state.focused) {
|
||||||
e = e | bgcolor(Color::Red);
|
e = e | bgcolor(Color::Red);
|
||||||
if (state.active)
|
}
|
||||||
|
if (state.active) {
|
||||||
e = e | bold;
|
e = e | bold;
|
||||||
|
}
|
||||||
return e;
|
return e;
|
||||||
};
|
};
|
||||||
return Menu(entries, selected, option);
|
return Menu(entries, selected, option);
|
||||||
@@ -144,13 +148,16 @@ Component VMenu3(std::vector<std::string>* entries, int* selected) {
|
|||||||
option.entries_option.transform = [](EntryState state) {
|
option.entries_option.transform = [](EntryState state) {
|
||||||
Element e = state.active ? text("[" + state.label + "]")
|
Element e = state.active ? text("[" + state.label + "]")
|
||||||
: text(" " + state.label + " ");
|
: text(" " + state.label + " ");
|
||||||
if (state.focused)
|
if (state.focused) {
|
||||||
e = e | bold;
|
e = e | bold;
|
||||||
|
}
|
||||||
|
|
||||||
if (state.focused)
|
if (state.focused) {
|
||||||
e = e | color(Color::Blue);
|
e = e | color(Color::Blue);
|
||||||
if (state.active)
|
}
|
||||||
|
if (state.active) {
|
||||||
e = e | bold;
|
e = e | bold;
|
||||||
|
}
|
||||||
return e;
|
return e;
|
||||||
};
|
};
|
||||||
return Menu(entries, selected, option);
|
return Menu(entries, selected, option);
|
||||||
@@ -245,10 +252,12 @@ Component HMenu5(std::vector<std::string>* entries, int* selected) {
|
|||||||
animation::easing::ElasticOut);
|
animation::easing::ElasticOut);
|
||||||
option.entries_option.transform = [](EntryState state) {
|
option.entries_option.transform = [](EntryState state) {
|
||||||
Element e = text(state.label) | hcenter | flex;
|
Element e = text(state.label) | hcenter | flex;
|
||||||
if (state.active && state.focused)
|
if (state.active && state.focused) {
|
||||||
e = e | bold;
|
e = e | bold;
|
||||||
if (!state.focused && !state.active)
|
}
|
||||||
|
if (!state.focused && !state.active) {
|
||||||
e = e | dim;
|
e = e | dim;
|
||||||
|
}
|
||||||
return e;
|
return e;
|
||||||
};
|
};
|
||||||
option.underline.color_inactive = Color::Default;
|
option.underline.color_inactive = Color::Default;
|
||||||
|
|||||||
@@ -20,8 +20,9 @@ using namespace ftxui;
|
|||||||
Component DummyComponent(int id) {
|
Component DummyComponent(int id) {
|
||||||
return Renderer([id](bool focused) {
|
return Renderer([id](bool focused) {
|
||||||
auto t = text("component " + std::to_string(id));
|
auto t = text("component " + std::to_string(id));
|
||||||
if (focused)
|
if (focused) {
|
||||||
t = t | inverted;
|
t = t | inverted;
|
||||||
|
}
|
||||||
return t;
|
return t;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ int main() {
|
|||||||
std::vector<std::string> entries;
|
std::vector<std::string> entries;
|
||||||
int selected = 0;
|
int selected = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 30; ++i)
|
for (int i = 0; i < 30; ++i) {
|
||||||
entries.push_back("RadioBox " + std::to_string(i));
|
entries.push_back("RadioBox " + std::to_string(i));
|
||||||
|
}
|
||||||
auto radiobox = Radiobox(&entries, &selected);
|
auto radiobox = Radiobox(&entries, &selected);
|
||||||
auto renderer = Renderer(radiobox, [&] {
|
auto renderer = Renderer(radiobox, [&] {
|
||||||
return radiobox->Render() | vscroll_indicator | frame |
|
return radiobox->Render() | vscroll_indicator | frame |
|
||||||
|
|||||||
@@ -19,10 +19,11 @@ int main() {
|
|||||||
|
|
||||||
// 1. Example of focusable renderer:
|
// 1. Example of focusable renderer:
|
||||||
auto renderer_focusable = Renderer([](bool focused) {
|
auto renderer_focusable = Renderer([](bool focused) {
|
||||||
if (focused)
|
if (focused) {
|
||||||
return text("FOCUSABLE RENDERER()") | center | bold | border;
|
return text("FOCUSABLE RENDERER()") | center | bold | border;
|
||||||
else
|
} else {
|
||||||
return text(" Focusable renderer() ") | center | border;
|
return text(" Focusable renderer() ") | center | border;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. Examples of a non focusable renderer.
|
// 2. Examples of a non focusable renderer.
|
||||||
@@ -33,10 +34,11 @@ int main() {
|
|||||||
// 3. Renderer can wrap other components to redefine their Render() function.
|
// 3. Renderer can wrap other components to redefine their Render() function.
|
||||||
auto button = Button("Wrapped quit button", screen.ExitLoopClosure());
|
auto button = Button("Wrapped quit button", screen.ExitLoopClosure());
|
||||||
auto renderer_wrap = Renderer(button, [&] {
|
auto renderer_wrap = Renderer(button, [&] {
|
||||||
if (button->Focused())
|
if (button->Focused()) {
|
||||||
return button->Render() | bold | color(Color::Red);
|
return button->Render() | bold | color(Color::Red);
|
||||||
else
|
} else {
|
||||||
return button->Render();
|
return button->Render();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Let's renderer everyone:
|
// Let's renderer everyone:
|
||||||
|
|||||||
@@ -32,10 +32,12 @@ int main() {
|
|||||||
|
|
||||||
// Plot a function:
|
// Plot a function:
|
||||||
std::vector<int> ys(100);
|
std::vector<int> ys(100);
|
||||||
for (int x = 0; x < 100; x++)
|
for (int x = 0; x < 100; x++) {
|
||||||
ys[x] = int(80 + 20 * cos(x * 0.2));
|
ys[x] = int(80 + 20 * cos(x * 0.2));
|
||||||
for (int x = 0; x < 99; x++)
|
}
|
||||||
|
for (int x = 0; x < 99; x++) {
|
||||||
c.DrawPointLine(x, ys[x], x + 1, ys[x + 1], Color::Red);
|
c.DrawPointLine(x, ys[x], x + 1, ys[x + 1], Color::Red);
|
||||||
|
}
|
||||||
|
|
||||||
auto document = canvas(&c) | border;
|
auto document = canvas(&c) | border;
|
||||||
|
|
||||||
|
|||||||
@@ -86,8 +86,9 @@ int main() {
|
|||||||
|
|
||||||
auto render = [&]() {
|
auto render = [&]() {
|
||||||
std::vector<Element> entries;
|
std::vector<Element> entries;
|
||||||
for (auto& task : displayed_task)
|
for (auto& task : displayed_task) {
|
||||||
entries.push_back(renderTask(task));
|
entries.push_back(renderTask(task));
|
||||||
|
}
|
||||||
|
|
||||||
return vbox({
|
return vbox({
|
||||||
// List of tasks.
|
// List of tasks.
|
||||||
@@ -138,8 +139,9 @@ int main() {
|
|||||||
std::this_thread::sleep_for(0.01s);
|
std::this_thread::sleep_for(0.01s);
|
||||||
|
|
||||||
// Exit
|
// Exit
|
||||||
if (nb_active + nb_queued == 0)
|
if (nb_active + nb_queued == 0) {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Update the model for the next frame.
|
// Update the model for the next frame.
|
||||||
updateModel();
|
updateModel();
|
||||||
|
|||||||
@@ -21,8 +21,9 @@ int main() {
|
|||||||
for (int index = 0; index < 200; ++index) {
|
for (int index = 0; index < 200; ++index) {
|
||||||
std::vector<Element> entries;
|
std::vector<Element> entries;
|
||||||
for (int i = 0; i < 23; ++i) {
|
for (int i = 0; i < 23; ++i) {
|
||||||
if (i != 0)
|
if (i != 0) {
|
||||||
entries.push_back(separator());
|
entries.push_back(separator());
|
||||||
|
}
|
||||||
entries.push_back( //
|
entries.push_back( //
|
||||||
hbox({
|
hbox({
|
||||||
text(std::to_string(i)) | size(WIDTH, EQUAL, 2),
|
text(std::to_string(i)) | size(WIDTH, EQUAL, 2),
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <memory> // for make_shared, shared_ptr
|
#include <memory> // for make_shared, shared_ptr
|
||||||
#include <utility> // for forward
|
#include <utility> // for forward
|
||||||
|
|
||||||
|
#include <ftxui/util/warn_windows_macro.hpp>
|
||||||
#include "ftxui/component/component_base.hpp" // for Component, Components
|
#include "ftxui/component/component_base.hpp" // for Component, Components
|
||||||
#include "ftxui/component/component_options.hpp" // for ButtonOption, CheckboxOption, MenuOption
|
#include "ftxui/component/component_options.hpp" // for ButtonOption, CheckboxOption, MenuOption
|
||||||
#include "ftxui/dom/elements.hpp" // for Element
|
#include "ftxui/dom/elements.hpp" // for Element
|
||||||
|
|||||||
@@ -9,8 +9,9 @@
|
|||||||
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Left, Direction::Right, Direction::Down
|
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Left, Direction::Right, Direction::Down
|
||||||
#include <ftxui/dom/elements.hpp> // for Element, separator
|
#include <ftxui/dom/elements.hpp> // for Element, separator
|
||||||
#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
|
#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
|
||||||
#include <functional> // for function
|
#include <ftxui/util/warn_windows_macro.hpp>
|
||||||
#include <string> // for string
|
#include <functional> // for function
|
||||||
|
#include <string> // for string
|
||||||
|
|
||||||
#include "ftxui/component/component_base.hpp" // for Component
|
#include "ftxui/component/component_base.hpp" // for Component
|
||||||
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
|
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::White
|
||||||
|
|||||||
@@ -4,6 +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.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
|
||||||
|
|||||||
@@ -4,10 +4,10 @@
|
|||||||
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
|
#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
|
||||||
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
|
#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP
|
||||||
|
|
||||||
#include <atomic> // for atomic
|
#include <atomic> // for atomic
|
||||||
#include <functional> // for function
|
#include <functional> // for function
|
||||||
#include <memory> // for shared_ptr
|
#include <memory> // for shared_ptr
|
||||||
#include <string> // for string
|
#include <string> // for string
|
||||||
|
|
||||||
#include "ftxui/component/animation.hpp" // for TimePoint
|
#include "ftxui/component/animation.hpp" // for TimePoint
|
||||||
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
||||||
@@ -25,7 +25,7 @@ using Component = std::shared_ptr<ComponentBase>;
|
|||||||
class ScreenInteractivePrivate;
|
class ScreenInteractivePrivate;
|
||||||
|
|
||||||
namespace task {
|
namespace task {
|
||||||
class TaskRunner;
|
class TaskRunner;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief ScreenInteractive is a `Screen` that can handle events, run a main
|
/// @brief ScreenInteractive is a `Screen` that can handle events, run a main
|
||||||
@@ -43,10 +43,11 @@ class ScreenInteractive : public Screen {
|
|||||||
static ScreenInteractive TerminalOutput();
|
static ScreenInteractive TerminalOutput();
|
||||||
|
|
||||||
// Destructor.
|
// Destructor.
|
||||||
~ScreenInteractive();
|
~ScreenInteractive() override;
|
||||||
|
|
||||||
// Options. Must be called before Loop().
|
// Options. Must be called before Loop().
|
||||||
void TrackMouse(bool enable = true);
|
void TrackMouse(bool enable = true);
|
||||||
|
void HandlePipedInput(bool enable = true);
|
||||||
|
|
||||||
// Return the currently active screen, nullptr if none.
|
// Return the currently active screen, nullptr if none.
|
||||||
static ScreenInteractive* Active();
|
static ScreenInteractive* Active();
|
||||||
@@ -100,6 +101,8 @@ 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();
|
||||||
@@ -117,6 +120,7 @@ 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_;
|
||||||
|
|
||||||
@@ -141,6 +145,9 @@ 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;
|
||||||
|
|
||||||
|
// Piped input handling state (POSIX only)
|
||||||
|
bool handle_piped_input_ = true;
|
||||||
|
|
||||||
// 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;
|
||||||
|
|
||||||
@@ -168,7 +175,6 @@ class ScreenInteractive : public Screen {
|
|||||||
|
|
||||||
Component component_;
|
Component component_;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
class Private {
|
class Private {
|
||||||
public:
|
public:
|
||||||
|
|||||||
18
include/ftxui/util/warn_windows_macro.hpp
Normal file
18
include/ftxui/util/warn_windows_macro.hpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
#ifndef FTXUI_UTIL_WARN_WINDOWS_MACRO_H_
|
||||||
|
#define FTXUI_UTIL_WARN_WINDOWS_MACRO_H_
|
||||||
|
|
||||||
|
#ifdef min
|
||||||
|
#error \
|
||||||
|
"The macro 'min' 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
|
||||||
|
|
||||||
|
#ifdef max
|
||||||
|
#error \
|
||||||
|
"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 // FTXUI_UTIL_WARN_WINDOWS_MACRO_H_
|
||||||
@@ -23,8 +23,9 @@ bool GeneratorBool(const char*& data, size_t& size) {
|
|||||||
|
|
||||||
std::string GeneratorString(const char*& data, size_t& size) {
|
std::string GeneratorString(const char*& data, size_t& size) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (index < size && data[index])
|
while (index < size && data[index]) {
|
||||||
++index;
|
++index;
|
||||||
|
}
|
||||||
|
|
||||||
auto out = std::string(data, data + index);
|
auto out = std::string(data, data + index);
|
||||||
data += index;
|
data += index;
|
||||||
@@ -40,8 +41,9 @@ std::string GeneratorString(const char*& data, size_t& size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int GeneratorInt(const char* data, size_t size) {
|
int GeneratorInt(const char* data, size_t size) {
|
||||||
if (size == 0)
|
if (size == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
auto out = int(data[0]);
|
auto out = int(data[0]);
|
||||||
data++;
|
data++;
|
||||||
size--;
|
size--;
|
||||||
@@ -113,8 +115,9 @@ Components GeneratorComponents(const char*& data, size_t& size, int depth);
|
|||||||
Component GeneratorComponent(const char*& data, size_t& size, int depth) {
|
Component GeneratorComponent(const char*& data, size_t& size, int depth) {
|
||||||
depth--;
|
depth--;
|
||||||
int value = GeneratorInt(data, size);
|
int value = GeneratorInt(data, size);
|
||||||
if (depth <= 0)
|
if (depth <= 0) {
|
||||||
return Button(GeneratorString(data, size), [] {});
|
return Button(GeneratorString(data, size), [] {});
|
||||||
|
}
|
||||||
|
|
||||||
constexpr int value_max = 19;
|
constexpr int value_max = 19;
|
||||||
value = (value % value_max + value_max) % value_max;
|
value = (value % value_max + value_max) % value_max;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
#include "ftxui/component/screen_interactive.hpp"
|
#include "ftxui/component/screen_interactive.hpp"
|
||||||
#include <algorithm> // for copy, max, min
|
#include <algorithm> // for copy, max, min
|
||||||
#include <array> // for array
|
#include <array> // for array
|
||||||
#include <array>
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono> // for operator-, milliseconds, operator>=, duration, common_type<>::type, time_point
|
#include <chrono> // for operator-, milliseconds, operator>=, duration, common_type<>::type, time_point
|
||||||
#include <csignal> // for signal, SIGTSTP, SIGABRT, SIGWINCH, raise, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM, __sighandler_t, size_t
|
#include <csignal> // for signal, SIGTSTP, SIGABRT, SIGWINCH, raise, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM, __sighandler_t, size_t
|
||||||
@@ -18,26 +17,22 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <stack> // for stack
|
#include <stack> // for stack
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread> // for thread, sleep_for
|
#include <thread> // for thread, sleep_for
|
||||||
#include <tuple> // for _Swallow_assign, ignore
|
#include <tuple> // for _Swallow_assign, ignore
|
||||||
#include <type_traits> // for decay_t
|
#include <utility> // for move, swap
|
||||||
#include <utility> // for move, swap
|
#include <variant> // for visit, variant
|
||||||
#include <variant> // for visit, variant
|
#include <vector> // for vector
|
||||||
#include <vector> // for vector
|
|
||||||
#include "ftxui/component/animation.hpp" // for TimePoint, Clock, Duration, Params, RequestAnimationFrame
|
#include "ftxui/component/animation.hpp" // for TimePoint, Clock, Duration, Params, RequestAnimationFrame
|
||||||
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse, CapturedMouseInterface
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse, CapturedMouseInterface
|
||||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||||
#include "ftxui/component/event.hpp" // for Event
|
#include "ftxui/component/event.hpp" // for Event
|
||||||
#include "ftxui/component/loop.hpp" // for Loop
|
#include "ftxui/component/loop.hpp" // for Loop
|
||||||
#include "ftxui/component/receiver.hpp" // for ReceiverImpl, Sender, MakeReceiver, SenderImpl, Receiver
|
#include "ftxui/component/task_runner.hpp"
|
||||||
#include "ftxui/component/terminal_input_parser.hpp" // for TerminalInputParser
|
#include "ftxui/component/terminal_input_parser.hpp" // for TerminalInputParser
|
||||||
#include "ftxui/core/task_runner.hpp"
|
#include "ftxui/dom/node.hpp" // for Node, Render
|
||||||
#include "ftxui/dom/node.hpp" // for Node, Render
|
#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
|
||||||
#include "ftxui/dom/requirement.hpp" // for Requirement
|
#include "ftxui/screen/util.hpp" // for util::clamp
|
||||||
#include "ftxui/screen/pixel.hpp" // for Pixel
|
#include "ftxui/util/autoreset.hpp" // for AutoReset
|
||||||
#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
|
|
||||||
#include "ftxui/screen/util.hpp" // for util::clamp
|
|
||||||
#include "ftxui/util/autoreset.hpp" // for AutoReset
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#define DEFINE_CONSOLEV2_PROPERTIES
|
#define DEFINE_CONSOLEV2_PROPERTIES
|
||||||
@@ -377,6 +372,18 @@ void ScreenInteractive::TrackMouse(bool enable) {
|
|||||||
track_mouse_ = 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.
|
/// @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.
|
||||||
void ScreenInteractive::Post(Task task) {
|
void ScreenInteractive::Post(Task task) {
|
||||||
@@ -583,15 +590,15 @@ void ScreenInteractive::Install() {
|
|||||||
|
|
||||||
SetConsoleMode(stdin_handle, in_mode);
|
SetConsoleMode(stdin_handle, in_mode);
|
||||||
SetConsoleMode(stdout_handle, out_mode);
|
SetConsoleMode(stdout_handle, out_mode);
|
||||||
#else // POSIX (Linux & Mac)
|
#else // POSIX (Linux & Mac)
|
||||||
//#if defined(__EMSCRIPTEN__)
|
// #if defined(__EMSCRIPTEN__)
|
||||||
//// Reading stdin isn't blocking.
|
//// Reading stdin isn't blocking.
|
||||||
//int flags = fcntl(0, F_GETFL, 0);
|
// int flags = fcntl(0, F_GETFL, 0);
|
||||||
//fcntl(0, F_SETFL, flags | O_NONBLOCK);
|
// fcntl(0, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
|
||||||
//// Restore the terminal configuration on exit.
|
//// Restore the terminal configuration on exit.
|
||||||
//on_exit_functions.emplace([flags] { fcntl(0, F_SETFL, flags); });
|
// on_exit_functions.emplace([flags] { fcntl(0, F_SETFL, flags); });
|
||||||
//#endif
|
// #endif
|
||||||
for (const int signal : {SIGWINCH, SIGTSTP}) {
|
for (const int signal : {SIGWINCH, SIGTSTP}) {
|
||||||
InstallSignalHandler(signal);
|
InstallSignalHandler(signal);
|
||||||
}
|
}
|
||||||
@@ -663,11 +670,55 @@ 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() {
|
||||||
|
#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();
|
||||||
@@ -684,8 +735,8 @@ void ScreenInteractive::RunOnceBlocking(Component component) {
|
|||||||
size_t executed_task = internal_->task_runner.ExecutedTasks();
|
size_t executed_task = internal_->task_runner.ExecutedTasks();
|
||||||
|
|
||||||
// Wait for at least one task to execute.
|
// Wait for at least one task to execute.
|
||||||
while(executed_task == internal_->task_runner.ExecutedTasks() &&
|
while (executed_task == internal_->task_runner.ExecutedTasks() &&
|
||||||
!HasQuitted()) {
|
!HasQuitted()) {
|
||||||
RunOnce(component);
|
RunOnce(component);
|
||||||
|
|
||||||
const auto now = std::chrono::steady_clock::now();
|
const auto now = std::chrono::steady_clock::now();
|
||||||
@@ -732,7 +783,7 @@ void ScreenInteractive::HandleTask(Component component, Task& task) {
|
|||||||
[&](auto&& arg) {
|
[&](auto&& arg) {
|
||||||
using T = std::decay_t<decltype(arg)>;
|
using T = std::decay_t<decltype(arg)>;
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
// Handle Event.
|
// Handle Event.
|
||||||
if constexpr (std::is_same_v<T, Event>) {
|
if constexpr (std::is_same_v<T, Event>) {
|
||||||
|
|
||||||
@@ -1021,23 +1072,23 @@ void ScreenInteractive::Signal(int signal) {
|
|||||||
|
|
||||||
void ScreenInteractive::FetchTerminalEvents() {
|
void ScreenInteractive::FetchTerminalEvents() {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
auto get_input_records = [&] {
|
auto get_input_records = [&]() -> std::vector<INPUT_RECORD> {
|
||||||
// Check if there is input in the console.
|
// Check if there is input in the console.
|
||||||
auto console = GetStdHandle(STD_INPUT_HANDLE);
|
auto console = GetStdHandle(STD_INPUT_HANDLE);
|
||||||
DWORD number_of_events = 0;
|
DWORD number_of_events = 0;
|
||||||
if (!GetNumberOfConsoleInputEvents(console, &number_of_events)) {
|
if (!GetNumberOfConsoleInputEvents(console, &number_of_events)) {
|
||||||
return;
|
return std::vector<INPUT_RECORD>();
|
||||||
}
|
}
|
||||||
if (number_of_events <= 0) {
|
if (number_of_events <= 0) {
|
||||||
// No input, return.
|
// No input, return.
|
||||||
return;
|
return std::vector<INPUT_RECORD>();
|
||||||
}
|
}
|
||||||
// Read the input events.
|
// Read the input events.
|
||||||
std::vector<INPUT_RECORD> records(number_of_events);
|
std::vector<INPUT_RECORD> records(number_of_events);
|
||||||
DWORD number_of_events_read = 0;
|
DWORD number_of_events_read = 0;
|
||||||
if (!ReadConsoleInput(console, records.data(), (DWORD)records.size(),
|
if (!ReadConsoleInput(console, records.data(), (DWORD)records.size(),
|
||||||
&number_of_events_read)) {
|
&number_of_events_read)) {
|
||||||
return;
|
return std::vector<INPUT_RECORD>();
|
||||||
}
|
}
|
||||||
records.resize(number_of_events_read);
|
records.resize(number_of_events_read);
|
||||||
return records;
|
return records;
|
||||||
@@ -1048,7 +1099,7 @@ void ScreenInteractive::FetchTerminalEvents() {
|
|||||||
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_microseconds =
|
const size_t timeout_microseconds =
|
||||||
std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
|
std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
|
||||||
internal_->terminal_input_parser.Timeout(timeout_microseconds);
|
internal_->terminal_input_parser.Timeout(timeout_microseconds);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1062,8 +1113,9 @@ void ScreenInteractive::FetchTerminalEvents() {
|
|||||||
case KEY_EVENT: {
|
case KEY_EVENT: {
|
||||||
auto key_event = r.Event.KeyEvent;
|
auto key_event = r.Event.KeyEvent;
|
||||||
// ignore UP key events
|
// ignore UP key events
|
||||||
if (key_event.bKeyDown == FALSE)
|
if (key_event.bKeyDown == FALSE) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
std::wstring wstring;
|
std::wstring wstring;
|
||||||
wstring += key_event.uChar.UnicodeChar;
|
wstring += key_event.uChar.UnicodeChar;
|
||||||
for (auto it : to_string(wstring)) {
|
for (auto it : to_string(wstring)) {
|
||||||
@@ -1089,17 +1141,17 @@ void ScreenInteractive::FetchTerminalEvents() {
|
|||||||
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_microseconds =
|
const size_t timeout_microseconds =
|
||||||
std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
|
std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
|
||||||
internal_->terminal_input_parser.Timeout(timeout_microseconds);
|
internal_->terminal_input_parser.Timeout(timeout_microseconds);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
internal_->last_char_time = std::chrono::steady_clock::now();
|
internal_->last_char_time = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
// 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) {
|
||||||
internal_->terminal_input_parser.Add(out[i]);
|
internal_->terminal_input_parser.Add(out[i]);
|
||||||
}
|
}
|
||||||
#else // POSIX (Linux & Mac)
|
#else // POSIX (Linux & Mac)
|
||||||
if (!CheckStdinReady()) {
|
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;
|
||||||
@@ -1115,7 +1167,7 @@ void ScreenInteractive::FetchTerminalEvents() {
|
|||||||
size_t l = read(fileno(stdin), 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) {
|
||||||
internal_->terminal_input_parser.Add(out[i]);
|
internal_->terminal_input_parser.Add(out[i]);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -1126,8 +1178,8 @@ void ScreenInteractive::PostAnimationTask() {
|
|||||||
|
|
||||||
// Repeat the animation task every 15ms. This correspond to a frame rate
|
// Repeat the animation task every 15ms. This correspond to a frame rate
|
||||||
// of around 66fps.
|
// of around 66fps.
|
||||||
internal_->task_runner.PostDelayedTask(
|
internal_->task_runner.PostDelayedTask([this] { PostAnimationTask(); },
|
||||||
[this] { PostAnimationTask(); }, std::chrono::milliseconds(15));
|
std::chrono::milliseconds(15));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ScreenInteractive::SelectionData::operator==(
|
bool ScreenInteractive::SelectionData::operator==(
|
||||||
|
|||||||
220
src/ftxui/component/screen_interactive_piped_input_test.cpp
Normal file
220
src/ftxui/component/screen_interactive_piped_input_test.cpp
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
// 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__)
|
||||||
@@ -28,8 +28,9 @@ namespace {
|
|||||||
class StdCapture {
|
class StdCapture {
|
||||||
public:
|
public:
|
||||||
explicit StdCapture(std::string* captured) : captured_(captured) {
|
explicit StdCapture(std::string* captured) : captured_(captured) {
|
||||||
if (pipe(pipefd_) != 0)
|
if (pipe(pipefd_) != 0) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
old_stdout_ = dup(fileno(stdout));
|
old_stdout_ = dup(fileno(stdout));
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
dup2(pipefd_[1], fileno(stdout));
|
dup2(pipefd_[1], fileno(stdout));
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright 2024 Arthur Sonzogni. All rights reserved.
|
// Copyright 2024 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 "task.hpp"
|
#include "ftxui/component/task_internal.hpp"
|
||||||
|
|
||||||
namespace ftxui::task {
|
namespace ftxui::task {
|
||||||
bool PendingTask::operator<(const PendingTask& other) const {
|
bool PendingTask::operator<(const PendingTask& other) const {
|
||||||
@@ -16,5 +16,4 @@ bool PendingTask::operator<(const PendingTask& other) const {
|
|||||||
}
|
}
|
||||||
return time.value() > other.time.value();
|
return time.value() > other.time.value();
|
||||||
}
|
}
|
||||||
} // namespace ftxui::task
|
} // namespace ftxui::task
|
||||||
|
|
||||||
@@ -17,12 +17,11 @@ using Task = std::function<void()>;
|
|||||||
/// specific time, or as soon as possible.
|
/// specific time, or as soon as possible.
|
||||||
struct PendingTask {
|
struct PendingTask {
|
||||||
// Immediate task:
|
// Immediate task:
|
||||||
PendingTask(Task task) : task(std::move(task)) {} // NOLINT
|
PendingTask(Task t) : task(std::move(t)) {} // NOLINT
|
||||||
|
|
||||||
// Delayed task with a duration
|
// Delayed task with a duration
|
||||||
PendingTask(Task task, std::chrono::steady_clock::duration duration)
|
PendingTask(Task t, std::chrono::steady_clock::duration duration)
|
||||||
: task(std::move(task)),
|
: task(std::move(t)), time(std::chrono::steady_clock::now() + duration) {}
|
||||||
time(std::chrono::steady_clock::now() + duration) {}
|
|
||||||
|
|
||||||
/// The task to be executed.
|
/// The task to be executed.
|
||||||
Task task;
|
Task task;
|
||||||
@@ -36,7 +35,6 @@ struct PendingTask {
|
|||||||
bool operator<(const PendingTask& other) const;
|
bool operator<(const PendingTask& other) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ftxui::task
|
} // namespace ftxui::task
|
||||||
|
|
||||||
|
|
||||||
#endif // TASK_HPP_
|
#endif // TASK_HPP_
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright 2024 Arthur Sonzogni. All rights reserved.
|
// Copyright 2024 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 "task_queue.hpp"
|
#include "ftxui/component/task_queue.hpp"
|
||||||
|
|
||||||
namespace ftxui::task {
|
namespace ftxui::task {
|
||||||
|
|
||||||
@@ -50,5 +50,4 @@ auto TaskQueue::Get() -> MaybeTask {
|
|||||||
return std::monostate{};
|
return std::monostate{};
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ftxui::task
|
} // namespace ftxui::task
|
||||||
|
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
#include "task.hpp"
|
#include "ftxui/component/task_internal.hpp" // for PendingTask, Task
|
||||||
|
|
||||||
namespace ftxui::task {
|
namespace ftxui::task {
|
||||||
|
|
||||||
@@ -25,16 +25,13 @@ struct TaskQueue {
|
|||||||
std::variant<Task, std::chrono::steady_clock::duration, std::monostate>;
|
std::variant<Task, std::chrono::steady_clock::duration, std::monostate>;
|
||||||
auto Get() -> MaybeTask;
|
auto Get() -> MaybeTask;
|
||||||
|
|
||||||
bool HasImmediateTasks() const {
|
bool HasImmediateTasks() const { return !immediate_tasks_.empty(); }
|
||||||
return !immediate_tasks_.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::queue<PendingTask> immediate_tasks_;
|
std::queue<PendingTask> immediate_tasks_;
|
||||||
std::priority_queue<PendingTask> delayed_tasks_;
|
std::priority_queue<PendingTask> delayed_tasks_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ftxui::task
|
} // namespace ftxui::task
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright 2024 Arthur Sonzogni. All rights reserved.
|
// Copyright 2024 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 "task_runner.hpp"
|
#include "ftxui/component/task_runner.hpp"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@@ -20,7 +20,6 @@ TaskRunner::~TaskRunner() {
|
|||||||
current_task_runner = previous_task_runner_;
|
current_task_runner = previous_task_runner_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
auto TaskRunner::Current() -> TaskRunner* {
|
auto TaskRunner::Current() -> TaskRunner* {
|
||||||
assert(current_task_runner);
|
assert(current_task_runner);
|
||||||
@@ -73,5 +72,4 @@ auto TaskRunner::Run() -> void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ftxui::task
|
} // namespace ftxui::task
|
||||||
|
|
||||||
@@ -4,8 +4,8 @@
|
|||||||
#ifndef TASK_RUNNER_HPP
|
#ifndef TASK_RUNNER_HPP
|
||||||
#define TASK_RUNNER_HPP
|
#define TASK_RUNNER_HPP
|
||||||
|
|
||||||
#include "task.hpp"
|
#include "ftxui/component/task_internal.hpp"
|
||||||
#include "task_queue.hpp"
|
#include "ftxui/component/task_queue.hpp"
|
||||||
|
|
||||||
namespace ftxui::task {
|
namespace ftxui::task {
|
||||||
|
|
||||||
@@ -21,8 +21,8 @@ class TaskRunner {
|
|||||||
auto PostTask(Task task) -> void;
|
auto PostTask(Task task) -> void;
|
||||||
|
|
||||||
/// Schedules a task to be executed after a certain duration.
|
/// Schedules a task to be executed after a certain duration.
|
||||||
auto PostDelayedTask(Task task,
|
auto PostDelayedTask(Task task, std::chrono::steady_clock::duration duration)
|
||||||
std::chrono::steady_clock::duration duration) -> void;
|
-> void;
|
||||||
|
|
||||||
/// Runs the tasks in the queue, return the delay until the next delayed task
|
/// Runs the tasks in the queue, return the delay until the next delayed task
|
||||||
/// can be executed.
|
/// can be executed.
|
||||||
@@ -31,9 +31,7 @@ class TaskRunner {
|
|||||||
// Runs the tasks in the queue, blocking until all tasks are executed.
|
// Runs the tasks in the queue, blocking until all tasks are executed.
|
||||||
auto Run() -> void;
|
auto Run() -> void;
|
||||||
|
|
||||||
bool HasImmediateTasks() const {
|
bool HasImmediateTasks() const { return queue_.HasImmediateTasks(); }
|
||||||
return queue_.HasImmediateTasks();
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t ExecutedTasks() const { return executed_tasks_; }
|
size_t ExecutedTasks() const { return executed_tasks_; }
|
||||||
|
|
||||||
@@ -43,7 +41,6 @@ class TaskRunner {
|
|||||||
size_t executed_tasks_ = 0;
|
size_t executed_tasks_ = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ftxui::task
|
} // namespace ftxui::task
|
||||||
|
|
||||||
|
|
||||||
#endif // TASK_RUNNER_HPP
|
#endif // TASK_RUNNER_HPP
|
||||||
@@ -4,11 +4,12 @@
|
|||||||
// Copyright 2024 Arthur Sonzogni. All rights reserved.
|
// Copyright 2024 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 "task.hpp"
|
#include "ftxui/component/task_internal.hpp"
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#include "task_runner.hpp"
|
#include <thread> // for sleep_for
|
||||||
|
#include "ftxui/component/task_runner.hpp"
|
||||||
|
|
||||||
namespace ftxui::task {
|
namespace ftxui::task {
|
||||||
|
|
||||||
@@ -90,6 +91,4 @@ TEST(TaskTest, RunDelayedTask) {
|
|||||||
EXPECT_EQ(values, (std::vector<int>{1, 2, 3}));
|
EXPECT_EQ(values, (std::vector<int>{1, 2, 3}));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace ftxui::task
|
} // namespace ftxui::task
|
||||||
|
|
||||||
|
|
||||||
@@ -152,8 +152,8 @@ void TerminalInputParser::Send(TerminalInputParser::Output output) {
|
|||||||
|
|
||||||
case CURSOR_POSITION:
|
case CURSOR_POSITION:
|
||||||
out_(Event::CursorPosition(std::move(pending_), // NOLINT
|
out_(Event::CursorPosition(std::move(pending_), // NOLINT
|
||||||
output.cursor.x, // NOLINT
|
output.cursor.x, // NOLINT
|
||||||
output.cursor.y)); // NOLINT
|
output.cursor.y)); // NOLINT
|
||||||
pending_.clear();
|
pending_.clear();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
@@ -17,16 +17,19 @@ namespace ftxui {
|
|||||||
// Test char |c| to are trivially converted into |Event::Character(c)|.
|
// Test char |c| to are trivially converted into |Event::Character(c)|.
|
||||||
TEST(Event, Character) {
|
TEST(Event, Character) {
|
||||||
std::vector<char> basic_char;
|
std::vector<char> basic_char;
|
||||||
for (char c = 'a'; c <= 'z'; ++c)
|
for (char c = 'a'; c <= 'z'; ++c) {
|
||||||
basic_char.push_back(c);
|
basic_char.push_back(c);
|
||||||
for (char c = 'A'; c <= 'Z'; ++c)
|
}
|
||||||
|
for (char c = 'A'; c <= 'Z'; ++c) {
|
||||||
basic_char.push_back(c);
|
basic_char.push_back(c);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<Event> received_events;
|
std::vector<Event> received_events;
|
||||||
auto parser = TerminalInputParser(
|
auto parser = TerminalInputParser(
|
||||||
[&](Event event) { received_events.push_back(std::move(event)); });
|
[&](Event event) { received_events.push_back(std::move(event)); });
|
||||||
for (char c : basic_char)
|
for (char c : basic_char) {
|
||||||
parser.Add(c);
|
parser.Add(c);
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < basic_char.size(); ++i) {
|
for (size_t i = 0; i < basic_char.size(); ++i) {
|
||||||
EXPECT_TRUE(received_events[i].is_character());
|
EXPECT_TRUE(received_events[i].is_character());
|
||||||
@@ -285,8 +288,9 @@ TEST(Event, UTF8) {
|
|||||||
std::vector<Event> received_events;
|
std::vector<Event> received_events;
|
||||||
auto parser = TerminalInputParser(
|
auto parser = TerminalInputParser(
|
||||||
[&](Event event) { received_events.push_back(std::move(event)); });
|
[&](Event event) { received_events.push_back(std::move(event)); });
|
||||||
for (auto input : test.input)
|
for (auto input : test.input) {
|
||||||
parser.Add(input);
|
parser.Add(input);
|
||||||
|
}
|
||||||
|
|
||||||
if (test.valid) {
|
if (test.valid) {
|
||||||
EXPECT_EQ(1, received_events.size());
|
EXPECT_EQ(1, received_events.size());
|
||||||
@@ -315,8 +319,9 @@ TEST(Event, Control) {
|
|||||||
};
|
};
|
||||||
std::vector<TestCase> cases;
|
std::vector<TestCase> cases;
|
||||||
for (int i = 0; i < 32; ++i) {
|
for (int i = 0; i < 32; ++i) {
|
||||||
if (i == 8 || i == 13 || i == 24 || i == 26 || i == 27)
|
if (i == 8 || i == 13 || i == 24 || i == 26 || i == 27) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
cases.push_back({char(i), false});
|
cases.push_back({char(i), false});
|
||||||
}
|
}
|
||||||
cases.push_back({char(24), false});
|
cases.push_back({char(24), false});
|
||||||
@@ -341,8 +346,9 @@ TEST(Event, Control) {
|
|||||||
TEST(Event, Special) {
|
TEST(Event, Special) {
|
||||||
auto str = [](std::string input) {
|
auto str = [](std::string input) {
|
||||||
std::vector<unsigned char> output;
|
std::vector<unsigned char> output;
|
||||||
for (auto it : input)
|
for (auto it : input) {
|
||||||
output.push_back(it);
|
output.push_back(it);
|
||||||
|
}
|
||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -351,9 +357,12 @@ TEST(Event, Special) {
|
|||||||
Event expected;
|
Event expected;
|
||||||
} kTestCase[] = {
|
} kTestCase[] = {
|
||||||
// Arrow (default cursor mode)
|
// Arrow (default cursor mode)
|
||||||
{str("[A"), Event::ArrowUp}, {str("[B"), Event::ArrowDown},
|
{str("[A"), Event::ArrowUp},
|
||||||
{str("[C"), Event::ArrowRight}, {str("[D"), Event::ArrowLeft},
|
{str("[B"), Event::ArrowDown},
|
||||||
{str("[H"), Event::Home}, {str("[F"), Event::End},
|
{str("[C"), Event::ArrowRight},
|
||||||
|
{str("[D"), Event::ArrowLeft},
|
||||||
|
{str("[H"), Event::Home},
|
||||||
|
{str("[F"), Event::End},
|
||||||
|
|
||||||
// Arrow (application cursor mode)
|
// Arrow (application cursor mode)
|
||||||
{str("\x1BOA"), Event::ArrowUp},
|
{str("\x1BOA"), Event::ArrowUp},
|
||||||
|
|||||||
@@ -47,8 +47,9 @@ namespace {
|
|||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
void WindowsEmulateVT100Terminal() {
|
void WindowsEmulateVT100Terminal() {
|
||||||
static bool done = false;
|
static bool done = false;
|
||||||
if (done)
|
if (done) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
done = true;
|
done = true;
|
||||||
|
|
||||||
// Enable VT processing on stdout and stdin
|
// Enable VT processing on stdout and stdin
|
||||||
|
|||||||
@@ -1284,8 +1284,9 @@ bool IsCombining(uint32_t ucs) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool IsFullWidth(uint32_t ucs) {
|
bool IsFullWidth(uint32_t ucs) {
|
||||||
if (ucs < 0x0300) // Quick path: // NOLINT
|
if (ucs < 0x0300) { // Quick path: // NOLINT
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return Bisearch(ucs, g_full_width_characters);
|
return Bisearch(ucs, g_full_width_characters);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// Copyright 2024 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.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user