-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
124 lines (103 loc) · 3.63 KB
/
main.cpp
File metadata and controls
124 lines (103 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "loren.h"
#include <thread>
#include <chrono>
#include <rxterm/terminal.hpp>
#include <rxterm/style.hpp>
#include <rxterm/image.hpp>
#include <rxterm/components/text.hpp>
#include <rxterm/components/stacklayout.hpp>
#include <rxterm/components/flowlayout.hpp>
#include <rxterm/components/progress.hpp>
#include <rxterm/components/maxwidth.hpp>
#include "components/logview.hpp"
#include "components/phasediagram.hpp"
using namespace rxterm;
int main() {
using namespace std;
using namespace chrono_literals;
using namespace string_literals;
struct State {
string header;
struct Log {
vector<string> buffer;
void push(const string &line) {
if (buffer.size() > 100) buffer.erase(buffer.begin());
buffer.push_back(line);
}
} log;
struct Progress {
string title;
unsigned position;
unsigned limit;
Progress() : position{0}, limit{0} {}
[[nodiscard]] auto value() const { return float(position) / float(limit); }
[[nodiscard]] string percent() const {
char buf[6];
snprintf(buf, sizeof(buf), "%3d%% ", lround(value() * 100));
return buf;
}
[[nodiscard]] string suffix() const {
return " 7568/10000 [00:33<00:10, 229.00it/s] ";
}
} progress;
struct Status {
std::string title;
unsigned value;
Status() : value{0} {}
} status;
int startTime;
State() : startTime{0} {}
} state;
auto renderToTerm = [](auto const &vt, const State &state, unsigned width) {
struct Layout {
struct {
unsigned prefix, body, suffix;
} pb;
unsigned log;
unsigned pd;
Layout(const State &state, unsigned width) : pd{19} {
pb.prefix = state.progress.title.size() + state.progress.percent().size();
pb.suffix = state.progress.suffix().size();
pb.body = width - pd - pb.prefix - pb.suffix;
log = width - pd - 1;
}
} l{state, width};
FlowLayout<> layout{
StackLayout<>{
Text{state.header},
FlowLayout<>{
Text{state.progress.title},
Text{state.progress.percent()},
MaxWidth{l.pb.body, Progress{state.progress.value()}},
Text{{FontColor::Blue}, state.progress.suffix()}
},
FlowLayout<>{
Text{state.status.title},
Text{{FontColor::Magenta}, state.status.value}
},
MaxWidth{l.log, LogView{{Color::Yellow, FontColor::Magenta}, state.log.buffer, 6}}
},
PhaseDiagram{"hhj"}
};
return vt.flip(layout.render(width).toString());
};
VirtualTerminal vt;
auto w = VirtualTerminal::width();
if (!w) w = 120;
state.header = "Data processor";
state.status.title = "Initializing...";
state.progress.title = "Process ";
state.progress.limit = 64 * 1024 * 1024;
vt = renderToTerm(vt, state, w);
std::this_thread::sleep_for(200ms);
state.status.title = "Total steps: ";
for (;;) {
state.progress.position += 64 * 1024 * 10;
state.status.value++;
state.log.push(nextLoren());
vt = renderToTerm(vt, state, w);
if (state.progress.position >= state.progress.limit) break;
std::this_thread::sleep_for(100ms);
}
return 0;
}