Pipeable decoration and the package_manager example.

- Pipeable decorator.
- package_manager example.
This commit is contained in:
Arthur Sonzogni 2019-01-05 02:03:49 +01:00
parent 178feaa6a9
commit 961e3dcb50
32 changed files with 351 additions and 128 deletions

View File

@ -3,3 +3,4 @@ example(menu)
example(menu2) example(menu2)
example(menu_style) example(menu_style)
example(toggle) example(toggle)
example(tab)

View File

@ -45,7 +45,7 @@ class MyComponent : ComponentVertical {
}; };
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
ftxui::ScreenInteractive screen(60, 5); auto screen = ftxui::ScreenInteractive::TerminalOutput();
MyComponent component(screen.delegate()); MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure(); component.on_enter = screen.ExitLoopClosure();
screen.Loop(); screen.Loop();

View File

@ -7,7 +7,7 @@
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
ftxui::ScreenInteractive screen(30,3); auto screen = ftxui::ScreenInteractive::FixedSize(30, 3);
ftxui::component::Menu menu(screen.delegate()); ftxui::component::Menu menu(screen.delegate());
menu.entries = { menu.entries = {
L"entry 1", L"entry 1",

View File

@ -40,29 +40,25 @@ class MyComponent : ComponentHorizontal {
// -------- Top panel -------------- // -------- Top panel --------------
hbox( hbox(
// -------- Left Menu -------------- // -------- Left Menu --------------
flex( vbox(
vbox( hcenter(bold(text(L"Percentage by 10%"))),
hcenter(bold(text(L"Percentage by 10%"))), separator(),
separator(), left_menu.Render()
left_menu.Render() ) | flex,
)
),
// -------- Right Menu -------------- // -------- Right Menu --------------
flex( vbox(
vbox( hcenter(bold(text(L"Percentage by 1%"))),
hcenter(bold(text(L"Percentage by 1%"))), separator(),
separator(), right_menu.Render()
right_menu.Render() ) | flex,
) filler()
),
flex()
), ),
separator(), separator(),
// -------- Bottom panel -------------- // -------- Bottom panel --------------
flex(vbox( vbox(
hbox(text(L" gauge : "), gauge(sum/100.0)), hbox(text(L" gauge : "), gauge(sum/100.0)),
hbox(text(L" text : "), text(to_wstring(std::to_string(sum) + " %"))) hbox(text(L" text : "), text(to_wstring(std::to_string(sum) + " %")))
)) ) | flex
) )
); );
} }
@ -70,7 +66,7 @@ class MyComponent : ComponentHorizontal {
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
ftxui::ScreenInteractive screen(60,18); auto screen = ftxui::ScreenInteractive::TerminalOutput();
MyComponent component(screen.delegate()); MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure(); component.on_enter = screen.ExitLoopClosure();
screen.Loop(); screen.Loop();

View File

@ -33,7 +33,7 @@ class MyComponent : ComponentHorizontal {
} }
menu_2.selected_style = color(Color::Blue); menu_2.selected_style = color(Color::Blue);
menu_2.active_style = compose(bold, color(Color::Blue)); menu_2.active_style = bold | color(Color::Blue);
menu_3.selected_style = color(Color::Blue); menu_3.selected_style = color(Color::Blue);
menu_3.active_style = bgcolor(Color::Blue); menu_3.active_style = bgcolor(Color::Blue);
@ -45,9 +45,9 @@ class MyComponent : ComponentHorizontal {
menu_5.selected_style = bgcolor(Color::Yellow); menu_5.selected_style = bgcolor(Color::Yellow);
menu_5.active_style = bgcolor(Color::Red); menu_5.active_style = bgcolor(Color::Red);
menu_6.normal_style = compose(dim, color(Color::Blue)); menu_6.normal_style = dim | color(Color::Blue);
menu_6.selected_style = compose(nothing, color(Color::Blue)); menu_6.selected_style = color(Color::Blue);
menu_6.active_style = compose(bold, color(Color::Blue)); menu_6.active_style = bold | color(Color::Blue);
Focus(&menu_1); Focus(&menu_1);
} }
@ -63,24 +63,21 @@ class MyComponent : ComponentHorizontal {
Element Render() override { Element Render() override {
return return
vbox(
hbox( hbox(
flex(frame(center(text(L" menu_1 ")), menu_1.Render())), menu_1.Render() | flex, separator(),
flex(frame(center(text(L" menu_2 ")), menu_2.Render())), menu_2.Render() | flex, separator(),
flex(frame(center(text(L" menu_3 ")), menu_3.Render())) menu_3.Render() | flex, separator(),
), menu_4.Render() | flex, separator(),
hbox( menu_5.Render() | flex, separator(),
flex(frame(center(text(L" menu_4 ")), menu_4.Render())), menu_6.Render() | flex
flex(frame(center(text(L" menu_5 ")), menu_5.Render())), ) | frame;
flex(frame(center(text(L" menu_6 ")), menu_6.Render()))
)
);
} }
}; };
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
ftxui::ScreenInteractive screen(90,14); //auto screen = ftxui::ScreenInteractive::TerminalOutput();
auto screen = ftxui::ScreenInteractive::Fullscreen();
MyComponent component(screen.delegate()); MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure(); component.on_enter = screen.ExitLoopClosure();
screen.Loop(); screen.Loop();

