From 5887114793b6312ee7b7d33becbf915ffaf83197 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Sun, 6 Jan 2019 17:10:35 +0100 Subject: [PATCH] Refactor directory structure. The goal is to increase the separation in between: * ftxui::screen * ftxui::dom * ftxui::component --- examples/component/input.cpp | 9 +-- examples/component/menu.cpp | 17 ++-- examples/component/menu2.cpp | 8 +- examples/component/menu_style.cpp | 8 +- examples/component/tab.cpp | 9 +-- examples/component/toggle.cpp | 9 +-- examples/dom/blink.cpp | 6 +- examples/dom/bold.cpp | 6 +- examples/dom/color.cpp | 6 +- examples/dom/dbox.cpp | 5 +- examples/dom/dim.cpp | 6 +- examples/dom/frame.cpp | 5 +- examples/dom/gauge.cpp | 14 ++-- examples/dom/inverted.cpp | 6 +- examples/dom/package_manager.cpp | 10 +-- examples/dom/separator.cpp | 5 +- examples/dom/style_gallery.cpp | 6 +- examples/dom/underlined.cpp | 6 +- examples/dom/vbox_hbox.cpp | 6 +- examples/print_key_press.cpp | 24 +++--- ftxui/CMakeLists.txt | 6 +- ftxui/include/ftxui/box.hpp | 11 --- ftxui/include/ftxui/component/component.hpp | 8 +- .../ftxui/component/component_horizontal.hpp | 6 +- .../ftxui/component/component_vertical.hpp | 6 +- ftxui/include/ftxui/component/delegate.hpp | 6 +- ftxui/include/ftxui/{ => component}/event.hpp | 10 +-- ftxui/include/ftxui/component/input.hpp | 6 +- ftxui/include/ftxui/component/menu.hpp | 6 +- .../{ => component}/screen_interactive.hpp | 28 +++---- ftxui/include/ftxui/component/toggle.hpp | 6 +- ftxui/include/ftxui/dom/box.hpp | 15 ++++ ftxui/include/ftxui/dom/elements.hpp | 9 +-- ftxui/include/ftxui/dom/node.hpp | 22 +++-- ftxui/include/ftxui/{ => dom}/requirement.hpp | 8 +- ftxui/include/ftxui/{ => screen}/color.hpp | 8 +- ftxui/include/ftxui/{ => screen}/screen.hpp | 15 ++-- ftxui/src/ftxui/component/component.cpp | 6 +- .../ftxui/component/component_direction.cpp | 6 +- .../ftxui/component/component_horizontal.cpp | 6 +- .../ftxui/component/component_vertical.cpp | 6 +- ftxui/src/ftxui/{ => component}/event.cpp | 6 +- ftxui/src/ftxui/component/input.cpp | 6 +- ftxui/src/ftxui/component/menu.cpp | 6 +- .../{ => component}/screen_interactive.cpp | 80 ++++++++++--------- ftxui/src/ftxui/component/toggle.cpp | 6 +- ftxui/src/ftxui/dom/blink.cpp | 8 +- ftxui/src/ftxui/dom/bold.cpp | 8 +- ftxui/src/ftxui/dom/color.cpp | 10 +-- ftxui/src/ftxui/dom/composite_decorator.cpp | 6 +- ftxui/src/ftxui/dom/dbox.cpp | 6 +- ftxui/src/ftxui/dom/dim.cpp | 8 +- ftxui/src/ftxui/dom/flex.cpp | 6 +- ftxui/src/ftxui/dom/frame.cpp | 8 +- ftxui/src/ftxui/dom/gauge.cpp | 8 +- ftxui/src/ftxui/dom/gauge_test.cpp | 9 +-- ftxui/src/ftxui/dom/hbox.cpp | 6 +- ftxui/src/ftxui/dom/hbox_test.cpp | 9 +-- ftxui/src/ftxui/dom/inverted.cpp | 8 +- ftxui/src/ftxui/dom/node.cpp | 8 +- ftxui/src/ftxui/dom/node_decorator.cpp | 6 +- ftxui/src/ftxui/dom/node_decorator.hpp | 6 +- ftxui/src/ftxui/dom/separator.cpp | 8 +- ftxui/src/ftxui/dom/text.cpp | 8 +- ftxui/src/ftxui/dom/text_test.cpp | 9 +-- ftxui/src/ftxui/dom/underlined.cpp | 8 +- ftxui/src/ftxui/dom/util.cpp | 6 +- ftxui/src/ftxui/dom/vbox.cpp | 6 +- ftxui/src/ftxui/dom/vbox_test.cpp | 9 +-- ftxui/src/ftxui/{ => screen}/screen.cpp | 36 +++++++-- 70 files changed, 324 insertions(+), 361 deletions(-) delete mode 100644 ftxui/include/ftxui/box.hpp rename ftxui/include/ftxui/{ => component}/event.hpp (75%) rename ftxui/include/ftxui/{ => component}/screen_interactive.hpp (55%) create mode 100644 ftxui/include/ftxui/dom/box.hpp rename ftxui/include/ftxui/{ => dom}/requirement.hpp (69%) rename ftxui/include/ftxui/{ => screen}/color.hpp (80%) rename ftxui/include/ftxui/{ => screen}/screen.hpp (82%) rename ftxui/src/ftxui/{ => component}/event.cpp (90%) rename ftxui/src/ftxui/{ => component}/screen_interactive.cpp (81%) rename ftxui/src/ftxui/{ => screen}/screen.cpp (70%) diff --git a/examples/component/input.cpp b/examples/component/input.cpp index 1a521bd..2238de1 100644 --- a/examples/component/input.cpp +++ b/examples/component/input.cpp @@ -2,18 +2,17 @@ #include #include -#include "ftxui/screen_interactive.hpp" -#include "ftxui/component/input.hpp" #include "ftxui/component/component_vertical.hpp" +#include "ftxui/component/input.hpp" +#include "ftxui/component/screen_interactive.hpp" #include "ftxui/util/string.hpp" using namespace ftxui::component; using namespace ftxui::dom; -using namespace ftxui; class MyComponent : ComponentVertical { public: - MyComponent(ftxui::component::Delegate* delegate) + MyComponent(Delegate* delegate) : ComponentVertical(delegate), input_1(delegate->NewChild()), input_2(delegate->NewChild()), @@ -45,7 +44,7 @@ class MyComponent : ComponentVertical { }; int main(int argc, const char* argv[]) { - auto screen = ftxui::ScreenInteractive::TerminalOutput(); + auto screen = ScreenInteractive::TerminalOutput(); MyComponent component(screen.delegate()); component.on_enter = screen.ExitLoopClosure(); screen.Loop(); diff --git a/examples/component/menu.cpp b/examples/component/menu.cpp index 177bd74..d9f8434 100644 --- a/examples/component/menu.cpp +++ b/examples/component/menu.cpp @@ -2,18 +2,15 @@ #include #include -#include "ftxui/screen_interactive.hpp" #include "ftxui/component/menu.hpp" +#include "ftxui/component/screen_interactive.hpp" -int main(int argc, const char *argv[]) -{ - auto screen = ftxui::ScreenInteractive::FixedSize(30, 3); - ftxui::component::Menu menu(screen.delegate()); - menu.entries = { - L"entry 1", - L"entry 2", - L"entry 3" - }; +int main(int argc, const char* argv[]) { + using namespace ftxui::component; + using namespace ftxui::screen; + auto screen = ScreenInteractive::FixedSize(30, 3); + Menu menu(screen.delegate()); + menu.entries = {L"entry 1", L"entry 2", L"entry 3"}; menu.selected = 0; menu.on_enter = screen.ExitLoopClosure(); diff --git a/examples/component/menu2.cpp b/examples/component/menu2.cpp index 50a7ba0..5e1da52 100644 --- a/examples/component/menu2.cpp +++ b/examples/component/menu2.cpp @@ -2,10 +2,10 @@ #include #include -#include "ftxui/screen_interactive.hpp" -#include "ftxui/component/menu.hpp" #include "ftxui/component/component_horizontal.hpp" #include "ftxui/component/component_vertical.hpp" +#include "ftxui/component/menu.hpp" +#include "ftxui/component/screen_interactive.hpp" #include "ftxui/util/string.hpp" using namespace ftxui::component; @@ -13,7 +13,7 @@ using namespace ftxui::dom; class MyComponent : ComponentHorizontal { public: - MyComponent(ftxui::component::Delegate* delegate) + MyComponent(Delegate* delegate) : ComponentHorizontal(delegate), left_menu(delegate->NewChild()), right_menu(delegate->NewChild()) { @@ -66,7 +66,7 @@ class MyComponent : ComponentHorizontal { int main(int argc, const char *argv[]) { - auto screen = ftxui::ScreenInteractive::TerminalOutput(); + auto screen = ScreenInteractive::TerminalOutput(); MyComponent component(screen.delegate()); component.on_enter = screen.ExitLoopClosure(); screen.Loop(); diff --git a/examples/component/menu_style.cpp b/examples/component/menu_style.cpp index 342e2e9..dc7f8ca 100644 --- a/examples/component/menu_style.cpp +++ b/examples/component/menu_style.cpp @@ -3,16 +3,15 @@ #include "ftxui/component/component_horizontal.hpp" #include "ftxui/component/menu.hpp" -#include "ftxui/screen_interactive.hpp" +#include "ftxui/component/screen_interactive.hpp" #include "ftxui/util/string.hpp" -using namespace ftxui; using namespace ftxui::component; using namespace ftxui::dom; class MyComponent : ComponentHorizontal { public: - MyComponent(ftxui::component::Delegate* delegate) + MyComponent(Delegate* delegate) : ComponentHorizontal(delegate), menu_1(delegate->NewChild()), menu_2(delegate->NewChild()), @@ -76,8 +75,7 @@ class MyComponent : ComponentHorizontal { int main(int argc, const char *argv[]) { - //auto screen = ftxui::ScreenInteractive::TerminalOutput(); - auto screen = ftxui::ScreenInteractive::Fullscreen(); + auto screen = ScreenInteractive::TerminalOutput(); MyComponent component(screen.delegate()); component.on_enter = screen.ExitLoopClosure(); screen.Loop(); diff --git a/examples/component/tab.cpp b/examples/component/tab.cpp index 228aaa4..88375dc 100644 --- a/examples/component/tab.cpp +++ b/examples/component/tab.cpp @@ -2,18 +2,17 @@ #include #include "ftxui/component/component_vertical.hpp" -#include "ftxui/component/toggle.hpp" #include "ftxui/component/menu.hpp" -#include "ftxui/screen_interactive.hpp" +#include "ftxui/component/screen_interactive.hpp" +#include "ftxui/component/toggle.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) + MyComponent(Delegate* delegate) : ComponentVertical(delegate), toggle(delegate->NewChild()), menu(delegate->NewChild()) { @@ -39,7 +38,7 @@ class MyComponent : ComponentVertical { int main(int argc, const char *argv[]) { - auto screen = ftxui::ScreenInteractive::TerminalOutput(); + auto screen = ScreenInteractive::TerminalOutput(); MyComponent component(screen.delegate()); component.on_enter = screen.ExitLoopClosure(); screen.Loop(); diff --git a/examples/component/toggle.cpp b/examples/component/toggle.cpp index 8a6d0fd..db679f8 100644 --- a/examples/component/toggle.cpp +++ b/examples/component/toggle.cpp @@ -2,19 +2,18 @@ #include #include -#include "ftxui/screen_interactive.hpp" -#include "ftxui/component/toggle.hpp" #include "ftxui/component/component_horizontal.hpp" #include "ftxui/component/component_vertical.hpp" +#include "ftxui/component/screen_interactive.hpp" +#include "ftxui/component/toggle.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) + MyComponent(Delegate* delegate) : ComponentVertical(delegate), toggle_1(delegate->NewChild()), toggle_2(delegate->NewChild()), @@ -62,7 +61,7 @@ class MyComponent : ComponentVertical { }; int main(int argc, const char* argv[]) { - auto screen = ftxui::ScreenInteractive::TerminalOutput(); + auto screen = ScreenInteractive::TerminalOutput(); MyComponent component(screen.delegate()); component.on_enter = screen.ExitLoopClosure(); screen.Loop(); diff --git a/examples/dom/blink.cpp b/examples/dom/blink.cpp index 4214a7d..e5c0b02 100644 --- a/examples/dom/blink.cpp +++ b/examples/dom/blink.cpp @@ -1,10 +1,10 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { - using namespace ftxui; + using namespace ftxui::screen; using namespace ftxui::dom; auto document = hbox( @@ -12,7 +12,7 @@ int main(int argc, const char *argv[]) text(L"blink") | blink, text(L". Do you like it?") ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/bold.cpp b/examples/dom/bold.cpp index 1a03b83..9ee59a1 100644 --- a/examples/dom/bold.cpp +++ b/examples/dom/bold.cpp @@ -1,10 +1,10 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { - using namespace ftxui; + using namespace ftxui::screen; using namespace ftxui::dom; auto document = hbox( @@ -12,7 +12,7 @@ int main(int argc, const char *argv[]) text(L"bold") | bold, text(L". Do you like it?") ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/color.cpp b/examples/dom/color.cpp index d8f8065..7bc42a7 100644 --- a/examples/dom/color.cpp +++ b/examples/dom/color.cpp @@ -1,10 +1,10 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { - using namespace ftxui; + using namespace ftxui::screen; using namespace ftxui::dom; auto document = hbox( @@ -49,7 +49,7 @@ int main(int argc, const char *argv[]) filler() ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/dbox.cpp b/examples/dom/dbox.cpp index 0a2e31d..09483a3 100644 --- a/examples/dom/dbox.cpp +++ b/examples/dom/dbox.cpp @@ -1,10 +1,11 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { using namespace ftxui::dom; + using namespace ftxui::screen; auto document = dbox( frame( @@ -22,7 +23,7 @@ int main(int argc, const char *argv[]) ) ) ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/dim.cpp b/examples/dom/dim.cpp index a838643..7b834c4 100644 --- a/examples/dom/dim.cpp +++ b/examples/dom/dim.cpp @@ -1,10 +1,10 @@ -#include "ftxui/screen.hpp" #include "ftxui/dom/elements.hpp" +#include "ftxui/screen/screen.hpp" #include int main(int argc, const char *argv[]) { - using namespace ftxui; + using namespace ftxui::screen; using namespace ftxui::dom; auto document = hbox( @@ -12,7 +12,7 @@ int main(int argc, const char *argv[]) text(L"dim") | dim, text(L". Do you like it?") ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/frame.cpp b/examples/dom/frame.cpp index 0cfccbc..b995d0d 100644 --- a/examples/dom/frame.cpp +++ b/examples/dom/frame.cpp @@ -2,12 +2,13 @@ #include #include -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" int main(int argc, const char *argv[]) { using namespace ftxui::dom; + using namespace ftxui::screen; auto document = hbox( window(text(L" main frame ") | hcenter, @@ -43,7 +44,7 @@ int main(int argc, const char *argv[]) ), filler() ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString() << std::endl; } diff --git a/examples/dom/gauge.cpp b/examples/dom/gauge.cpp index 54ea3ae..3b097e8 100644 --- a/examples/dom/gauge.cpp +++ b/examples/dom/gauge.cpp @@ -2,26 +2,30 @@ #include #include -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" int main(int argc, const char *argv[]) { + using namespace ftxui::dom; + using namespace ftxui::screen; + using namespace std::chrono_literals; + + std::string reset_position; for(float percentage = 0; percentage <= 1.0; percentage+=0.002) { std::wstring data_downloaded = std::to_wstring(int(percentage * 5000)) + L"/5000"; - using namespace ftxui::dom; auto document = hbox( text(L"downloading:"), gauge(percentage) | flex, text(L" " + data_downloaded) ); - auto screen = ftxui::Screen(100, 1); + auto screen = Screen(100, 1); Render(screen, document.get()); - std::cout << '\r' << screen.ToString() << std::flush; + std::cout << reset_position << screen.ToString() << std::flush; + reset_position = screen.ResetPosition(); - using namespace std::chrono_literals; std::this_thread::sleep_for(0.01s); } std::cout << std::endl; diff --git a/examples/dom/inverted.cpp b/examples/dom/inverted.cpp index 176dd6d..286a0bd 100644 --- a/examples/dom/inverted.cpp +++ b/examples/dom/inverted.cpp @@ -1,10 +1,10 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { - using namespace ftxui; + using namespace ftxui::screen; using namespace ftxui::dom; auto document = hbox( @@ -12,7 +12,7 @@ int main(int argc, const char *argv[]) text(L"inverted") | inverted, text(L". Do you like it?") ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/package_manager.cpp b/examples/dom/package_manager.cpp index 1987bec..590797b 100644 --- a/examples/dom/package_manager.cpp +++ b/examples/dom/package_manager.cpp @@ -2,17 +2,17 @@ #include #include -#include "ftxui/screen.hpp" #include "ftxui/dom/elements.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/util/string.hpp" #include #include -using namespace ftxui; -using namespace ftxui::dom; - int main(int argc, const char *argv[]) { + using namespace ftxui::screen; + using namespace ftxui::dom; + struct Task { std::wstring name; int number_of_threads; @@ -119,7 +119,7 @@ int main(int argc, const char *argv[]) // Draw. auto document = render(); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << reset_position << screen.ToString() << std::flush; reset_position = screen.ResetPosition(); diff --git a/examples/dom/separator.cpp b/examples/dom/separator.cpp index d25a14e..18ae1b9 100644 --- a/examples/dom/separator.cpp +++ b/examples/dom/separator.cpp @@ -1,10 +1,11 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { using namespace ftxui::dom; + using namespace ftxui::screen; auto document = hbox( text(L"left-column"), @@ -15,7 +16,7 @@ int main(int argc, const char *argv[]) center(text(L"bottom-column")) )) ); - auto screen = ftxui::Screen::TerminalFullscreen(); + auto screen = Screen::TerminalFullscreen(); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/style_gallery.cpp b/examples/dom/style_gallery.cpp index f44df66..44ec43c 100644 --- a/examples/dom/style_gallery.cpp +++ b/examples/dom/style_gallery.cpp @@ -1,10 +1,10 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { - using namespace ftxui; + using namespace ftxui::screen; using namespace ftxui::dom; auto document = hbox( @@ -17,7 +17,7 @@ int main(int argc, const char *argv[]) text(L"color") | color(Color::Blue) , text(L" ") , text(L"bgcolor") | bgcolor(Color::Blue) ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/underlined.cpp b/examples/dom/underlined.cpp index 9f50dd2..dcc3817 100644 --- a/examples/dom/underlined.cpp +++ b/examples/dom/underlined.cpp @@ -1,10 +1,10 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include int main(int argc, const char *argv[]) { - using namespace ftxui; + using namespace ftxui::screen; using namespace ftxui::dom; auto document = hbox( @@ -12,7 +12,7 @@ int main(int argc, const char *argv[]) text(L"underlined") | underlined, text(L". Do you like it?") ); - auto screen = ftxui::Screen::TerminalOutput(document); + auto screen = Screen::TerminalOutput(document); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/dom/vbox_hbox.cpp b/examples/dom/vbox_hbox.cpp index 281f791..b67b0a6 100644 --- a/examples/dom/vbox_hbox.cpp +++ b/examples/dom/vbox_hbox.cpp @@ -1,9 +1,11 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/elements.hpp" #include + int main(int argc, const char *argv[]) { + using namespace ftxui::screen; using namespace ftxui::dom; auto document = vbox( @@ -27,7 +29,7 @@ int main(int argc, const char *argv[]) text(L"south-east") ) ); - auto screen = ftxui::Screen::TerminalFullscreen(); + auto screen = Screen::TerminalFullscreen(); Render(screen, document.get()); std::cout << screen.ToString(); diff --git a/examples/print_key_press.cpp b/examples/print_key_press.cpp index 6bdf7f7..4281530 100644 --- a/examples/print_key_press.cpp +++ b/examples/print_key_press.cpp @@ -2,26 +2,28 @@ #include #include -#include "ftxui/screen_interactive.hpp" #include "ftxui/component/component.hpp" +#include "ftxui/component/screen_interactive.hpp" #include "ftxui/util/string.hpp" -class DrawKey : public ftxui::component::Component { +using namespace ftxui::component; + +class DrawKey : public Component { public: - DrawKey(ftxui::component::Delegate* delegate) - : ftxui::component::Component(delegate) {} + DrawKey(Delegate* delegate) + : Component(delegate) {} ftxui::dom::Element Render() override { using namespace ftxui::dom; Children children; for (size_t i = std::max(0, (int)keys.size() - 10); i < keys.size(); ++i) { - std::string code = ""; - for(size_t j = 0; j<5; ++j) + std::string code = ""; + for (size_t j = 0; j < 5; ++j) code += " " + std::to_string(keys[i].values[j]); try { - std::string line = code + " -> " + std::to_string(keys[i].values[0]) + " (" + - char(keys[i].values[0]) + ")"; + std::string line = code + " -> " + std::to_string(keys[i].values[0]) + + " (" + char(keys[i].values[0]) + ")"; children.push_back(text(to_wstring(line))); } catch (...) { std::string line = @@ -32,17 +34,17 @@ class DrawKey : public ftxui::component::Component { return vbox(std::move(children)); } - bool OnEvent(ftxui::Event event) override { + bool OnEvent(Event event) override { keys.push_back(event); return true; } private: - std::vector keys; + std::vector keys; }; int main(int argc, const char* argv[]) { - auto screen = ftxui::ScreenInteractive::FixedSize(80,10); + auto screen = ScreenInteractive::FixedSize(80, 10); DrawKey draw_key(screen.delegate()); screen.Loop(); } diff --git a/ftxui/CMakeLists.txt b/ftxui/CMakeLists.txt index e5a8628..73aba01 100644 --- a/ftxui/CMakeLists.txt +++ b/ftxui/CMakeLists.txt @@ -5,8 +5,10 @@ add_library(ftxui src/ftxui/component/component_direction.cpp src/ftxui/component/component_horizontal.cpp src/ftxui/component/component_vertical.cpp + src/ftxui/component/event.cpp src/ftxui/component/input.cpp src/ftxui/component/menu.cpp + src/ftxui/component/screen_interactive.cpp src/ftxui/component/toggle.cpp src/ftxui/dom/blink.cpp src/ftxui/dom/bold.cpp @@ -26,9 +28,7 @@ add_library(ftxui src/ftxui/dom/underlined.cpp src/ftxui/dom/util.cpp src/ftxui/dom/vbox.cpp - src/ftxui/event.cpp - src/ftxui/screen.cpp - src/ftxui/screen_interactive.cpp + src/ftxui/screen/screen.cpp src/ftxui/terminal.cpp src/ftxui/util/string.cpp ) diff --git a/ftxui/include/ftxui/box.hpp b/ftxui/include/ftxui/box.hpp deleted file mode 100644 index 021a594..0000000 --- a/ftxui/include/ftxui/box.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef FTX_UI_CORE_BOX -#define FTX_UI_CORE_BOX - -struct Box { - int left; - int right; - int top; - int bottom; -}; - -#endif /* end of include guard: FTX_UI_CORE_BOX */ diff --git a/ftxui/include/ftxui/component/component.hpp b/ftxui/include/ftxui/component/component.hpp index 1c575ef..3ce9107 100644 --- a/ftxui/include/ftxui/component/component.hpp +++ b/ftxui/include/ftxui/component/component.hpp @@ -2,11 +2,10 @@ #define FTXUI_COMPONENT_COMPONENT_HPP #include "ftxui/component/delegate.hpp" +#include "ftxui/component/event.hpp" #include "ftxui/dom/elements.hpp" -#include "ftxui/event.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { class Delegate; class Focus; @@ -39,7 +38,6 @@ class Component { Delegate* delegate_; }; -} // namespace component -} // namespace ftxui +} // namespace ftxui::component #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HPP */ diff --git a/ftxui/include/ftxui/component/component_horizontal.hpp b/ftxui/include/ftxui/component/component_horizontal.hpp index 35dadf2..d238613 100644 --- a/ftxui/include/ftxui/component/component_horizontal.hpp +++ b/ftxui/include/ftxui/component/component_horizontal.hpp @@ -3,8 +3,7 @@ #include "ftxui/component/component_direction.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { // A component where focus and events are automatically handled for you. // It assumes its children are put in the horizontal direction. @@ -14,7 +13,6 @@ class ComponentHorizontal : public ComponentDirection { bool HandleDirection(Event) override; }; -} // namespace component -} // namespace ftxui +} // namespace ftxui::component #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HORIZONTAL_H_ */ diff --git a/ftxui/include/ftxui/component/component_vertical.hpp b/ftxui/include/ftxui/component/component_vertical.hpp index 22c91f2..78d59e4 100644 --- a/ftxui/include/ftxui/component/component_vertical.hpp +++ b/ftxui/include/ftxui/component/component_vertical.hpp @@ -3,8 +3,7 @@ #include "ftxui/component/component_direction.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { // A component where focus and events are automatically handled for you. // It assumes its children are put in the vertical direction. @@ -14,7 +13,6 @@ class ComponentVertical : public ComponentDirection { bool HandleDirection(Event) override; }; -} // namespace component -} // namespace ftxui +} // namespace ftxui::component #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_VERTICAL_H_ */ diff --git a/ftxui/include/ftxui/component/delegate.hpp b/ftxui/include/ftxui/component/delegate.hpp index d4bc236..89be580 100644 --- a/ftxui/include/ftxui/component/delegate.hpp +++ b/ftxui/include/ftxui/component/delegate.hpp @@ -3,8 +3,7 @@ #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { class Component; @@ -28,7 +27,6 @@ class Delegate { virtual Delegate* Root() = 0; }; -} // namespace component -} // namespace ftxui +} // namespace ftxui::component #endif /* end of include guard: FTXUI_COMPONENT_DELEGATE_HPP */ diff --git a/ftxui/include/ftxui/event.hpp b/ftxui/include/ftxui/component/event.hpp similarity index 75% rename from ftxui/include/ftxui/event.hpp rename to ftxui/include/ftxui/component/event.hpp index c276c24..75c26e7 100644 --- a/ftxui/include/ftxui/event.hpp +++ b/ftxui/include/ftxui/component/event.hpp @@ -1,10 +1,10 @@ -#ifndef FTXUI_EVENT_H_ -#define FTXUI_EVENT_H_ +#ifndef FTXUI_COMPONENT_EVENT_HPP +#define FTXUI_COMPONENT_EVENT_HPP #include #include -namespace ftxui { +namespace ftxui::component { struct Event{ public: @@ -31,7 +31,7 @@ struct Event{ }; -} // namespace ftxui +} // namespace ftxui::component -#endif /* end of include guard: FTXUI_EVENT_H_ */ +#endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */ diff --git a/ftxui/include/ftxui/component/input.hpp b/ftxui/include/ftxui/component/input.hpp index 6f8537c..8a79bfd 100644 --- a/ftxui/include/ftxui/component/input.hpp +++ b/ftxui/include/ftxui/component/input.hpp @@ -4,8 +4,7 @@ #include "ftxui/component/component.hpp" #include -namespace ftxui { -namespace component { +namespace ftxui::component { class Input : public Component { public: @@ -29,7 +28,6 @@ class Input : public Component { int cursor_position = 0; }; -} // namespace component -} // namespace ftxui +} // namespace ftxui::component #endif /* end of include guard: FTXUI_COMPONENT_INPUT_H_ */ diff --git a/ftxui/include/ftxui/component/menu.hpp b/ftxui/include/ftxui/component/menu.hpp index 6b4f045..f3800d3 100644 --- a/ftxui/include/ftxui/component/menu.hpp +++ b/ftxui/include/ftxui/component/menu.hpp @@ -5,8 +5,7 @@ #include "ftxui/dom/elements.hpp" #include -namespace ftxui { -namespace component { +namespace ftxui::component { class Menu : public Component { public: @@ -30,7 +29,6 @@ class Menu : public Component { bool OnEvent(Event) override; }; -} // namespace component -} // namespace ftxui +} // namespace ftxui::Component #endif /* end of include guard: FTXUI_COMPONENT_MENU */ diff --git a/ftxui/include/ftxui/screen_interactive.hpp b/ftxui/include/ftxui/component/screen_interactive.hpp similarity index 55% rename from ftxui/include/ftxui/screen_interactive.hpp rename to ftxui/include/ftxui/component/screen_interactive.hpp index 6ae80be..5a45348 100644 --- a/ftxui/include/ftxui/screen_interactive.hpp +++ b/ftxui/include/ftxui/component/screen_interactive.hpp @@ -1,18 +1,16 @@ -#ifndef FTXUI_SCREEN_INTERACTIVE -#define FTXUI_SCREEN_INTERACTIVE +#ifndef FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP +#define FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include #include -namespace ftxui { +namespace ftxui::component { -namespace component { - class Delegate; - class Component; -} // namespace component +class Delegate; +class Component; -class ScreenInteractive : public Screen { +class ScreenInteractive : public ftxui::screen::Screen { public: static ScreenInteractive FixedSize(size_t dimx, size_t dimy); static ScreenInteractive Fullscreen(); @@ -21,15 +19,15 @@ class ScreenInteractive : public Screen { ~ScreenInteractive(); component::Delegate* delegate(); void Loop(); - std::function ExitLoopClosure(); + std::function ExitLoopClosure(); private: class Delegate; std::unique_ptr delegate_; - void Clear(); - void Draw(); - bool quit_ = false; + void Clear(); + void Draw(); + bool quit_ = false; enum class Dimension { Fixed, @@ -41,6 +39,6 @@ class ScreenInteractive : public Screen { ScreenInteractive(size_t dimx, size_t dimy, Dimension dimension); }; -} // namespace ftxui +} // namespace ftxui::component -#endif /* end of include guard: FTXUI_SCREEN_INTERACTIVE */ +#endif /* end of include guard: FTXUI_COMPONENT_SCREEN_INTERACTIVE_HPP */ diff --git a/ftxui/include/ftxui/component/toggle.hpp b/ftxui/include/ftxui/component/toggle.hpp index 5bfe6df..ef6a89a 100644 --- a/ftxui/include/ftxui/component/toggle.hpp +++ b/ftxui/include/ftxui/component/toggle.hpp @@ -5,8 +5,7 @@ #include #include -namespace ftxui { -namespace component { +namespace ftxui::component { class Toggle : public Component { public: @@ -25,7 +24,6 @@ class Toggle : public Component { bool OnEvent(Event) override; }; -} // namespace component -} // namespace ftxui +} // namespace ftxui::Component #endif /* end of include guard: FTXUI_COMPONENT_TOGGLE_H_ */ diff --git a/ftxui/include/ftxui/dom/box.hpp b/ftxui/include/ftxui/dom/box.hpp new file mode 100644 index 0000000..8710465 --- /dev/null +++ b/ftxui/include/ftxui/dom/box.hpp @@ -0,0 +1,15 @@ +#ifndef FTXUI_DOM_BOX_HPP +#define FTXUI_DOM_BOX_HPP + +namespace ftxui::dom { + +struct Box { + int left; + int right; + int top; + int bottom; +}; + +}; // namespace ftxui::dom + +#endif /* end of include guard: FTXUI_DOM_BOX_HPP */ diff --git a/ftxui/include/ftxui/dom/elements.hpp b/ftxui/include/ftxui/dom/elements.hpp index 67ebe7d..f21d76d 100644 --- a/ftxui/include/ftxui/dom/elements.hpp +++ b/ftxui/include/ftxui/dom/elements.hpp @@ -3,16 +3,16 @@ #include -#include "ftxui/color.hpp" #include "ftxui/dom/node.hpp" +#include "ftxui/screen/color.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { using Element = std::unique_ptr; using Decorator = std::function; using Child = std::unique_ptr; using Children = std::vector; +using Color = ftxui::screen::Color; // --- Layout ---- Element vbox(Children); @@ -58,7 +58,6 @@ TAKE_ANY_ARGS(vbox) TAKE_ANY_ARGS(hbox) TAKE_ANY_ARGS(dbox) -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom #endif /* end of include guard: FTXUI_DOM_ELEMENTS_HPP */ diff --git a/ftxui/include/ftxui/dom/node.hpp b/ftxui/include/ftxui/dom/node.hpp index 7f92be7..ecd4b99 100644 --- a/ftxui/include/ftxui/dom/node.hpp +++ b/ftxui/include/ftxui/dom/node.hpp @@ -1,15 +1,14 @@ -#ifndef DOM_NODE_HPP -#define DOM_NODE_HPP +#ifndef FTXUI_DOM_NODE_HPP +#define FTXUI_DOM_NODE_HPP #include #include -#include "ftxui/requirement.hpp" -#include "ftxui/screen.hpp" -#include "ftxui/box.hpp" +#include "ftxui/dom/box.hpp" +#include "ftxui/dom/requirement.hpp" +#include "ftxui/screen/screen.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class Node { public: @@ -28,7 +27,7 @@ class Node { virtual void SetBox(Box box); // Step 3: Draw this element. - virtual void Render(Screen& screen); + virtual void Render(screen::Screen& screen); std::vector> children; protected: @@ -36,9 +35,8 @@ class Node { Box box_; }; -void Render(Screen& screen, Node* node); +void Render(screen::Screen& screen, Node* node); -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom -#endif /* end of include guard: DOM_NODE_HPP */ +#endif /* end of include guard: FTXUI_DOM_NODE_HPP */ diff --git a/ftxui/include/ftxui/requirement.hpp b/ftxui/include/ftxui/dom/requirement.hpp similarity index 69% rename from ftxui/include/ftxui/requirement.hpp rename to ftxui/include/ftxui/dom/requirement.hpp index f5a9605..d91c5ba 100644 --- a/ftxui/include/ftxui/requirement.hpp +++ b/ftxui/include/ftxui/dom/requirement.hpp @@ -1,7 +1,7 @@ -#ifndef FTXUI_REQUIREMENT_HPP -#define FTXUI_REQUIREMENT_HPP +#ifndef FTXUI_DOM_REQUIREMENT_HPP +#define FTXUI_DOM_REQUIREMENT_HPP -namespace ftxui { +namespace ftxui::dom { struct Requirement { // The required size to fully draw the element. @@ -11,6 +11,6 @@ struct Requirement { struct { int x = 0; int y = 0; } flex; }; -}; // namespace ftxui +}; // namespace ftxui::dom #endif /* end of include guard: FTXUI_REQUIREMENT_HPP */ diff --git a/ftxui/include/ftxui/color.hpp b/ftxui/include/ftxui/screen/color.hpp similarity index 80% rename from ftxui/include/ftxui/color.hpp rename to ftxui/include/ftxui/screen/color.hpp index 4853c5c..43e5b42 100644 --- a/ftxui/include/ftxui/color.hpp +++ b/ftxui/include/ftxui/screen/color.hpp @@ -1,9 +1,9 @@ -#ifndef FTXUI_COLOR_H_ -#define FTXUI_COLOR_H_ +#ifndef FTXUI_SCREEN_COLOR +#define FTXUI_SCREEN_COLOR #include -namespace ftxui { +namespace ftxui::screen { enum class Color : uint8_t { // --- Transparent ----- @@ -35,6 +35,6 @@ enum class Color : uint8_t { YellowLight = 93, }; -}; // namespace ftxui +}; // namespace ftxui::screen #endif /* end of include guard: FTXUI_COLOR_H_ */ diff --git a/ftxui/include/ftxui/screen.hpp b/ftxui/include/ftxui/screen/screen.hpp similarity index 82% rename from ftxui/include/ftxui/screen.hpp rename to ftxui/include/ftxui/screen/screen.hpp index 6a858b5..04829d4 100644 --- a/ftxui/include/ftxui/screen.hpp +++ b/ftxui/include/ftxui/screen/screen.hpp @@ -1,17 +1,18 @@ -#ifndef FTXUI_SCREEN -#define FTXUI_SCREEN +#ifndef FTXUI_SCREEN_SCREEN +#define FTXUI_SCREEN_SCREEN #include #include #include -#include +#include "ftxui/screen/color.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class Node; } +namespace ftxui::screen { + struct Pixel { wchar_t character = U' '; bool blink = false; @@ -55,6 +56,6 @@ class Screen { std::vector> pixels_; }; -}; // namespace ftxui +}; // namespace ftxui::screen -#endif /* end of include guard: FTXUI_SCREEN */ +#endif /* end of include guard: FTXUI_SCREEN_SCREEN */ diff --git a/ftxui/src/ftxui/component/component.cpp b/ftxui/src/ftxui/component/component.cpp index 9b40d59..b796031 100644 --- a/ftxui/src/ftxui/component/component.cpp +++ b/ftxui/src/ftxui/component/component.cpp @@ -2,8 +2,7 @@ #include "ftxui/component/delegate.hpp" #include -namespace ftxui { -namespace component { +namespace ftxui::component { Component::Component(Delegate* delegate) { delegate_ = delegate; @@ -55,5 +54,4 @@ Component* Component::Parent() { return parent_delegate->component(); } -} // namespace component -} // namespace ftxui +} // namespace ftxui::component diff --git a/ftxui/src/ftxui/component/component_direction.cpp b/ftxui/src/ftxui/component/component_direction.cpp index 74dc846..3a49e5f 100644 --- a/ftxui/src/ftxui/component/component_direction.cpp +++ b/ftxui/src/ftxui/component/component_direction.cpp @@ -1,7 +1,6 @@ #include "ftxui/component/component_direction.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { ComponentDirection::ComponentDirection(Delegate* delegate) : Component(delegate), active_child_(nullptr) {} @@ -27,5 +26,4 @@ void ComponentDirection::Focus(Component* child) { active_child_ = child; } -} // namespace component -} // namespace ftxui +} // namespace ftxui::Component diff --git a/ftxui/src/ftxui/component/component_horizontal.cpp b/ftxui/src/ftxui/component/component_horizontal.cpp index b9079cc..fb83e77 100644 --- a/ftxui/src/ftxui/component/component_horizontal.cpp +++ b/ftxui/src/ftxui/component/component_horizontal.cpp @@ -1,7 +1,6 @@ #include "ftxui/component/component_horizontal.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { ComponentHorizontal::ComponentHorizontal(Delegate* delegate) : ComponentDirection(delegate) {} @@ -28,5 +27,4 @@ bool ComponentHorizontal::HandleDirection(Event event) { return false; } -} // namespace component -} // namespace ftxui +} // namespace ftxui::component diff --git a/ftxui/src/ftxui/component/component_vertical.cpp b/ftxui/src/ftxui/component/component_vertical.cpp index ec0141f..2b3b009 100644 --- a/ftxui/src/ftxui/component/component_vertical.cpp +++ b/ftxui/src/ftxui/component/component_vertical.cpp @@ -1,7 +1,6 @@ #include "ftxui/component/component_vertical.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { ComponentVertical::ComponentVertical(Delegate* delegate) : ComponentDirection(delegate) {} @@ -28,5 +27,4 @@ bool ComponentVertical::HandleDirection(Event event) { return false; } -} // namespace component -} // namespace ftxui +} // namespace ftxui::component diff --git a/ftxui/src/ftxui/event.cpp b/ftxui/src/ftxui/component/event.cpp similarity index 90% rename from ftxui/src/ftxui/event.cpp rename to ftxui/src/ftxui/component/event.cpp index ed232f2..b032c2b 100644 --- a/ftxui/src/ftxui/event.cpp +++ b/ftxui/src/ftxui/component/event.cpp @@ -1,6 +1,6 @@ -#include "ftxui/event.hpp" +#include "ftxui/component/event.hpp" -namespace ftxui { +namespace ftxui::component { constexpr int ESC = int(27); @@ -34,4 +34,4 @@ Event Event::F10{ESC, '[', '2', '1', '~'}; Event Event::F11{ESC, '[', '2', '1', '~'}; // Same as F10 ? Event Event::F12{ESC, '[', '2', '4', '~'}; -} // namespace ftxui +} // namespace ftxui::component diff --git a/ftxui/src/ftxui/component/input.cpp b/ftxui/src/ftxui/component/input.cpp index adfd82a..d80863e 100644 --- a/ftxui/src/ftxui/component/input.cpp +++ b/ftxui/src/ftxui/component/input.cpp @@ -1,8 +1,7 @@ #include "ftxui/component/input.hpp" #include "ftxui/util/string.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { Input::Input(Delegate* delegate): Component(delegate) {} Input::~Input() {} @@ -76,5 +75,4 @@ bool Input::OnEvent(Event event) { return false; } -} // namespace component -} // namespace ftxui +} // namespace ftxui::component diff --git a/ftxui/src/ftxui/component/menu.cpp b/ftxui/src/ftxui/component/menu.cpp index 0c9ff32..d1c0a99 100644 --- a/ftxui/src/ftxui/component/menu.cpp +++ b/ftxui/src/ftxui/component/menu.cpp @@ -2,8 +2,7 @@ #include #include -namespace ftxui { -namespace component { +namespace ftxui::component { Menu::Menu(Delegate* delegate) : Component(delegate) {} @@ -50,5 +49,4 @@ bool Menu::OnEvent(Event event) { return false; } -} // namespace component -} // namespace ftxui +} // namespace ftxui::component diff --git a/ftxui/src/ftxui/screen_interactive.cpp b/ftxui/src/ftxui/component/screen_interactive.cpp similarity index 81% rename from ftxui/src/ftxui/screen_interactive.cpp rename to ftxui/src/ftxui/component/screen_interactive.cpp index 8ba98fb..7111e23 100644 --- a/ftxui/src/ftxui/screen_interactive.cpp +++ b/ftxui/src/ftxui/component/screen_interactive.cpp @@ -1,48 +1,48 @@ -#include "ftxui/screen_interactive.hpp" +#include "ftxui/component/screen_interactive.hpp" -#include "ftxui/component/component.hpp" -#include "ftxui/component/delegate.hpp" -#include "ftxui/terminal.hpp" -#include #include #include #include +#include +#include "ftxui/component/component.hpp" +#include "ftxui/component/delegate.hpp" +#include "ftxui/terminal.hpp" -namespace ftxui { +namespace ftxui::component { namespace { - constexpr int ESC = 27; - constexpr int WAT = 195; - constexpr int WAT2 = 194; - constexpr int WATWAIT = 91; +constexpr int ESC = 27; +constexpr int WAT = 195; +constexpr int WAT2 = 194; +constexpr int WATWAIT = 91; - Event GetEvent() { - int v1 = getchar(); - if (v1 == ESC) { - int v2 = getchar(); - int v3 = getchar(); +Event GetEvent() { + int v1 = getchar(); + if (v1 == ESC) { + int v2 = getchar(); + int v3 = getchar(); - //if (v2 == WATWAIT) { - //int v4 = getchar(); - //int v5 = getchar(); - //return Event{v1, v2, v3, v4, v5}; - //} - return Event{v1, v2, v3}; - } + // if (v2 == WATWAIT) { + // int v4 = getchar(); + // int v5 = getchar(); + // return Event{v1, v2, v3, v4, v5}; + //} + return Event{v1, v2, v3}; + } - if (v1 == WAT) { - int v2 = getchar(); - return Event{v1, v2}; - } + if (v1 == WAT) { + int v2 = getchar(); + return Event{v1, v2}; + } - if (v1 == WAT2) { - int v2 = getchar(); - return Event{v1, v2}; - } + if (v1 == WAT2) { + int v2 = getchar(); + return Event{v1, v2}; + } - return Event{v1}; - }; + return Event{v1}; }; +}; // namespace class ScreenInteractive::Delegate : public component::Delegate { public: @@ -67,7 +67,6 @@ class ScreenInteractive::Delegate : public component::Delegate { void OnEvent(Event event) { component_->OnEvent(event); } - std::vector children() override { std::vector ret; for (auto& it : child_) @@ -129,12 +128,14 @@ void ScreenInteractive::Loop() { tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_new); Draw(); - while(!quit_) { + while (!quit_) { delegate_->OnEvent(GetEvent()); Clear(); Draw(); - } while(!quit_); - //std::cout << std::endl; + } + while (!quit_) + ; + // std::cout << std::endl; // Restore the old terminal configuration. tcsetattr(STDIN_FILENO, TCSANOW, &terminal_configuration_old); @@ -144,7 +145,7 @@ void ScreenInteractive::Draw() { auto document = delegate_->component()->Render(); size_t dimx; size_t dimy; - switch(dimension_) { + switch (dimension_) { case Dimension::Fixed: break; case Dimension::TerminalOutput: @@ -162,7 +163,8 @@ void ScreenInteractive::Draw() { if (dimx != dimx_ || dimy != dimy_) { dimx_ = dimx; dimy_ = dimy; - pixels_ = std::vector>(dimy, std::vector(dimx)); + pixels_ = std::vector>( + dimy, std::vector(dimx)); } Render(*this, document.get()); @@ -182,4 +184,4 @@ std::function ScreenInteractive::ExitLoopClosure() { return [this]() { quit_ = true; }; } -} // namespace ftxui +} // namespace ftxui::component. diff --git a/ftxui/src/ftxui/component/toggle.cpp b/ftxui/src/ftxui/component/toggle.cpp index 39f12e7..a2d04c3 100644 --- a/ftxui/src/ftxui/component/toggle.cpp +++ b/ftxui/src/ftxui/component/toggle.cpp @@ -1,7 +1,6 @@ #include "ftxui/component/toggle.hpp" -namespace ftxui { -namespace component { +namespace ftxui::component { Toggle::Toggle(Delegate* delegate) : Component(delegate) {} @@ -41,5 +40,4 @@ bool Toggle::OnEvent(Event event) { return false; } -} // namespace component -} // namespace ftxui +} // namespace ftxui::component diff --git a/ftxui/src/ftxui/dom/blink.cpp b/ftxui/src/ftxui/dom/blink.cpp index a24f20a..4a6aa34 100644 --- a/ftxui/src/ftxui/dom/blink.cpp +++ b/ftxui/src/ftxui/dom/blink.cpp @@ -1,15 +1,14 @@ #include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class Blink : public NodeDecorator { public: Blink(Children children) : NodeDecorator(std::move(children)) {} ~Blink() override {} - void Render(Screen& screen) override { + void Render(screen::Screen& screen) override { Node::Render(screen); for (int y = box_.top; y <= box_.bottom; ++y) { for (int x = box_.left; x <= box_.right; ++x) { @@ -23,5 +22,4 @@ std::unique_ptr blink(Child child) { return std::make_unique(unpack(std::move(child))); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/bold.cpp b/ftxui/src/ftxui/dom/bold.cpp index 49c214a..8ba87f5 100644 --- a/ftxui/src/ftxui/dom/bold.cpp +++ b/ftxui/src/ftxui/dom/bold.cpp @@ -1,15 +1,14 @@ #include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class Bold : public NodeDecorator { public: Bold(Children children) : NodeDecorator(std::move(children)) {} ~Bold() override {} - void Render(Screen& screen) override { + void Render(screen::Screen& screen) override { for (int y = box_.top; y <= box_.bottom; ++y) { for (int x = box_.left; x <= box_.right; ++x) { screen.PixelAt(x,y).bold = true; @@ -23,5 +22,4 @@ std::unique_ptr bold(Child child) { return std::make_unique(unpack(std::move(child))); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/color.cpp b/ftxui/src/ftxui/dom/color.cpp index cf63704..bace671 100644 --- a/ftxui/src/ftxui/dom/color.cpp +++ b/ftxui/src/ftxui/dom/color.cpp @@ -1,15 +1,14 @@ #include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class BgColor : public NodeDecorator { public: BgColor(Children children, Color color) : NodeDecorator(std::move(children)), color_(color) {} - void Render(Screen& screen) override { + void Render(screen::Screen& screen) override { for (int y = box_.top; y <= box_.bottom; ++y) { for (int x = box_.left; x <= box_.right; ++x) { screen.PixelAt(x, y).background_color = color_; @@ -27,7 +26,7 @@ class FgColor : public NodeDecorator { : NodeDecorator(std::move(children)), color_(color) {} ~FgColor() override {} - void Render(Screen& screen) override { + void Render(screen::Screen& screen) override { for (int y = box_.top; y <= box_.bottom; ++y) { for (int x = box_.left; x <= box_.right; ++x) { screen.PixelAt(x, y).foreground_color = color_; @@ -59,5 +58,4 @@ Decorator bgcolor(Color c) { }; } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/composite_decorator.cpp b/ftxui/src/ftxui/dom/composite_decorator.cpp index 2f64105..eda0155 100644 --- a/ftxui/src/ftxui/dom/composite_decorator.cpp +++ b/ftxui/src/ftxui/dom/composite_decorator.cpp @@ -1,8 +1,7 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { std::unique_ptr hcenter(Element child) { return hbox(filler(), std::move(child), filler()); @@ -16,5 +15,4 @@ std::unique_ptr center(Element child) { return hcenter(vcenter(std::move(child))); } -} // namespace dom -} // namespace ftxui +} // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/dbox.cpp b/ftxui/src/ftxui/dom/dbox.cpp index 3704387..9639da4 100644 --- a/ftxui/src/ftxui/dom/dbox.cpp +++ b/ftxui/src/ftxui/dom/dbox.cpp @@ -1,8 +1,7 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class DBox : public Node { public: @@ -33,5 +32,4 @@ std::unique_ptr dbox(Children children) { return std::make_unique(std::move(children)); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/dim.cpp b/ftxui/src/ftxui/dom/dim.cpp index ef7cdb9..e72b9d6 100644 --- a/ftxui/src/ftxui/dom/dim.cpp +++ b/ftxui/src/ftxui/dom/dim.cpp @@ -1,8 +1,9 @@ #include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using ftxui::screen::Screen; class Dim : public NodeDecorator { public: @@ -23,5 +24,4 @@ std::unique_ptr dim(Child child) { return std::make_unique(unpack(std::move(child))); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/flex.cpp b/ftxui/src/ftxui/dom/flex.cpp index 7741a77..fbd79ba 100644 --- a/ftxui/src/ftxui/dom/flex.cpp +++ b/ftxui/src/ftxui/dom/flex.cpp @@ -1,8 +1,7 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class Flex : public Node { public: @@ -35,5 +34,4 @@ std::unique_ptr flex(Element child) { return std::make_unique(std::move(child)); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/frame.cpp b/ftxui/src/ftxui/dom/frame.cpp index 1bbe056..41fcedb 100644 --- a/ftxui/src/ftxui/dom/frame.cpp +++ b/ftxui/src/ftxui/dom/frame.cpp @@ -1,8 +1,9 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using namespace ftxui::screen; static wchar_t charset[] = L"┌┐└┘─│┬┴┤├"; @@ -94,5 +95,4 @@ Decorator boxed() { }; } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/gauge.cpp b/ftxui/src/ftxui/dom/gauge.cpp index 603d5e2..c6ccebe 100644 --- a/ftxui/src/ftxui/dom/gauge.cpp +++ b/ftxui/src/ftxui/dom/gauge.cpp @@ -1,8 +1,9 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using namespace ftxui::screen; static wchar_t charset[] = L" ▏▎▍▌▋▊▉█"; @@ -35,5 +36,4 @@ std::unique_ptr gauge(float progress) { return std::make_unique(progress); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/gauge_test.cpp b/ftxui/src/ftxui/dom/gauge_test.cpp index ef78951..9b7b2b8 100644 --- a/ftxui/src/ftxui/dom/gauge_test.cpp +++ b/ftxui/src/ftxui/dom/gauge_test.cpp @@ -1,9 +1,9 @@ #include "ftxui/dom/elements.hpp" -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "gtest/gtest.h" -namespace ftxui { -namespace dom { +using namespace ftxui::screen; +using namespace ftxui::dom; TEST(GaugeTest, zero) { auto root = gauge(0); @@ -29,6 +29,3 @@ TEST(GaugeTest, one) { EXPECT_EQ("███████████", screen.ToString()); } - -} // namespace dom -} // namespace ftxui diff --git a/ftxui/src/ftxui/dom/hbox.cpp b/ftxui/src/ftxui/dom/hbox.cpp index a96049e..6cbc202 100644 --- a/ftxui/src/ftxui/dom/hbox.cpp +++ b/ftxui/src/ftxui/dom/hbox.cpp @@ -1,8 +1,7 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class HBox : public Node { public: @@ -64,5 +63,4 @@ std::unique_ptr hbox(Children children) { return std::make_unique(std::move(children)); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/hbox_test.cpp b/ftxui/src/ftxui/dom/hbox_test.cpp index 859852e..a81723f 100644 --- a/ftxui/src/ftxui/dom/hbox_test.cpp +++ b/ftxui/src/ftxui/dom/hbox_test.cpp @@ -1,9 +1,9 @@ #include "ftxui/dom/elements.hpp" -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "gtest/gtest.h" -namespace ftxui { -namespace dom { +using namespace ftxui::screen; +using namespace ftxui::dom; TEST(HBoxTest, ScreenSmaller1) { auto root = hbox( @@ -118,6 +118,3 @@ TEST(HBoxTest, ScreenBigger2Flex) { EXPECT_EQ("text_1 text_2", screen.ToString()); } - -}; // namespace dom -}; // namespace ftxui diff --git a/ftxui/src/ftxui/dom/inverted.cpp b/ftxui/src/ftxui/dom/inverted.cpp index 2e231ee..b1b3e92 100644 --- a/ftxui/src/ftxui/dom/inverted.cpp +++ b/ftxui/src/ftxui/dom/inverted.cpp @@ -1,8 +1,9 @@ #include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using ftxui::screen::Screen; class Inverted : public NodeDecorator { public: @@ -23,5 +24,4 @@ std::unique_ptr inverted(Child child) { return std::make_unique(unpack(std::move(child))); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/node.cpp b/ftxui/src/ftxui/dom/node.cpp index 648c121..5e01589 100644 --- a/ftxui/src/ftxui/dom/node.cpp +++ b/ftxui/src/ftxui/dom/node.cpp @@ -1,7 +1,8 @@ #include "ftxui/dom/node.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using ftxui::screen::Screen; Node::Node() {} Node::Node(std::vector> children) @@ -39,5 +40,4 @@ void Render(Screen& screen, Node* node) { node->Render(screen); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/node_decorator.cpp b/ftxui/src/ftxui/dom/node_decorator.cpp index 8ba35a9..ec21706 100644 --- a/ftxui/src/ftxui/dom/node_decorator.cpp +++ b/ftxui/src/ftxui/dom/node_decorator.cpp @@ -1,7 +1,6 @@ #include "ftxui/dom/node_decorator.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { void NodeDecorator::ComputeRequirement() { Node::ComputeRequirement(); @@ -13,5 +12,4 @@ void NodeDecorator::SetBox(Box box) { children[0]->SetBox(box); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/node_decorator.hpp b/ftxui/src/ftxui/dom/node_decorator.hpp index cc53ef3..23e6a34 100644 --- a/ftxui/src/ftxui/dom/node_decorator.hpp +++ b/ftxui/src/ftxui/dom/node_decorator.hpp @@ -4,8 +4,7 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { // Helper class. class NodeDecorator : public Node { @@ -16,7 +15,6 @@ class NodeDecorator : public Node { void SetBox(Box box) override; }; -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom #endif /* end of include guard: FTXUI_DOM_NODE_DECORATOR_H_ */ diff --git a/ftxui/src/ftxui/dom/separator.cpp b/ftxui/src/ftxui/dom/separator.cpp index b298e06..fcbfb5b 100644 --- a/ftxui/src/ftxui/dom/separator.cpp +++ b/ftxui/src/ftxui/dom/separator.cpp @@ -1,7 +1,8 @@ #include "ftxui/dom/node.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using ftxui::screen::Screen; class Separator : public Node { public: @@ -34,5 +35,4 @@ std::unique_ptr separator() { return std::make_unique(); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/text.cpp b/ftxui/src/ftxui/dom/text.cpp index 218ed06..6b38093 100644 --- a/ftxui/src/ftxui/dom/text.cpp +++ b/ftxui/src/ftxui/dom/text.cpp @@ -1,7 +1,8 @@ #include "ftxui/dom/node.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using ftxui::screen::Screen; class Text : public Node { public: @@ -33,5 +34,4 @@ std::unique_ptr text(std::wstring text) { return std::make_unique(text); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/text_test.cpp b/ftxui/src/ftxui/dom/text_test.cpp index f18f99d..4765496 100644 --- a/ftxui/src/ftxui/dom/text_test.cpp +++ b/ftxui/src/ftxui/dom/text_test.cpp @@ -1,9 +1,9 @@ #include "ftxui/dom/elements.hpp" -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "gtest/gtest.h" -namespace ftxui { -namespace dom { +using namespace ftxui::screen; +using namespace ftxui::dom; TEST(TextTest, ScreenHeightSmaller) { auto element = text(L"test"); @@ -44,6 +44,3 @@ TEST(TextTest, ScreenBigger2) { EXPECT_EQ("test \n ", screen.ToString()); } - -}; // namespace dom -}; // namespace ftxui diff --git a/ftxui/src/ftxui/dom/underlined.cpp b/ftxui/src/ftxui/dom/underlined.cpp index b2cd7f9..32fe3ca 100644 --- a/ftxui/src/ftxui/dom/underlined.cpp +++ b/ftxui/src/ftxui/dom/underlined.cpp @@ -1,8 +1,9 @@ #include "ftxui/dom/node_decorator.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { + +using ftxui::screen::Screen; class Underlined : public NodeDecorator { public: @@ -23,5 +24,4 @@ std::unique_ptr underlined(Child child) { return std::make_unique(unpack(std::move(child))); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/util.cpp b/ftxui/src/ftxui/dom/util.cpp index afdb3de..3ca4b78 100644 --- a/ftxui/src/ftxui/dom/util.cpp +++ b/ftxui/src/ftxui/dom/util.cpp @@ -1,7 +1,6 @@ #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { Element nothing(Element element) { return std::move(element); @@ -24,5 +23,4 @@ Element operator|(Element e, Decorator d) { return d(std::move(e)); } -} // namespace dom -} // namespace ftxui +} // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/vbox.cpp b/ftxui/src/ftxui/dom/vbox.cpp index 3f504e6..ab2e2b7 100644 --- a/ftxui/src/ftxui/dom/vbox.cpp +++ b/ftxui/src/ftxui/dom/vbox.cpp @@ -1,8 +1,7 @@ #include "ftxui/dom/node.hpp" #include "ftxui/dom/elements.hpp" -namespace ftxui { -namespace dom { +namespace ftxui::dom { class VBox : public Node { public: @@ -64,5 +63,4 @@ std::unique_ptr vbox(Children children) { return std::make_unique(std::move(children)); } -}; // namespace dom -}; // namespace ftxui +}; // namespace ftxui::dom diff --git a/ftxui/src/ftxui/dom/vbox_test.cpp b/ftxui/src/ftxui/dom/vbox_test.cpp index 6f24fb8..1ac7d39 100644 --- a/ftxui/src/ftxui/dom/vbox_test.cpp +++ b/ftxui/src/ftxui/dom/vbox_test.cpp @@ -1,9 +1,9 @@ #include "ftxui/dom/elements.hpp" -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "gtest/gtest.h" -namespace ftxui { -namespace dom { +using namespace ftxui::screen; +using namespace ftxui::dom; TEST(VBoxTest, ScreenSmaller1) { auto root = vbox(text(L"text_1"), text(L"text_2")); @@ -66,6 +66,3 @@ TEST(VBoxTest, ScreenBigger2Flex) { EXPECT_EQ("text_1\n \n \ntext_2", screen.ToString()); } - -}; // namespace dom -}; // namespace ftxui diff --git a/ftxui/src/ftxui/screen.cpp b/ftxui/src/ftxui/screen/screen.cpp similarity index 70% rename from ftxui/src/ftxui/screen.cpp rename to ftxui/src/ftxui/screen/screen.cpp index 27cc923..30f0bfb 100644 --- a/ftxui/src/ftxui/screen.cpp +++ b/ftxui/src/ftxui/screen/screen.cpp @@ -1,26 +1,46 @@ -#include "ftxui/screen.hpp" +#include "ftxui/screen/screen.hpp" #include "ftxui/dom/node.hpp" #include "ftxui/terminal.hpp" #include "ftxui/util/string.hpp" #include -namespace ftxui { +namespace ftxui::screen { + +static const wchar_t* BOLD_SET = L"\e[1m"; +static const wchar_t* BOLD_RESET = L"\e[22m"; // Can't use 21 here. + +static const wchar_t* DIM_SET = L"\e[2m"; +static const wchar_t* DIM_RESET = L"\e[22m"; + +static const wchar_t* UNDERLINED_SET = L"\e[4m"; +static const wchar_t* UNDERLINED_RESET = L"\e[24m"; + +static const wchar_t* BLINK_SET = L"\e[5m"; +static const wchar_t* BLINK_RESET = L"\e[25m"; + +static const wchar_t* INVERTED_SET = L"\e[7m"; +static const wchar_t* INVERTED_RESET = L"\e[27m"; Screen::Screen(size_t dimx, size_t dimy) : dimx_(dimx), dimy_(dimy), pixels_(dimy, std::vector(dimx)) {} void UpdatePixelStyle(std::wstringstream& ss, Pixel& previous, Pixel& next) { if (next.bold != previous.bold) - ss << (next.bold ? L"\e[1m" : L"\e[22m"); // Can't use 21 here. + ss << (next.bold ? BOLD_SET : BOLD_RESET); + if (next.dim != previous.dim) - ss << (next.dim ? L"\e[2m" : L"\e[22m"); + ss << (next.dim ? DIM_SET : DIM_RESET); + if (next.underlined != previous.underlined) - ss << (next.underlined ? L"\e[4m" : L"\e[24m"); + ss << (next.underlined ? UNDERLINED_SET : UNDERLINED_RESET); + if (next.blink != previous.blink) - ss << (next.blink ? L"\e[5m" : L"\e[25m"); + ss << (next.blink ? BLINK_SET : BLINK_RESET); + if (next.inverted != previous.inverted) - ss << (next.inverted ? L"\e[7m" : L"\e[27m"); + ss << (next.inverted ? INVERTED_SET : INVERTED_RESET); + if (next.foreground_color != previous.foreground_color || next.background_color != previous.background_color) { ss << L"\e[" + to_wstring(std::to_string((uint8_t)next.foreground_color)) + L"m"; @@ -84,4 +104,4 @@ void Screen::Clear() { std::vector(dimx_, Pixel())); } -}; // namespace ftxui +}; // namespace ftxui::screen