FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
package_manager.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <chrono> // for operator""s, chrono_literals
5#include <ftxui/dom/elements.hpp> // for operator|, text, Element, hbox, bold, color, filler, separator, vbox, window, gauge, Fit, size, dim, EQUAL, WIDTH
6#include <ftxui/screen/screen.hpp> // for Full, Screen
7#include <iostream> // for cout, endl, ostream
8#include <list> // for list, operator==, _List_iterator, _List_iterator<>::_Self
9#include <memory> // for allocator, shared_ptr, allocator_traits<>::value_type
10#include <string> // for string, operator<<, to_string
11#include <thread> // for sleep_for
12#include <utility> // for move
13#include <vector> // for vector
14
15#include "ftxui/dom/node.hpp" // for Render
16#include "ftxui/screen/color.hpp" // for Color, Color::Green, Color::Red, Color::RedLight, ftxui
17
18int main() {
19 using namespace ftxui;
20
21 struct Task {
22 std::string name;
23 int number_of_threads;
24 int downloaded;
25 int size;
26 };
27
28 std::list<Task> remaining_tasks = {
29 {"contact server ", 10, 0, 6 * 25},
30 {"download index.html ", 10, 0, 9 * 25},
31 {"download script.js ", 1, 0, 3 * 25},
32 {"download style.js ", 1, 0, 4 * 25},
33 {"download image.png ", 1, 0, 5 * 25},
34 {"download big_1.png ", 1, 0, 30 * 25},
35 {"download icon_1.png ", 1, 0, 7 * 25},
36 {"download icon_2.png ", 1, 0, 8 * 25},
37 {"download big_2.png ", 1, 0, 30 * 25},
38 {"download small_1.png ", 1, 0, 10 * 25},
39 {"download small_2.png ", 1, 0, 11 * 25},
40 {"download small_3.png ", 1, 0, 12 * 25},
41 };
42
43 std::list<Task> displayed_task;
44
45 int remaining_threads = 12;
46
47 int nb_queued = (int)remaining_tasks.size();
48 int nb_active = 0;
49 int nb_done = 0;
50
51 auto to_text = [](int number) {
52 return text(std::to_string(number)) | size(WIDTH, EQUAL, 3);
53 };
54
55 auto renderTask = [&](const Task& task) {
56 auto style = (task.downloaded == task.size) ? dim : bold;
57 return hbox({
58 text(task.name) | style,
59 separator(),
60 to_text(task.downloaded),
61 text("/"),
62 to_text(task.size),
63 separator(),
64 gauge(task.downloaded / float(task.size)),
65 });
66 };
67
68 auto renderSummary = [&]() {
69 auto summary = vbox({
70 hbox({
71 text("- done: "),
72 to_text(nb_done) | bold,
73 }) | color(Color::Green),
74 hbox({
75 text("- active: "),
76 to_text(nb_active) | bold,
77 }) | color(Color::RedLight),
78 hbox({
79 text("- queue: "),
80 to_text(nb_queued) | bold,
81 }) | color(Color::Red),
82 });
83
84 return window(text(" Summary "), summary);
85 };
86
87 auto render = [&]() {
88 std::vector<Element> entries;
89 for (auto& task : displayed_task)
90 entries.push_back(renderTask(task));
91
92 return vbox({
93 // List of tasks.
94 window(text(" Task "), vbox(std::move(entries))),
95
96 // Summary.
97 hbox({
98 renderSummary(),
99 filler(),
100 }),
101 });
102 };
103
104 auto updateModel = [&]() {
105 for (auto& task : displayed_task) {
106 if (task.downloaded != task.size) {
107 task.downloaded++;
108 } else if (task.number_of_threads) {
109 remaining_threads += task.number_of_threads;
110 task.number_of_threads = 0;
111 nb_active--;
112 nb_done++;
113 }
114 }
115
116 if (remaining_tasks.size() &&
117 remaining_tasks.front().number_of_threads <= remaining_threads) {
118 remaining_threads -= remaining_tasks.front().number_of_threads;
119 displayed_task.push_back(remaining_tasks.front());
120 remaining_tasks.pop_front();
121 nb_queued--;
122 nb_active++;
123 }
124 };
125
126 std::string reset_position;
127 for (;;) {
128 // Draw.
129 auto document = render();
130 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
131 Render(screen, document);
132 std::cout << reset_position;
133 screen.Print();
134 reset_position = screen.ResetPosition();
135
136 // Simulate time.
137 using namespace std::chrono_literals;
138 std::this_thread::sleep_for(0.01s);
139
140 // Exit
141 if (nb_active + nb_queued == 0)
142 break;
143
144 // Update the model for the next frame.
145 updateModel();
146 }
147 std::cout << std::endl;
148}
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
int main()