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
93 return vbox({
94 // List of tasks.
95 window(text(" Task "), vbox(std::move(entries))),
96
97 // Summary.
98 hbox({
99 renderSummary(),
100 filler(),
101 }),
102 });
103 };
104
105 auto updateModel = [&]() {
106 for (auto& task : displayed_task) {
107 if (task.downloaded != task.size) {
108 task.downloaded++;
109 } else if (task.number_of_threads) {
110 remaining_threads += task.number_of_threads;
111 task.number_of_threads = 0;
112 nb_active--;
113 nb_done++;
114 }
115 }
116
117 if (remaining_tasks.size() &&
118 remaining_tasks.front().number_of_threads <= remaining_threads) {
119 remaining_threads -= remaining_tasks.front().number_of_threads;
120 displayed_task.push_back(remaining_tasks.front());
121 remaining_tasks.pop_front();
122 nb_queued--;
123 nb_active++;
124 }
125 };
126
127 std::string reset_position;
128 for (;;) {
129 // Draw.
130 auto document = render();
131 auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
132 Render(screen, document);
133 std::cout << reset_position;
134 screen.Print();
135 reset_position = screen.ResetPosition();
136
137 // Simulate time.
138 using namespace std::chrono_literals;
139 std::this_thread::sleep_for(0.01s);
140
141 // Exit
142 if (nb_active + nb_queued == 0) {
143 break;
144 }
145
146 // Update the model for the next frame.
147 updateModel();
148 }
149 std::cout << std::endl;
150}
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
int main()