View File

@ -0,0 +1,46 @@
#include <iostream>
#include <thread>
#include "ftxui/component/component_vertical.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/screen_interactive.hpp"
#include "ftxui/util/string.hpp"
using namespace ftxui;
using namespace ftxui::component;
using namespace ftxui::dom;
class MyComponent : ComponentVertical {
public:
MyComponent(ftxui::component::Delegate* delegate)
: ComponentVertical(delegate),
toggle(delegate->NewChild()),
menu(delegate->NewChild()) {
toggle.options = {L" left ", L" middle ", L" end "};
menu.entries = {L" top ", L" middle ", L" bottom "};
Focus(&toggle);
}
std::function<void()> on_enter = [](){};
private:
Toggle toggle;
Menu menu;
Element Render() override {
return
vbox(
hbox(frame(toggle.Render()), filler()),
frame(menu.Render()));
}
};
int main(int argc, const char *argv[])
{
auto screen = ftxui::ScreenInteractive::TerminalOutput();
MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure();
screen.Loop();
}

View File

@ -62,7 +62,7 @@ class MyComponent : ComponentVertical {
}; };
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
ftxui::ScreenInteractive screen(70,7); auto screen = ftxui::ScreenInteractive::TerminalOutput();
MyComponent component(screen.delegate()); MyComponent component(screen.delegate());
component.on_enter = screen.ExitLoopClosure(); component.on_enter = screen.ExitLoopClosure();
screen.Loop(); screen.Loop();

View File

@ -2,5 +2,6 @@ example(color)
example(dbox) example(dbox)
example(frame) example(frame)
example(gauge) example(gauge)
example(package_manager)
example(separator) example(separator)
example(vbox_hbox) example(vbox_hbox)

View File

@ -46,7 +46,7 @@ int main(int argc, const char *argv[])
bgcolor(Color::Yellow, text(L"Yellow")), bgcolor(Color::Yellow, text(L"Yellow")),
bgcolor(Color::YellowLight, text(L"YellowLight")) bgcolor(Color::YellowLight, text(L"YellowLight"))
), ),
flex() filler()
); );
auto screen = ftxui::Screen::TerminalOutput(document); auto screen = ftxui::Screen::TerminalOutput(document);

View File

