From 1e92db7ec01f39a7c16b458de250af75c0321881 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Wed, 23 Jan 2019 02:16:00 +0100 Subject: [PATCH] Add html_like example. Improve take_any_args. --- examples/dom/CMakeLists.txt | 1 + examples/dom/html_like.cpp | 52 +++++++++++++++++++++++ ftxui/include/ftxui/dom/take_any_args.hpp | 28 ++++++++---- ftxui/src/ftxui/dom/paragraph.cpp | 4 +- ftxui/src/ftxui/dom/size.cpp | 22 +++++++++- 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 examples/dom/html_like.cpp diff --git a/examples/dom/CMakeLists.txt b/examples/dom/CMakeLists.txt index 9131408..1aa7aa5 100644 --- a/examples/dom/CMakeLists.txt +++ b/examples/dom/CMakeLists.txt @@ -20,3 +20,4 @@ example(size) example(vbox_hbox) example(hflow) example(paragraph) +example(html_like) diff --git a/examples/dom/html_like.cpp b/examples/dom/html_like.cpp new file mode 100644 index 0000000..2a4f667 --- /dev/null +++ b/examples/dom/html_like.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include "ftxui/dom/elements.hpp" +#include "ftxui/screen/screen.hpp" +#include "ftxui/screen/string.hpp" + +int main(int argc, const char* argv[]) { + using namespace ftxui; + using namespace std::chrono_literals; + + auto img1 = []() { return text(L"img") | border; }; + auto img2 = []() { return vbox(text(L"big"), text(L"image")) | border; }; + + std::string reset_position; + for (int i = 0;; ++i) { + auto document = + hflow( + paragraph(L"Hello world! Here is an image:"), img1(), + paragraph(L" Here is a text "), text(L"underlined ") | underlined, + paragraph(L" Here is a text "), text(L"bold ") | bold, + paragraph(L"Hello world! Here is an image:"), img2(), + paragraph( + L"Le Lorem Ipsum est simplement du faux texte employé dans la " + L"composition et la mise en page avant impression. Le Lorem " + L"Ipsum est le faux texte standard de l'imprimerie depuis les " + L"années 1500, quand un imprimeur anonyme assembla ensemble " + L"des morceaux de texte pour réaliser un livre spécimen de " + L"polices de texte. Il n'a pas fait que survivre cinq siècles, " + L"mais s'est aussi adapté à la bureautique informatique, sans " + L"que son contenu n'en soit modifié. Il a été popularisé dans " + L"les années 1960 grâce à la vente de feuilles Letraset " + L"contenant des passages du Lorem Ipsum, et, plus récemment, " + L"par son inclusion dans des applications de mise en page de " + L"texte, comme Aldus PageMaker."), + paragraph(L" Here is a text "), text(L"dim ") | dim, + paragraph(L"Hello world! Here is an image:"), img1(), + paragraph(L" Here is a text "), text(L"red ") | color(Color::Red), + paragraph(L" A spinner "), spinner(6, i / 10)) | + border; + + auto screen = Screen::TerminalFullscreen(); + Render(screen, document.get()); + std::cout << reset_position << screen.ToString() << std::flush; + + reset_position = screen.ResetPosition(); + + std::this_thread::sleep_for(0.01s); + } + + return 0; +} diff --git a/ftxui/include/ftxui/dom/take_any_args.hpp b/ftxui/include/ftxui/dom/take_any_args.hpp index cd97be6..6c80620 100644 --- a/ftxui/include/ftxui/dom/take_any_args.hpp +++ b/ftxui/include/ftxui/dom/take_any_args.hpp @@ -1,18 +1,30 @@ #include +template +void Merge(Elements& container, T t) {} + +template <> +inline void Merge(Elements& container, Element element) { + container.push_back(std::move(element)); +} + +template <> +inline void Merge(Elements& container, Elements elements) { + for(auto& element : elements) + container.push_back(std::move(element)); +} + // Turn a set of arguments into a vector. template Elements unpack(Args... args) { - using T = std::common_type_t; - std::vector vec; - (vec.push_back(std::forward(args)), ...); + std::vector vec; + (Merge(vec, std::move(args)), ...); return vec; } // Make |container| able to take any number of argments. #define TAKE_ANY_ARGS(container) \ - template \ - Element container(Args... children) { \ - return container(unpack(std::forward(children)...)); \ - } \ - +template \ +Element container(Args... children) { \ + return container(unpack(std::forward(children)...)); \ +} diff --git a/ftxui/src/ftxui/dom/paragraph.cpp b/ftxui/src/ftxui/dom/paragraph.cpp index 5cfcfa5..28e76a1 100644 --- a/ftxui/src/ftxui/dom/paragraph.cpp +++ b/ftxui/src/ftxui/dom/paragraph.cpp @@ -8,9 +8,7 @@ Elements paragraph(std::wstring the_text) { std::wstringstream ss(the_text); std::wstring word; while (std::getline(ss, word, L' ')) { - if (word.size()) { - output.push_back(text(word + L' ')); - } + output.push_back(text(word + L' ')); } return output; } diff --git a/ftxui/src/ftxui/dom/size.cpp b/ftxui/src/ftxui/dom/size.cpp index 26d93a0..0439551 100644 --- a/ftxui/src/ftxui/dom/size.cpp +++ b/ftxui/src/ftxui/dom/size.cpp @@ -39,8 +39,26 @@ class Size : public Node { void SetBox(Box box) override { Node::SetBox(box); - if (constraint_ == LESS_THAN) - box.x_max = std::min(box.x_min + value_ + 1, box.x_max); + + if (direction_ == WIDTH) { + switch(constraint_) { + case LESS_THAN: + case EQUAL: + box.x_max = std::min(box.x_min + value_ + 1, box.x_max); + break; + case GREATER_THAN: + break; + } + } else { + switch(constraint_) { + case LESS_THAN: + case EQUAL: + box.y_max = std::min(box.y_min + value_ + 1, box.y_max); + break; + case GREATER_THAN: + break; + } + } children[0]->SetBox(box); }