Use shared_ptr instead of unique_ptr for elements.

This allow users to pass it into initializer list. Then clang-format
will produce 'acceptable' indentations.

This fixes:
https://github.com/ArthurSonzogni/FTXUI/issues/18
This commit is contained in:
ArthurSonzogni
2020-05-20 20:36:47 +02:00
parent 0aabc258a9
commit e1a71d5b9f
53 changed files with 520 additions and 523 deletions

View File

@@ -11,41 +11,33 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
auto document = hbox({
window(text(L" main frame ") | hcenter,
vbox(
text(L"Line 1"),
text(L"Line 2"),
text(L"Line 3"),
vbox(
text(L"Line 4"),
text(L"Line 5"),
text(L"Line 6")
) | border,
hbox(
window(text(L"frame 2"),
vbox(
text(L"Line 4"),
text(L"Line 5"),
text(L"Line 6")
)
),
window(text(L"frame 3"),
vbox(
text(L"Line 7"),
text(L"Line 8"),
text(L"Line 9")
)
)
),
text(L"footer footer footer footer footer")
)
),
filler()
);
// clang-format on
vbox({
text(L"Line 1"),
text(L"Line 2"),
text(L"Line 3"),
vbox({
text(L"Line 4"),
text(L"Line 5"),
text(L"Line 6"),
}) | border,
hbox({
window(text(L"frame 2"), vbox({
text(L"Line 4"),
text(L"Line 5"),
text(L"Line 6"),
})),
window(text(L"frame 3"), vbox({
text(L"Line 7"),
text(L"Line 8"),
text(L"Line 9"),
})),
}),
text(L"footer footer footer footer footer"),
})),
filler(),
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());
std::cout << screen.ToString() << std::endl;

View File

