-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
167 lines (152 loc) · 4.5 KB
/
main.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include "ftxui/component/component.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"
#include "scripts.h"
using namespace ftxui;
Component tlptui()
{
const int INFOWIDTH = 25;
class Impl : public ComponentBase
{
private:
int threshold;
std::string mode;
Element tlpInfo;
Element thresholdInfo;
Component tlpBox;
std::vector<std::string> tlpSelection = {"AC", "BAT"};
int selected;
Component thresholdSlider;
Component thresholdRenderer;
std::vector<std::string> tab_values = {"TLP mode", "Threshold"};
int tabSelected = 0;
Component menu;
Component tabContainer;
Component horizontalContainer;
public:
Impl()
{
updateTLP();
updateThreshold();
threshold = getCurrentThreshold();
getTLPMode() == AC ? selected = 0 : selected = 1;
tlpBox = Radiobox(&tlpSelection, &selected);
thresholdSlider = Slider("Threshold: ", &threshold, 0) | size(WIDTH, EQUAL, 30);
thresholdRenderer = Renderer(thresholdSlider, [&]
{
return vbox(
{
separatorEmpty(),
hbox(
{
thresholdSlider->Render(), text(std::to_string(threshold))
})
});
});
menu = Menu(&tab_values, &tabSelected) | size(WIDTH, EQUAL, 15);
tabContainer = Container::Tab(
{
tlpBox,
thresholdRenderer,
}, &tabSelected);
horizontalContainer = Container::Horizontal(
{
menu,
tabContainer
});
Add(horizontalContainer);
}
bool updateTLP()
{
if (getTLPMode() == AC)
{
mode = "AC";
}
else
{
mode = "BAT";
}
tlpInfo = text(mode) | color(Color::Red);
return true;
}
bool updateThreshold()
{
threshold = getCurrentThreshold();
thresholdInfo = text(std::to_string(threshold));
return true;
}
bool OnEvent(Event event) override
{
if (event.is_character())
{
if (event.character() == "a")
{
setThreshold(threshold);
if (selected == 0)
setTLPMode(AC);
else
setTLPMode(BAT);
updateTLP();
updateThreshold();
return true;
}
}
return ComponentBase::OnEvent(event);
}
Element Render() override
{
//info area
auto infoTitle = text("Info") | center;
auto navTitle = text("navigation") | center;
auto controlTitle = text("control") | center;
threshold = std::clamp(threshold, 60, 100);
return vbox(
{
hbox(
{
window(infoTitle, vbox(
{
hbox({text("TLP mode: "), tlpInfo}),
hbox({text("Bat threshold: "), thresholdInfo})
})) |size(WIDTH, GREATER_THAN, INFOWIDTH),
window(navTitle, vbox(
{
text("Press A to apply settings"),
text("Press Q to exit")
}))
}),
window(controlTitle, hbox(
{
menu->Render(),
separator(),
tabContainer->Render()
})) | size(HEIGHT, EQUAL, 5)
});
}
bool Focusable() const override
{
return true;
}
};
return Make<Impl>();
}
int main()
{
if (!isRoot())
{
std::cout << "Root required! Please run as sudo.\n";
return 1;
}
auto screen = ScreenInteractive::FitComponent();
auto comp = tlptui();
auto component = CatchEvent(comp, [&](Event event) {
if (event == Event::Character('q')) {
screen.ExitLoopClosure()();
return true;
}
return false;
});
screen.Loop(component);
return 0;
}