Add {Const,}StringRef to simplify components.

This commit is contained in:
ArthurSonzogni 2021-05-14 21:43:35 +02:00
parent 9fdf235836
commit 048efb6912
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
29 changed files with 201 additions and 164 deletions

View File

@ -1,20 +1,21 @@
#include <string> // for operator+, to_wstring, allocator, wstring #include <memory> // for __shared_ptr_access, shared_ptr
#include <string> // for operator+, to_wstring
#include "ftxui/component/component.hpp" // for Button, Make #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, Element, gauge, text, operator|, vbox, border
using namespace ftxui; using namespace ftxui;
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
int value = 50; int value = 50;
std::wstring label_dec = L"decrease";
std::wstring label_inc = L"increase";
// The tree of components. This defines how to navigate using the keyboard. // The tree of components. This defines how to navigate using the keyboard.
auto buttons = Container::Horizontal({ auto buttons = Container::Horizontal({
Button(&label_dec, [&] { value--; }), Button("Decrease", [&] { value--; }),
Button(&label_inc, [&] { value++; }), Button("Increase", [&] { value++; }),
}); });
// Modify the way to render them on screen: // Modify the way to render them on screen:

View File

@ -1,24 +1,19 @@
#include "ftxui/component/checkbox.hpp" #include "ftxui/component/checkbox.hpp"
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Make #include "ftxui/component/component.hpp" // for Checkbox, Vertical
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component
using namespace ftxui; using namespace ftxui;
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
std::wstring build_examples_label = L"Build examples";
std::wstring build_tests_label = L"Build tests";
std::wstring use_webassembly_label = L"Use WebAssembly";
bool build_examples_state = false; bool build_examples_state = false;
bool build_tests_state = false; bool build_tests_state = false;
bool use_webassembly_state = true; bool use_webassembly_state = true;
auto component = Container::Vertical({ auto component = Container::Vertical({
Checkbox(&build_examples_label, &build_examples_state), Checkbox("Build examples", &build_examples_state),
Checkbox(&build_tests_label, &build_tests_state), Checkbox("Build tests", &build_tests_state),
Checkbox(&use_webassembly_label, &use_webassembly_state), Checkbox("Use WebAssembly", &use_webassembly_state),
}); });
auto screen = ScreenInteractive::TerminalOutput(); auto screen = ScreenInteractive::TerminalOutput();

View File