@ -10,7 +10,7 @@ int main(int argc, const char *argv[])
using namespace ftxui::dom; using namespace ftxui::dom;
auto document = auto document =
hbox( hbox(
frame(hcenter(text(L" main frame ")), window(hcenter(text(L" main frame ")),
vbox( vbox(
text(L"Line 1"), text(L"Line 1"),
text(L"Line 2"), text(L"Line 2"),
@ -23,14 +23,14 @@ int main(int argc, const char *argv[])
) )
), ),
hbox( hbox(
frame(text(L"frame 2"), window(text(L"frame 2"),
vbox( vbox(
text(L"Line 4"), text(L"Line 4"),
text(L"Line 5"), text(L"Line 5"),
text(L"Line 6") text(L"Line 6")
) )
), ),
frame(text(L"frame 3"), window(text(L"frame 3"),
vbox( vbox(
text(L"Line 7"), text(L"Line 7"),
text(L"Line 8"), text(L"Line 8"),
@ -41,7 +41,7 @@ int main(int argc, const char *argv[])
text(L"footer footer footer footer footer") text(L"footer footer footer footer footer")
) )
), ),
flex() filler()
); );
auto screen = ftxui::Screen::TerminalOutput(document); auto screen = ftxui::Screen::TerminalOutput(document);
Render(screen, document.get()); Render(screen, document.get());

View File

@ -14,7 +14,7 @@ int main(int argc, const char *argv[])
auto document = auto document =
hbox( hbox(
text(L"downloading:"), text(L"downloading:"),
flex(gauge(percentage)), gauge(percentage) | flex,
text(L" " + data_downloaded) text(L" " + data_downloaded)
); );
auto screen = ftxui::Screen(100, 1); auto screen = ftxui::Screen(100, 1);

View File

@ -0,0 +1,123 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "ftxui/screen.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/util/string.hpp"
#include <list>
#include <vector>
using namespace ftxui;
using namespace ftxui::dom;
int main(int argc, const char *argv[])
{
struct Task {
std::wstring name;
int number_of_threads;
int downloaded;
int size;
};
std::list<Task> remaining_tasks = {
{L"contact server " , 10 , 0 , 6*25} ,
{L"download index.html " , 10 , 0 , 9*25} ,
{L"download script.js " , 1 , 0 , 3*25} ,
{L"download style.js " , 1 , 0 , 4*25} ,
{L"download image.png " , 1 , 0 , 5*25} ,
{L"download big_1.png " , 1 , 0 , 30*25} ,
{L"download icon_1.png " , 1 , 0 , 7*25} ,
{L"download icon_2.png " , 1 , 0 , 8*25} ,
{L"download big_2.png " , 1 , 0 , 30*25} ,
{L"download small_1.png " , 1 , 0 , 10*25} ,
{L"download small_2.png " , 1 , 0 , 11*25} ,
{L"download small_3.png " , 1 , 0 , 12*25} ,
};
std::list<Task> displayed_task;
int remaining_threads = 12;
std::string reset_position;
int nb_queued = remaining_tasks.size();
int nb_active = 0;
int nb_done = 0;
auto to_text = [](int number) {
std::wstring t = to_wstring(std::to_string(number));
while(t.size() < 3)
t = L" " + t;
return text(t);
};
for(;;) {
std::vector<Element> entries;
for(auto& task : displayed_task) {
auto style = (task.downloaded == task.size) ? dim : bold;
entries.push_back(
hbox(
text(task.name) | style,
separator(),
to_text(task.downloaded),
text(L"/"),
to_text(task.size),
separator(),
gauge(task.downloaded / float(task.size))
)
);
}
auto document =
vbox(
window(text(L" Task "),
vbox(std::move(entries))
),
hbox(
window(text(L" Summary "),
vbox(
hbox(text(L"- done: "), to_text(nb_done) | bold) | color(Color::Green),
hbox(text(L"- active: "), to_text(nb_active) | bold ) | color(Color::RedLight),
hbox(text(L"- queue: "), to_text(nb_queued) | bold) | color(Color::Red)
)
)
)
);
// Draw.
//if (step != 0) screen.Clear();
auto screen = ftxui::Screen::TerminalOutput(document);
Render(screen, document.get());
std::cout << reset_position << screen.ToString() << std::flush;
reset_position = screen.ResetPosition();
// Simulate time.
using namespace std::chrono_literals;
std::this_thread::sleep_for(0.01s);
if (nb_active + nb_queued == 0)
break;
// Update the model.
for(auto& task : displayed_task) {
if (task.downloaded != task.size) {
task.downloaded++;
} else if (task.number_of_threads) {
remaining_threads += task.number_of_threads;
task.number_of_threads = 0;
nb_active--;
nb_done++;
}
}
if (remaining_tasks.size() &&
remaining_tasks.front().number_of_threads <= remaining_threads) {
remaining_threads -= remaining_tasks.front().number_of_threads;
displayed_task.push_back(remaining_tasks.front());
remaining_tasks.pop_front();
nb_queued--;
nb_active++;
}
}
std::cout << std::endl;
}

View File

@ -10,7 +10,7 @@ int main(int argc, const char *argv[])
text(L"left-column"), text(L"left-column"),
separator(), separator(),
flex(vbox( flex(vbox(
flex(center(text(L"right-column"))), center(text(L"right-column")) | flex,
separator(), separator(),
center(text(L"bottom-column")) center(text(L"bottom-column"))
)) ))

View File

@ -9,21 +9,21 @@ int main(int argc, const char *argv[])
vbox( vbox(
hbox( hbox(
text(L"north-west"), text(L"north-west"),
flex(), filler(),
text(L"north-east") text(L"north-east")
), ),
flex(), filler(),
hbox( hbox(
hbox( hbox(
flex(), filler(),
text(L"center"), text(L"center"),
flex() filler()
) )
), ),
flex(), filler(),
hbox( hbox(
text(L"south-west"), text(L"south-west"),
flex(), filler(),
text(L"south-east") text(L"south-east")
) )
); );

View File

@ -42,7 +42,7 @@ class DrawKey : public ftxui::component::Component {
}; };
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
ftxui::ScreenInteractive screen(80,10); auto screen = ftxui::ScreenInteractive::FixedSize(80,10);
DrawKey draw_key(screen.delegate()); DrawKey draw_key(screen.delegate());
screen.Loop(); screen.Loop();
} }

