-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.cpp
62 lines (52 loc) · 1.29 KB
/
status.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
#include "status.hpp"
namespace dwmstatus {
periodic::periodic(status_ptr status, seconds period)
: status_ { move(status) }
, period_ { period }
{
}
optional<string> periodic::update(system_clock::time_point t)
{
if (t > t0_ && (t - t0_) < period_) {
return nullopt;
}
t0_ = t;
if (!status_) {
return nullopt;
}
return status_->update(t);
}
//----------------------------------------------------------------------------
bar::bar(vector<status_ptr> items, string_view sep, string_view prefix, string_view suffix)
: items_ { move(items) }
, str_ { items_.size() }
, sep_ { sep }
, prefix_ { prefix }
, suffix_ { suffix }
{
}
optional<string> bar::update(system_clock::time_point t)
{
bool changed = false;
for (unsigned i = 0; i < items_.size(); ++i) {
if (!items_[i]) {
continue;
}
if (const auto str = items_[i]->update(t); str) {
changed = true;
str_[i] = str.value();
}
}
if (changed) {
string ans = prefix_;
for (unsigned i = 0; i < str_.size(); ++i) {
if (i != 0) {
ans += sep_;
}
ans += str_[i];
}
return ans + suffix_;
}
return nullopt;
}
} // namespace dwmstatus