mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-16 08:04:21 +08:00
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:
@@ -9,7 +9,7 @@
|
||||
#include "ftxui/component/component_base.hpp" // for Component, Components
|
||||
#include "ftxui/component/component_options.hpp" // for ButtonOption, CheckboxOption, InputOption, MenuOption, RadioboxOption, ToggleOption
|
||||
#include "ftxui/dom/elements.hpp" // for Element
|
||||
#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef, StringRef
|
||||
#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef, ConstStringListRef, StringRef
|
||||
|
||||
namespace ftxui {
|
||||
struct ButtonOption;
|
||||
@@ -34,17 +34,17 @@ Component Checkbox(ConstStringRef label,
|
||||
Component Input(StringRef content,
|
||||
ConstStringRef placeholder,
|
||||
Ref<InputOption> option = {});
|
||||
Component Menu(const std::vector<std::wstring>* entries,
|
||||
Component Menu(ConstStringListRef entries,
|
||||
int* selected_,
|
||||
Ref<MenuOption> = {});
|
||||
Component Radiobox(const std::vector<std::wstring>* entries,
|
||||
Component Radiobox(ConstStringListRef entries,
|
||||
int* selected_,
|
||||
Ref<RadioboxOption> option = {});
|
||||
Component Toggle(const std::vector<std::wstring>* entries,
|
||||
Component Toggle(ConstStringListRef entries,
|
||||
int* selected,
|
||||
Ref<ToggleOption> option = {});
|
||||
template <class T> // T = {int, float, long}
|
||||
Component Slider(StringRef label, T* value, T min, T max, T increment);
|
||||
Component Slider(ConstStringRef label, T* value, T min, T max, T increment);
|
||||
Component ResizableSplitLeft(Component main, Component back, int* main_size);
|
||||
Component ResizableSplitRight(Component main, Component back, int* main_size);
|
||||
Component ResizableSplitTop(Component main, Component back, int* main_size);
|
||||
@@ -65,6 +65,9 @@ Component Tab(Components children, int* selector);
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
// Include component using the old deprecated wstring.
|
||||
#include "ftxui/component/deprecated.hpp"
|
||||
|
||||
#endif /* end of include guard: FTXUI_COMPONENT_HPP */
|
||||
|
||||
// Copyright 2021 Arthur Sonzogni. All rights reserved.
|
||||
|
@@ -33,10 +33,10 @@ struct ButtonOption {
|
||||
/// @brief Option for the Checkbox component.
|
||||
/// @ingroup component
|
||||
struct CheckboxOption {
|
||||
std::wstring style_checked = L"▣ "; ///< Prefix for a "checked" state.
|
||||
std::wstring style_unchecked = L"☐ "; ///< Prefix for a "unchecked" state.
|
||||
Decorator style_focused = inverted; ///< Decorator used when focused.
|
||||
Decorator style_unfocused = nothing; ///< Decorator used when unfocused.
|
||||
std::string style_checked = "▣ "; ///< Prefix for a "checked" state.
|
||||
std::string style_unchecked = "☐ "; ///< Prefix for a "unchecked" state.
|
||||
Decorator style_focused = inverted; ///< Decorator used when focused.
|
||||
Decorator style_unfocused = nothing; ///< Decorator used when unfocused.
|
||||
|
||||
/// Called when the user change the state.
|
||||
std::function<void()> on_change = []() {};
|
||||
@@ -59,10 +59,10 @@ struct InputOption {
|
||||
/// @brief Option for the Radiobox component.
|
||||
/// @ingroup component
|
||||
struct RadioboxOption {
|
||||
std::wstring style_checked = L"◉ "; ///< Prefix for a "checked" state.
|
||||
std::wstring style_unchecked = L"○ "; ///< Prefix for a "unchecked" state.
|
||||
Decorator style_focused = inverted; ///< Decorator used when focused.
|
||||
Decorator style_unfocused = nothing; ///< Decorator used when unfocused.
|
||||
std::string style_checked = "◉ "; ///< Prefix for a "checked" state.
|
||||
std::string style_unchecked = "○ "; ///< Prefix for a "unchecked" state.
|
||||
Decorator style_focused = inverted; ///< Decorator used when focused.
|
||||
Decorator style_unfocused = nothing; ///< Decorator used when unfocused.
|
||||
|
||||
/// Called when the selected entry changes.
|
||||
std::function<void()> on_change = []() {};
|
||||
|
17
include/ftxui/component/deprecated.hpp
Normal file
17
include/ftxui/component/deprecated.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef FTXUI_COMPONENT_DEPRECATED_HPP
|
||||
#define FTXUI_COMPONENT_DEPRECATED_HPP
|
||||
|
||||
#include "ftxui/component/component.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Component Input(WideStringRef content,
|
||||
ConstStringRef placeholder,
|
||||
Ref<InputOption> option = {});
|
||||
} // namespace ftxui
|
||||
|
||||
#endif /* FTXUI_COMPONENT_DEPRECATED_HPP */
|
||||
|
||||
// Copyright 2021 Arthur Sonzogni. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
@@ -24,10 +24,9 @@ class ComponentBase;
|
||||
/// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
|
||||
struct Event {
|
||||
// --- Constructor section ---------------------------------------------------
|
||||
static Event Character(std::string);
|
||||
static Event Character(char);
|
||||
static Event Character(wchar_t);
|
||||
|
||||
static Event Character(std::string);
|
||||
static Event Special(std::string);
|
||||
static Event Mouse(std::string, Mouse mouse);
|
||||
static Event CursorReporting(std::string, int x, int y);
|
||||
@@ -58,7 +57,7 @@ struct Event {
|
||||
|
||||
//--- Method section ---------------------------------------------------------
|
||||
bool is_character() const { return type_ == Type::Character; }
|
||||
wchar_t character() const { return character_; }
|
||||
std::string character() const { return input_; }
|
||||
|
||||
bool is_mouse() const { return type_ == Type::Mouse; }
|
||||
struct Mouse& mouse() {
|
||||
@@ -92,7 +91,6 @@ struct Event {
|
||||
};
|
||||
|
||||
union {
|
||||
wchar_t character_ = U'?';
|
||||
struct Mouse mouse_;
|
||||
struct Cursor cursor_;
|
||||
};
|
||||
|
16
include/ftxui/dom/deprecated.hpp
Normal file
16
include/ftxui/dom/deprecated.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef FTXUI_DOM_DEPRECRATED_HPP
|
||||
#define FTXUI_DOM_DEPRECRATED_HPP
|
||||
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
Element text(std::wstring text);
|
||||
Element vtext(std::wstring text);
|
||||
Elements paragraph(std::wstring text);
|
||||
} // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_DOM_DEPRECRATED_HPP */
|
||||
|
||||
// Copyright 2021 Arthur Sonzogni. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
@@ -26,8 +26,8 @@ Elements operator|(Elements, Decorator);
|
||||
Decorator operator|(Decorator, Decorator);
|
||||
|
||||
// --- Widget ---
|
||||
Element text(std::wstring text);
|
||||
Element vtext(std::wstring text);
|
||||
Element text(std::string text);
|
||||
Element vtext(std::string text);
|
||||
Element separator(void);
|
||||
Element separator(Pixel);
|
||||
Element gauge(float ratio);
|
||||
@@ -35,7 +35,7 @@ Element border(Element);
|
||||
Decorator borderWith(Pixel);
|
||||
Element window(Element title, Element content);
|
||||
Element spinner(int charset_index, size_t image_index);
|
||||
Elements paragraph(std::wstring text); // Use inside hflow(). Split by space.
|
||||
Elements paragraph(std::string text); // Use inside hflow(). Split by space.
|
||||
Element graph(GraphFunction);
|
||||
|
||||
// -- Decorator ---
|
||||
@@ -112,6 +112,8 @@ Dimensions Fit(Element&);
|
||||
// Make container able to take any number of children as input.
|
||||
#include "ftxui/dom/take_any_args.hpp"
|
||||
|
||||
// Include old definitions using wstring.
|
||||
#include "ftxui/dom/deprecated.hpp"
|
||||
#endif /* end of include guard: FTXUI_DOM_ELEMENTS_HPP */
|
||||
|
||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
||||
|
@@ -303,7 +303,7 @@ class Color {
|
||||
bool operator==(const Color& rhs) const;
|
||||
bool operator!=(const Color& rhs) const;
|
||||
|
||||
std::wstring Print(bool is_background_color) const;
|
||||
std::string Print(bool is_background_color) const;
|
||||
|
||||
private:
|
||||
enum class ColorType : uint8_t {
|
||||
|
@@ -2,7 +2,7 @@
|
||||
#define FTXUI_SCREEN_SCREEN
|
||||
|
||||
#include <memory>
|
||||
#include <string> // for allocator, wstring, string, basic_string
|
||||
#include <string> // for string, allocator, basic_string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/screen/box.hpp" // for Box
|
||||
@@ -16,8 +16,7 @@ namespace ftxui {
|
||||
struct Pixel {
|
||||
// The graphemes stored into the pixel. To support combining characters,
|
||||
// like: a⃦, this can potentially contains multiple codepoitns.
|
||||
// Required: character.size() >= 1;
|
||||
std::wstring character = L" ";
|
||||
std::string character = " ";
|
||||
|
||||
// Colors:
|
||||
Color background_color = Color::Default;
|
||||
@@ -55,7 +54,7 @@ class Screen {
|
||||
static Screen Create(Dimensions width, Dimensions height);
|
||||
|
||||
// Node write into the screen using Screen::at.
|
||||
wchar_t& at(int x, int y);
|
||||
std::string& at(int x, int y);
|
||||
Pixel& PixelAt(int x, int y);
|
||||
|
||||
// Convert the screen into a printable string in the terminal.
|
||||
|
@@ -2,6 +2,7 @@
|
||||
#define FTXUI_SCREEN_STRING_HPP
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ftxui {
|
||||
std::string to_string(const std::wstring& s);
|
||||
@@ -12,12 +13,10 @@ std::wstring to_wstring(T s) {
|
||||
return to_wstring(std::to_string(s));
|
||||
}
|
||||
|
||||
int wchar_width(char32_t);
|
||||
int wchar_width(wchar_t);
|
||||
int wchar_width_cjk(char32_t);
|
||||
int wchar_width_cjk(wchar_t);
|
||||
int wstring_width(const std::wstring&);
|
||||
int wstring_width_cjk(const std::wstring&);
|
||||
int string_width(const std::string&);
|
||||
std::vector<std::string> Utf8ToGlyphs(const std::string& input);
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
|
@@ -42,11 +42,27 @@ class Ref {
|
||||
/// class convert multiple mutable string toward a shared representation.
|
||||
class StringRef {
|
||||
public:
|
||||
StringRef(std::wstring* ref) : address_(ref) {}
|
||||
StringRef(std::wstring ref) : owned_(std::move(ref)) {}
|
||||
StringRef(const wchar_t* ref) : StringRef(std::wstring(ref)) {}
|
||||
StringRef(const char* ref) : StringRef(to_wstring(std::string(ref))) {}
|
||||
StringRef(std::string ref) : StringRef(to_wstring(std::move(ref))) {}
|
||||
StringRef(std::string* ref) : address_(ref) {}
|
||||
StringRef(std::string ref) : owned_(std::move(ref)) {}
|
||||
StringRef(const wchar_t* ref) : StringRef(to_string(std::wstring(ref))) {}
|
||||
StringRef(const char* ref) : StringRef(std::string(ref)) {}
|
||||
std::string& operator*() { return address_ ? *address_ : owned_; }
|
||||
std::string* operator->() { return address_ ? address_ : &owned_; }
|
||||
|
||||
private:
|
||||
std::string owned_;
|
||||
std::string* address_ = nullptr;
|
||||
};
|
||||
|
||||
/// @brief An adapter. Own or reference a constant string. For convenience, this
|
||||
/// class convert multiple mutable string toward a shared representation.
|
||||
class WideStringRef {
|
||||
public:
|
||||
WideStringRef(std::wstring* ref) : address_(ref) {}
|
||||
WideStringRef(std::wstring ref) : owned_(std::move(ref)) {}
|
||||
WideStringRef(const wchar_t* ref) : WideStringRef(std::wstring(ref)) {}
|
||||
WideStringRef(const char* ref)
|
||||
: WideStringRef(to_wstring(std::string(ref))) {}
|
||||
std::wstring& operator*() { return address_ ? *address_ : owned_; }
|
||||
std::wstring* operator->() { return address_ ? address_ : &owned_; }
|
||||
|
||||
@@ -59,19 +75,35 @@ class StringRef {
|
||||
/// class convert multiple immutable string toward a shared representation.
|
||||
class ConstStringRef {
|
||||
public:
|
||||
ConstStringRef(const std::wstring* ref) : address_(ref) {}
|
||||
ConstStringRef(std::wstring ref) : owned_(std::move(ref)) {}
|
||||
ConstStringRef(const std::string* ref) : address_(ref) {}
|
||||
ConstStringRef(const std::wstring* ref) : ConstStringRef(to_string(*ref)) {}
|
||||
ConstStringRef(std::string ref) : owned_(std::move(ref)) {}
|
||||
ConstStringRef(std::wstring ref) : ConstStringRef(to_string(ref)) {}
|
||||
ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
|
||||
ConstStringRef(const char* ref)
|
||||
: ConstStringRef(to_wstring(std::string(ref))) {}
|
||||
ConstStringRef(std::string ref)
|
||||
: ConstStringRef(to_wstring(std::move(ref))) {}
|
||||
const std::wstring& operator*() { return address_ ? *address_ : owned_; }
|
||||
const std::wstring* operator->() { return address_ ? address_ : &owned_; }
|
||||
const std::string& operator*() { return address_ ? *address_ : owned_; }
|
||||
const std::string* operator->() { return address_ ? address_ : &owned_; }
|
||||
|
||||
private:
|
||||
const std::wstring owned_;
|
||||
const std::wstring* address_ = nullptr;
|
||||
const std::string owned_;
|
||||
const std::string* address_ = nullptr;
|
||||
};
|
||||
|
||||
/// @brief An adapter. Reference a list of strings.
|
||||
class ConstStringListRef {
|
||||
public:
|
||||
ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
|
||||
ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
|
||||
|
||||
size_t size() const { return ref_ ? ref_->size() : ref_wide_->size(); }
|
||||
std::string operator[](size_t i) const {
|
||||
return ref_ ? (*ref_)[i] : to_string((*ref_wide_)[i]);
|
||||
}
|
||||
|
||||
private:
|
||||
const std::vector<std::string>* ref_ = nullptr;
|
||||
const std::vector<std::wstring>* ref_wide_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
Reference in New Issue
Block a user