Prefer std::string over std::wstring. (#179)

In the past, FTXUI switched from std::string to std::wstring to support
fullwidth characters. The reasons was that fullwidth characters can be
stored inside a single wchar_t.

Then FTXUI added support for combining characters. A single glygh
doesn't even fit a wchar_t. Instead, a glyph can be arbitrary large.

The usage of wstring doesn't really fit the new model and have several
drawbacks:
1. It doesn't simplify the implementation of FTXUI, because of combining
   characters.
2. It reduces drawing performance by 2x.
3. It increase Screen's memory allocation by 2x.

This patch converts FTXUI to use std::string internally. It now exposes
std::string based API. The std::wstring API remains, but is now
deprecated.

Tests and examples haven't been update to show the breakage is limited.
They will be updated in a second set of patches.

Bug: https://github.com/ArthurSonzogni/FTXUI/issues/153
Co-authored-by: Tushar Maheshwari <tushar27192@gmail.com>
This commit is contained in:
Arthur Sonzogni
2021-08-08 23:25:20 +02:00
committed by GitHub
parent 1ff894f6f5
commit 3b4ab618a3
84 changed files with 1234 additions and 996 deletions

View File

@@ -6,7 +6,8 @@
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for ButtonOption
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for separator, gauge, Element, operator|, vbox, border
using namespace ftxui;

View File

@@ -6,7 +6,8 @@
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for ButtonOption
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, separator, Element, operator|, vbox, border
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for separator, Element, operator|, vbox, border
using namespace ftxui;

View File

@@ -4,10 +4,12 @@
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Input, Menu, Radiobox, Toggle
#include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Menu, Radiobox, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/deprecated.hpp" // for Input
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, Element, operator|, size, xflex, text, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, xflex, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
using namespace ftxui;

View File

@@ -9,12 +9,14 @@
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Renderer, Horizontal, Vertical, Input, Menu, Radiobox, Tab, Toggle
#include "ftxui/component/component.hpp" // for Checkbox, Renderer, Horizontal, Vertical, Menu, Radiobox, Tab, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for InputOption
#include "ftxui/component/deprecated.hpp" // for Input
#include "ftxui/component/event.hpp" // for Event, Event::Custom
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, operator|, color, bgcolor, filler, Element, 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/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for operator|, color, bgcolor, filler, Element, 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;

View File

@@ -1,12 +1,14 @@
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for char_traits, operator+, wstring, basic_string
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for InputOption
#include "ftxui/component/deprecated.hpp" // for Input
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, hbox, separator, Element, operator|, vbox, border
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for hbox, separator, Element, operator|, vbox, border
int main(int argc, const char* argv[]) {
using namespace ftxui;

View File

@@ -8,7 +8,8 @@
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for MenuOption
#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/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for separator, bold, hcenter, vbox, hbox, gauge, Element, operator|, border
#include "ftxui/screen/string.hpp" // for to_wstring
int main(int argc, const char* argv[]) {

View File

@@ -1,13 +1,14 @@
#include <stdlib.h> // for EXIT_SUCCESS
#include <memory> // for __shared_ptr_access
#include <string> // for wstring, allocator, operator+, basic_string, char_traits
#include <vector> // for vector, __alloc_traits<>::value_type
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for wstring, operator+, basic_string, char_traits
#include <vector> // for vector, __alloc_traits<>::value_type
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu, Renderer, Horizontal, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, Element, operator|, window, flex, vbox
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for Element, operator|, window, flex, vbox
#include "ftxui/screen/string.hpp" // for to_wstring
using namespace ftxui;

View File

@@ -1,12 +1,13 @@
#include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for wstring, operator+, basic_string, char_traits
#include <memory> // for allocator, shared_ptr, __shared_ptr_access
#include <string> // for wstring, basic_string, char_traits, operator+
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#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
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for operator|, Element, filler, hbox, separator, center, vbox, bold, border, clear_under, dbox, size, GREATER_THAN, HEIGHT
int main(int argc, const char* argv[]) {
using namespace ftxui;

View File

@@ -4,8 +4,8 @@
#include <stddef.h> // for size_t
#include <algorithm> // for max
#include <memory> // for shared_ptr
#include <string> // for allocator, char_traits, operator+, wstring, basic_string, to_wstring, string
#include <memory> // for allocator, shared_ptr
#include <string> // for char_traits, operator+, string, basic_string, to_string
#include <utility> // for move
#include <vector> // for vector
@@ -18,56 +18,56 @@
using namespace ftxui;
std::wstring Stringify(Event event) {
std::wstring out;
std::string Stringify(Event event) {
std::string out;
for (auto& it : event.input())
out += L" " + std::to_wstring((unsigned int)it);
out += " " + std::to_string((unsigned int)it);
out = L"(" + out + L" ) -> ";
out = "(" + out + " ) -> ";
if (event.is_character()) {
out += std::wstring(L"character(") + event.character() + L")";
out += "character(" + event.character() + ")";
} else if (event.is_mouse()) {
out += L"mouse";
out += "mouse";
switch (event.mouse().button) {
case Mouse::Left:
out += L"_left";
out += "_left";
break;
case Mouse::Middle:
out += L"_middle";
out += "_middle";
break;
case Mouse::Right:
out += L"_right";
out += "_right";
break;
case Mouse::None:
out += L"_none";
out += "_none";
break;
case Mouse::WheelUp:
out += L"_wheel_up";
out += "_wheel_up";
break;
case Mouse::WheelDown:
out += L"_wheel_down";
out += "_wheel_down";
break;
}
switch (event.mouse().motion) {
case Mouse::Pressed:
out += L"_pressed";
out += "_pressed";
break;
case Mouse::Released:
out += L"_released";
out += "_released";
break;
}
if (event.mouse().control)
out += L"_control";
out += "_control";
if (event.mouse().shift)
out += L"_shift";
out += "_shift";
if (event.mouse().meta)
out += L"_meta";
out += "_meta";
out += L"(" + //
std::to_wstring(event.mouse().x) + L"," +
std::to_wstring(event.mouse().y) + L")";
out += "(" + //
std::to_string(event.mouse().x) + "," +
std::to_string(event.mouse().y) + ")";
} else {
out += L"(special)";
out += "(special)";
}
return out;
}
@@ -81,7 +81,7 @@ int main(int argc, const char* argv[]) {
Elements children;
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i)
children.push_back(text(Stringify(keys[i])));
return window(text(L"keys"), vbox(std::move(children)));
return window(text("keys"), vbox(std::move(children)));
});
component = CatchEvent(component, [&](Event event) {

View File

@@ -4,7 +4,8 @@
#include "ftxui/component/component.hpp" // for Renderer, Button, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for operator|, Element, text, bold, border, center, color
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for operator|, Element, bold, border, center, color
#include "ftxui/screen/color.hpp" // for Color, Color::Red
int main(int argc, const char* argv[]) {

View File

@@ -4,7 +4,8 @@
#include "ftxui/component/component.hpp" // for Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, text, center, border
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for Element, operator|, center, border
using namespace ftxui;

View File

@@ -1,11 +1,12 @@
#include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for operator+, to_wstring, char_traits
#include <memory> // for allocator, shared_ptr, __shared_ptr_access
#include <string> // for char_traits, operator+, to_wstring
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#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/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, vbox, xflex, bgcolor, hbox, GREATER_THAN, WIDTH, border, HEIGHT, LESS_THAN
#include "ftxui/screen/color.hpp" // for Color
using namespace ftxui;

View File

@@ -6,7 +6,8 @@
#include "ftxui/component/component.hpp" // for Toggle, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, hbox, vbox, Element
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for hbox, vbox, Element
using namespace ftxui;