Add html_like example. Improve take_any_args.

This commit is contained in:
Arthur Sonzogni 2019-01-23 02:16:00 +01:00
parent ce7867ab03
commit 1e92db7ec0
5 changed files with 94 additions and 13 deletions

View File

@ -20,3 +20,4 @@ example(size)
example(vbox_hbox)
example(hflow)
example(paragraph)
example(html_like)

View File

@ -0,0 +1,52 @@
#include <chrono>
#include <iostream>
#include <thread>
#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;
}

View File

@ -1,11 +1,24 @@
#include <type_traits>
template <class T>
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 <class... Args>
Elements unpack(Args... args) {
using T = std::common_type_t<Args...>;
std::vector<T> vec;
(vec.push_back(std::forward<Args>(args)), ...);
std::vector<Element> vec;
(Merge(vec, std::move(args)), ...);
return vec;
}
@ -14,5 +27,4 @@ Elements unpack(Args... args) {
template <class... Args> \
Element container(Args... children) { \
return container(unpack(std::forward<Args>(children)...)); \
} \
}

View File

@ -8,10 +8,8 @@ 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' '));
}
}
return output;
}

View File

@ -39,8 +39,26 @@ class Size : public Node {
void SetBox(Box box) override {
Node::SetBox(box);
if (constraint_ == LESS_THAN)
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);
}