2021-08-09 05:25:20 +08:00
|
|
|
#include <chrono> // for operator""s, chrono_literals
|
2021-08-09 06:27:37 +08:00
|
|
|
#include <ftxui/dom/elements.hpp> // for operator|, text, Element, hbox, bold, color, filler, separator, vbox, window, gauge, Fit, size, dim, EQUAL, WIDTH
|
|
|
|
#include <ftxui/screen/screen.hpp> // for Full, Screen
|
2021-08-09 05:25:20 +08:00
|
|
|
#include <iostream> // for cout, endl, ostream
|
2022-12-20 01:51:25 +08:00
|
|
|
#include <list> // for list, operator==, _List_iterator, _List_iterator<>::_Self
|
2021-08-09 05:25:20 +08:00
|
|
|
#include <memory> // for allocator, shared_ptr, allocator_traits<>::value_type
|
2021-08-09 06:27:37 +08:00
|
|
|
#include <string> // for string, operator<<, to_string
|
2021-08-09 05:25:20 +08:00
|
|
|
#include <thread> // for sleep_for
|
|
|
|
#include <utility> // for move
|
|
|
|
#include <vector> // for vector
|
2019-01-05 09:03:49 +08:00
|
|
|
|
2022-01-07 18:03:54 +08:00
|
|
|
#include "ftxui/dom/node.hpp" // for Render
|
|
|
|
#include "ftxui/screen/color.hpp" // for Color, Color::Green, Color::Red, Color::RedLight, ftxui
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
int main(int argc, const char* argv[]) {
|
2019-01-12 22:00:08 +08:00
|
|
|
using namespace ftxui;
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2019-01-05 09:03:49 +08:00
|
|
|
struct Task {
|
2021-08-09 06:27:37 +08:00
|
|
|
std::string name;
|
2019-01-05 09:03:49 +08:00
|
|
|
int number_of_threads;
|
|
|
|
int downloaded;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::list<Task> remaining_tasks = {
|
2021-08-09 06:27:37 +08:00
|
|
|
{"contact server ", 10, 0, 6 * 25},
|
|
|
|
{"download index.html ", 10, 0, 9 * 25},
|
|
|
|
{"download script.js ", 1, 0, 3 * 25},
|
|
|
|
{"download style.js ", 1, 0, 4 * 25},
|
|
|
|
{"download image.png ", 1, 0, 5 * 25},
|
|
|
|
{"download big_1.png ", 1, 0, 30 * 25},
|
|
|
|
{"download icon_1.png ", 1, 0, 7 * 25},
|
|
|
|
{"download icon_2.png ", 1, 0, 8 * 25},
|
|
|
|
{"download big_2.png ", 1, 0, 30 * 25},
|
|
|
|
{"download small_1.png ", 1, 0, 10 * 25},
|
|
|
|
{"download small_2.png ", 1, 0, 11 * 25},
|
|
|
|
{"download small_3.png ", 1, 0, 12 * 25},
|
2019-01-05 09:03:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::list<Task> displayed_task;
|
|
|
|
|
|
|
|
int remaining_threads = 12;
|
|
|
|
|
2022-02-13 17:51:47 +08:00
|
|
|
int nb_queued = (int)remaining_tasks.size();
|
2019-01-05 09:03:49 +08:00
|
|
|
int nb_active = 0;
|
|
|
|
int nb_done = 0;
|
|
|
|
|
|
|
|
auto to_text = [](int number) {
|
2021-08-09 06:27:37 +08:00
|
|
|
return text(std::to_string(number)) | size(WIDTH, EQUAL, 3);
|
2019-01-05 09:03:49 +08:00
|
|
|
};
|
2019-01-06 07:51:56 +08:00
|
|
|
|
|
|
|
auto renderTask = [&](const Task& task) {
|
|
|
|
auto style = (task.downloaded == task.size) ? dim : bold;
|
2020-05-21 02:36:47 +08:00
|
|
|
return hbox({
|
2019-01-06 07:51:56 +08:00
|
|
|
text(task.name) | style,
|
|
|
|
separator(),
|
|
|
|
to_text(task.downloaded),
|
2021-08-09 06:27:37 +08:00
|
|
|
text("/"),
|
2019-01-06 07:51:56 +08:00
|
|
|
to_text(task.size),
|
|
|
|
separator(),
|
2020-05-21 02:36:47 +08:00
|
|
|
gauge(task.downloaded / float(task.size)),
|
|
|
|
});
|
2019-01-06 07:51:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
auto renderSummary = [&]() {
|
2020-05-21 02:36:47 +08:00
|
|
|
auto summary = vbox({
|
|
|
|
hbox({
|
2021-08-09 06:27:37 +08:00
|
|
|
text("- done: "),
|
2020-05-21 02:36:47 +08:00
|
|
|
to_text(nb_done) | bold,
|
|
|
|
}) | color(Color::Green),
|
|
|
|
hbox({
|
2021-08-09 06:27:37 +08:00
|
|
|
text("- active: "),
|
2020-05-21 02:36:47 +08:00
|
|
|
to_text(nb_active) | bold,
|
|
|
|
}) | color(Color::RedLight),
|
|
|
|
hbox({
|
2021-08-09 06:27:37 +08:00
|
|
|
text("- queue: "),
|
2020-05-21 02:36:47 +08:00
|
|
|
to_text(nb_queued) | bold,
|
|
|
|
}) | color(Color::Red),
|
|
|
|
});
|
|
|
|
|
2021-08-09 06:27:37 +08:00
|
|
|
return window(text(" Summary "), summary);
|
2019-01-06 07:51:56 +08:00
|
|
|
};
|
|
|
|
|
2020-05-21 02:36:47 +08:00
|
|
|
auto render = [&]() {
|
2019-01-06 07:51:56 +08:00
|
|
|
std::vector<Element> entries;
|
2020-05-21 02:36:47 +08:00
|
|
|
for (auto& task : displayed_task)
|
2019-01-06 07:51:56 +08:00
|
|
|
entries.push_back(renderTask(task));
|
2019-01-05 09:03:49 +08:00
|
|
|
|
2020-05-21 02:36:47 +08:00
|
|
|
return vbox({
|
2019-01-06 07:51:56 +08:00
|
|
|
// List of tasks.
|
2021-08-09 06:27:37 +08:00
|
|
|
window(text(" Task "), vbox(std::move(entries))),
|
2019-01-05 09:03:49 +08:00
|
|
|
|
2019-01-06 07:51:56 +08:00
|
|
|
// Summary.
|
2020-05-21 02:36:47 +08:00
|
|
|
hbox({
|
|
|
|
renderSummary(),
|
|
|
|
filler(),
|
|
|
|
}),
|
|
|
|
});
|
2019-01-06 07:51:56 +08:00
|
|
|
};
|
2019-01-05 09:03:49 +08:00
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
auto updateModel = [&]() {
|
|
|
|
for (auto& task : displayed_task) {
|
2019-01-05 09:03:49 +08:00
|
|
|
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++;
|
|
|
|
}
|
2019-01-06 07:51:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
std::string reset_position;
|
2020-03-23 05:32:44 +08:00
|
|
|
for (;;) {
|
2019-01-06 07:51:56 +08:00
|
|
|
// Draw.
|
|
|
|
auto document = render();
|
2019-01-27 04:52:55 +08:00
|
|
|
auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
|
2020-05-21 03:23:59 +08:00
|
|
|
Render(screen, document);
|
2021-03-22 05:54:39 +08:00
|
|
|
std::cout << reset_position;
|
|
|
|
screen.Print();
|
2019-01-06 07:51:56 +08:00
|
|
|
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();
|
2019-01-05 09:03:49 +08:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2020-09-06 19:46:56 +08:00
|
|
|
|
|
|
|
// 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.
|