View File

@ -22,7 +22,7 @@
## Level 3: ftxui::component::Component ## Level 3: ftxui::component::Component
A hierarchical set of component. A component render itself by producing A hierarchical set of component. A component render itself by producing
ftxui::dom::Node in Component::Render(). ftxui::dom::Node in Component::Render().
Some component can handle events: Some component can handle events:
* keyboard * keyboard
* mouse * mouse

View File

@ -18,17 +18,17 @@ using Children = std::vector<Child>;
Element vbox(Children); Element vbox(Children);
Element hbox(Children); Element hbox(Children);
Element dbox(Children); Element dbox(Children);
Element flex(); Element filler();
Element flex(Element); Element flex(Element);
// --- Widget -- // --- Widget --
Element text(std::wstring text); Element text(std::wstring text);
Element separator(); Element separator();
Element gauge(float ratio); Element gauge(float ratio);
Element frame(Child); Element frame(Element);
Element frame(Child title, Child content); Element window(Child title, Child content);
// -- Style --- // -- Decorator ---
Element bold(Element); Element bold(Element);
Element dim(Element); Element dim(Element);
Element inverted(Element); Element inverted(Element);
@ -46,8 +46,13 @@ Element center(Element);
// --- Util --- // --- Util ---
Element nothing(Element element); Element nothing(Element element);
Decorator compose(Decorator, Decorator);
// Pipe elements into decorator togethers.
// Examples: text("ftxui") | bold | underlined;
Element operator|(Element, Decorator);
Decorator operator|(Decorator, Decorator);
// Make container able to take several children.
template <class... Args> template <class... Args>
Children unpack(Args... args) { Children unpack(Args... args) {
Children vec; Children vec;
@ -55,20 +60,15 @@ Children unpack(Args... args) {
return vec; return vec;
} }
template <class... Args> #define TAKE_ANY_ARGS(container) \
Element vbox(Args... children) { template <class... Args> \
return vbox(unpack(std::forward<Args>(children)...)); Element container(Args... children) { \
} return container(unpack(std::forward<Args>(children)...)); \
} \
template <class... Args> TAKE_ANY_ARGS(vbox)
Element hbox(Args... children) { TAKE_ANY_ARGS(hbox)
return hbox(unpack(std::forward<Args>(children)...)); TAKE_ANY_ARGS(dbox)
}
template <class... Args>
Element dbox(Args... children) {
return dbox(unpack(std::forward<Args>(children)...));
}
}; // namespace dom }; // namespace dom
}; // namespace ftxui }; // namespace ftxui

View File

