فهرست منبع

Working Macros. Ctrl-A/B/C/D, ^ for Enter.

Steve Thielemann 3 سال پیش
والد
کامیت
3ba6c2f376
2فایلهای تغییر یافته به همراه168 افزوده شده و 16 حذف شده
  1. 160 16
      director.cpp
  2. 8 0
      director.h

+ 160 - 16
director.cpp

@@ -75,8 +75,10 @@ void Director::client_input(const std::string &input) {
     // easter-eggs:
 
     if (prompt == "Enter your choice: ") {
-      ANSIColor c1(COLOR::CYAN, ATTR::BOLD);
-      ANSIColor c2(COLOR::WHITE, ATTR::BOLD);
+      // ANSIColor c1(COLOR::CYAN, ATTR::BOLD);
+      // ANSIColor c2(COLOR::WHITE, ATTR::BOLD);
+      ANSIColor c1("BOLD CYAN");
+      ANSIColor c2("BOLD WHITE");
 
       to_client(std::string("\n\r") + c1() + "I'd choose " + c2() + "`T`" +
                 c1() + ", but that's how I was coded.\n\r");
@@ -86,7 +88,7 @@ void Director::client_input(const std::string &input) {
 
     // easter-egg
     if (prompt == "[Pause]") {
-      ANSIColor c1(COLOR::CYAN, ATTR::BOLD);
+      ANSIColor c1("BOLD CYAN");  // COLOR::CYAN, ATTR::BOLD);
       to_client(std::string(" ") + c1() + "PAWS" + reset() + "\n\r");
       to_client(current_raw_prompt);
       return;
@@ -104,6 +106,19 @@ void Director::client_input(const std::string &input) {
       proxy_activate();
       return;
     }
+  } else if (input == "\x01" || input == "\x02" || input == "\x03") {
+    // MACROS
+    std::string macro;
+    macro.assign(1, input[0] + 'A' - 1);
+    std::string to_send;
+    if (galaxy.meta.contains("macros") &&
+        galaxy.meta["macros"].contains(macro)) {
+      to_send = galaxy.meta["macros"][macro].get<std::string>();
+      BUGZ_LOG(fatal) << "Sending Macro " << macro << ": [" << to_send << "]";
+      replace(to_send, "^", "\r");
+      to_server(to_send, "MACRO");
+      return;
+    }
   }
   // Ok...
   if (talk_direct) to_server(input, "Director::client_input");
@@ -310,15 +325,11 @@ void Director::build_menu(void) {
   // "\x1b[0;31;40m\xdb\xb2\xb1\xb0 \x1b[31;40mRED "
   // "\x1b[32;40mGREEN\x1b[30;42m\xdb\xb2\xb1\xb0 \x1b[0m : ";
   md->lazy = true;
-  md->menu = {{"C", "Configure"},
-              {"D", "Display Report"},
-              {"E", "Export Data/Save"},
-              {"I", "Information"},
-              {"P", "Port CIM"},
-              {"W", "Warp CIM"},
-              {"T", "Trading Report (same as D)"},
-              {"S", "Scripts"},
-              {"X", "eXit"}};
+  md->menu = {{"C", "Configure"},        {"D", "Display Report"},
+              {"E", "Export Data/Save"}, {"I", "Information"},
+              {"P", "Port CIM"},         {"W", "Warp CIM"},
+              {"M", "Macros"},           {"T", "Trading Report (same as D)"},
+              {"S", "Scripts"},          {"X", "eXit"}};
   md->setNotify([this]() { this->menu_choice(); });
 
   cim = std::make_shared<CIMDispatch>(*this, "CIMDirector");
@@ -483,6 +494,10 @@ void Director::menu_choice(void) {
           chain->activate();
           return;
           break;
+        case 'M':  // Macros
+          macro_edit();
+          return;
+          break;
         case 'W':  // Warp CIM
           chain = cim;
           to_server("^IQ", "Director::Warp CIM");
@@ -628,19 +643,20 @@ void Director::scripts_done(void) {
             to_client(text);
           } else {
             to_client("I don't see any best trades.\n\r");
-          } 
-          const char * foe[3] = { "Fuel", "Organics", "Equipment"};
+          }
+          const char *foe[3] = {"Fuel", "Organics", "Equipment"};
 
           for (int x = 0; x < 3; ++x) {
             auto s = galaxy.find_nearest_selling(current_sector, x);
-            text = str(boost::format("Closest Selling %1% is %2%\n\r") % foe[x] % s);
+            text = str(boost::format("Closest Selling %1% is %2%\n\r") %
+                       foe[x] % s);
             to_client(text);
           }
           to_client("Good places to hide:\n\r");
           auto sectors = galaxy.find_safe();
           text = "";
           int c = 0;
-          for (auto const & s: sectors) {
+          for (auto const &s : sectors) {
             ++c;
             text += str(boost::format("%1$5d ") % s);
             if (c % 10 == 0) {
@@ -861,6 +877,134 @@ void Director::config_have_input(void) {
   }
 }
 
+InputDispatch *Director::init_macro_input(void) {
+  InputDispatch *id;
+  if (macro_input) {
+    // Yes, it has been setup before.
+    id = dynamic_cast<InputDispatch *>(&(*macro_input));
+
+    ANSIColor by{1, 33};
+    ANSIColor cyan{36};
+    ANSIColor bg{1, 32};
+    std::string prompt =
+        by() + "M" + cyan() + "acro " + bg() + "=>" + reset() + " ";
+    id->prompt = prompt;  // "Config => ";
+    id->numeric = false;
+    id->max_length = 1;
+    config_item.clear();
+    return id;
+  } else {
+    // set it up
+    macro_input = std::make_shared<InputDispatch>(*this, "InputDirector");
+    id = static_cast<InputDispatch *>(&(*macro_input));
+    ANSIColor by{1, 33};
+    ANSIColor cyan{36};
+    ANSIColor bg{1, 32};
+    std::string prompt =
+        by() + "M" + cyan() + "acro " + bg() + "=>" + reset() + " ";
+    id->prompt = prompt;
+    id->numeric = false;
+    id->max_length = 1;
+    id->setNotify([this]() { this->macro_have_input(); });
+    macro_item.clear();
+    return id;
+  }
+}
+
+void Director::macro_edit(void) {
+  // display current config
+  std::string menu_box_color = "\x1b[1;33;44m";
+  std::string menu_text_color = "\x1b[1;37;44m";
+  auto abox = Boxes::alert("    Macros:   ", menu_box_color, menu_text_color,
+                           14, 1, true);
+  for (auto line : abox) {
+    to_client(line);
+  }
+
+  ANSIColor number(COLOR::CYAN);
+  ANSIColor key(COLOR::GREEN, ATTR::BOLD);
+  ANSIColor value(COLOR::BLUE, ATTR::BOLD);
+
+  if (!galaxy.meta.contains("macros")) {
+    galaxy.meta["macros"]["A"] = "";
+    galaxy.meta["macros"]["B"] = "";
+    galaxy.meta["macros"]["C"] = "";
+    galaxy.meta["macros"]["D"] = "";
+    galaxy.meta["macros"]["E"] = "";
+    galaxy.meta["macros"]["F"] = "";
+  }
+
+  for (auto const &cfg : galaxy.meta["macros"].items()) {
+    // for (auto const &cfg : galaxy.config) {
+    std::string output = str(boost::format("%1%%2%: %3%%4$s%5%\n\r") % key() %
+                             cfg.key() % value() % cfg.value() % reset());
+    to_client(output);
+  }
+  std::string message =
+      number() + "Enter Letter to edit, " + key() + "blank to exit.\n\r";
+  to_client(message);
+
+  // setup call to config_input:
+  InputDispatch *id = init_macro_input();
+  chain = macro_input;
+  id->input.clear();
+  macro_item.clear();
+  id->activate();
+}
+
+void Director::macro_have_input(void) {
+  InputDispatch *id = dynamic_cast<InputDispatch *>(&(*macro_input));
+
+  if (macro_item.empty()) {
+    // Macro selection
+    if (id->input.empty()) {
+      chain = main_menu;
+      MenuDispatch *md = dynamic_cast<MenuDispatch *>(&(*chain));
+      md->activate();
+      macro_input.reset();
+      return;
+    } else {
+      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);
+        std::string output =
+            str(boost::format("%1%%2% : %3%%4%\n\r") % key() % macro_item %
+                value() % galaxy.meta["macros"][macro_item]);
+        to_client(output);
+        id->max_length = 100;
+        id->numeric = false;
+        ANSIColor by{1, 33};
+        ANSIColor cyan{36};
+        ANSIColor bg{1, 32};
+        std::string prompt = by() + "M" + cyan() + "acro " + macro_item + bg() +
+                             "=>" + reset() + " ";
+        id->prompt = prompt;
+        id->activate();
+        return;
+      } else {
+        to_client("I don't understand.\n\r");
+        macro_edit();
+        return;
+      }
+    }
+  } else {
+    // clear out macro_item when done.
+    if (id->input.empty()) {
+      to_client("No change.\n\r");
+      macro_item.clear();
+      macro_edit();
+      return;
+    } else {
+      galaxy.meta["macros"][macro_item] = id->input;
+      macro_item.clear();
+      macro_edit();
+      return;
+    }
+  }
+}
+
 void Director::have_input(void) {
   ++count;
   InputDispatch *id = dynamic_cast<InputDispatch *>(&(*chain));

+ 8 - 0
director.h

@@ -67,10 +67,14 @@ class Director {
   std::shared_ptr<Dispatch> main_menu;
   std::shared_ptr<Dispatch> cim;
   std::shared_ptr<Dispatch> config_input;
+  std::shared_ptr<Dispatch> edit_input;
   std::shared_ptr<Dispatch> scripts_menu;
   std::shared_ptr<Dispatch> script;
+  std::shared_ptr<Dispatch> macro_input;
 
   InputDispatch *init_config_input(void);
+  InputDispatch *init_edit_input(void);
+  InputDispatch *init_macro_input(void);
   MenuDispatch *init_scripts_menu(void);
 
   // notifications
@@ -85,6 +89,10 @@ class Director {
   void config_have_input(void);
   std::string config_item;  // current item being edited
 
+  void macro_edit(void);
+  void macro_have_input(void);
+  std::string macro_item;
+  
   StringFunc SL_parser;
   StringFunc SF_cimline, SF_sectorline, SF_portline, SF_warpline, SF_infoline,
       SF_densityline, SF_computer_portline, SF_planetline;