mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-12-16 01:48:56 +08:00
Implement a lot of new features.
This commit deserve to be cut into at least 8 sub commit. Sorry, I acknowledge this is bad... Here are the new features: * dom decorator: bold, dim, underlined, inverted. * component mechanism * components * menu * toogle
This commit is contained in:
59
ftxui/src/ftxui/component/component.cpp
Normal file
59
ftxui/src/ftxui/component/component.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "ftxui/component/component.hpp"
|
||||
#include "ftxui/component/delegate.hpp"
|
||||
#include <assert.h>
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
Component::Component(Delegate* delegate) {
|
||||
delegate_ = delegate;
|
||||
delegate_->Register(this);
|
||||
}
|
||||
|
||||
Component::~Component() {}
|
||||
|
||||
dom::Element Component::Render() {
|
||||
using namespace ftxui::dom;
|
||||
return text(L"Not implemented component");
|
||||
}
|
||||
|
||||
bool Component::Event(int key) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Component::Focused() {
|
||||
Delegate* current = delegate_->Root();
|
||||
while (current) {
|
||||
if (current == delegate_)
|
||||
return true;
|
||||
|
||||
Component* active_child = current->component()->GetActiveChild();
|
||||
current = active_child ? active_child->delegate_ : nullptr;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Component::Active() {
|
||||
Delegate* parent = delegate_->Parent();
|
||||
return parent && parent->component()->GetActiveChild() == this;
|
||||
}
|
||||
|
||||
Component* Component::PreviousSibling() {
|
||||
Delegate* sibling = delegate_->PreviousSibling();
|
||||
return sibling ? sibling->component() : nullptr;
|
||||
}
|
||||
|
||||
Component* Component::NextSibling() {
|
||||
Delegate* sibling = delegate_->NextSibling();
|
||||
return sibling ? sibling->component() : nullptr;
|
||||
}
|
||||
|
||||
Component* Component::Parent() {
|
||||
Delegate* parent_delegate = delegate_->Parent();
|
||||
if (!parent_delegate)
|
||||
return nullptr;
|
||||
return parent_delegate->component();
|
||||
}
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
31
ftxui/src/ftxui/component/component_direction.cpp
Normal file
31
ftxui/src/ftxui/component/component_direction.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "ftxui/component/component_direction.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
ComponentDirection::ComponentDirection(Delegate* delegate)
|
||||
: Component(delegate), active_child_(nullptr) {}
|
||||
|
||||
bool ComponentDirection::Event(int key) {
|
||||
if (!Focused())
|
||||
return false;
|
||||
|
||||
if (!active_child_)
|
||||
return false;
|
||||
|
||||
if (active_child_->Event(key))
|
||||
return true;
|
||||
|
||||
return HandleDirection(key);
|
||||
}
|
||||
|
||||
Component* ComponentDirection::GetActiveChild() {
|
||||
return active_child_;
|
||||
}
|
||||
|
||||
void ComponentDirection::Focus(Component* child) {
|
||||
active_child_ = child;
|
||||
}
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
32
ftxui/src/ftxui/component/component_horizontal.cpp
Normal file
32
ftxui/src/ftxui/component/component_horizontal.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "ftxui/component/component_horizontal.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
ComponentHorizontal::ComponentHorizontal(Delegate* delegate)
|
||||
: ComponentDirection(delegate) {}
|
||||
|
||||
bool ComponentHorizontal::HandleDirection(int key) {
|
||||
// Left pressed ?
|
||||
if (key == 68 || key == 'h') {
|
||||
Component* previous_sibling = active_child_->PreviousSibling();
|
||||
if (previous_sibling) {
|
||||
active_child_ = previous_sibling;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Left pressed ?
|
||||
if (key == 67 || key == 'l') {
|
||||
Component* next_sibling = active_child_->NextSibling();
|
||||
if (next_sibling) {
|
||||
active_child_ = next_sibling;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
32
ftxui/src/ftxui/component/component_vertical.cpp
Normal file
32
ftxui/src/ftxui/component/component_vertical.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "ftxui/component/component_vertical.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
ComponentVertical::ComponentVertical(Delegate* delegate)
|
||||
: ComponentDirection(delegate) {}
|
||||
|
||||
bool ComponentVertical::HandleDirection(int key) {
|
||||
// Up pressed ?
|
||||
if (key == 65 || key == 'k') {
|
||||
Component* previous_sibling = active_child_->PreviousSibling();
|
||||
if (previous_sibling) {
|
||||
active_child_ = previous_sibling;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Down pressed ?
|
||||
if (key == 66 || key == 'j') {
|
||||
Component* next_sibling = active_child_->NextSibling();
|
||||
if (next_sibling) {
|
||||
active_child_ = next_sibling;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
53
ftxui/src/ftxui/component/menu.cpp
Normal file
53
ftxui/src/ftxui/component/menu.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include "ftxui/component/menu.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
Menu::Menu(Delegate* delegate) : Component(delegate) {}
|
||||
|
||||
dom::Element Menu::Render() {
|
||||
using namespace dom;
|
||||
std::vector<Element> elements;
|
||||
bool focused = Focused();
|
||||
for (size_t i = 0; i < entries.size(); ++i) {
|
||||
if (size_t(selected) == i) {
|
||||
if (focused)
|
||||
elements.push_back(inverted(text(L"> " + entries[i])));
|
||||
else
|
||||
elements.push_back(bold(text(L"> " + entries[i])));
|
||||
}
|
||||
else {
|
||||
elements.push_back(text(L" " + entries[i]));
|
||||
}
|
||||
}
|
||||
return vbox(std::move(elements));
|
||||
}
|
||||
|
||||
bool Menu::Event(int key) {
|
||||
if (!Focused())
|
||||
return false;
|
||||
|
||||
int new_selected = selected;
|
||||
if (key == 65 || key == 'k')
|
||||
new_selected--;
|
||||
if (key == 66 || key == 'j')
|
||||
new_selected++;
|
||||
new_selected = std::max(0, std::min(int(entries.size())-1, new_selected));
|
||||
|
||||
if (selected != new_selected) {
|
||||
selected = new_selected;
|
||||
on_change();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (key == 10) {
|
||||
on_enter();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
47
ftxui/src/ftxui/component/toggle.cpp
Normal file
47
ftxui/src/ftxui/component/toggle.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "ftxui/component/toggle.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace component {
|
||||
|
||||
Toggle::Toggle(Delegate* delegate) : Component(delegate) {}
|
||||
|
||||
dom::Element Toggle::Render() {
|
||||
using namespace dom;
|
||||
auto highlight = Focused() ? inverted : bold;
|
||||
|
||||
Children children;
|
||||
children.push_back(text(L"["));
|
||||
if (activated) {
|
||||
children.push_back(highlight(text(on)));
|
||||
children.push_back(text(L"|"));
|
||||
children.push_back(dim(text(off)));
|
||||
} else {
|
||||
children.push_back(dim(text(on)));
|
||||
children.push_back(text(L"|"));
|
||||
children.push_back(highlight(text(off)));
|
||||
}
|
||||
children.push_back(text(L"]"));
|
||||
return hbox(std::move(children));
|
||||
}
|
||||
|
||||
bool Toggle::Event(int key) {
|
||||
|
||||
if (activated) {
|
||||
if (key == 67 || key == 'l') {
|
||||
activated = false;
|
||||
on_change();
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (key == 68 || key == 'h') {
|
||||
activated = true;
|
||||
on_change();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace component
|
||||
} // namespace ftxui
|
||||
@@ -1,5 +0,0 @@
|
||||
#include "ftxui/core/component.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
void Component::ComputeRequirement() {}
|
||||
} // namespace ftxui.
|
||||
@@ -1,57 +0,0 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
static wchar_t charset[] = L"┌┐└┘─│";
|
||||
|
||||
class Frame : public Node {
|
||||
public:
|
||||
Frame(Child child) : Node(unpack(std::move(child))) {}
|
||||
~Frame() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
children[0]->ComputeRequirement();
|
||||
requirement_ = children[0]->requirement();
|
||||
requirement_.min.x += 2;
|
||||
requirement_.min.y += 2;
|
||||
}
|
||||
|
||||
void SetBox(Box box) override {
|
||||
Node::SetBox(box);
|
||||
box.left++;
|
||||
box.right--;
|
||||
box.top++;
|
||||
box.bottom--;
|
||||
children[0]->SetBox(box);
|
||||
}
|
||||
|
||||
void Render(Screen& screen) override {
|
||||
if (box_.left >= box_.right || box_.top >= box_.bottom)
|
||||
return;
|
||||
|
||||
screen.at(box_.left, box_.top) = charset[0];
|
||||
screen.at(box_.right, box_.top) = charset[1];
|
||||
screen.at(box_.left, box_.bottom) = charset[2];
|
||||
screen.at(box_.right, box_.bottom) = charset[3];
|
||||
for(float x = box_.left + 1; x<box_.right; ++x) {
|
||||
screen.at(x, box_.top) = charset[4];
|
||||
screen.at(x, box_.bottom) = charset[4];
|
||||
}
|
||||
for(float y = box_.top + 1; y<box_.bottom; ++y) {
|
||||
screen.at(box_.left, y) = charset[5];
|
||||
screen.at(box_.right,y) = charset[5];
|
||||
}
|
||||
children[0]->Render(screen);
|
||||
}
|
||||
private:
|
||||
float progress_;
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> frame(Child child) {
|
||||
return std::make_unique<Frame>(std::move(child));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
@@ -1,38 +0,0 @@
|
||||
#include "ftxui/core/screen.hpp"
|
||||
#include "ftxui/core/terminal.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Screen::Screen(size_t dimx, size_t dimy)
|
||||
: dimx_(dimx), dimy_(dimy), lines_(dimy, std::wstring(dimx, U' ')) {}
|
||||
|
||||
std::string Screen::ToString() {
|
||||
std::stringstream ss;
|
||||
for (size_t y = 0; y < dimy_; ++y) {
|
||||
ss << to_string(lines_[y]);
|
||||
if (y + 1 < dimy_)
|
||||
ss << '\n';
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
wchar_t& Screen::at(size_t x, size_t y) {
|
||||
return lines_[y][x];
|
||||
}
|
||||
|
||||
Screen Screen::WholeTerminal() {
|
||||
Terminal::Dimensions size = Terminal::Size();
|
||||
return Screen(size.dimx, size.dimy);
|
||||
}
|
||||
|
||||
Screen Screen::TerminalOutput(std::unique_ptr<dom::Node>& element) {
|
||||
element->ComputeRequirement();
|
||||
Terminal::Dimensions size = Terminal::Size();
|
||||
return Screen(size.dimx, element->requirement().min.y);
|
||||
}
|
||||
|
||||
}; // namespace ftxui
|
||||
37
ftxui/src/ftxui/dom/bold.cpp
Normal file
37
ftxui/src/ftxui/dom/bold.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
class Bold : public Node {
|
||||
public:
|
||||
Bold(Children children) : Node(std::move(children)) {}
|
||||
~Bold() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
Node::ComputeRequirement();
|
||||
requirement_ = children[0]->requirement();
|
||||
}
|
||||
|
||||
void SetBox(Box box) override {
|
||||
Node::SetBox(box);
|
||||
children[0]->SetBox(box);
|
||||
}
|
||||
|
||||
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).bold = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> bold(Child child) {
|
||||
return std::make_unique<Bold>(unpack(std::move(child)));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
37
ftxui/src/ftxui/dom/dim.cpp
Normal file
37
ftxui/src/ftxui/dom/dim.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
class Dim : public Node {
|
||||
public:
|
||||
Dim(Children children) : Node(std::move(children)) {}
|
||||
~Dim() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
Node::ComputeRequirement();
|
||||
requirement_ = children[0]->requirement();
|
||||
}
|
||||
|
||||
void SetBox(Box box) override {
|
||||
Node::SetBox(box);
|
||||
children[0]->SetBox(box);
|
||||
}
|
||||
|
||||
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).dim = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> dim(Child child) {
|
||||
return std::make_unique<Dim>(unpack(std::move(child)));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
92
ftxui/src/ftxui/dom/frame.cpp
Normal file
92
ftxui/src/ftxui/dom/frame.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
static wchar_t charset[] = L"┌┐└┘─│┬┴┤├";
|
||||
|
||||
class Frame : public Node {
|
||||
public:
|
||||
Frame(Children children) : Node(std::move(children)) {}
|
||||
~Frame() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
Node::ComputeRequirement();
|
||||
requirement_ = children[0]->requirement();
|
||||
requirement_.min.x += 2;
|
||||
requirement_.min.y += 2;
|
||||
if (children.size() == 2) {
|
||||
requirement_.min.x =
|
||||
std::max(requirement_.min.x, children[1]->requirement().min.x + 2);
|
||||
}
|
||||
}
|
||||
|
||||
void SetBox(Box box) override {
|
||||
Node::SetBox(box);
|
||||
if (children.size() == 2) {
|
||||
Box title_box;
|
||||
title_box.left = box.left + 1;
|
||||
title_box.right = box.right - 1;
|
||||
title_box.top = box.top;
|
||||
title_box.bottom = box.top;
|
||||
children[1]->SetBox(title_box);
|
||||
}
|
||||
box.left++;
|
||||
box.right--;
|
||||
box.top++;
|
||||
box.bottom--;
|
||||
children[0]->SetBox(box);
|
||||
}
|
||||
|
||||
void Render(Screen& screen) override {
|
||||
// Draw content.
|
||||
children[0]->Render(screen);
|
||||
|
||||
// Draw the frame.
|
||||
if (box_.left >= box_.right || box_.top >= box_.bottom)
|
||||
return;
|
||||
|
||||
screen.at(box_.left, box_.top) = charset[0];
|
||||
screen.at(box_.right, box_.top) = charset[1];
|
||||
screen.at(box_.left, box_.bottom) = charset[2];
|
||||
screen.at(box_.right, box_.bottom) = charset[3];
|
||||
for(float x = box_.left + 1; x<box_.right; ++x) {
|
||||
screen.at(x, box_.top) = charset[4];
|
||||
screen.at(x, box_.bottom) = charset[4];
|
||||
}
|
||||
for(float y = box_.top + 1; y<box_.bottom; ++y) {
|
||||
screen.at(box_.left, y) = charset[5];
|
||||
screen.at(box_.right,y) = charset[5];
|
||||
}
|
||||
|
||||
// Try to merge with separator.
|
||||
for(float x = box_.left + 1; x<box_.right; ++x) {
|
||||
if (screen.at(x, box_.top + 1) == charset[5])
|
||||
screen.at(x, box_.top) = charset[6];
|
||||
if (screen.at(x, box_.bottom - 1) == charset[5])
|
||||
screen.at(x, box_.bottom) = charset[7];
|
||||
}
|
||||
for(float y = box_.top + 1; y<box_.bottom; ++y) {
|
||||
if (screen.at(box_.left+1, y) == charset[4])
|
||||
screen.at(box_.left, y) = charset[9];
|
||||
if (screen.at(box_.right-1, y) == charset[4])
|
||||
screen.at(box_.right,y) = charset[8];
|
||||
}
|
||||
|
||||
// Draw title.
|
||||
if (children.size() == 2)
|
||||
children[1]->Render(screen);
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> frame(Child child) {
|
||||
return std::make_unique<Frame>(unpack(std::move(child)));
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> frame(Child title, Child content) {
|
||||
return std::make_unique<Frame>(unpack(std::move(content), std::move(title)));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
34
ftxui/src/ftxui/dom/gauge_test.cpp
Normal file
34
ftxui/src/ftxui/dom/gauge_test.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
TEST(GaugeTest, zero) {
|
||||
auto root = gauge(0);
|
||||
Screen screen(11,1);
|
||||
Render(screen, root.get());
|
||||
|
||||
EXPECT_EQ(" ", screen.ToString());
|
||||
}
|
||||
|
||||
TEST(GaugeTest, half) {
|
||||
auto root = gauge(0.5);
|
||||
Screen screen(11,1);
|
||||
Render(screen, root.get());
|
||||
|
||||
EXPECT_EQ("█████▏▋ ", screen.ToString());
|
||||
//" ▏▎▍▌▊▉█";
|
||||
}
|
||||
|
||||
TEST(GaugeTest, one) {
|
||||
auto root = gauge(1.0);
|
||||
Screen screen(11,1);
|
||||
Render(screen, root.get());
|
||||
|
||||
EXPECT_EQ("███████████", screen.ToString());
|
||||
}
|
||||
|
||||
} // namespace dom
|
||||
} // namespace ftxui
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/core/screen.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace ftxui {
|
||||
37
ftxui/src/ftxui/dom/inverted.cpp
Normal file
37
ftxui/src/ftxui/dom/inverted.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
class Inverted : public Node {
|
||||
public:
|
||||
Inverted(Children children) : Node(std::move(children)) {}
|
||||
~Inverted() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
Node::ComputeRequirement();
|
||||
requirement_ = children[0]->requirement();
|
||||
}
|
||||
|
||||
void SetBox(Box box) override {
|
||||
Node::SetBox(box);
|
||||
children[0]->SetBox(box);
|
||||
}
|
||||
|
||||
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).inverted = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> inverted(Child child) {
|
||||
return std::make_unique<Inverted>(unpack(std::move(child)));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
@@ -8,10 +8,15 @@ Node::Node(std::vector<std::unique_ptr<Node>> children)
|
||||
: children(std::move(children)) {}
|
||||
Node::~Node() {}
|
||||
|
||||
void Node::ComputeRequirement() {}
|
||||
void Node::ComputeRequirement() {
|
||||
for(auto& child : children)
|
||||
child->ComputeRequirement();
|
||||
}
|
||||
|
||||
void Node::SetBox(Box box) {
|
||||
box_ = box;
|
||||
}
|
||||
|
||||
void Node::Render(Screen& screen) {
|
||||
for(auto& child : children)
|
||||
child->Render(screen);
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/core/screen.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace ftxui {
|
||||
37
ftxui/src/ftxui/dom/underlined.cpp
Normal file
37
ftxui/src/ftxui/dom/underlined.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
|
||||
class Underlined : public Node {
|
||||
public:
|
||||
Underlined(Children children) : Node(std::move(children)) {}
|
||||
~Underlined() override {}
|
||||
|
||||
void ComputeRequirement() override {
|
||||
Node::ComputeRequirement();
|
||||
requirement_ = children[0]->requirement();
|
||||
}
|
||||
|
||||
void SetBox(Box box) override {
|
||||
Node::SetBox(box);
|
||||
children[0]->SetBox(box);
|
||||
}
|
||||
|
||||
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).underlined = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<Node> underlined(Child child) {
|
||||
return std::make_unique<Underlined>(unpack(std::move(child)));
|
||||
}
|
||||
|
||||
}; // namespace dom
|
||||
}; // namespace ftxui
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/node.hpp"
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
namespace dom {
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "ftxui/core/dom/elements.hpp"
|
||||
#include "ftxui/core/screen.hpp"
|
||||
#include "ftxui/dom/elements.hpp"
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace ftxui {
|
||||
91
ftxui/src/ftxui/screen.cpp
Normal file
91
ftxui/src/ftxui/screen.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/screen.hpp"
|
||||
#include "ftxui/terminal.hpp"
|
||||
#include "ftxui/util/string.hpp"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Screen::Screen(size_t dimx, size_t dimy)
|
||||
: dimx_(dimx), dimy_(dimy), pixels_(dimy, std::vector<Pixel>(dimx)) {}
|
||||
|
||||
std::string Screen::ToString() {
|
||||
std::wstringstream ss;
|
||||
|
||||
Pixel previous_pixel;
|
||||
|
||||
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";
|
||||
}
|
||||
}
|
||||
ss << pixels_[y][x].character;
|
||||
previous_pixel = pixels_[y][x];
|
||||
}
|
||||
if (y + 1 < dimy_)
|
||||
ss << '\n';
|
||||
}
|
||||
return to_string(ss.str());
|
||||
}
|
||||
|
||||
wchar_t& Screen::at(size_t x, size_t y) {
|
||||
return pixels_[y][x].character;
|
||||
}
|
||||
|
||||
Pixel& Screen::PixelAt(size_t x, size_t y) {
|
||||
return pixels_[y][x];
|
||||
}
|
||||
|
||||
// static
|
||||
Screen Screen::TerminalFullscreen() {
|
||||
Terminal::Dimensions size = Terminal::Size();
|
||||
return Screen(size.dimx, size.dimy);
|
||||
}
|
||||
|
||||
// static
|
||||
Screen Screen::TerminalOutput(std::unique_ptr<dom::Node>& element) {
|
||||
element->ComputeRequirement();
|
||||
Terminal::Dimensions size = Terminal::Size();
|
||||
return Screen(size.dimx, element->requirement().min.y);
|
||||
}
|
||||
|
||||
std::string Screen::ResetPosition() {
|
||||
std::stringstream ss;
|
||||
for(size_t y = 1; y<dimy_; ++y) {
|
||||
ss << "\e[2K\r\e[1A";
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void Screen::Clear() {
|
||||
pixels_ = std::vector<std::vector<Pixel>>(dimy_,
|
||||
std::vector<Pixel>(dimx_, Pixel()));
|
||||
}
|
||||
|
||||
}; // namespace ftxui
|
||||
112
ftxui/src/ftxui/screen_interactive.cpp
Normal file
112
ftxui/src/ftxui/screen_interactive.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "ftxui/screen_interactive.hpp"
|
||||
#include "ftxui/component/component.hpp"
|
||||
#include "ftxui/component/delegate.hpp"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
class ScreenInteractive::Delegate : public component::Delegate {
|
||||
public:
|
||||
Delegate() : root_(this) {}
|
||||
|
||||
void Register(component::Component* c) override { component_ = c; }
|
||||
|
||||
std::vector<std::unique_ptr<Delegate>> child_;
|
||||
Delegate* NewChild() override {
|
||||
Delegate* child = new Delegate;
|
||||
child->root_ = root_;
|
||||
child->parent_ = this;
|
||||
|
||||
if (!child_.empty()) {
|
||||
child_.back()->next_sibling_ = child;
|
||||
child->previous_sibling_ = child_.back().get();
|
||||
}
|
||||
|
||||
child_.emplace_back(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
void Event(int key) { component_->Event(key); }
|
||||
|
||||
|
||||
std::vector<component::Delegate*> children() override {
|
||||
std::vector<component::Delegate*> ret;
|
||||
for (auto& it : child_)
|
||||
ret.push_back(it.get());
|
||||
return ret;
|
||||
}
|
||||
|
||||
Delegate* root_;
|
||||
Delegate* parent_ = nullptr;
|
||||
Delegate* previous_sibling_ = nullptr;
|
||||
Delegate* next_sibling_ = nullptr;
|
||||
component::Component* component_;
|
||||
|
||||
Delegate* Root() override { return root_; }
|
||||
Delegate* Parent() override { return parent_; }
|
||||
Delegate* PreviousSibling() override { return previous_sibling_; }
|
||||
Delegate* NextSibling() override { return next_sibling_; }
|
||||
component::Component* component() override { return component_; }
|
||||
};
|
||||
|
||||
ScreenInteractive::ScreenInteractive(size_t dimx, size_t dimy)
|
||||
: Screen(dimx, dimy), delegate_(new Delegate) {}
|
||||
ScreenInteractive::~ScreenInteractive() {}
|
||||
|
||||
void ScreenInteractive::Loop() {
|
||||
std::cout << "\033[?9h"; /* Send Mouse Row & Column on Button Press */
|
||||
std::cout << "\033[?1000h"; /* Send Mouse X & Y on button press and release */
|
||||
std::cout << std::flush;
|
||||
|
||||
// Save the old terminal configuration.
|
||||
struct termios terminal_configuration_old;
|
||||
tcgetattr(STDIN_FILENO, &terminal_configuration_old);
|
||||
|
||||
// Set the new terminal configuration
|
||||
struct termios terminal_configuration_new;
|
||||
terminal_configuration_new = terminal_configuration_old;
|
||||
|
||||
// Non canonique terminal.
|
||||
terminal_configuration_new.c_lflag &= ~ICANON;
|
||||
// Do not print after a key press.
|
||||
terminal_configuration_new.c_lflag &= ~ECHO;
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new);
|
||||
|
||||
Draw();
|
||||
while (!quit_) {
|
||||
int key = getchar();
|
||||
delegate_->Event(key);
|
||||
|
||||
Clear();
|
||||
Draw();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
//Clear();
|
||||
|
||||
// Restore the old terminal configuration.
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_old);
|
||||
}
|
||||
|
||||
void ScreenInteractive::Draw() {
|
||||
auto document = delegate_->component()->Render();
|
||||
Render(*this, document.get());
|
||||
std::cout << ToString() << std::flush;
|
||||
}
|
||||
|
||||
void ScreenInteractive::Clear() {
|
||||
std::cout << ResetPosition();
|
||||
Screen::Clear();
|
||||
}
|
||||
|
||||
component::Delegate* ScreenInteractive::delegate() {
|
||||
return delegate_.get();
|
||||
}
|
||||
|
||||
std::function<void()> ScreenInteractive::ExitLoopClosure() {
|
||||
return [this]() { quit_ = true; };
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "ftxui/core/terminal.hpp"
|
||||
#include "ftxui/terminal.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Reference in New Issue
Block a user