mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Add string view overloads (#1154)
Some checks failed
Build / Bazel, cl, windows-latest (push) Has been cancelled
Build / Bazel, clang++, macos-latest (push) Has been cancelled
Build / Bazel, clang++, ubuntu-latest (push) Has been cancelled
Build / Bazel, g++, macos-latest (push) Has been cancelled
Build / Bazel, g++, ubuntu-latest (push) Has been cancelled
Build / CMake, cl, windows-latest (push) Has been cancelled
Build / CMake, gcc, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, macos-latest (push) Has been cancelled
Build / Test modules (llvm, ubuntu-latest) (push) Has been cancelled
Documentation / documentation (push) Has been cancelled
Some checks failed
Build / Bazel, cl, windows-latest (push) Has been cancelled
Build / Bazel, clang++, macos-latest (push) Has been cancelled
Build / Bazel, clang++, ubuntu-latest (push) Has been cancelled
Build / Bazel, g++, macos-latest (push) Has been cancelled
Build / Bazel, g++, ubuntu-latest (push) Has been cancelled
Build / CMake, cl, windows-latest (push) Has been cancelled
Build / CMake, gcc, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, ubuntu-latest (push) Has been cancelled
Build / CMake, llvm, macos-latest (push) Has been cancelled
Build / Test modules (llvm, ubuntu-latest) (push) Has been cancelled
Documentation / documentation (push) Has been cancelled
This is better ergonomic, as `std::string_view` is lightweight and accept more conversion than `const std::string&`. Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <ftxui/component/mouse.hpp> // for Mouse
|
||||
#include <string> // for string, operator==
|
||||
#include <string_view>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
@@ -28,13 +29,13 @@ class ComponentBase;
|
||||
/// @ingroup component
|
||||
struct Event {
|
||||
// --- Constructor section ---------------------------------------------------
|
||||
static Event Character(std::string);
|
||||
static Event Character(std::string_view);
|
||||
static Event Character(char);
|
||||
static Event Character(wchar_t);
|
||||
static Event Special(std::string);
|
||||
static Event Mouse(std::string, Mouse mouse);
|
||||
static Event CursorPosition(std::string, int x, int y); // Internal
|
||||
static Event CursorShape(std::string, int shape); // Internal
|
||||
static Event Special(std::string_view);
|
||||
static Event Mouse(std::string_view, Mouse mouse);
|
||||
static Event CursorPosition(std::string_view, int x, int y); // Internal
|
||||
static Event CursorShape(std::string_view, int shape); // Internal
|
||||
|
||||
// --- Arrow ---
|
||||
static const Event ArrowLeft;
|
||||
|
||||
@@ -106,9 +106,9 @@ struct Canvas {
|
||||
// Draw using character of size 2x4 at position (x,y)
|
||||
// x is considered to be a multiple of 2.
|
||||
// y is considered to be a multiple of 4.
|
||||
void DrawText(int x, int y, const std::string& value);
|
||||
void DrawText(int x, int y, const std::string& value, const Color& color);
|
||||
void DrawText(int x, int y, const std::string& value, const Stylizer& style);
|
||||
void DrawText(int x, int y, std::string_view value);
|
||||
void DrawText(int x, int y, std::string_view value, const Color& color);
|
||||
void DrawText(int x, int y, std::string_view value, const Stylizer& style);
|
||||
|
||||
// Draw using directly pixels or images --------------------------------------
|
||||
// x is considered to be a multiple of 2.
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include <string_view>
|
||||
#include "ftxui/dom/canvas.hpp"
|
||||
#include "ftxui/dom/direction.hpp"
|
||||
#include "ftxui/dom/flexbox_config.hpp"
|
||||
@@ -51,8 +52,8 @@ Elements operator|(Elements, Decorator);
|
||||
Decorator operator|(Decorator, Decorator);
|
||||
|
||||
// --- Widget ---
|
||||
Element text(std::string text);
|
||||
Element vtext(std::string text);
|
||||
Element text(std::string_view text);
|
||||
Element vtext(std::string_view text);
|
||||
Element separator();
|
||||
Element separatorLight();
|
||||
Element separatorDashed();
|
||||
@@ -61,7 +62,7 @@ Element separatorDouble();
|
||||
Element separatorEmpty();
|
||||
Element separatorStyled(BorderStyle);
|
||||
Element separator(Pixel);
|
||||
Element separatorCharacter(std::string);
|
||||
Element separatorCharacter(std::string_view);
|
||||
Element separatorHSelector(float left,
|
||||
float right,
|
||||
Color unselected_color,
|
||||
@@ -89,11 +90,11 @@ Decorator borderStyled(Color);
|
||||
Decorator borderWith(const Pixel&);
|
||||
Element window(Element title, Element content, BorderStyle border = ROUNDED);
|
||||
Element spinner(int charset_index, size_t image_index);
|
||||
Element paragraph(const std::string& text);
|
||||
Element paragraphAlignLeft(const std::string& text);
|
||||
Element paragraphAlignRight(const std::string& text);
|
||||
Element paragraphAlignCenter(const std::string& text);
|
||||
Element paragraphAlignJustify(const std::string& text);
|
||||
Element paragraph(std::string_view text);
|
||||
Element paragraphAlignLeft(std::string_view text);
|
||||
Element paragraphAlignRight(std::string_view text);
|
||||
Element paragraphAlignCenter(std::string_view text);
|
||||
Element paragraphAlignJustify(std::string_view text);
|
||||
Element graph(GraphFunction);
|
||||
Element emptyElement();
|
||||
Element canvas(ConstRef<Canvas>);
|
||||
@@ -120,8 +121,8 @@ Element bgcolor(const LinearGradient&, Element);
|
||||
Decorator focusPosition(int x, int y);
|
||||
Decorator focusPositionRelative(float x, float y);
|
||||
Element automerge(Element child);
|
||||
Decorator hyperlink(std::string link);
|
||||
Element hyperlink(std::string link, Element child);
|
||||
Decorator hyperlink(std::string_view link);
|
||||
Element hyperlink(std::string_view link, Element child);
|
||||
Element selectionStyleReset(Element);
|
||||
Decorator selectionColor(Color foreground);
|
||||
Decorator selectionBackgroundColor(Color foreground);
|
||||
|
||||
@@ -30,7 +30,7 @@ class Selection {
|
||||
Selection SaturateVertical(Box box);
|
||||
bool IsEmpty() const { return empty_; }
|
||||
|
||||
void AddPart(const std::string& part, int y, int left, int right);
|
||||
void AddPart(std::string_view part, int y, int left, int right);
|
||||
std::string GetParts() { return parts_.str(); }
|
||||
|
||||
private:
|
||||
|
||||
@@ -68,7 +68,7 @@ class Screen : public Image {
|
||||
|
||||
// Store an hyperlink in the screen. Return the id of the hyperlink. The id is
|
||||
// used to identify the hyperlink when the user click on it.
|
||||
uint8_t RegisterHyperlink(const std::string& link);
|
||||
uint8_t RegisterHyperlink(std::string_view link);
|
||||
const std::string& Hyperlink(uint8_t id) const;
|
||||
|
||||
using SelectionStyle = std::function<void(Pixel&)>;
|
||||
|
||||
@@ -4,27 +4,32 @@
|
||||
#ifndef FTXUI_SCREEN_STRING_HPP
|
||||
#define FTXUI_SCREEN_STRING_HPP
|
||||
|
||||
#include <string> // for string, wstring, to_string
|
||||
#include <vector> // for vector
|
||||
#include <string> // for string, wstring, to_string
|
||||
#include <string_view> // for string_view
|
||||
#include <vector> // for vector
|
||||
|
||||
namespace ftxui {
|
||||
std::string to_string(const std::wstring& s);
|
||||
std::wstring to_wstring(const std::string& s);
|
||||
std::string to_string(std::wstring_view s);
|
||||
std::wstring to_wstring(std::string_view s);
|
||||
|
||||
template <typename T>
|
||||
std::wstring to_wstring(T s) {
|
||||
return to_wstring(std::to_string(s));
|
||||
return to_wstring(std::string_view(std::to_string(s)));
|
||||
}
|
||||
template <>
|
||||
inline std::wstring to_wstring(const char* s) {
|
||||
return to_wstring(std::string_view(s));
|
||||
}
|
||||
|
||||
int string_width(const std::string&);
|
||||
int string_width(std::string_view);
|
||||
|
||||
// Split the string into a its glyphs. An empty one is inserted ater fullwidth
|
||||
// ones.
|
||||
std::vector<std::string> Utf8ToGlyphs(const std::string& input);
|
||||
std::vector<std::string> Utf8ToGlyphs(std::string_view input);
|
||||
|
||||
// Map every cells drawn by |input| to their corresponding Glyphs. Half-size
|
||||
// Glyphs takes one cell, full-size Glyphs take two cells.
|
||||
std::vector<int> CellToGlyphIndex(const std::string& input);
|
||||
std::vector<int> CellToGlyphIndex(std::string_view input);
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <ftxui/screen/string.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
@@ -125,13 +126,15 @@ class ConstStringListRef {
|
||||
Adapter& operator=(Adapter&&) = default;
|
||||
virtual ~Adapter() = default;
|
||||
virtual size_t size() const = 0;
|
||||
virtual std::string operator[](size_t i) const = 0;
|
||||
virtual std::string_view operator[](size_t i) const = 0;
|
||||
};
|
||||
using Variant = std::variant<const std::vector<std::string>, //
|
||||
const std::vector<std::string>*, //
|
||||
const std::vector<std::wstring>*, //
|
||||
Adapter*, //
|
||||
std::unique_ptr<Adapter> //
|
||||
using Variant = std::variant<const std::vector<std::string>, //
|
||||
const std::vector<std::string>*, //
|
||||
const std::vector<std::string_view>, //
|
||||
const std::vector<std::string_view>*, //
|
||||
const std::vector<std::wstring>*, //
|
||||
Adapter*, //
|
||||
std::unique_ptr<Adapter> //
|
||||
>;
|
||||
|
||||
ConstStringListRef() = default;
|
||||
@@ -149,6 +152,14 @@ class ConstStringListRef {
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
}
|
||||
ConstStringListRef(std::vector<std::string_view> value) // NOLINT
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
}
|
||||
ConstStringListRef(const std::vector<std::string_view>* value) // NOLINT
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
}
|
||||
ConstStringListRef(const std::vector<std::wstring>* value) // NOLINT
|
||||
{
|
||||
variant_ = std::make_shared<Variant>(value);
|
||||
@@ -169,7 +180,62 @@ class ConstStringListRef {
|
||||
}
|
||||
|
||||
std::string operator[](size_t i) const {
|
||||
return variant_ ? std::visit(IndexedGetter(i), *variant_) : "";
|
||||
if (!variant_) {
|
||||
return "";
|
||||
}
|
||||
auto& v = *variant_;
|
||||
if (std::holds_alternative<const std::vector<std::string>>(v)) {
|
||||
return std::get<const std::vector<std::string>>(v)[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string>*>(v)) {
|
||||
return (*std::get<const std::vector<std::string>*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>>(v)) {
|
||||
return std::string(std::get<const std::vector<std::string_view>>(v)[i]);
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>*>(v)) {
|
||||
return std::string(
|
||||
(*std::get<const std::vector<std::string_view>*>(v))[i]);
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::wstring>*>(v)) {
|
||||
return to_string((*std::get<const std::vector<std::wstring>*>(v))[i]);
|
||||
}
|
||||
if (std::holds_alternative<Adapter*>(v)) {
|
||||
return std::string((*std::get<Adapter*>(v))[i]);
|
||||
}
|
||||
if (std::holds_alternative<std::unique_ptr<Adapter>>(v)) {
|
||||
return std::string((*std::get<std::unique_ptr<Adapter>>(v))[i]);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string_view at(size_t i) const {
|
||||
if (!variant_) {
|
||||
return "";
|
||||
}
|
||||
auto& v = *variant_;
|
||||
if (std::holds_alternative<const std::vector<std::string>>(v)) {
|
||||
return std::get<const std::vector<std::string>>(v)[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string>*>(v)) {
|
||||
return (*std::get<const std::vector<std::string>*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>>(v)) {
|
||||
return std::get<const std::vector<std::string_view>>(v)[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::string_view>*>(v)) {
|
||||
return (*std::get<const std::vector<std::string_view>*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<const std::vector<std::wstring>*>(v)) {
|
||||
return {};
|
||||
}
|
||||
if (std::holds_alternative<Adapter*>(v)) {
|
||||
return (*std::get<Adapter*>(v))[i];
|
||||
}
|
||||
if (std::holds_alternative<std::unique_ptr<Adapter>>(v)) {
|
||||
return (*std::get<std::unique_ptr<Adapter>>(v))[i];
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -180,6 +246,12 @@ class ConstStringListRef {
|
||||
size_t operator()(const std::vector<std::string>* v) const {
|
||||
return v->size();
|
||||
}
|
||||
size_t operator()(const std::vector<std::string_view>& v) const {
|
||||
return v.size();
|
||||
}
|
||||
size_t operator()(const std::vector<std::string_view>* v) const {
|
||||
return v->size();
|
||||
}
|
||||
size_t operator()(const std::vector<std::wstring>* v) const {
|
||||
return v->size();
|
||||
}
|
||||
@@ -189,25 +261,6 @@ class ConstStringListRef {
|
||||
}
|
||||
};
|
||||
|
||||
struct IndexedGetter {
|
||||
IndexedGetter(size_t index) // NOLINT
|
||||
: index_(index) {}
|
||||
size_t index_;
|
||||
std::string operator()(const std::vector<std::string>& v) const {
|
||||
return v[index_];
|
||||
}
|
||||
std::string operator()(const std::vector<std::string>* v) const {
|
||||
return (*v)[index_];
|
||||
}
|
||||
std::string operator()(const std::vector<std::wstring>* v) const {
|
||||
return to_string((*v)[index_]);
|
||||
}
|
||||
std::string operator()(const Adapter* v) const { return (*v)[index_]; }
|
||||
std::string operator()(const std::unique_ptr<Adapter>& v) const {
|
||||
return (*v)[index_];
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<Variant> variant_;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user