#include <chrono>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <thread>
#include <utility>
#include <vector>
int main() {
std::string name;
int number_of_threads;
int downloaded;
};
std::list<Task> remaining_tasks = {
{"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},
};
std::list<Task> displayed_task;
int remaining_threads = 12;
int nb_queued = (int)remaining_tasks.size();
int nb_active = 0;
int nb_done = 0;
auto to_text = [](int number) {
};
auto renderTask = [&](
const Task& task) {
auto style = (task.downloaded == task.size) ?
dim :
bold;
to_text(task.downloaded),
to_text(task.size),
gauge(task.downloaded /
float(task.size)),
});
};
auto renderSummary = [&]() {
to_text(nb_active) |
bold,
to_text(nb_queued) |
bold,
});
};
auto render = [&]() {
std::vector<Element> entries;
for (auto& task : displayed_task)
entries.push_back(renderTask(task));
renderSummary(),
}),
});
};
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 (;;) {
auto document = render();
std::cout << reset_position;
screen.Print();
reset_position = screen.ResetPosition();
using namespace std::chrono_literals;
std::this_thread::sleep_for(0.01s);
if (nb_active + nb_queued == 0)
break;
updateModel();
}
std::cout << std::endl;
}
static Screen Create(Dimensions dimension)
Create a screen with the given dimension.
Dimensions Fit(Element &)
Element window(Element title, Element content, BorderStyle border=ROUNDED)
Draw window with a title and a border around the element.
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Element bold(Element)
Use a bold font, for elements with more emphasis.
Element hbox(Elements)
A container displaying elements horizontally one by one.
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Element text(std::wstring text)
Display a piece of unicode text.
std::variant< Event, Closure, AnimationTask > Task
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Element filler()
An element that will take expand proportionally to the space left in a container.
Element dim(Element)
Use a light font, for elements with less emphasis.
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Element gauge(float progress)
Draw a high definition progress bar.
Decorator color(Color)
Decorate using a foreground color.
Element vbox(Elements)
A container displaying elements vertically one by one.