Browse Source

Working BarLine watch routines.

Steve Thielemann 3 years ago
parent
commit
e555be1e34
2 changed files with 59 additions and 19 deletions
  1. 52 14
      bar.cpp
  2. 7 5
      door.h

+ 52 - 14
bar.cpp

@@ -6,27 +6,19 @@
 
 namespace door {
 
-BarLine::BarLine(const std::string &txt, int width)
-    : Line(txt, width), barstyle{BarStyle::SOLID},
+BarLine::BarLine(int width)
+    : Line("", width), barstyle{BarStyle::SOLID},
       current_percent{0}, length{width} {
   init();
 }
 
-BarLine::BarLine(const char *txt, int width)
-    : Line(txt, width), barstyle{BarStyle::SOLID},
-      current_percent{0}, length{width} {
-  init();
-}
-
-BarLine::BarLine(const std::string &txt, int width, ANSIColor c)
-    : Line(txt, width, c), barstyle{BarStyle::SOLID},
-      current_percent{0}, length{width} {
+BarLine::BarLine(int width, BarStyle style)
+    : Line("", width), barstyle{style}, current_percent{0}, length{width} {
   init();
 }
 
-BarLine::BarLine(const char *txt, int width, ANSIColor c)
-    : Line(txt, width, c), barstyle{BarStyle::SOLID},
-      current_percent{0}, length{width} {
+BarLine::BarLine(int width, BarStyle style, ANSIColor c)
+    : Line("", width, c), barstyle{style}, current_percent{0}, length{width} {
   init();
 }
 
@@ -53,6 +45,52 @@ void BarLine::init(void) {
   update();
 }
 
+void BarLine::watch(float &percent) {
+  door::updateFunction lineWatchUpdate = [this, &percent]() -> std::string {
+    this->set(percent);
+    if (!colorRange.empty()) {
+      // Ok, there is a range, so test for it.
+      ANSIColor ac;
+      // unsigned long p;
+      for (auto bc : colorRange) {
+        if (current_percent <= bc.percent) {
+          ac = bc.c;
+          // p = bc.percent;
+          break;
+        };
+      }
+      // cout << "!" << current_percent << "," << p << " ";
+      setColor(ac);
+    }
+    return this->update_bar();
+  };
+  setUpdater(lineWatchUpdate);
+  update();
+}
+
+void BarLine::watch(int &value, int &max) {
+  door::updateFunction lineWatchUpdate = [this, &value, &max]() -> std::string {
+    this->set(value, max);
+    if (!colorRange.empty()) {
+      // Ok, there is a range, so test for it.
+      ANSIColor ac;
+      // unsigned long p;
+      for (auto bc : colorRange) {
+        if (current_percent <= bc.percent) {
+          ac = bc.c;
+          // p = bc.percent;
+          break;
+        };
+      }
+      // cout << "!" << current_percent << "," << p << " ";
+      setColor(ac);
+    }
+    return this->update_bar();
+  };
+  setUpdater(lineWatchUpdate);
+  update();
+}
+
 void BarLine::setStyle(BarStyle s) { barstyle = s; }
 void BarLine::set(int value, int max) {
   unsigned long percentage = value * 100 * 100 / max;

+ 7 - 5
door.h

@@ -615,11 +615,13 @@ protected:
   vector<BarColorRange> colorRange;
 
 public:
-  BarLine(const std::string &txt, int width);
-  BarLine(const char *txt, int width);
-  BarLine(const std::string &txt, int width, ANSIColor c);
-  BarLine(const char *txt, int width, ANSIColor c);
-
+  BarLine(int width);
+  BarLine(int width, BarStyle style);
+  BarLine(int width, BarStyle style, ANSIColor c);
+  
+  void watch(float &percent);
+  void watch(int &value, int &max);
+  
   void setStyle(BarStyle s);
   void set(int value, int max);
   void set(float percent);