Browse Source

Working constexpr that fails compile on invalid.

If you use something the constexpr ANSIColor
doesn't understand it won't compile if you
use constexpr before the ANSIColor variable
declaration.
Steve Thielemann 3 years ago
parent
commit
e77f62b5c0
2 changed files with 6 additions and 3 deletions
  1. 4 1
      ansicolor.h
  2. 2 2
      director.cpp

+ 4 - 1
ansicolor.h

@@ -3,6 +3,7 @@
 
 #include <cstdint>
 #include <string>
+#include <stdexcept>
 
 #define CSI "\x1b["
 
@@ -58,6 +59,7 @@ constexpr long strhash(const char *txt) {
   for (int x = 0; x < 3; ++x) {
     if (txt[x] == 0) break;
     char c = txt[x];
+    // toupper
     if ((c >= 'a') && (c <= 'z')) c &= ~0x20;
     result = (result << 8) | c;
   }
@@ -85,7 +87,6 @@ class ANSIColor {
   COLOR fg;
   /** Background color */
   COLOR bg;
-
   int attr;
 
  public:
@@ -184,6 +185,8 @@ class ANSIColor {
           else
             fg = COLOR::WHITE;
           break;
+        default:
+          throw std::range_error("Invalid/unknown color or attribute");
       }
 
       // skip to the space character

+ 2 - 2
director.cpp

@@ -967,8 +967,8 @@ void Director::macro_have_input(void) {
       char c = toupper(id->input[0]);
       macro_item.assign(1, c);
       if (galaxy.meta["macros"].contains(macro_item)) {
-        ANSIColor key(COLOR::GREEN, ATTR::BOLD);
-        ANSIColor value(COLOR::BLUE, ATTR::BOLD);
+        constexpr ANSIColor key("BOLD GREEN"); // COLOR::GREEN, ATTR::BOLD);
+        constexpr ANSIColor value("BOLD BLUE"); // COLOR::BLUE, ATTR::BOLD);
         std::string output =
             str(boost::format("%1%%2% : %3%%4%\n\r") % key() % macro_item %
                 value() % galaxy.meta["macros"][macro_item]);