@ -1,18 +1,17 @@
#include <memory> // for unique_ptr, make_unique, __shared_ptr_access #include <memory> // for __shared_ptr_access, allocator_traits<>::value_type, shared_ptr
#include <string> // for operator+, wstring #include <string> // for operator+
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/component.hpp" // for Checkbox, Make #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/dom/elements.hpp" // for Element, operator|, size, border, frame, HEIGHT, LESS_THAN
#include "ftxui/dom/elements.hpp" // for Element, operator|, size, vbox, border, frame, Elements, HEIGHT, LESS_THAN
#include "ftxui/screen/string.hpp" // for to_wstring #include "ftxui/screen/string.hpp" // for to_wstring
using namespace ftxui; using namespace ftxui;
struct CheckboxState { struct CheckboxState {
std::wstring label;
bool checked; bool checked;
}; };
@ -22,8 +21,7 @@ int main(int argc, const char* argv[]) {
auto container = Container::Vertical({}); auto container = Container::Vertical({});
for (int i = 0; i < size; ++i) { for (int i = 0; i < size; ++i) {
states[i].checked = false; states[i].checked = false;
states[i].label = L"Checkbox " + to_wstring(i); container->Add(Checkbox(L"Checkbox" + to_wstring(i), &states[i].checked));
container->Add(Checkbox(&states[i].label, &states[i].checked));
} }
auto component = Renderer(container, [&] { auto component = Renderer(container, [&] {

View File

@ -1,14 +1,13 @@
#include <functional> // for function #include <functional> // for function
#include <memory> // for allocator, __shared_ptr_access #include <memory> // for shared_ptr, allocator, __shared_ptr_access
#include <string> // for wstring, basic_string #include <string> // for wstring, basic_string
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Checkbox, Button, Input, Make, Menu, Radiobox, Toggle #include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Input, Menu, Radiobox, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, xflex, text, WIDTH, hbox, vbox, EQUAL, LESS_THAN, border, GREATER_THAN #include "ftxui/dom/elements.hpp" // for separator, Element, operator|, size, xflex, text, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
using namespace ftxui; using namespace ftxui;
@ -27,7 +26,8 @@ Component Wrap(std::wstring name, Component component) {
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::FitComponent(); auto screen = ScreenInteractive::FitComponent();
// -- Menu ---------------------------------------------------------------------- // -- Menu
// ----------------------------------------------------------------------
const std::vector<std::wstring> menu_entries = { const std::vector<std::wstring> menu_entries = {
L"Menu 1", L"Menu 1",
L"Menu 2", L"Menu 2",
@ -48,14 +48,12 @@ int main(int argc, const char* argv[]) {
toggle = Wrap(L"Toggle", toggle); toggle = Wrap(L"Toggle", toggle);
// -- Checkbox --------------------------------------------------------------- // -- Checkbox ---------------------------------------------------------------
std::wstring checkbox_1_label = L"checkbox1";
std::wstring checkbox_2_label = L"checkbox2";
bool checkbox_1_selected = false; bool checkbox_1_selected = false;
bool checkbox_2_selected = false; bool checkbox_2_selected = false;
auto checkboxes = Container::Vertical({ auto checkboxes = Container::Vertical({
Checkbox(&checkbox_1_label, &checkbox_1_selected), Checkbox("checkbox1", &checkbox_1_selected),
Checkbox(&checkbox_2_label, &checkbox_2_selected), Checkbox("checkbox2", &checkbox_2_selected),
}); });
checkboxes = Wrap(L"Checkbox", checkboxes); checkboxes = Wrap(L"Checkbox", checkboxes);
@ -72,8 +70,7 @@ int main(int argc, const char* argv[]) {
// -- Input ------------------------------------------------------------------ // -- Input ------------------------------------------------------------------
std::wstring input_label; std::wstring input_label;
std::wstring input_placeholder = L"input"; auto input = Input(&input_label, L"placeholder");
auto input = Input(&input_label, &input_placeholder);
input = Wrap(L"Input", input); input = Wrap(L"Input", input);
// -- Button ----------------------------------------------------------------- // -- Button -----------------------------------------------------------------

View File

@ -2,20 +2,19 @@
#include <chrono> // for operator""s, chrono_literals #include <chrono> // for operator""s, chrono_literals
#include <cmath> // for sin #include <cmath> // for sin
#include <functional> // for ref, reference_wrapper, function #include <functional> // for ref, reference_wrapper, function
#include <memory> // for make_shared, __shared_ptr_access #include <memory> // for allocator, shared_ptr, __shared_ptr_access
#include <string> // for allocator, wstring, basic_string, operator+, to_wstring #include <string> // for wstring, basic_string, operator+, to_wstring
#include <thread> // for sleep_for, thread #include <thread> // for sleep_for, thread
#include <utility> // for move #include <utility> // for move
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Input, Menu, Radiobox, Toggle #include "ftxui/component/component.hpp" // for Checkbox, Renderer, Horizontal, Vertical, Input, Menu, Radiobox, Tab, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/event.hpp" // for Event, Event::Custom #include "ftxui/component/event.hpp" // for Event, Event::Custom
#include "ftxui/component/input.hpp" // for InputBase #include "ftxui/component/input.hpp" // for InputBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, operator|, color, bgcolor, Element, filler, size, vbox, flex, hbox, graph, separator, EQUAL, WIDTH, hcenter, bold, border, window, Elements, HEIGHT, hflow, flex_grow, frame, gauge, LESS_THAN, spinner, dim, GREATER_THAN #include "ftxui/dom/elements.hpp" // for text, operator|, color, bgcolor, Element, filler, size, vbox, flex, hbox, graph, separator, EQUAL, WIDTH, hcenter, bold, border, window, HEIGHT, Elements, hflow, flex_grow, frame, gauge, LESS_THAN, spinner, dim, GREATER_THAN
#include "ftxui/screen/color.hpp" // for Color, Color::BlueLight, Color::RedLight, Color::Black, Color::Blue, Color::Cyan, Color::CyanLight, Color::GrayDark, Color::GrayLight, Color::Green, Color::GreenLight, Color::Magenta, Color::MagentaLight, Color::Red, Color::White, Color::Yellow, Color::YellowLight, Color::Default #include "ftxui/screen/color.hpp" // for Color, Color::BlueLight, Color::RedLight, Color::Black, Color::Blue, Color::Cyan, Color::CyanLight, Color::GrayDark, Color::GrayLight, Color::Green, Color::GreenLight, Color::Magenta, Color::MagentaLight, Color::Red, Color::White, Color::Yellow, Color::YellowLight, Color::Default
using namespace ftxui; using namespace ftxui;
@ -157,17 +156,15 @@ int main(int argc, const char* argv[]) {
false, false,
false, false,
}; };
std::wstring input_add_content = L""; std::wstring input_add_content;
std::wstring input_add_placeholder = L"input_files"; Component input_add = Input(&input_add_content, "input files");
Component input_add = Input(&input_add_content, &input_add_placeholder);
std::vector<std::wstring> input_entries; std::vector<std::wstring> input_entries;
int input_selected = 0; int input_selected = 0;
Component input = Menu(&input_entries, &input_selected); Component input = Menu(&input_entries, &input_selected);
std::wstring executable_content_ = L""; std::wstring executable_content_ = L"";
std::wstring executable_placeholder_ = L"executable"; Component executable_ = Input(&executable_content_, "executable");
Component executable_ = Input(&executable_content_, &executable_placeholder_);
Component flags = Container::Vertical({ Component flags = Container::Vertical({
Checkbox(&options_label[0], &options_state[0]), Checkbox(&options_label[0], &options_state[0]),

View File

@ -1,23 +1,20 @@
#include <memory> // for allocator, __shared_ptr_access #include <memory> // for allocator, __shared_ptr_access
#include <string> // for operator+, wstring, char_traits #include <string> // for operator+, char_traits, wstring
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Input, Make #include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, hbox, separator, border, vbox, Element #include "ftxui/dom/elements.hpp" // for text, hbox, Element, separator, operator|, vbox, border
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;
std::wstring first_name_; std::wstring first_name_;
std::wstring last_name_; std::wstring last_name_;
std::wstring first_name_placeholder_ = L"first_name";
std::wstring last_name_placeholder_ = L"last_name";
Component input_first_name_ = Input(&first_name_, &first_name_placeholder_); Component input_first_name_ = Input(&first_name_, "first name");
Component input_last_name_ = Input(&last_name_, &last_name_placeholder_); Component input_last_name_ = Input(&last_name_, "last name");
auto component = Container::Vertical({ auto component = Container::Vertical({
input_first_name_, input_first_name_,

View File

@ -1,12 +1,11 @@
#include <functional> // for function #include <functional> // for function
#include <memory> // for __shared_ptr_access, shared_ptr #include <memory> // for allocator, __shared_ptr_access
#include <string> // for wstring, allocator, operator+, to_string, basic_string #include <string> // for wstring, operator+, to_string, basic_string
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu, Make #include "ftxui/component/component.hpp" // for Menu, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" // for MenuBase #include "ftxui/component/menu.hpp" // for MenuBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, separator, bold, hcenter, vbox, hbox, gauge, Element, operator|, border #include "ftxui/dom/elements.hpp" // for text, separator, bold, hcenter, vbox, hbox, gauge, Element, operator|, border

View File

@ -1,15 +1,14 @@
#include <functional> // for function #include <functional> // for function
#include <initializer_list> // for initializer_list #include <initializer_list> // for initializer_list
#include <memory> // for __shared_ptr_access #include <memory> // for __shared_ptr_access, shared_ptr, allocator
#include <string> // for wstring, allocator, basic_string #include <string> // for wstring, basic_string
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu, Make #include "ftxui/component/component.hpp" // for Menu, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" // for MenuBase #include "ftxui/component/menu.hpp" // for MenuBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component
#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, bgcolor, color, flex, Decorator, bold, hbox, border, dim #include "ftxui/dom/elements.hpp" // for operator|, Element, separator, bgcolor, color, flex, Decorator, bold, hbox, border, dim
#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::BlueLight, Color::Red, Color::Yellow #include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::BlueLight, Color::Red, Color::Yellow

View File

@ -1,13 +1,11 @@
#include <functional> // for function #include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <memory> // for allocator, __shared_ptr_access, shared_ptr, make_shared
#include <string> // for wstring, operator+, basic_string, char_traits #include <string> // for wstring, operator+, basic_string, char_traits
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Button, Make #include "ftxui/component/component.hpp" // for Button, Renderer, Horizontal, Tab
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, filler, text, hbox, separator, center, vbox, bold, border, clear_under, dbox, size, GREATER_THAN, HEIGHT #include "ftxui/dom/elements.hpp" // for Element, operator|, filler, text, hbox, separator, center, vbox, bold, border, clear_under, dbox, size, GREATER_THAN, HEIGHT
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
@ -21,10 +19,8 @@ int main(int argc, const char* argv[]) {
std::wstring rating = L"3/5 stars"; std::wstring rating = L"3/5 stars";
// At depth=0, two buttons. One for rating FTXUI and one for quitting. // At depth=0, two buttons. One for rating FTXUI and one for quitting.
std::wstring label_rate_ftxui = L"Rate FTXUI"; auto button_rate_ftxui = Button("Rate FTXUI", [&] { depth = 1; });
std::wstring label_quit = L"Quit"; auto button_quit = Button("Quit", screen.ExitLoopClosure());
auto button_rate_ftxui = Button(&label_rate_ftxui, [&] { depth = 1; });
auto button_quit = Button(&label_quit, screen.ExitLoopClosure());
auto depth_0_container = Container::Horizontal({ auto depth_0_container = Container::Horizontal({
button_rate_ftxui, button_rate_ftxui,

View File

@ -1,8 +1,9 @@
#include <memory> // for __shared_ptr_access, shared_ptr
#include <string> // for wstring, operator+ #include <string> // for wstring, operator+
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Make, Radiobox #include "ftxui/component/component.hpp" // for Radiobox, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, size, border, frame, HEIGHT, LESS_THAN #include "ftxui/dom/elements.hpp" // for Element, operator|, size, border, frame, HEIGHT, LESS_THAN

View File

@ -1,5 +1,3 @@
#include <memory> // for allocator
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider #include "ftxui/component/component.hpp" // for Slider
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive

View File

@ -1,14 +1,11 @@
#include <functional> // for function #include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for operator+, to_wstring, char_traits #include <string> // for operator+, to_wstring, char_traits
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Make #include "ftxui/component/component.hpp" // for Slider, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/event.hpp" // for Event, Event::Escape, Event::Return #include "ftxui/dom/elements.hpp" // for separator, Element, operator|, size, text, vbox, xflex, bgcolor, hbox, GREATER_THAN, WIDTH, border, HEIGHT, LESS_THAN
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, text, vbox, xflex, bgcolor, hbox, GREATER_THAN, WIDTH, border, HEIGHT, LESS_THAN
#include "ftxui/screen/color.hpp" // for Color #include "ftxui/screen/color.hpp" // for Color
using namespace ftxui; using namespace ftxui;

View File

@ -1,12 +1,11 @@
#include <memory> // for __shared_ptr_access #include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for wstring, allocator, basic_string #include <string> // for wstring, basic_string
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Radiobox, Make, Toggle #include "ftxui/component/component.hpp" // for Radiobox, Renderer, Tab, Toggle, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, separator, operator|, vbox, border #include "ftxui/dom/elements.hpp" // for Element, separator, operator|, vbox, border
using namespace ftxui; using namespace ftxui;

View File

@ -1,13 +1,12 @@
#include <memory> // for __shared_ptr_access #include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for wstring, allocator, basic_string #include <string> // for wstring, basic_string
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Radiobox, Make, Toggle #include "ftxui/component/component.hpp" // for Radiobox, Horizontal, Menu, Renderer, Tab
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/dom/elements.hpp" // for Element, separator, hbox, operator|, border
#include "ftxui/dom/elements.hpp" // for Element, separator, operator|, vbox, border
using namespace ftxui; using namespace ftxui;

View File

@ -1,13 +1,10 @@
#include <functional> // for function
#include <memory> // for allocator, __shared_ptr_access #include <memory> // for allocator, __shared_ptr_access
#include <string> // for wstring, basic_string #include <string> // for wstring, basic_string
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Toggle, Make #include "ftxui/component/component.hpp" // for Toggle, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/event.hpp" // for Event, Event::Return
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, hbox, vbox, Element #include "ftxui/dom/elements.hpp" // for text, hbox, vbox, Element

View File

@ -8,6 +8,7 @@
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element #include "ftxui/dom/elements.hpp" // for Element
#include "ftxui/screen/box.hpp" // for Box #include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/string.hpp" // for ConstStringRef
namespace ftxui { namespace ftxui {
struct Event; struct Event;
@ -20,7 +21,7 @@ class ButtonBase : public ComponentBase {
static ButtonBase* From(Component); static ButtonBase* From(Component);
// Constructor. // Constructor.
ButtonBase(const std::wstring* label, std::function<void()> on_click); ButtonBase(ConstStringRef label, std::function<void()> on_click);
~ButtonBase() override = default; ~ButtonBase() override = default;
// Component implementation. // Component implementation.
@ -28,7 +29,7 @@ class ButtonBase : public ComponentBase {
bool OnEvent(Event) override; bool OnEvent(Event) override;
private: private:
const std::wstring* label_; ConstStringRef label_;
std::function<void()> on_click_; std::function<void()> on_click_;
Box box_; Box box_;
}; };

View File

@ -2,12 +2,13 @@
#define FTXUI_COMPONENT_CHECKBOX_HPP #define FTXUI_COMPONENT_CHECKBOX_HPP
#include <functional> // for function #include <functional> // for function
#include <string> // for wstring, allocator #include <string> // for allocator, wstring
#include "ftxui/component/component.hpp" // for Component #include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing #include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing
#include "ftxui/screen/box.hpp" // for Box #include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/string.hpp" // for ConstStringRef
namespace ftxui { namespace ftxui {
struct Event; struct Event;
@ -21,7 +22,7 @@ class CheckboxBase : public ComponentBase {
static CheckboxBase* From(Component component); static CheckboxBase* From(Component component);
// Constructor. // Constructor.
CheckboxBase(const std::wstring* label, bool* state); CheckboxBase(ConstStringRef label, bool* state);
~CheckboxBase() override = default; ~CheckboxBase() override = default;
#if defined(_WIN32) #if defined(_WIN32)
@ -45,7 +46,7 @@ class CheckboxBase : public ComponentBase {
private: private:
bool OnMouseEvent(Event event); bool OnMouseEvent(Event event);
const std::wstring* const label_; ConstStringRef label_;
bool* const state_; bool* const state_;
int cursor_position = 0; int cursor_position = 0;
Box box_; Box box_;

View File

@ -7,6 +7,8 @@
#include <vector> // for vector #include <vector> // for vector
#include "ftxui/component/component_base.hpp" #include "ftxui/component/component_base.hpp"
#include "ftxui/dom/elements.hpp" // for Element
#include "ftxui/screen/string.hpp" // for ConstStringRef, StringRef
namespace ftxui { namespace ftxui {
@ -20,9 +22,9 @@ std::shared_ptr<T> Make(Args&&... args) {
return std::make_shared<T>(args...); return std::make_shared<T>(args...);
} }
Component Button(const std::wstring* label, std::function<void()> on_click); Component Button(ConstStringRef label, std::function<void()> on_click);
Component Checkbox(const std::wstring* label, bool* checked); Component Checkbox(ConstStringRef label, bool* checked);
Component Input(std::wstring* content, const std::wstring* placeholder); Component Input(StringRef content, ConstStringRef placeholder);
Component Menu(const std::vector<std::wstring>* entries, int* selected_); Component Menu(const std::vector<std::wstring>* entries, int* selected_);
Component Radiobox(const std::vector<std::wstring>* entries, int* selected_); Component Radiobox(const std::vector<std::wstring>* entries, int* selected_);
Component Toggle(const std::vector<std::wstring>* entries, int* selected); Component Toggle(const std::vector<std::wstring>* entries, int* selected);
@ -30,7 +32,7 @@ Component Renderer(Component child, std::function<Element()>);
Component Renderer(std::function<Element()>); Component Renderer(std::function<Element()>);
template <class T> // T = {int, float} template <class T> // T = {int, float}
Component Slider(std::wstring label, T* value, T min, T max, T increment); Component Slider(StringRef label, T* value, T min, T max, T increment);
namespace Container { namespace Container {
Component Vertical(Components children); Component Vertical(Components children);

View File

@ -8,6 +8,7 @@
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element #include "ftxui/dom/elements.hpp" // for Element
#include "ftxui/screen/box.hpp" // for Box #include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/string.hpp" // for ConstStringRef, StringRef
namespace ftxui { namespace ftxui {
struct Event; struct Event;
@ -20,7 +21,7 @@ class InputBase : public ComponentBase {
static InputBase* From(Component component); static InputBase* From(Component component);
// Constructor. // Constructor.
InputBase(std::wstring* content, const std::wstring* placeholder); InputBase(StringRef content, ConstStringRef placeholder);
~InputBase() override = default; ~InputBase() override = default;
// State. // State.
@ -35,8 +36,8 @@ class InputBase : public ComponentBase {
bool OnEvent(Event) override; bool OnEvent(Event) override;
private: private:
std::wstring* const content_; StringRef content_;
const std::wstring* const placeholder_; ConstStringRef placeholder_;
bool OnMouseEvent(Event); bool OnMouseEvent(Event);
Box input_box_; Box input_box_;

View File

@ -16,6 +16,41 @@ int wchar_width(wchar_t);
int wchar_width_cjk(wchar_t); int wchar_width_cjk(wchar_t);
int wstring_width(const std::wstring&); int wstring_width(const std::wstring&);
int wstring_width_cjk(const std::wstring&); int wstring_width_cjk(const std::wstring&);
/// @brief For convenience, this class convert multiple mutable string
/// references toward a shared representation.
class StringRef {
public:
StringRef(std::wstring& ref);
StringRef(std::wstring* ref);
StringRef(const wchar_t* ref);
StringRef(const char* ref);
std::wstring& operator*();
std::wstring* operator->();
private:
std::wstring* const borrowed_ = nullptr;
std::wstring owned_;
};
/// @brief For convenience, this class convert multiple immutable string
/// references toward shared representation.
class ConstStringRef {
public:
ConstStringRef(const std::wstring& ref);
ConstStringRef(const std::wstring* ref);
ConstStringRef(const wchar_t* ref);
ConstStringRef(const char* ref);
const std::wstring& operator*();
const std::wstring* operator->();
private:
const std::wstring* const borrowed_ = nullptr;
const std::wstring owned_;
};
} // namespace ftxui } // namespace ftxui
#endif /* end of include guard: FTXUI_SCREEN_STRING_HPP */ #endif /* end of include guard: FTXUI_SCREEN_STRING_HPP */

View File

@ -31,7 +31,7 @@ namespace ftxui {
/// │Click to quit│ /// │Click to quit│
/// └─────────────┘ /// └─────────────┘
/// ``` /// ```
Component Button(const std::wstring* label, std::function<void()> on_click) { Component Button(ConstStringRef label, std::function<void()> on_click) {
return Make<ButtonBase>(label, on_click); return Make<ButtonBase>(label, on_click);
} }
@ -40,8 +40,7 @@ ButtonBase* ButtonBase::From(Component component) {
return static_cast<ButtonBase*>(component.get()); return static_cast<ButtonBase*>(component.get());
} }
ButtonBase::ButtonBase(const std::wstring* label, ButtonBase::ButtonBase(ConstStringRef label, std::function<void()> on_click)
std::function<void()> on_click)
: label_(label), on_click_(on_click) {} : label_(label), on_click_(on_click) {}
Element ButtonBase::Render() { Element ButtonBase::Render() {

View File

@ -30,7 +30,7 @@ namespace ftxui {
/// ```bash /// ```bash
/// ☐ Make a sandwitch /// ☐ Make a sandwitch
/// ``` /// ```
Component Checkbox(const std::wstring* label, bool* checked) { Component Checkbox(ConstStringRef label, bool* checked) {
return Make<CheckboxBase>(label, checked); return Make<CheckboxBase>(label, checked);
} }
@ -39,7 +39,7 @@ CheckboxBase* From(Component component) {
return static_cast<CheckboxBase*>(component.get()); return static_cast<CheckboxBase*>(component.get());
} }
CheckboxBase::CheckboxBase(const std::wstring* label, bool* state) CheckboxBase::CheckboxBase(ConstStringRef label, bool* state)
: label_(label), state_(state) {} : label_(label), state_(state) {}
Element CheckboxBase::Render() { Element CheckboxBase::Render() {

View File

@ -83,7 +83,7 @@ Component ContainerBase::Vertical() {
// static // static
Component ContainerBase::Vertical(Components children) { Component ContainerBase::Vertical(Components children) {
auto container = std::make_shared<Container>(); auto container = std::make_shared<ContainerBase>();
container->event_handler_ = &ContainerBase::VerticalEvent; container->event_handler_ = &ContainerBase::VerticalEvent;
container->render_handler_ = &ContainerBase::VerticalRender; container->render_handler_ = &ContainerBase::VerticalRender;
for (Component& child : children) for (Component& child : children)
@ -98,7 +98,7 @@ Component ContainerBase::Horizontal() {
// static // static
Component ContainerBase::Horizontal(Components children) { Component ContainerBase::Horizontal(Components children) {
auto container = std::make_shared<Container>(); auto container = std::make_shared<ContainerBase>();
container->event_handler_ = &ContainerBase::HorizontalEvent; container->event_handler_ = &ContainerBase::HorizontalEvent;
container->render_handler_ = &ContainerBase::HorizontalRender; container->render_handler_ = &ContainerBase::HorizontalRender;
for (Component& child : children) for (Component& child : children)
@ -113,7 +113,7 @@ Component ContainerBase::Tab(int* selector) {
// static // static
Component ContainerBase::Tab(int* selector, Components children) { Component ContainerBase::Tab(int* selector, Components children) {
auto container = std::make_shared<Container>(); auto container = std::make_shared<ContainerBase>();
container->selector_ = selector; container->selector_ = selector;
container->event_handler_ = &ContainerBase::TabEvent; container->event_handler_ = &ContainerBase::TabEvent;
container->render_handler_ = &ContainerBase::TabRender; container->render_handler_ = &ContainerBase::TabRender;

View File

@ -9,7 +9,7 @@
using namespace ftxui; using namespace ftxui;
TEST(ContainerTest, HorizontalEvent) { TEST(ContainerTest, HorizontalEvent) {
auto container = Container::Horizontal(); auto container = ContainerBase::Horizontal();
Component c0, c1, c2; Component c0, c1, c2;
container->Add(c0); container->Add(c0);
container->Add(c1); container->Add(c1);
@ -80,7 +80,7 @@ TEST(ContainerTest, HorizontalEvent) {
} }
TEST(ContainerTest, VerticalEvent) { TEST(ContainerTest, VerticalEvent) {
auto container = Container::Vertical(); auto container = ContainerBase::Vertical();
Component c0, c1, c2; Component c0, c1, c2;
container->Add(c0); container->Add(c0);
container->Add(c1); container->Add(c1);
@ -151,7 +151,7 @@ TEST(ContainerTest, VerticalEvent) {
} }
TEST(ContainerTest, SetActiveChild) { TEST(ContainerTest, SetActiveChild) {
auto container = Container::Horizontal(); auto container = ContainerBase::Horizontal();
Component c0, c1, c2; Component c0, c1, c2;
container->Add(c0); container->Add(c0);
container->Add(c1); container->Add(c1);
@ -203,16 +203,16 @@ TEST(ContainerTest, SetActiveChild) {
} }
TEST(ContainerTest, TakeFocus) { TEST(ContainerTest, TakeFocus) {
auto c = Container::Horizontal(); auto c = ContainerBase::Horizontal();
auto c1 = Container::Vertical(); auto c1 = ContainerBase::Vertical();
auto c2 = Container::Vertical(); auto c2 = ContainerBase::Vertical();
auto c3 = Container::Vertical(); auto c3 = ContainerBase::Vertical();
auto c11 = Container::Horizontal(); auto c11 = ContainerBase::Horizontal();
auto c12 = Container::Horizontal(); auto c12 = ContainerBase::Horizontal();
auto c13 = Container::Horizontal(); auto c13 = ContainerBase::Horizontal();
auto c21 = Container::Horizontal(); auto c21 = ContainerBase::Horizontal();
auto c22 = Container::Horizontal(); auto c22 = ContainerBase::Horizontal();
auto c23 = Container::Horizontal(); auto c23 = ContainerBase::Horizontal();
c->Add(c1); c->Add(c1);
c->Add(c2); c->Add(c2);

View File

@ -1,5 +1,6 @@
#include <algorithm> // for max, min #include <algorithm> // for max, min
#include <memory> // for shared_ptr #include <memory> // for shared_ptr
#include <string> // for wstring, allocator, basic_string
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Home, Event::Return #include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Home, Event::Return
@ -30,7 +31,7 @@ namespace ftxui {
/// ```bash /// ```bash
/// placeholder /// placeholder
/// ``` /// ```
Component Input(std::wstring* content, const std::wstring* placeholder) { Component Input(StringRef content, ConstStringRef placeholder) {
return Make<InputBase>(content, placeholder); return Make<InputBase>(content, placeholder);
} }
@ -39,7 +40,7 @@ InputBase* InputBase::From(Component component) {
return static_cast<InputBase*>(component.get()); return static_cast<InputBase*>(component.get());
} }
InputBase::InputBase(std::wstring* content, const std::wstring* placeholder) InputBase::InputBase(StringRef content, ConstStringRef placeholder)
: content_(content), placeholder_(placeholder) {} : content_(content), placeholder_(placeholder) {}
// Component implementation. // Component implementation.

View File

@ -1,11 +1,12 @@
#include <gtest/gtest-message.h> // for Message #include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult #include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
#include <memory> // for __shared_ptr_access #include <memory> // for __shared_ptr_access
#include <string> // for wstring, allocator
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home #include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home
#include "ftxui/component/input.hpp" #include "ftxui/component/input.hpp"
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, SuiteApiResolver, TEST, TestFactoryImpl #include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
using namespace ftxui; using namespace ftxui;

View File

@ -1,7 +1,8 @@
#include <functional> // for function #include <functional> // for function
#include <memory> // for shared_ptr #include <memory> // for __shared_ptr_access
#include <utility> // for move
#include "ftxui/component/component.hpp" // for Make #include "ftxui/component/component.hpp" // for Component, Make, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element #include "ftxui/dom/elements.hpp" // for Element

View File

@ -1,4 +1,4 @@
#include <string> // for allocator, wstring #include <string> // for allocator
#include <utility> // for move #include <utility> // for move
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
@ -10,13 +10,14 @@
#include "ftxui/dom/elements.hpp" // for Element, text, color, operator|, xflex, gauge, dim, hbox, reflect, underlined, vcenter #include "ftxui/dom/elements.hpp" // for Element, text, color, operator|, xflex, gauge, dim, hbox, reflect, underlined, vcenter
#include "ftxui/screen/box.hpp" // for Box #include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::GrayLight #include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::GrayLight
#include "ftxui/screen/string.hpp" // for StringRef
namespace ftxui { namespace ftxui {
template <class T> template <class T>
class SliderBase : public ComponentBase { class SliderBase : public ComponentBase {
public: public:
SliderBase(std::wstring label, T* value, T min, T max, T increment) SliderBase(StringRef label, T* value, T min, T max, T increment)
: label_(label), : label_(label),
value_(value), value_(value),
min_(min), min_(min),
@ -28,7 +29,7 @@ class SliderBase : public ComponentBase {
Focused() ? color(Color::GrayLight) : color(Color::GrayDark); Focused() ? color(Color::GrayLight) : color(Color::GrayDark);
float percent = float(*value_ - min_) / float(max_ - min_); float percent = float(*value_ - min_) / float(max_ - min_);
return hbox({ return hbox({
text(label_) | dim | vcenter, text(*label_) | dim | vcenter,
hbox({ hbox({
text(L"["), text(L"["),
gauge(percent) | underlined | xflex | reflect(gauge_box_), gauge(percent) | underlined | xflex | reflect(gauge_box_),
@ -84,7 +85,7 @@ class SliderBase : public ComponentBase {
} }
private: private:
std::wstring label_; StringRef label_;
T* value_; T* value_;
T min_; T min_;
T max_; T max_;
@ -117,17 +118,17 @@ class SliderBase : public ComponentBase {
/// Value:[██████████████████████████ ] /// Value:[██████████████████████████ ]
/// ``` /// ```
template <class T> template <class T>
Component Slider(std::wstring label, T* value, T min, T max, T increment) { Component Slider(StringRef label, T* value, T min, T max, T increment) {
return Make<SliderBase<T>>(std::move(label), value, min, max, increment); return Make<SliderBase<T>>(std::move(label), value, min, max, increment);
} }
template Component Slider(std::wstring label, template Component Slider(StringRef label,
int* value, int* value,
int min, int min,
int max, int max,
int increment); int increment);
template Component Slider(std::wstring label, template Component Slider(StringRef label,
float* value, float* value,
float min, float min,
float max, float max,

View File

@ -25,6 +25,30 @@ std::wstring to_wstring(const std::string& s) {
#pragma warning(pop) #pragma warning(pop)
#endif #endif
StringRef::StringRef(std::wstring& ref) : borrowed_(&ref) {}
StringRef::StringRef(std::wstring* ref) : borrowed_(ref) {}
StringRef::StringRef(const wchar_t* ref) : owned_(ref) {}
StringRef::StringRef(const char* ref) : owned_(to_wstring(std::string(ref))) {}
std::wstring& StringRef::operator*() {
return borrowed_ ? *borrowed_ : owned_;
}
std::wstring* StringRef::operator->() {
return borrowed_ ? borrowed_ : &owned_;
}
ConstStringRef::ConstStringRef(const std::wstring& ref) : borrowed_(&ref) {}
ConstStringRef::ConstStringRef(const std::wstring* ref) : borrowed_(ref) {}
ConstStringRef::ConstStringRef(const wchar_t* ref) : owned_(ref) {}
ConstStringRef::ConstStringRef(const char* ref)
: owned_(to_wstring(std::string(ref))) {}
const std::wstring& ConstStringRef::operator*() {
return borrowed_ ? *borrowed_ : owned_;
}
const std::wstring* ConstStringRef::operator->() {
return borrowed_ ? borrowed_ : &owned_;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.