@@ -9,19 +9,16 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
dbox(
vbox(
text(L"line_1"),
text(L"line_2"),
text(L"line_3"),
text(L"line_4"),
text(L"line_5")
) | border,
text(L"overlay") | border | center
);
// clang-format on
auto document = dbox({
vbox({
text(L"line_1"),
text(L"line_2"),
text(L"line_3"),
text(L"line_4"),
text(L"line_5"),
}) | border,
text(L"overlay") | border | center,
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -17,14 +17,11 @@ int main(int argc, const char* argv[]) {
for (float percentage = 0.0f; percentage <= 1.0f; percentage += 0.002f) {
std::wstring data_downloaded =
std::to_wstring(int(percentage * 5000)) + L"/5000";
// clang-format off
auto document =
hbox(
text(L"downloading:"),
gauge(percentage) | flex,
text(L" " + data_downloaded)
);
// clang-format on
auto document = hbox({
text(L"downloading:"),
gauge(percentage) | flex,
text(L" " + data_downloaded),
});
auto screen = Screen(100, 1);
Render(screen, document.get());
std::cout << reset_position << screen.ToString() << std::flush;

View File

@@ -45,23 +45,23 @@ int main(int argc, const char* argv[]) {
std::string reset_position;
for (int i = 0;; ++i) {
// clang-format off
auto document =
hbox(
vbox(
graph(std::ref(my_graph)), separator(),
graph(triangle) | inverted
) | flex,
separator(),
vbox(
graph(std::ref(my_graph)) | color(Color::BlueLight), separator(),
graph(std::ref(my_graph)) | color(Color::RedLight), separator(),
graph(std::ref(my_graph)) | color(Color::YellowLight)
) | flex
)
| border
| size(HEIGHT, GREATER_THAN, 40);
// clang-format on
hbox({
vbox({
graph(std::ref(my_graph)),
separator(),
graph(triangle) | inverted,
}) | flex,
separator(),
vbox({
graph(std::ref(my_graph)) | color(Color::BlueLight),
separator(),
graph(std::ref(my_graph)) | color(Color::RedLight),
separator(),
graph(std::ref(my_graph)) | color(Color::YellowLight),
}) | flex,
}) |
border | size(HEIGHT, GREATER_THAN, 40);
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -12,25 +12,35 @@ int main(int argc, const char* argv[]) {
using namespace ftxui;
auto make_box = [](size_t dimx, size_t dimy) {
std::wstring title = to_wstring(dimx) + L"x" + to_wstring(dimy);
// clang-format off
return window(text(title) | hcenter | bold,
text(L"content") | hcenter | dim) |
size(WIDTH, EQUAL, dimx) | size(HEIGHT, EQUAL, dimy);
// clang-format on
};
// clang-format off
auto document =
hflow(make_box(7, 7), make_box(7, 5), make_box(5, 7), make_box(10, 4),
make_box(10, 4), make_box(10, 4), make_box(10, 4), make_box(11, 4),
make_box(11, 4), make_box(11, 4), make_box(11, 4), make_box(12, 4),
make_box(12, 5), make_box(12, 4), make_box(13, 4), make_box(13, 3),
make_box(13, 3), make_box(10, 3))
| size(WIDTH, GREATER_THAN, 20)
| border
| size(HEIGHT, GREATER_THAN, 30)
| size(WIDTH, LESS_THAN, 50);
// clang-format on
auto style = size(WIDTH, GREATER_THAN, 20) | border |
size(HEIGHT, GREATER_THAN, 30) | size(WIDTH, LESS_THAN, 50);
auto document = hflow({
make_box(7, 7),
make_box(7, 5),
make_box(5, 7),
make_box(10, 4),
make_box(10, 4),
make_box(10, 4),
make_box(10, 4),
make_box(11, 4),
make_box(11, 4),
make_box(11, 4),
make_box(11, 4),
make_box(12, 4),
make_box(12, 5),
make_box(12, 4),
make_box(13, 4),
make_box(13, 3),
make_box(13, 3),
make_box(10, 3),
}) |
style;
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -15,11 +15,11 @@ int main(int argc, const char* argv[]) {
using namespace std::chrono_literals;
auto img1 = []() { return text(L"img") | border; };
auto img2 = []() { return vbox(text(L"big"), text(L"image")) | border; };
auto img2 = []() { return vbox({text(L"big"), text(L"image")}) | border; };
std::string reset_position;
for (int i = 0;; ++i) {
auto document =
auto document = //
hflow(
paragraph(L"Hello world! Here is an image:"), img1(),
paragraph(L" Here is a text "), text(L"underlined ") | underlined,

View File

@@ -49,49 +49,54 @@ int main(int argc, const char* argv[]) {
return text(to_wstring(number)) | size(WIDTH, EQUAL, 3);
};
// clang-format off
auto renderTask = [&](const Task& task) {
auto style = (task.downloaded == task.size) ? dim : bold;
return
hbox(
return hbox({
text(task.name) | style,
separator(),
to_text(task.downloaded),
text(L"/"),
to_text(task.size),
separator(),
gauge(task.downloaded / float(task.size))
);
gauge(task.downloaded / float(task.size)),
});
};
auto renderSummary = [&]() {
return
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)
)
);
auto 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),
});
return window(text(L" Summary "), summary);
};
auto render = [&](){
auto render = [&]() {
std::vector<Element> entries;
for(auto& task : displayed_task)
for (auto& task : displayed_task)
entries.push_back(renderTask(task));
return
vbox(
return vbox({
// List of tasks.
window(text(L" Task "),
vbox(std::move(entries))
),
window(text(L" Task "), vbox(std::move(entries))),
// Summary.
hbox(renderSummary(), filler())
);
hbox({
renderSummary(),
filler(),
}),
});
};
// clang-format on
auto updateModel = [&]() {
for (auto& task : displayed_task) {

View File

@@ -13,25 +13,22 @@ int main(int argc, const char* argv[]) {
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.)";
// clang-format off
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
);
// clang-format on
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::Create(Dimension::Full(), Dimension::Fit(document));
auto screen = Screen::Create(Dimension::Full(), Dimension::Full());
Render(screen, document.get());
std::cout << screen.ToString();
getchar();

View File

@@ -9,20 +9,18 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
text(L"left-column"),
separator(),
vbox(
center(text(L"right-top")) | flex,
separator(),
center(text(L"bottom-bottom"))
) | flex,
separator(),
text(L"right-column")
) | border;
// clang-format on
auto document = hbox({
text(L"left-column"),
separator(),
vbox({
center(text(L"right-top")) | flex,
separator(),
center(text(L"bottom-bottom")),
}) | flex,
separator(),
text(L"right-column"),
}) |
border;
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -10,7 +10,6 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto make_box = [](const std::wstring title) {
return
window(
@@ -26,7 +25,7 @@ int main(int argc, const char* argv[]) {
| size(WIDTH, EQUAL, x)
);
}
// clang-format on
auto document = hbox(std::move(content));
auto screen = Screen::Create(Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -20,17 +20,17 @@ int main(int argc, const char* argv[]) {
for (int i = 0; i < 22; ++i) {
if (i != 0)
entries.push_back(separator());
// clang-format off
entries.push_back(
hbox(
text(to_wstring(i)) | size(WIDTH, EQUAL, 2),
separator(),
spinner(i, index) | bold
)
);
// clang-format on
entries.push_back( //
hbox({
text(to_wstring(i)) | size(WIDTH, EQUAL, 2),
separator(),
spinner(i, index) | bold,
}));
}
auto document = hbox(vbox(std::move(entries)) | border, filler());
auto document = hbox({
vbox(std::move(entries)) | border,
filler(),
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());
std::cout << reset_position << screen.ToString() << std::flush;

View File

@@ -9,14 +9,12 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
text(L"This text is "),
text(L"blink") | blink,
text(L". Do you like it?")
);
// clang-format on
auto document = //
hbox({
text(L"This text is "),
text(L"blink") | blink,
text(L". Do you like it?"),
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -9,14 +9,12 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
text(L"This text is "),
text(L"bold") | bold,
text(L". Do you like it?")
);
// clang-format on
auto document = //
hbox({
text(L"This text is "),
text(L"bold") | bold,
text(L". Do you like it?"),
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -9,14 +9,12 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
text(L"This text is "),
text(L"dim") | dim,
text(L". Do you like it?")
);
// clang-format on
auto document = //
hbox({
text(L"This text is "),
text(L"dim") | dim,
text(L". Do you like it?"),
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -11,7 +11,7 @@ int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
hbox({
text(L"normal") , text(L" ") ,
text(L"bold") | bold , text(L" ") ,
text(L"dim") | dim , text(L" ") ,
@@ -19,8 +19,8 @@ int main(int argc, const char* argv[]) {
text(L"underlined")| underlined , text(L" ") ,
text(L"blink") | blink , text(L" ") ,
text(L"color") | color(Color::Blue) , text(L" ") ,
text(L"bgcolor") | bgcolor(Color::Blue)
);
text(L"bgcolor") | bgcolor(Color::Blue),
});
// clang-format on
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -9,14 +9,11 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
auto document = hbox({
text(L"This text is "),
text(L"inverted") | inverted,
text(L". Do you like it?")
);
// clang-format on
text(L". Do you like it?"),
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -9,14 +9,12 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
hbox(
text(L"This text is "),
text(L"underlined") | underlined,
text(L". Do you like it?")
);
// clang-format on
auto document = //
hbox({
text(L"This text is "),
text(L"underlined") | underlined,
text(L". Do you like it?"),
});
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
Render(screen, document.get());

View File

@@ -9,34 +9,31 @@
int main(int argc, const char* argv[]) {
using namespace ftxui;
// clang-format off
auto document =
vbox(
hbox(
text(L"north-west"),
filler(),
text(L"north-east")
),
filler(),
hbox(
hbox(
auto document = //
vbox({
hbox({
text(L"north-west"),
filler(),
text(L"north-east"),
}),
filler(),
text(L"center"),
filler()
)
),
filler(),
hbox(
text(L"south-west"),
filler(),
text(L"south-east")
)
);
// clang-format on
hbox({
filler(),
text(L"center"),
filler(),
}),
filler(),
hbox({
text(L"south-west"),
filler(),
text(L"south-east"),
}),
});
auto screen = Screen::Create(Dimension::Full());
Render(screen, document.get());
std::cout << screen.ToString();
getchar();
return 0;
}