Skip to content

Commit e555be1

Browse files
committed
Working BarLine watch routines.
1 parent b76683d commit e555be1

File tree

2 files changed

+59
-19
lines changed

2 files changed

+59
-19
lines changed

bar.cpp

+52-14
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,19 @@
66

77
namespace door {
88

9-
BarLine::BarLine(const std::string &txt, int width)
10-
: Line(txt, width), barstyle{BarStyle::SOLID},
9+
BarLine::BarLine(int width)
10+
: Line("", width), barstyle{BarStyle::SOLID},
1111
current_percent{0}, length{width} {
1212
init();
1313
}
1414

15-
BarLine::BarLine(const char *txt, int width)
16-
: Line(txt, width), barstyle{BarStyle::SOLID},
17-
current_percent{0}, length{width} {
18-
init();
19-
}
20-
21-
BarLine::BarLine(const std::string &txt, int width, ANSIColor c)
22-
: Line(txt, width, c), barstyle{BarStyle::SOLID},
23-
current_percent{0}, length{width} {
15+
BarLine::BarLine(int width, BarStyle style)
16+
: Line("", width), barstyle{style}, current_percent{0}, length{width} {
2417
init();
2518
}
2619

27-
BarLine::BarLine(const char *txt, int width, ANSIColor c)
28-
: Line(txt, width, c), barstyle{BarStyle::SOLID},
29-
current_percent{0}, length{width} {
20+
BarLine::BarLine(int width, BarStyle style, ANSIColor c)
21+
: Line("", width, c), barstyle{style}, current_percent{0}, length{width} {
3022
init();
3123
}
3224

@@ -53,6 +45,52 @@ void BarLine::init(void) {
5345
update();
5446
}
5547

48+
void BarLine::watch(float &percent) {
49+
door::updateFunction lineWatchUpdate = [this, &percent]() -> std::string {
50+
this->set(percent);
51+
if (!colorRange.empty()) {
52+
// Ok, there is a range, so test for it.
53+
ANSIColor ac;
54+
// unsigned long p;
55+
for (auto bc : colorRange) {
56+
if (current_percent <= bc.percent) {
57+
ac = bc.c;
58+
// p = bc.percent;
59+
break;
60+
};
61+
}
62+
// cout << "!" << current_percent << "," << p << " ";
63+
setColor(ac);
64+
}
65+
return this->update_bar();
66+
};
67+
setUpdater(lineWatchUpdate);
68+
update();
69+
}
70+
71+
void BarLine::watch(int &value, int &max) {
72+
door::updateFunction lineWatchUpdate = [this, &value, &max]() -> std::string {
73+
this->set(value, max);
74+
if (!colorRange.empty()) {
75+
// Ok, there is a range, so test for it.
76+
ANSIColor ac;
77+
// unsigned long p;
78+
for (auto bc : colorRange) {
79+
if (current_percent <= bc.percent) {
80+
ac = bc.c;
81+
// p = bc.percent;
82+
break;
83+
};
84+
}
85+
// cout << "!" << current_percent << "," << p << " ";
86+
setColor(ac);
87+
}
88+
return this->update_bar();
89+
};
90+
setUpdater(lineWatchUpdate);
91+
update();
92+
}
93+
5694
void BarLine::setStyle(BarStyle s) { barstyle = s; }
5795
void BarLine::set(int value, int max) {
5896
unsigned long percentage = value * 100 * 100 / max;

door.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -615,11 +615,13 @@ class BarLine : public Line {
615615
vector<BarColorRange> colorRange;
616616

617617
public:
618-
BarLine(const std::string &txt, int width);
619-
BarLine(const char *txt, int width);
620-
BarLine(const std::string &txt, int width, ANSIColor c);
621-
BarLine(const char *txt, int width, ANSIColor c);
622-
618+
BarLine(int width);
619+
BarLine(int width, BarStyle style);
620+
BarLine(int width, BarStyle style, ANSIColor c);
621+
622+
void watch(float &percent);
623+
void watch(int &value, int &max);
624+
623625
void setStyle(BarStyle s);
624626
void set(int value, int max);
625627
void set(float percent);

0 commit comments

Comments
 (0)