mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-05-06 07:16:45 +08:00

In the past, FTXUI switched from std::string to std::wstring to support fullwidth characters. The reasons was that fullwidth characters can be stored inside a single wchar_t. Then FTXUI added support for combining characters. A single glygh doesn't even fit a wchar_t. Instead, a glyph can be arbitrary large. The usage of wstring doesn't really fit the new model and have several drawbacks: 1. It doesn't simplify the implementation of FTXUI, because of combining characters. 2. It reduces drawing performance by 2x. 3. It increase Screen's memory allocation by 2x. This patch converts FTXUI to use std::string internally. It now exposes std::string based API. The std::wstring API remains, but is now deprecated. Tests and examples haven't been update to show the breakage is limited. They will be updated in a second set of patches. Bug: https://github.com/ArthurSonzogni/FTXUI/issues/153 Co-authored-by: Tushar Maheshwari <tushar27192@gmail.com>
153 lines
4.4 KiB
C++
153 lines
4.4 KiB
C++
#include <chrono> // for operator""s, chrono_literals
|
|
#include <ftxui/dom/elements.hpp> // for operator|, Element, hbox, bold, color, filler, separator, vbox, window, gauge, size, dim, EQUAL, WIDTH
|
|
#include <ftxui/screen/screen.hpp> // for Screen, Dimension
|
|
#include <ftxui/screen/string.hpp> // for to_wstring
|
|
#include <iostream> // for cout, endl, ostream
|
|
#include <list> // for list, operator!=, _List_iterator, _List_iterator<>::_Self
|
|
#include <memory> // for allocator, shared_ptr, allocator_traits<>::value_type
|
|
#include <string> // for wstring, operator<<, string
|
|
#include <thread> // for sleep_for
|
|
#include <utility> // for move
|
|
#include <vector> // for vector
|
|
|
|
#include "ftxui/dom/deprecated.hpp" // for text
|
|
#include "ftxui/dom/node.hpp" // for Render
|
|
#include "ftxui/screen/box.hpp" // for ftxui
|
|
#include "ftxui/screen/color.hpp" // for Color, Color::Green, Color::Red, Color::RedLight
|
|
|
|
int main(int argc, const char* argv[]) {
|
|
using namespace ftxui;
|
|
|
|
struct Task {
|
|
std::wstring name;
|
|
int number_of_threads;
|
|
int downloaded;
|
|
int size;
|
|
};
|
|
|
|
std::list<Task> remaining_tasks = {
|
|
{L"contact server ", 10, 0, 6 * 25},
|
|
{L"download index.html ", 10, 0, 9 * 25},
|
|
{L"download script.js ", 1, 0, 3 * 25},
|
|
{L"download style.js ", 1, 0, 4 * 25},
|
|
{L"download image.png ", 1, 0, 5 * 25},
|
|
{L"download big_1.png ", 1, 0, 30 * 25},
|
|
{L"download icon_1.png ", 1, 0, 7 * 25},
|
|
{L"download icon_2.png ", 1, 0, 8 * 25},
|
|
{L"download big_2.png ", 1, 0, 30 * 25},
|
|
{L"download small_1.png ", 1, 0, 10 * 25},
|
|
{L"download small_2.png ", 1, 0, 11 * 25},
|
|
{L"download small_3.png ", 1, 0, 12 * 25},
|
|
};
|
|
|
|
std::list<Task> displayed_task;
|
|
|
|
int remaining_threads = 12;
|
|
|
|
int nb_queued = remaining_tasks.size();
|
|
int nb_active = 0;
|
|
int nb_done = 0;
|
|
|
|
auto to_text = [](int number) {
|
|
return text(to_wstring(number)) | size(WIDTH, EQUAL, 3);
|
|
};
|
|
|
|
auto renderTask = [&](const Task& task) {
|
|
auto style = (task.downloaded == task.size) ? dim : bold;
|
|
return hbox({
|
|
text(task.name) | style,
|
|
separator(),
|
|
to_text(task.downloaded),
|
|
text(L"/"),
|
|
to_text(task.size),
|
|
separator(),
|
|
gauge(task.downloaded / float(task.size)),
|
|
});
|
|
};
|
|
|
|
auto renderSummary = [&]() {
|
|
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 = [&]() {
|
|
std::vector<Element> entries;
|
|
for (auto& task : displayed_task)
|
|
entries.push_back(renderTask(task));
|
|
|
|
return vbox({
|
|
// List of tasks.
|
|
window(text(L" Task "), vbox(std::move(entries))),
|
|
|
|
// Summary.
|
|
hbox({
|
|
renderSummary(),
|
|
filler(),
|
|
}),
|
|
});
|
|
};
|
|
|
|
auto updateModel = [&]() {
|
|
for (auto& task : displayed_task) {
|
|
if (task.downloaded != task.size) {
|
|
task.downloaded++;
|
|
} else if (task.number_of_threads) {
|
|
remaining_threads += task.number_of_threads;
|
|
task.number_of_threads = 0;
|
|
nb_active--;
|
|
nb_done++;
|
|
}
|
|
}
|
|
|
|
if (remaining_tasks.size() &&
|
|
remaining_tasks.front().number_of_threads <= remaining_threads) {
|
|
remaining_threads -= remaining_tasks.front().number_of_threads;
|
|
displayed_task.push_back(remaining_tasks.front());
|
|
remaining_tasks.pop_front();
|
|
nb_queued--;
|
|
nb_active++;
|
|
}
|
|
};
|
|
|
|
std::string reset_position;
|
|
for (;;) {
|
|
// Draw.
|
|
auto document = render();
|
|
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
|
|
Render(screen, document);
|
|
std::cout << reset_position;
|
|
screen.Print();
|
|
reset_position = screen.ResetPosition();
|
|
|
|
// Simulate time.
|
|
using namespace std::chrono_literals;
|
|
std::this_thread::sleep_for(0.01s);
|
|
|
|
// Exit
|
|
if (nb_active + nb_queued == 0)
|
|
break;
|
|
|
|
// Update the model for the next frame.
|
|
updateModel();
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
// the LICENSE file.
|