mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-16 08:04:21 +08:00
Add Blink. Refactor examples.
This commit is contained in:
@@ -7,6 +7,7 @@ namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
// A component where focus and events are automatically handled for you.
|
||||
// Please use ComponentVertical or ComponentHorizontal.
|
||||
class ComponentDirection : public Component {
|
||||
public:
|
||||
ComponentDirection(Delegate* delegate);
|
||||
|
@@ -15,6 +15,7 @@ using Children = std::vector<Child>;
|
||||
Element vbox(Children);
|
||||
Element hbox(Children);
|
||||
Element flex();
|
||||
Element flex(Element);
|
||||
|
||||
// --- Widget --
|
||||
Element text(std::wstring text);
|
||||
@@ -23,7 +24,7 @@ Element gauge(float ratio);
|
||||
Element frame(Child);
|
||||
Element frame(Child title, Child content);
|
||||
|
||||
// -- Decorator (Style) ---
|
||||
// -- Style ---
|
||||
Element bold(Element);
|
||||
Element dim(Element);
|
||||
Element inverted(Element);
|
||||
@@ -32,11 +33,10 @@ Element blink(Element);
|
||||
Element color(Color, Element);
|
||||
Element bgcolor(Color, Element);
|
||||
|
||||
// --- Decorator ---
|
||||
// --- Util ---
|
||||
Element hcenter(Element);
|
||||
Element vcenter(Element);
|
||||
Element center(Element);
|
||||
Element flex(Element);
|
||||
|
||||
// --- Util ---
|
||||
Element nothing(Element element);
|
||||
|
@@ -17,14 +17,14 @@ class Node {
|
||||
Node(std::vector<std::unique_ptr<Node>> children);
|
||||
virtual ~Node();
|
||||
|
||||
// Step 1: Direction parent <= children.
|
||||
// Compute layout requirement. Tell parent what dimensions this
|
||||
// Step 1: Compute layout requirement. Tell parent what dimensions this
|
||||
// element wants to be.
|
||||
// Propagated from Children to Parents.
|
||||
virtual void ComputeRequirement();
|
||||
Requirement requirement() { return requirement_; }
|
||||
|
||||
// Step 2: Direction parent => children.
|
||||
// Assign this element its final dimensions.
|
||||
// Step 2: Assign this element its final dimensions.
|
||||
// Propagated from Parents to Children.
|
||||
virtual void SetBox(Box box);
|
||||
|
||||
// Step 3: Draw this element.
|
||||
|
@@ -24,6 +24,9 @@ dom::Element Input::Render() {
|
||||
if (!is_focused)
|
||||
return flex(text(content));
|
||||
|
||||
std::wstring sub_content = content;
|
||||
size_t sub_cursor_position = cursor_position;
|
||||
|
||||
std::wstring part_before_cursor = content.substr(0,cursor_position);
|
||||
std::wstring part_at_cursor = cursor_position < (int)content.size()
|
||||
? content.substr(cursor_position, 1)
|
||||
|
27
ftxui/src/ftxui/dom/blink.cpp
Normal file
27
ftxui/src/ftxui/dom/blink.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "ftxui/dom/node_decorator.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
class Blink : public NodeDecorator {
|
||||
public:
|
||||
Blink(Children children) : NodeDecorator(std::move(children)) {}
|
||||
~Blink() override {}
|
||||
|
||||
void Render(Screen& screen) override {
|
||||
Node::Render(screen);
|
||||
for (int y = box_.top; y <= box_.bottom; ++y) {
|
||||
for (int x = box_.left; x <= box_.right; ++x) {
|
||||
screen.PixelAt(x, y).blink = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> blink(Child child) {
|
||||
return std::make_unique<Blink>(unpack(std::move(child)));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
11
ftxui/src/ftxui/dom/util.cpp
Normal file
11
ftxui/src/ftxui/dom/util.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
Element nothing(Element element) {
|
||||
return std::move(element);
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/terminal.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
|
||||
@@ -10,6 +10,35 @@ namespace ftxui {
|
||||
Screen::Screen(size_t dimx, size_t dimy)
|
||||
: dimx_(dimx), dimy_(dimy), pixels_(dimy, std::vector<Pixel>(dimx)) {}
|
||||
|
||||
void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) {
|
||||
if (next.bold != previous.bold)
|
||||
ss << (next.bold ? L"\e[1m" : L"\e[0m");
|
||||
|
||||
if (next.inverted != previous.inverted)
|
||||
ss << (next.inverted ? L"\e[7m" : L"\e[27m");
|
||||
|
||||
if (next.underlined != previous.underlined)
|
||||
ss << (next.underlined ? L"\e[4m" : L"\e[24m");
|
||||
|
||||
if (next.dim != previous.dim)
|
||||
ss << (next.dim ? L"\e[2m" : L"\e[22m");
|
||||
|
||||
if (next.blink != previous.blink)
|
||||
ss << (next.blink ? L"\e[5m" : L"\e[25m");
|
||||
|
||||
if (next.foreground_color != previous.foreground_color) {
|
||||
ss << L"\e[" + to_wstring(std::to_string((uint8_t)next.foreground_color)) +
|
||||
L"m";
|
||||
}
|
||||
if (next.background_color != previous.background_color) {
|
||||
ss << L"\e[" +
|
||||
to_wstring(std::to_string(10 + (uint8_t)next.background_color)) +
|
||||
L"m";
|
||||
}
|
||||
|
||||
previous = next;
|
||||
}
|
||||
|
||||
std::string Screen::ToString() {
|
||||
std::wstringstream ss;
|
||||
|
||||
@@ -17,57 +46,13 @@ std::string Screen::ToString() {
|
||||
|
||||
for (size_t y = 0; y < dimy_; ++y) {
|
||||
for (size_t x = 0; x < dimx_; ++x) {
|
||||
if (pixels_[y][x].bold != previous_pixel.bold) {
|
||||
if (pixels_[y][x].bold) {
|
||||
ss << L"\e[1m";
|
||||
} else {
|
||||
ss << L"\e[0m";
|
||||
}
|
||||
}
|
||||
if (pixels_[y][x].inverted != previous_pixel.inverted) {
|
||||
if (pixels_[y][x].inverted) {
|
||||
ss << L"\e[7m";
|
||||
} else {
|
||||
ss << L"\e[27m";
|
||||
}
|
||||
}
|
||||
if (pixels_[y][x].underlined != previous_pixel.underlined) {
|
||||
if (pixels_[y][x].underlined) {
|
||||
ss << L"\e[4m";
|
||||
} else {
|
||||
ss << L"\e[24m";
|
||||
}
|
||||
}
|
||||
if (pixels_[y][x].dim != previous_pixel.dim) {
|
||||
if (pixels_[y][x].dim) {
|
||||
ss << L"\e[2m";
|
||||
} else {
|
||||
ss << L"\e[22m";
|
||||
}
|
||||
}
|
||||
if (pixels_[y][x].blink != previous_pixel.blink) {
|
||||
if (pixels_[y][x].blink) {
|
||||
ss << L"\e[5m";
|
||||
} else {
|
||||
ss << L"\e[25m";
|
||||
}
|
||||
}
|
||||
if (pixels_[y][x].foreground_color != previous_pixel.foreground_color) {
|
||||
ss << L"\e[" + to_wstring(std::to_string(
|
||||
(uint8_t)pixels_[y][x].foreground_color)) +
|
||||
L"m";
|
||||
}
|
||||
if (pixels_[y][x].background_color != previous_pixel.background_color) {
|
||||
ss << L"\e[" + to_wstring(std::to_string(
|
||||
10 + (uint8_t)pixels_[y][x].background_color)) +
|
||||
L"m";
|
||||
}
|
||||
UpdatePixelStyle(ss, previous_pixel, pixels_[y][x]);
|
||||
ss << pixels_[y][x].character;
|
||||
previous_pixel = pixels_[y][x];
|
||||
}
|
||||
if (y + 1 < dimy_)
|
||||
ss << '\n';
|
||||
}
|
||||
|
||||
return to_string(ss.str());
|
||||
}
|
||||
|
||||
|
@@ -11,6 +11,7 @@ namespace ftxui {
|
||||
namespace {
|
||||
constexpr int ESC = 27;
|
||||
constexpr int WAT = 195;
|
||||
constexpr int WAT2 = 194;
|
||||
constexpr int WATWAIT = 91;
|
||||
|
||||
Event GetEvent() {
|
||||
@@ -32,6 +33,11 @@ namespace {
|
||||
return Event{v1, v2};
|
||||
}
|
||||
|
||||
if (v1 == WAT2) {
|
||||
int v2 = getchar();
|
||||
return Event{v1, v2};
|
||||
}
|
||||
|
||||
return Event{v1};
|
||||
};
|
||||
};
|
||||
|
Reference in New Issue
Block a user