@ -49,7 +49,7 @@ class Screen {
// Fill with space. // Fill with space.
void Clear(); void Clear();
private: protected:
size_t dimx_; size_t dimx_;
size_t dimy_; size_t dimy_;
std::vector<std::vector<Pixel>> pixels_; std::vector<std::vector<Pixel>> pixels_;

View File

@ -14,7 +14,10 @@ namespace component {
class ScreenInteractive : public Screen { class ScreenInteractive : public Screen {
public: public:
ScreenInteractive(size_t dimx, size_t dimy); static ScreenInteractive FixedSize(size_t dimx, size_t dimy);
static ScreenInteractive Fullscreen();
static ScreenInteractive TerminalOutput();
~ScreenInteractive(); ~ScreenInteractive();
component::Delegate* delegate(); component::Delegate* delegate();
void Loop(); void Loop();
@ -27,6 +30,15 @@ class ScreenInteractive : public Screen {
void Clear(); void Clear();
void Draw(); void Draw();
bool quit_ = false; bool quit_ = false;
enum class Dimension {
Fixed,
TerminalOutput,
Fullscreen,
};
Dimension dimension_ = Dimension::Fixed;
ScreenInteractive(size_t dimx, size_t dimy, Dimension dimension);
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -15,14 +15,14 @@ dom::Element Input::Render() {
// Placeholder. // Placeholder.
if (content.size() == 0) { if (content.size() == 0) {
if (is_focused) if (is_focused)
return flex(inverted(dim(text(placeholder)))); return text(placeholder) | dim | inverted | flex;
else else
return flex(dim(text(placeholder))); return text(placeholder) | dim | flex;
} }
// Not focused. // Not focused.
if (!is_focused) if (!is_focused)
return flex(text(content)); return text(content) | flex;
std::wstring part_before_cursor = content.substr(0,cursor_position); std::wstring part_before_cursor = content.substr(0,cursor_position);
std::wstring part_at_cursor = cursor_position < (int)content.size() std::wstring part_at_cursor = cursor_position < (int)content.size()
@ -31,11 +31,12 @@ dom::Element Input::Render() {
std::wstring part_after_cursor = cursor_position < (int)content.size() - 1 std::wstring part_after_cursor = cursor_position < (int)content.size() - 1
? content.substr(cursor_position + 1) ? content.substr(cursor_position + 1)
: L""; : L"";
return flex(inverted(hbox( // return
text(part_before_cursor), // hbox(
underlined(text(part_at_cursor)), // text(part_before_cursor),
text(part_after_cursor) // text(part_at_cursor) | underlined,
))); // text(part_after_cursor)
) | flex | inverted;
} }
bool Input::OnEvent(Event event) { bool Input::OnEvent(Event event) {
std::wstring c; std::wstring c;

View File

@ -10,18 +10,16 @@ dom::Element Toggle::Render() {
auto highlight = Focused() ? inverted : bold; auto highlight = Focused() ? inverted : bold;
Children children; Children children;
children.push_back(text(L"["));
for(size_t i = 0; i<options.size(); ++i) { for(size_t i = 0; i<options.size(); ++i) {
// Separator. // Separator.
if (i != 0) if (i != 0)
children.push_back(text(L"|")); children.push_back(separator());
// Entry. // Entry.
auto style = i == activated ? highlight : dim; auto style = i == activated ? highlight : dim;
children.push_back(style(text(options[i]))); children.push_back(style(text(options[i])));
} }
children.push_back(text(L"]"));
return hbox(std::move(children)); return hbox(std::move(children));
} }

View File

@ -10,12 +10,12 @@ class Bold : public NodeDecorator {
~Bold() override {} ~Bold() override {}
void Render(Screen& screen) override { void Render(Screen& screen) override {
Node::Render(screen);
for (int y = box_.top; y <= box_.bottom; ++y) { for (int y = box_.top; y <= box_.bottom; ++y) {
for (int x = box_.left; x <= box_.right; ++x) { for (int x = box_.left; x <= box_.right; ++x) {
screen.PixelAt(x,y).bold = true; screen.PixelAt(x,y).bold = true;
} }
} }
Node::Render(screen);
} }
}; };

View File

@ -10,12 +10,12 @@ class BgColor : public NodeDecorator {
: NodeDecorator(std::move(children)), color_(color) {} : NodeDecorator(std::move(children)), color_(color) {}
void Render(Screen& screen) override { void Render(Screen& screen) override {
NodeDecorator::Render(screen);
for (int y = box_.top; y <= box_.bottom; ++y) { for (int y = box_.top; y <= box_.bottom; ++y) {
for (int x = box_.left; x <= box_.right; ++x) { for (int x = box_.left; x <= box_.right; ++x) {
screen.PixelAt(x, y).background_color = color_; screen.PixelAt(x, y).background_color = color_;
} }
} }
NodeDecorator::Render(screen);
} }
Color color_; Color color_;
@ -28,12 +28,12 @@ class FgColor : public NodeDecorator {
~FgColor() override {} ~FgColor() override {}
void Render(Screen& screen) override { void Render(Screen& screen) override {
NodeDecorator::Render(screen);
for (int y = box_.top; y <= box_.bottom; ++y) { for (int y = box_.top; y <= box_.bottom; ++y) {
for (int x = box_.left; x <= box_.right; ++x) { for (int x = box_.left; x <= box_.right; ++x) {
screen.PixelAt(x, y).foreground_color = color_; screen.PixelAt(x, y).foreground_color = color_;
} }
} }
NodeDecorator::Render(screen);
} }
Color color_; Color color_;

View File

@ -5,11 +5,11 @@ namespace ftxui {
namespace dom { namespace dom {
std::unique_ptr<Node> hcenter(Element child) { std::unique_ptr<Node> hcenter(Element child) {
return hbox(flex(), std::move(child), flex()); return hbox(filler(), std::move(child), filler());
} }
std::unique_ptr<Node> vcenter(Element child) { std::unique_ptr<Node> vcenter(Element child) {
return vbox(flex(), std::move(child), flex()); return vbox(filler(), std::move(child), filler());
} }
std::unique_ptr<Node> center(Element child) { std::unique_ptr<Node> center(Element child) {

View File

@ -27,7 +27,7 @@ class Flex : public Node {
} }
}; };
std::unique_ptr<Node> flex() { std::unique_ptr<Node> filler() {
return std::make_unique<Flex>(); return std::make_unique<Flex>();
} }

View File

@ -84,9 +84,15 @@ std::unique_ptr<Node> frame(Child child) {
return std::make_unique<Frame>(unpack(std::move(child))); return std::make_unique<Frame>(unpack(std::move(child)));
} }
std::unique_ptr<Node> frame(Child title, Child content) { std::unique_ptr<Node> window(Child title, Child content) {
return std::make_unique<Frame>(unpack(std::move(content), std::move(title))); return std::make_unique<Frame>(unpack(std::move(content), std::move(title)));
} }
Decorator boxed() {
return [](Child child) {
return frame(std::move(child));
};
}
}; // namespace dom }; // namespace dom
}; // namespace ftxui }; // namespace ftxui

View File

@ -62,7 +62,7 @@ TEST(HBoxTest, ScreenBigger2) {
TEST(HBoxTest, ScreenSmaller1Flex) { TEST(HBoxTest, ScreenSmaller1Flex) {
auto root = hbox( auto root = hbox(
text(L"text_1"), text(L"text_1"),
flex(), filler(),
text(L"text_2") text(L"text_2")
); );
Screen screen(11,1); Screen screen(11,1);
@ -74,7 +74,7 @@ TEST(HBoxTest, ScreenSmaller1Flex) {
TEST(HBoxTest, ScreenSmaller2Flex) { TEST(HBoxTest, ScreenSmaller2Flex) {
auto root = hbox( auto root = hbox(
text(L"text_1"), text(L"text_1"),
flex(), filler(),
text(L"text_2") text(L"text_2")
); );
Screen screen(10,1); Screen screen(10,1);
@ -86,7 +86,7 @@ TEST(HBoxTest, ScreenSmaller2Flex) {
TEST(HBoxTest, ScreenFitFlex) { TEST(HBoxTest, ScreenFitFlex) {
auto root = hbox( auto root = hbox(
text(L"text_1"), text(L"text_1"),
flex(), filler(),
text(L"text_2") text(L"text_2")
); );
Screen screen(12,1); Screen screen(12,1);
@ -98,7 +98,7 @@ TEST(HBoxTest, ScreenFitFlex) {
TEST(HBoxTest, ScreenBigger1Flex) { TEST(HBoxTest, ScreenBigger1Flex) {
auto root = hbox( auto root = hbox(
text(L"text_1"), text(L"text_1"),
flex(), filler(),
text(L"text_2") text(L"text_2")
); );
Screen screen(13,1); Screen screen(13,1);
@ -110,7 +110,7 @@ TEST(HBoxTest, ScreenBigger1Flex) {
TEST(HBoxTest, ScreenBigger2Flex) { TEST(HBoxTest, ScreenBigger2Flex) {
auto root = hbox( auto root = hbox(
text(L"text_1"), text(L"text_1"),
flex(), filler(),
text(L"text_2") text(L"text_2")
); );
Screen screen(14,1); Screen screen(14,1);

View File

@ -19,7 +19,7 @@ class Separator : public Node {
wchar_t c = U'+'; wchar_t c = U'+';
if (is_line && !is_column) if (is_line && !is_column)
c = U''; c = U'';
else if (!is_line && is_column) else
c = U''; c = U'';
for (int y = box_.top; y <= box_.bottom; ++y) { for (int y = box_.top; y <= box_.bottom; ++y) {

View File

@ -12,9 +12,17 @@ Decorator compose(Decorator a, Decorator b) {
a = std::move(a), a = std::move(a),
b = std::move(b) b = std::move(b)
](Element element) { ](Element element) {
return a(b(std::move(element))); return b(a(std::move(element)));
}; };
} }
}; // namespace dom Decorator operator|(Decorator a, Decorator b) {
}; // namespace ftxui return compose(a, b);
}
Element operator|(Element e, Decorator d) {
return d(std::move(e));
}
} // namespace dom
} // namespace ftxui

View File

@ -37,7 +37,7 @@ TEST(VBoxTest, ScreenBigger2) {
} }
TEST(VBoxTest, ScreenSmaller1Flex) { TEST(VBoxTest, ScreenSmaller1Flex) {
auto root = vbox(text(L"text_1"), flex(), text(L"text_2")); auto root = vbox(text(L"text_1"), filler(), text(L"text_2"));
Screen screen(6, 1); Screen screen(6, 1);
Render(screen, root.get()); Render(screen, root.get());
@ -45,7 +45,7 @@ TEST(VBoxTest, ScreenSmaller1Flex) {
} }
TEST(VBoxTest, ScreenFitFlex) { TEST(VBoxTest, ScreenFitFlex) {
auto root = vbox(text(L"text_1"), flex(), text(L"text_2")); auto root = vbox(text(L"text_1"), filler(), text(L"text_2"));
Screen screen(6, 2); Screen screen(6, 2);
Render(screen, root.get()); Render(screen, root.get());
@ -53,14 +53,14 @@ TEST(VBoxTest, ScreenFitFlex) {
} }
TEST(VBoxTest, ScreenBigger1Flex) { TEST(VBoxTest, ScreenBigger1Flex) {
auto root = vbox(text(L"text_1"), flex(), text(L"text_2")); auto root = vbox(text(L"text_1"), filler(), text(L"text_2"));
Screen screen(6, 3); Screen screen(6, 3);
Render(screen, root.get()); Render(screen, root.get());
EXPECT_EQ("text_1\n \ntext_2", screen.ToString()); EXPECT_EQ("text_1\n \ntext_2", screen.ToString());
} }
TEST(VBoxTest, ScreenBigger2Flex) { TEST(VBoxTest, ScreenBigger2Flex) {
auto root = vbox(text(L"text_1"), flex(), text(L"text_2")); auto root = vbox(text(L"text_1"), filler(), text(L"text_2"));
Screen screen(6, 4); Screen screen(6, 4);
Render(screen, root.get()); Render(screen, root.get());

View File

@ -12,28 +12,19 @@ Screen::Screen(size_t dimx, size_t dimy)
void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) { void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) {
if (next.bold != previous.bold) if (next.bold != previous.bold)
ss << (next.bold ? L"\e[1m" : L"\e[0m"); ss << (next.bold ? L"\e[1m" : L"\e[22m"); // Can't use 21 here.
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) if (next.dim != previous.dim)
ss << (next.dim ? L"\e[2m" : L"\e[22m"); ss << (next.dim ? L"\e[2m" : L"\e[22m");
if (next.underlined != previous.underlined)
ss << (next.underlined ? L"\e[4m" : L"\e[24m");
if (next.blink != previous.blink) if (next.blink != previous.blink)
ss << (next.blink ? L"\e[5m" : L"\e[25m"); ss << (next.blink ? L"\e[5m" : L"\e[25m");
if (next.inverted != previous.inverted)
if (next.foreground_color != previous.foreground_color) { ss << (next.inverted ? L"\e[7m" : L"\e[27m");
ss << L"\e[" + to_wstring(std::to_string((uint8_t)next.foreground_color)) + if (next.foreground_color != previous.foreground_color ||
L"m"; next.background_color != previous.background_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";
ss << L"\e[" +
to_wstring(std::to_string(10 + (uint8_t)next.background_color)) +
L"m";
} }
previous = next; previous = next;
@ -49,8 +40,11 @@ std::string Screen::ToString() {
UpdatePixelStyle(ss, previous_pixel, pixels_[y][x]); UpdatePixelStyle(ss, previous_pixel, pixels_[y][x]);
ss << pixels_[y][x].character; ss << pixels_[y][x].character;
} }
if (y + 1 < dimy_) if (y + 1 < dimy_)
ss << '\n'; ss << '\n';
Pixel final_pixel;
UpdatePixelStyle(ss, previous_pixel, final_pixel);
} }
return to_string(ss.str()); return to_string(ss.str());

View File

@ -1,6 +1,8 @@
#include "ftxui/screen_interactive.hpp" #include "ftxui/screen_interactive.hpp"
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/component/delegate.hpp" #include "ftxui/component/delegate.hpp"
#include "ftxui/terminal.hpp"
#include <iostream> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <termios.h> #include <termios.h>
@ -86,10 +88,27 @@ class ScreenInteractive::Delegate : public component::Delegate {
component::Component* component() override { return component_; } component::Component* component() override { return component_; }
}; };
ScreenInteractive::ScreenInteractive(size_t dimx, size_t dimy) ScreenInteractive::ScreenInteractive(size_t dimx,
: Screen(dimx, dimy), delegate_(new Delegate) {} size_t dimy,
Dimension dimension)
: Screen(dimx, dimy), delegate_(new Delegate), dimension_(dimension) {}
ScreenInteractive::~ScreenInteractive() {} ScreenInteractive::~ScreenInteractive() {}
// static
ScreenInteractive ScreenInteractive::FixedSize(size_t dimx, size_t dimy) {
return ScreenInteractive(dimx, dimy, Dimension::Fixed);
}
// static
ScreenInteractive ScreenInteractive::Fullscreen() {
return ScreenInteractive(0, 0, Dimension::Fullscreen);
}
// static
ScreenInteractive ScreenInteractive::TerminalOutput() {
return ScreenInteractive(0, 0, Dimension::TerminalOutput);
}
void ScreenInteractive::Loop() { void ScreenInteractive::Loop() {
std::cout << "\033[?9h"; /* Send Mouse Row & Column on Button Press */ 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 << "\033[?1000h"; /* Send Mouse X & Y on button press and release */
@ -110,14 +129,12 @@ void ScreenInteractive::Loop() {
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new); tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new);
Draw(); Draw();
while (!quit_) { while(!quit_) {
delegate_->OnEvent(GetEvent()); delegate_->OnEvent(GetEvent());
Clear(); Clear();
Draw(); Draw();
} } while(!quit_);
std::cout << std::endl; //std::cout << std::endl;
//Clear();
// Restore the old terminal configuration. // Restore the old terminal configuration.
tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_old); tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_old);
@ -125,6 +142,29 @@ void ScreenInteractive::Loop() {
void ScreenInteractive::Draw() { void ScreenInteractive::Draw() {
auto document = delegate_->component()->Render(); auto document = delegate_->component()->Render();
size_t dimx;
size_t dimy;
switch(dimension_) {
case Dimension::Fixed:
break;
case Dimension::TerminalOutput:
document->ComputeRequirement();
dimx = Terminal::Size().dimx;
dimy = document->requirement().min.y;
break;
case Dimension::Fullscreen:
document->ComputeRequirement();
dimx = Terminal::Size().dimx;
dimy = Terminal::Size().dimy;
break;
}
if (dimx != dimx_ || dimy != dimy_) {
dimx_ = dimx;
dimy_ = dimy;
pixels_ = std::vector<std::vector<Pixel>>(dimy, std::vector<Pixel>(dimx));
}
Render(*this, document.get()); Render(*this, document.get());
std::cout << ToString() << std::flush; std::cout << ToString() << std::flush;
} }