From ce7867ab0380f27acb481980a7d491095fb8c7f1 Mon Sep 17 00:00:00 2001 From: Arthur Sonzogni Date: Wed, 23 Jan 2019 00:26:36 +0100 Subject: [PATCH] Add paragraph and fix hflow --- examples/dom/CMakeLists.txt | 1 + examples/dom/paragraph.cpp | 33 ++++++++++++++++++++++++++++ ftxui/CMakeLists.txt | 1 + ftxui/include/ftxui/dom/elements.hpp | 1 + ftxui/src/ftxui/dom/hflow.cpp | 10 ++++----- ftxui/src/ftxui/dom/paragraph.cpp | 18 +++++++++++++++ 6 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 examples/dom/paragraph.cpp create mode 100644 ftxui/src/ftxui/dom/paragraph.cpp diff --git a/examples/dom/CMakeLists.txt b/examples/dom/CMakeLists.txt index ee5e734..9131408 100644 --- a/examples/dom/CMakeLists.txt +++ b/examples/dom/CMakeLists.txt @@ -19,3 +19,4 @@ example(style_underlined) example(size) example(vbox_hbox) example(hflow) +example(paragraph) diff --git a/examples/dom/paragraph.cpp b/examples/dom/paragraph.cpp new file mode 100644 index 0000000..8c747c9 --- /dev/null +++ b/examples/dom/paragraph.cpp @@ -0,0 +1,33 @@ +#include "ftxui/screen/screen.hpp" +#include "ftxui/screen/string.hpp" +#include "ftxui/dom/elements.hpp" +#include + +int main(int argc, const char *argv[]) +{ + using namespace ftxui; + std::wstring p = LR"(In probability theory and statistics, Bayes' theorem (alternatively Bayes' law or Bayes' rule) describes the probability of an event, based on prior knowledge of conditions that might be related to the event. For example, if cancer is related to age, then, using Bayes' theorem, a person's age can be used to more accurately assess the probability that they have cancer, compared to the assessment of the probability of cancer made without knowledge of the person's age. One of the many applications of Bayes' theorem is Bayesian inference, a particular approach to statistical inference. When applied, the probabilities involved in Bayes' theorem may have different probability interpretations. With the Bayesian probability interpretation the theorem expresses how a subjective degree of belief should rationally change to account for availability of related evidence. Bayesian inference is fundamental to Bayesian statistics.)"; + + auto document = + vbox( + hbox( + hflow(paragraph(p)) | border, + hflow(paragraph(p)) | border, + hflow(paragraph(p)) | border + ) | flex, + hbox( + hflow(paragraph(p)) | border, + hflow(paragraph(p)) | border + ) | flex, + hbox( + hflow(paragraph(p)) | border + ) | flex + ); + + auto screen = Screen::TerminalFullscreen(); + Render(screen, document.get()); + std::cout << screen.ToString(); + getchar(); + + return 0; +} diff --git a/ftxui/CMakeLists.txt b/ftxui/CMakeLists.txt index d3c75dc..7cf89e5 100644 --- a/ftxui/CMakeLists.txt +++ b/ftxui/CMakeLists.txt @@ -23,6 +23,7 @@ add_library(dom src/ftxui/dom/inverted.cpp src/ftxui/dom/node.cpp src/ftxui/dom/node_decorator.cpp + src/ftxui/dom/paragraph.cpp src/ftxui/dom/separator.cpp src/ftxui/dom/size.cpp src/ftxui/dom/spinner.cpp diff --git a/ftxui/include/ftxui/dom/elements.hpp b/ftxui/include/ftxui/dom/elements.hpp index d4b56c0..8a2c35d 100644 --- a/ftxui/include/ftxui/dom/elements.hpp +++ b/ftxui/include/ftxui/dom/elements.hpp @@ -19,6 +19,7 @@ Element gauge(float ratio); Element border(Element); Element window(Element title, Element content); Element spinner(int charset_index, size_t image_index); +Elements paragraph(std::wstring text); // Use inside hflow(). Split by space. // -- Decorator --- Element bold(Element); diff --git a/ftxui/src/ftxui/dom/hflow.cpp b/ftxui/src/ftxui/dom/hflow.cpp index 2463709..05a5370 100644 --- a/ftxui/src/ftxui/dom/hflow.cpp +++ b/ftxui/src/ftxui/dom/hflow.cpp @@ -36,18 +36,18 @@ class HFlow : public Node { } // Does the current row big enough to contain the element? - if (y + requirement.min.y > box.y_max) + if (y + requirement.min.y > box.y_max + 1) break; // No? Ignore the element. Box children_box; children_box.x_min = x; - children_box.x_max = x + requirement.min.x; + children_box.x_max = x + requirement.min.x - 1; children_box.y_min = y; - children_box.y_max = y + requirement.min.y; + children_box.y_max = y + requirement.min.y - 1; child->SetBox(children_box); - x = x + requirement.min.x + 1; - y_next = std::max(y_next, y + requirement.min.y + 1); + x = x + requirement.min.x; + y_next = std::max(y_next, y + requirement.min.y); } } }; diff --git a/ftxui/src/ftxui/dom/paragraph.cpp b/ftxui/src/ftxui/dom/paragraph.cpp new file mode 100644 index 0000000..5cfcfa5 --- /dev/null +++ b/ftxui/src/ftxui/dom/paragraph.cpp @@ -0,0 +1,18 @@ +#include +#include "ftxui/dom/elements.hpp" + +namespace ftxui { + +Elements paragraph(std::wstring the_text) { + Elements output; + 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; +} + +} // namespace ftxui