From 048efb6912f1551409727304868087ca7e2319f1 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Fri, 14 May 2021 21:43:35 +0200 Subject: [PATCH] Add {Const,}StringRef to simplify components. --- examples/component/button.cpp | 15 +++++----- examples/component/checkbox.cpp | 17 ++++-------- examples/component/checkbox_in_frame.cpp | 20 ++++++-------- examples/component/gallery.cpp | 19 ++++++------- examples/component/homescreen.cpp | 23 +++++++--------- examples/component/input.cpp | 13 ++++----- examples/component/menu2.cpp | 9 +++--- examples/component/menu_style.cpp | 9 +++--- examples/component/modal_dialog.cpp | 16 ++++------- examples/component/radiobox_in_frame.cpp | 3 +- examples/component/slider.cpp | 2 -- examples/component/slider_rgb.cpp | 13 ++++----- examples/component/tab_horizontal.cpp | 11 ++++---- examples/component/tab_vertical.cpp | 13 ++++----- examples/component/toggle.cpp | 11 +++----- include/ftxui/component/button.hpp | 5 ++-- include/ftxui/component/checkbox.hpp | 11 ++++---- include/ftxui/component/component.hpp | 10 ++++--- include/ftxui/component/input.hpp | 7 +++-- include/ftxui/screen/string.hpp | 35 ++++++++++++++++++++++++ src/ftxui/component/button.cpp | 5 ++-- src/ftxui/component/checkbox.cpp | 4 +-- src/ftxui/component/container.cpp | 12 ++++---- src/ftxui/component/container_test.cpp | 26 +++++++++--------- src/ftxui/component/input.cpp | 5 ++-- src/ftxui/component/input_test.cpp | 7 +++-- src/ftxui/component/renderer.cpp | 5 ++-- src/ftxui/component/slider.cpp | 15 +++++----- src/ftxui/screen/string.cpp | 24 ++++++++++++++++ 29 files changed, 201 insertions(+), 164 deletions(-) diff --git a/examples/component/button.cpp b/examples/component/button.cpp index 6382890..5bc7852 100644 --- a/examples/component/button.cpp +++ b/examples/component/button.cpp @@ -1,20 +1,21 @@ -#include // for operator+, to_wstring, allocator, wstring +#include // for __shared_ptr_access, shared_ptr +#include // for operator+, to_wstring -#include "ftxui/component/component.hpp" // for Button, Make -#include "ftxui/component/container.hpp" // for Container +#include "ftxui/component/captured_mouse.hpp" // for ftxui +#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/dom/elements.hpp" // for separator, Element, gauge, text, operator|, vbox, border using namespace ftxui; int main(int argc, const char* argv[]) { 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. auto buttons = Container::Horizontal({ - Button(&label_dec, [&] { value--; }), - Button(&label_inc, [&] { value++; }), + Button("Decrease", [&] { value--; }), + Button("Increase", [&] { value++; }), }); // Modify the way to render them on screen: diff --git a/examples/component/checkbox.cpp b/examples/component/checkbox.cpp index cd2637e..361ee68 100644 --- a/examples/component/checkbox.cpp +++ b/examples/component/checkbox.cpp @@ -1,24 +1,19 @@ #include "ftxui/component/checkbox.hpp" -#include "ftxui/component/captured_mouse.hpp" // for ftxui -#include "ftxui/component/component.hpp" // for Checkbox, Make -#include "ftxui/component/container.hpp" // for Container -#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component +#include "ftxui/component/captured_mouse.hpp" // for ftxui +#include "ftxui/component/component.hpp" // for Checkbox, Vertical +#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive using namespace ftxui; 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_tests_state = false; bool use_webassembly_state = true; auto component = Container::Vertical({ - Checkbox(&build_examples_label, &build_examples_state), - Checkbox(&build_tests_label, &build_tests_state), - Checkbox(&use_webassembly_label, &use_webassembly_state), + Checkbox("Build examples", &build_examples_state), + Checkbox("Build tests", &build_tests_state), + Checkbox("Use WebAssembly", &use_webassembly_state), }); auto screen = ScreenInteractive::TerminalOutput(); diff --git a/examples/component/checkbox_in_frame.cpp b/examples/component/checkbox_in_frame.cpp index 75b30de..26f405b 100644 --- a/examples/component/checkbox_in_frame.cpp +++ b/examples/component/checkbox_in_frame.cpp @@ -1,18 +1,17 @@ -#include // for unique_ptr, make_unique, __shared_ptr_access -#include // for operator+, wstring +#include // for __shared_ptr_access, allocator_traits<>::value_type, shared_ptr +#include // for operator+ #include // for vector -#include "ftxui/component/component.hpp" // for Checkbox, Make -#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/dom/elements.hpp" // for Element, operator|, size, vbox, border, frame, Elements, HEIGHT, LESS_THAN +#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/screen_interactive.hpp" // for ScreenInteractive +#include "ftxui/dom/elements.hpp" // for Element, operator|, size, border, frame, HEIGHT, LESS_THAN #include "ftxui/screen/string.hpp" // for to_wstring using namespace ftxui; struct CheckboxState { - std::wstring label; bool checked; }; @@ -20,10 +19,9 @@ int main(int argc, const char* argv[]) { int size = 30; std::vector states(size); auto container = Container::Vertical({}); - for(int i = 0; iAdd(Checkbox(&states[i].label, &states[i].checked)); + container->Add(Checkbox(L"Checkbox" + to_wstring(i), &states[i].checked)); } auto component = Renderer(container, [&] { diff --git a/examples/component/gallery.cpp b/examples/component/gallery.cpp index 1d3564e..99f5af4 100644 --- a/examples/component/gallery.cpp +++ b/examples/component/gallery.cpp @@ -1,14 +1,13 @@ #include // for function -#include // for allocator, __shared_ptr_access +#include // for shared_ptr, allocator, __shared_ptr_access #include // for wstring, basic_string #include // for vector #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/container.hpp" // for Container #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; @@ -27,7 +26,8 @@ Component Wrap(std::wstring name, Component component) { int main(int argc, const char* argv[]) { auto screen = ScreenInteractive::FitComponent(); - // -- Menu ---------------------------------------------------------------------- + // -- Menu + // ---------------------------------------------------------------------- const std::vector menu_entries = { L"Menu 1", L"Menu 2", @@ -48,14 +48,12 @@ int main(int argc, const char* argv[]) { toggle = Wrap(L"Toggle", toggle); // -- Checkbox --------------------------------------------------------------- - std::wstring checkbox_1_label = L"checkbox1"; - std::wstring checkbox_2_label = L"checkbox2"; bool checkbox_1_selected = false; bool checkbox_2_selected = false; auto checkboxes = Container::Vertical({ - Checkbox(&checkbox_1_label, &checkbox_1_selected), - Checkbox(&checkbox_2_label, &checkbox_2_selected), + Checkbox("checkbox1", &checkbox_1_selected), + Checkbox("checkbox2", &checkbox_2_selected), }); checkboxes = Wrap(L"Checkbox", checkboxes); @@ -72,8 +70,7 @@ int main(int argc, const char* argv[]) { // -- Input ------------------------------------------------------------------ std::wstring input_label; - std::wstring input_placeholder = L"input"; - auto input = Input(&input_label, &input_placeholder); + auto input = Input(&input_label, L"placeholder"); input = Wrap(L"Input", input); // -- Button ----------------------------------------------------------------- diff --git a/examples/component/homescreen.cpp b/examples/component/homescreen.cpp index 876c3c2..6758e24 100644 --- a/examples/component/homescreen.cpp +++ b/examples/component/homescreen.cpp @@ -2,20 +2,19 @@ #include // for operator""s, chrono_literals #include // for sin #include // for ref, reference_wrapper, function -#include // for make_shared, __shared_ptr_access -#include // for allocator, wstring, basic_string, operator+, to_wstring -#include // for sleep_for, thread -#include // for move -#include // for vector +#include // for allocator, shared_ptr, __shared_ptr_access +#include // for wstring, basic_string, operator+, to_wstring +#include // for sleep_for, thread +#include // for move +#include // for vector #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/container.hpp" // for Container #include "ftxui/component/event.hpp" // for Event, Event::Custom #include "ftxui/component/input.hpp" // for InputBase #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 using namespace ftxui; @@ -157,17 +156,15 @@ int main(int argc, const char* argv[]) { false, false, }; - std::wstring input_add_content = L""; - std::wstring input_add_placeholder = L"input_files"; - Component input_add = Input(&input_add_content, &input_add_placeholder); + std::wstring input_add_content; + Component input_add = Input(&input_add_content, "input files"); std::vector input_entries; int input_selected = 0; Component input = Menu(&input_entries, &input_selected); std::wstring executable_content_ = L""; - std::wstring executable_placeholder_ = L"executable"; - Component executable_ = Input(&executable_content_, &executable_placeholder_); + Component executable_ = Input(&executable_content_, "executable"); Component flags = Container::Vertical({ Checkbox(&options_label[0], &options_state[0]), diff --git a/examples/component/input.cpp b/examples/component/input.cpp index e9d2841..c5a31c2 100644 --- a/examples/component/input.cpp +++ b/examples/component/input.cpp @@ -1,23 +1,20 @@ #include // for allocator, __shared_ptr_access -#include // for operator+, wstring, char_traits +#include // for operator+, char_traits, wstring #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/container.hpp" // for Container #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[]) { using namespace ftxui; std::wstring first_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_last_name_ = Input(&last_name_, &last_name_placeholder_); + Component input_first_name_ = Input(&first_name_, "first name"); + Component input_last_name_ = Input(&last_name_, "last name"); auto component = Container::Vertical({ input_first_name_, diff --git a/examples/component/menu2.cpp b/examples/component/menu2.cpp index 8bb71a4..82f7121 100644 --- a/examples/component/menu2.cpp +++ b/examples/component/menu2.cpp @@ -1,12 +1,11 @@ #include // for function -#include // for __shared_ptr_access, shared_ptr -#include // for wstring, allocator, operator+, to_string, basic_string -#include // for vector +#include // for allocator, __shared_ptr_access +#include // for wstring, operator+, to_string, basic_string +#include // for vector #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/container.hpp" // for Container #include "ftxui/component/menu.hpp" // for MenuBase #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 diff --git a/examples/component/menu_style.cpp b/examples/component/menu_style.cpp index 65a0dce..4be8f08 100644 --- a/examples/component/menu_style.cpp +++ b/examples/component/menu_style.cpp @@ -1,15 +1,14 @@ #include // for function #include // for initializer_list -#include // for __shared_ptr_access -#include // for wstring, allocator, basic_string +#include // for __shared_ptr_access, shared_ptr, allocator +#include // for wstring, basic_string #include // for vector #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/container.hpp" // for Container #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/screen/color.hpp" // for Color, Color::Blue, Color::BlueLight, Color::Red, Color::Yellow diff --git a/examples/component/modal_dialog.cpp b/examples/component/modal_dialog.cpp index 91317de..7db3758 100644 --- a/examples/component/modal_dialog.cpp +++ b/examples/component/modal_dialog.cpp @@ -1,13 +1,11 @@ -#include // for function -#include // for allocator, __shared_ptr_access, shared_ptr, make_shared +#include // for allocator, __shared_ptr_access, shared_ptr #include // for wstring, operator+, basic_string, char_traits #include // for vector #include "ftxui/component/captured_mouse.hpp" // for ftxui -#include "ftxui/component/component.hpp" // for Button, Make -#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/component.hpp" // for Button, Renderer, Horizontal, Tab +#include "ftxui/component/component_base.hpp" // for ComponentBase +#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #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[]) { @@ -21,10 +19,8 @@ int main(int argc, const char* argv[]) { std::wstring rating = L"3/5 stars"; // At depth=0, two buttons. One for rating FTXUI and one for quitting. - std::wstring label_rate_ftxui = L"Rate FTXUI"; - std::wstring label_quit = L"Quit"; - auto button_rate_ftxui = Button(&label_rate_ftxui, [&] { depth = 1; }); - auto button_quit = Button(&label_quit, screen.ExitLoopClosure()); + auto button_rate_ftxui = Button("Rate FTXUI", [&] { depth = 1; }); + auto button_quit = Button("Quit", screen.ExitLoopClosure()); auto depth_0_container = Container::Horizontal({ button_rate_ftxui, diff --git a/examples/component/radiobox_in_frame.cpp b/examples/component/radiobox_in_frame.cpp index ff5514e..b62d0d3 100644 --- a/examples/component/radiobox_in_frame.cpp +++ b/examples/component/radiobox_in_frame.cpp @@ -1,8 +1,9 @@ +#include // for __shared_ptr_access, shared_ptr #include // for wstring, operator+ #include // for vector #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/screen_interactive.hpp" // for ScreenInteractive #include "ftxui/dom/elements.hpp" // for Element, operator|, size, border, frame, HEIGHT, LESS_THAN diff --git a/examples/component/slider.cpp b/examples/component/slider.cpp index 7a3f51e..c4b41b6 100644 --- a/examples/component/slider.cpp +++ b/examples/component/slider.cpp @@ -1,5 +1,3 @@ -#include // for allocator - #include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/component.hpp" // for Slider #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive diff --git a/examples/component/slider_rgb.cpp b/examples/component/slider_rgb.cpp index 07f4b00..3eb10e6 100644 --- a/examples/component/slider_rgb.cpp +++ b/examples/component/slider_rgb.cpp @@ -1,14 +1,11 @@ -#include // for function -#include // for allocator, __shared_ptr_access -#include // for operator+, to_wstring, char_traits +#include // for allocator, __shared_ptr_access, shared_ptr +#include // for operator+, to_wstring, char_traits #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/container.hpp" // for Container -#include "ftxui/component/event.hpp" // for Event, Event::Escape, Event::Return -#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/component/screen_interactive.hpp" // for ScreenInteractive +#include "ftxui/dom/elements.hpp" // for separator, Element, operator|, size, text, vbox, xflex, bgcolor, hbox, GREATER_THAN, WIDTH, border, HEIGHT, LESS_THAN #include "ftxui/screen/color.hpp" // for Color using namespace ftxui; diff --git a/examples/component/tab_horizontal.cpp b/examples/component/tab_horizontal.cpp index 5b849c1..578dff2 100644 --- a/examples/component/tab_horizontal.cpp +++ b/examples/component/tab_horizontal.cpp @@ -1,12 +1,11 @@ -#include // for __shared_ptr_access -#include // for wstring, allocator, basic_string +#include // for allocator, __shared_ptr_access, shared_ptr +#include // for wstring, basic_string #include // for vector #include "ftxui/component/captured_mouse.hpp" // for ftxui -#include "ftxui/component/component.hpp" // for Radiobox, Make, Toggle -#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/component.hpp" // for Radiobox, Renderer, Tab, Toggle, Vertical +#include "ftxui/component/component_base.hpp" // for ComponentBase +#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include "ftxui/dom/elements.hpp" // for Element, separator, operator|, vbox, border using namespace ftxui; diff --git a/examples/component/tab_vertical.cpp b/examples/component/tab_vertical.cpp index f2c6c5f..ad454d7 100644 --- a/examples/component/tab_vertical.cpp +++ b/examples/component/tab_vertical.cpp @@ -1,13 +1,12 @@ -#include // for __shared_ptr_access -#include // for wstring, allocator, basic_string +#include // for allocator, __shared_ptr_access, shared_ptr +#include // for wstring, basic_string #include // for vector #include "ftxui/component/captured_mouse.hpp" // for ftxui -#include "ftxui/component/component.hpp" // for Radiobox, Make, Toggle -#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/dom/elements.hpp" // for Element, separator, operator|, vbox, border +#include "ftxui/component/component.hpp" // for Radiobox, Horizontal, Menu, Renderer, Tab +#include "ftxui/component/component_base.hpp" // for ComponentBase +#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive +#include "ftxui/dom/elements.hpp" // for Element, separator, hbox, operator|, border using namespace ftxui; diff --git a/examples/component/toggle.cpp b/examples/component/toggle.cpp index 6159d43..b56f211 100644 --- a/examples/component/toggle.cpp +++ b/examples/component/toggle.cpp @@ -1,13 +1,10 @@ -#include // for function -#include // for allocator, __shared_ptr_access -#include // for wstring, basic_string -#include // for vector +#include // for allocator, __shared_ptr_access +#include // for wstring, basic_string +#include // for vector #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/container.hpp" // for Container -#include "ftxui/component/event.hpp" // for Event, Event::Return #include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive #include "ftxui/dom/elements.hpp" // for text, hbox, vbox, Element diff --git a/include/ftxui/component/button.hpp b/include/ftxui/component/button.hpp index 073dbdc..8638823 100644 --- a/include/ftxui/component/button.hpp +++ b/include/ftxui/component/button.hpp @@ -8,6 +8,7 @@ #include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/dom/elements.hpp" // for Element #include "ftxui/screen/box.hpp" // for Box +#include "ftxui/screen/string.hpp" // for ConstStringRef namespace ftxui { struct Event; @@ -20,7 +21,7 @@ class ButtonBase : public ComponentBase { static ButtonBase* From(Component); // Constructor. - ButtonBase(const std::wstring* label, std::function on_click); + ButtonBase(ConstStringRef label, std::function on_click); ~ButtonBase() override = default; // Component implementation. @@ -28,7 +29,7 @@ class ButtonBase : public ComponentBase { bool OnEvent(Event) override; private: - const std::wstring* label_; + ConstStringRef label_; std::function on_click_; Box box_; }; diff --git a/include/ftxui/component/checkbox.hpp b/include/ftxui/component/checkbox.hpp index fd7959e..9cd1db4 100644 --- a/include/ftxui/component/checkbox.hpp +++ b/include/ftxui/component/checkbox.hpp @@ -2,12 +2,13 @@ #define FTXUI_COMPONENT_CHECKBOX_HPP #include // for function -#include // for wstring, allocator +#include // for allocator, wstring #include "ftxui/component/component.hpp" // for Component #include "ftxui/component/component_base.hpp" // for ComponentBase -#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing -#include "ftxui/screen/box.hpp" // for Box +#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing +#include "ftxui/screen/box.hpp" // for Box +#include "ftxui/screen/string.hpp" // for ConstStringRef namespace ftxui { struct Event; @@ -21,7 +22,7 @@ class CheckboxBase : public ComponentBase { static CheckboxBase* From(Component component); // Constructor. - CheckboxBase(const std::wstring* label, bool* state); + CheckboxBase(ConstStringRef label, bool* state); ~CheckboxBase() override = default; #if defined(_WIN32) @@ -45,7 +46,7 @@ class CheckboxBase : public ComponentBase { private: bool OnMouseEvent(Event event); - const std::wstring* const label_; + ConstStringRef label_; bool* const state_; int cursor_position = 0; Box box_; diff --git a/include/ftxui/component/component.hpp b/include/ftxui/component/component.hpp index 47e942c..cebbc54 100644 --- a/include/ftxui/component/component.hpp +++ b/include/ftxui/component/component.hpp @@ -7,6 +7,8 @@ #include // for vector #include "ftxui/component/component_base.hpp" +#include "ftxui/dom/elements.hpp" // for Element +#include "ftxui/screen/string.hpp" // for ConstStringRef, StringRef namespace ftxui { @@ -20,9 +22,9 @@ std::shared_ptr Make(Args&&... args) { return std::make_shared(args...); } -Component Button(const std::wstring* label, std::function on_click); -Component Checkbox(const std::wstring* label, bool* checked); -Component Input(std::wstring* content, const std::wstring* placeholder); +Component Button(ConstStringRef label, std::function on_click); +Component Checkbox(ConstStringRef label, bool* checked); +Component Input(StringRef content, ConstStringRef placeholder); Component Menu(const std::vector* entries, int* selected_); Component Radiobox(const std::vector* entries, int* selected_); Component Toggle(const std::vector* entries, int* selected); @@ -30,7 +32,7 @@ Component Renderer(Component child, std::function); Component Renderer(std::function); template // 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 { Component Vertical(Components children); diff --git a/include/ftxui/component/input.hpp b/include/ftxui/component/input.hpp index fdfc2af..605db99 100644 --- a/include/ftxui/component/input.hpp +++ b/include/ftxui/component/input.hpp @@ -8,6 +8,7 @@ #include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/dom/elements.hpp" // for Element #include "ftxui/screen/box.hpp" // for Box +#include "ftxui/screen/string.hpp" // for ConstStringRef, StringRef namespace ftxui { struct Event; @@ -20,7 +21,7 @@ class InputBase : public ComponentBase { static InputBase* From(Component component); // Constructor. - InputBase(std::wstring* content, const std::wstring* placeholder); + InputBase(StringRef content, ConstStringRef placeholder); ~InputBase() override = default; // State. @@ -35,8 +36,8 @@ class InputBase : public ComponentBase { bool OnEvent(Event) override; private: - std::wstring* const content_; - const std::wstring* const placeholder_; + StringRef content_; + ConstStringRef placeholder_; bool OnMouseEvent(Event); Box input_box_; diff --git a/include/ftxui/screen/string.hpp b/include/ftxui/screen/string.hpp index 86899cc..2d199bc 100644 --- a/include/ftxui/screen/string.hpp +++ b/include/ftxui/screen/string.hpp @@ -16,6 +16,41 @@ int wchar_width(wchar_t); int wchar_width_cjk(wchar_t); int wstring_width(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 #endif /* end of include guard: FTXUI_SCREEN_STRING_HPP */ diff --git a/src/ftxui/component/button.cpp b/src/ftxui/component/button.cpp index 9876879..5ed0ce0 100644 --- a/src/ftxui/component/button.cpp +++ b/src/ftxui/component/button.cpp @@ -31,7 +31,7 @@ namespace ftxui { /// │Click to quit│ /// └─────────────┘ /// ``` -Component Button(const std::wstring* label, std::function on_click) { +Component Button(ConstStringRef label, std::function on_click) { return Make(label, on_click); } @@ -40,8 +40,7 @@ ButtonBase* ButtonBase::From(Component component) { return static_cast(component.get()); } -ButtonBase::ButtonBase(const std::wstring* label, - std::function on_click) +ButtonBase::ButtonBase(ConstStringRef label, std::function on_click) : label_(label), on_click_(on_click) {} Element ButtonBase::Render() { diff --git a/src/ftxui/component/checkbox.cpp b/src/ftxui/component/checkbox.cpp index 011293f..d440bda 100644 --- a/src/ftxui/component/checkbox.cpp +++ b/src/ftxui/component/checkbox.cpp @@ -30,7 +30,7 @@ namespace ftxui { /// ```bash /// ☐ Make a sandwitch /// ``` -Component Checkbox(const std::wstring* label, bool* checked) { +Component Checkbox(ConstStringRef label, bool* checked) { return Make(label, checked); } @@ -39,7 +39,7 @@ CheckboxBase* From(Component component) { return static_cast(component.get()); } -CheckboxBase::CheckboxBase(const std::wstring* label, bool* state) +CheckboxBase::CheckboxBase(ConstStringRef label, bool* state) : label_(label), state_(state) {} Element CheckboxBase::Render() { diff --git a/src/ftxui/component/container.cpp b/src/ftxui/component/container.cpp index 3dc9fdf..a9d372b 100644 --- a/src/ftxui/component/container.cpp +++ b/src/ftxui/component/container.cpp @@ -15,7 +15,7 @@ namespace Container { /// vertically using up/down arrow key or 'j'/'k' keys. /// @param children the list of components. /// @ingroup component -/// @see ContainerBase +/// @see ContainerBase /// /// ### Example /// @@ -35,7 +35,7 @@ Component Vertical(Components children) { /// horizontally using left/right arrow key or 'h'/'l' keys. /// @param children the list of components. /// @ingroup component -/// @see ContainerBase +/// @see ContainerBase /// /// ### Example /// @@ -57,7 +57,7 @@ Component Horizontal(Components children) { /// @param selector The index of the drawn children. /// @param children the list of components. /// @ingroup component -/// @see ContainerBase +/// @see ContainerBase /// /// ### Example /// @@ -83,7 +83,7 @@ Component ContainerBase::Vertical() { // static Component ContainerBase::Vertical(Components children) { - auto container = std::make_shared(); + auto container = std::make_shared(); container->event_handler_ = &ContainerBase::VerticalEvent; container->render_handler_ = &ContainerBase::VerticalRender; for (Component& child : children) @@ -98,7 +98,7 @@ Component ContainerBase::Horizontal() { // static Component ContainerBase::Horizontal(Components children) { - auto container = std::make_shared(); + auto container = std::make_shared(); container->event_handler_ = &ContainerBase::HorizontalEvent; container->render_handler_ = &ContainerBase::HorizontalRender; for (Component& child : children) @@ -113,7 +113,7 @@ Component ContainerBase::Tab(int* selector) { // static Component ContainerBase::Tab(int* selector, Components children) { - auto container = std::make_shared(); + auto container = std::make_shared(); container->selector_ = selector; container->event_handler_ = &ContainerBase::TabEvent; container->render_handler_ = &ContainerBase::TabRender; diff --git a/src/ftxui/component/container_test.cpp b/src/ftxui/component/container_test.cpp index c4f4571..13cc19f 100644 --- a/src/ftxui/component/container_test.cpp +++ b/src/ftxui/component/container_test.cpp @@ -9,7 +9,7 @@ using namespace ftxui; TEST(ContainerTest, HorizontalEvent) { - auto container = Container::Horizontal(); + auto container = ContainerBase::Horizontal(); Component c0, c1, c2; container->Add(c0); container->Add(c1); @@ -80,7 +80,7 @@ TEST(ContainerTest, HorizontalEvent) { } TEST(ContainerTest, VerticalEvent) { - auto container = Container::Vertical(); + auto container = ContainerBase::Vertical(); Component c0, c1, c2; container->Add(c0); container->Add(c1); @@ -151,7 +151,7 @@ TEST(ContainerTest, VerticalEvent) { } TEST(ContainerTest, SetActiveChild) { - auto container = Container::Horizontal(); + auto container = ContainerBase::Horizontal(); Component c0, c1, c2; container->Add(c0); container->Add(c1); @@ -203,16 +203,16 @@ TEST(ContainerTest, SetActiveChild) { } TEST(ContainerTest, TakeFocus) { - auto c = Container::Horizontal(); - auto c1 = Container::Vertical(); - auto c2 = Container::Vertical(); - auto c3 = Container::Vertical(); - auto c11 = Container::Horizontal(); - auto c12 = Container::Horizontal(); - auto c13 = Container::Horizontal(); - auto c21 = Container::Horizontal(); - auto c22 = Container::Horizontal(); - auto c23 = Container::Horizontal(); + auto c = ContainerBase::Horizontal(); + auto c1 = ContainerBase::Vertical(); + auto c2 = ContainerBase::Vertical(); + auto c3 = ContainerBase::Vertical(); + auto c11 = ContainerBase::Horizontal(); + auto c12 = ContainerBase::Horizontal(); + auto c13 = ContainerBase::Horizontal(); + auto c21 = ContainerBase::Horizontal(); + auto c22 = ContainerBase::Horizontal(); + auto c23 = ContainerBase::Horizontal(); c->Add(c1); c->Add(c2); diff --git a/src/ftxui/component/input.cpp b/src/ftxui/component/input.cpp index 35a162f..3ecaa3a 100644 --- a/src/ftxui/component/input.cpp +++ b/src/ftxui/component/input.cpp @@ -1,5 +1,6 @@ #include // for max, min #include // for shared_ptr +#include // for wstring, allocator, basic_string #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 @@ -30,7 +31,7 @@ namespace ftxui { /// ```bash /// placeholder /// ``` -Component Input(std::wstring* content, const std::wstring* placeholder) { +Component Input(StringRef content, ConstStringRef placeholder) { return Make(content, placeholder); } @@ -39,7 +40,7 @@ InputBase* InputBase::From(Component component) { return static_cast(component.get()); } -InputBase::InputBase(std::wstring* content, const std::wstring* placeholder) +InputBase::InputBase(StringRef content, ConstStringRef placeholder) : content_(content), placeholder_(placeholder) {} // Component implementation. diff --git a/src/ftxui/component/input_test.cpp b/src/ftxui/component/input_test.cpp index c1d1b12..be98285 100644 --- a/src/ftxui/component/input_test.cpp +++ b/src/ftxui/component/input_test.cpp @@ -1,11 +1,12 @@ -#include // for Message -#include // for TestPartResult +#include // for Message +#include // for TestPartResult, SuiteApiResolver, TestFactoryImpl #include // for __shared_ptr_access +#include // for wstring, allocator #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/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; diff --git a/src/ftxui/component/renderer.cpp b/src/ftxui/component/renderer.cpp index 95fdd9a..0125b80 100644 --- a/src/ftxui/component/renderer.cpp +++ b/src/ftxui/component/renderer.cpp @@ -1,7 +1,8 @@ #include // for function -#include // for shared_ptr +#include // for __shared_ptr_access +#include // 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/dom/elements.hpp" // for Element diff --git a/src/ftxui/component/slider.cpp b/src/ftxui/component/slider.cpp index 8dbd538..0ec1091 100644 --- a/src/ftxui/component/slider.cpp +++ b/src/ftxui/component/slider.cpp @@ -1,4 +1,4 @@ -#include // for allocator, wstring +#include // for allocator #include // for move #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/screen/box.hpp" // for Box #include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::GrayLight +#include "ftxui/screen/string.hpp" // for StringRef namespace ftxui { template class SliderBase : public ComponentBase { 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), value_(value), min_(min), @@ -28,7 +29,7 @@ class SliderBase : public ComponentBase { Focused() ? color(Color::GrayLight) : color(Color::GrayDark); float percent = float(*value_ - min_) / float(max_ - min_); return hbox({ - text(label_) | dim | vcenter, + text(*label_) | dim | vcenter, hbox({ text(L"["), gauge(percent) | underlined | xflex | reflect(gauge_box_), @@ -84,7 +85,7 @@ class SliderBase : public ComponentBase { } private: - std::wstring label_; + StringRef label_; T* value_; T min_; T max_; @@ -117,17 +118,17 @@ class SliderBase : public ComponentBase { /// Value:[██████████████████████████ ] /// ``` template -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>(std::move(label), value, min, max, increment); } -template Component Slider(std::wstring label, +template Component Slider(StringRef label, int* value, int min, int max, int increment); -template Component Slider(std::wstring label, +template Component Slider(StringRef label, float* value, float min, float max, diff --git a/src/ftxui/screen/string.cpp b/src/ftxui/screen/string.cpp index d0ddcbb..7f13b6b 100644 --- a/src/ftxui/screen/string.cpp +++ b/src/ftxui/screen/string.cpp @@ -25,6 +25,30 @@ std::wstring to_wstring(const std::string& s) { #pragma warning(pop) #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 // Copyright 2020 Arthur Sonzogni. All rights reserved.