Browse Source

Adding boxes class.

Steve Thielemann 3 years ago
parent
commit
d5ed19fcc5
6 changed files with 182 additions and 22 deletions
  1. 1 1
      CMakeLists.txt
  2. 78 0
      boxes.cpp
  3. 56 0
      boxes.h
  4. 10 0
      dispatchers.cpp
  5. 19 21
      dispatchers.h
  6. 18 0
      session_common.h

+ 1 - 1
CMakeLists.txt

@@ -33,6 +33,6 @@ FIND_PACKAGE( Boost 1.60 COMPONENTS program_options log_setup log REQUIRED )
 INCLUDE_DIRECTORIES( ${Boost_INCLUDE_DIR} )
 
 
-ADD_EXECUTABLE( twproxy twproxy.cpp config.cpp session.cpp dispatchers.cpp )
+ADD_EXECUTABLE( twproxy twproxy.cpp config.cpp session.cpp dispatchers.cpp boxes.cpp )
 TARGET_LINK_LIBRARIES( twproxy ${Boost_LIBRARIES} pthread )
 

+ 78 - 0
boxes.cpp

@@ -0,0 +1,78 @@
+#include "boxes.h"
+
+Boxes::Boxes(int size, int style, int color, bool newline) {
+  size_ = size;
+  style_ = style;
+  color_ = color;
+  newline_ = newline;
+}
+
+std::string Boxes::top(void) {
+  std::string line;
+  box_style &bs = boxes[style_];
+  const char *clr = colors[color_];
+
+  line.append(clr);
+  line.append(bs.top_left);
+  for (int x = 0; x < size_; ++x)
+    line.append(bs.top);
+  line.append(bs.top_right);
+  line.append(reset);
+  if (newline_)
+    line.append(nl);
+  return line;
+}
+
+std::string Boxes::middle(void) {
+  std::string line;
+  box_style &bs = boxes[style_];
+  const char *clr = colors[color_];
+  line.append(clr);
+  line.append(bs.middle_left);
+  for (int x = 0; x < size_; ++x)
+    line.append(bs.top);
+  line.append(bs.middle_right);
+  line.append(reset);
+  if (newline_)
+    line.append(nl);
+
+  return line;
+}
+
+std::string Boxes::row(std::string &text) {
+  std::string line;
+  box_style &bs = boxes[style_];
+  const char *clr = colors[color_];
+  line.append(clr);
+  line.append(bs.side);
+  line.append(reset);
+  line.append(text);
+  line.append(clr);
+  line.append(bs.side);
+  line.append(reset);
+  if (newline_)
+    line.append(nl);
+
+  return line;
+}
+
+std::string Boxes::bottom(void) {
+  std::string line;
+  box_style &bs = boxes[style_];
+  const char *clr = colors[color_];
+  line.append(clr);
+  line.append(bs.bottom_left);
+  for (int x = 0; x < size_; ++x)
+    line.append(bs.top);
+  line.append(bs.bottom_right);
+  line.append(reset);
+  if (newline_)
+    line.append(nl);
+
+  return line;
+}
+
+/*
+std::tuple<std::string, std::string, std::string>
+Boxes::alert(std::string message, int size, int color, int style = 1) {}
+*/

+ 56 - 0
boxes.h

@@ -0,0 +1,56 @@
+#ifndef BOXES_H
+#define BOXES_H
+
+#include <string>
+#include <tuple>
+
+enum { NORMAL = 0, BRIGHT = 1 } ATTR;
+
+enum {
+
+} FORE;
+
+enum {
+
+} BACK;
+
+class Boxes {
+  struct box_style {
+    const char *top_left;
+    const char *top_right;
+    const char *top;
+    const char *side;
+    const char *bottom_left;
+    const char *bottom_right;
+    const char *middle_left;
+    const char *middle_right;
+  } boxes[4] = {
+      {"\xda", "\xbf", "\xc4", "\xb3", "\xc0", "\xd9", "\xc3", "\xb4"},
+      {"\xc9", "\xbb", "\xcd", "\xba", "\xc8", "\xbc", "\xcc", "\xb9"},
+      {"\xd6", "\xb8", "\xcd", "\xb3", "\xd4", "\xbe", "\xc6", "\xb5"},
+      {"\xd6", "\xb7", "\xc4", "\xba", "\xd3", "\xbd", "\xc7", "\xb6"}};
+  int style_;
+  int color_;
+  int size_;
+  bool newline_;
+  const char *nl = "\n\r";
+  const char *reset = "\x1b[0m";
+  // Black Red Green Yellow Blue Magenta Cyan White
+  const char *colors[8] = {"\x1b[1;33;40m", "\1b[1;33;41m",  "\x1b[1;33;42m",
+                           "\x1b[1;33;43m", "\x1b[1;33;44m", "\x1b[1;33;45m",
+                           "\x1b[1;33;46m", "\x1b[1;33;47m"};
+
+public:
+  Boxes(int size, int style = 1, int color = 1, bool newline = true);
+  std::string top(void);
+  std::string middle(void);
+  std::string row(std::string &line);
+  std::string bottom(void);
+
+  /*
+    std::tuple<std::string, std::string, std::string>
+    alert(std::string message, int size, int color, int style = 1);
+    */
+};
+
+#endif

+ 10 - 0
dispatchers.cpp

@@ -1,6 +1,7 @@
 #include <boost/format.hpp>
 #include <cctype>
 
+#include "boxes.h"
 #include "dispatchers.h"
 #include "logging.h"
 
@@ -38,6 +39,15 @@ void MainDispatch::activate(void) {
   sess->talk_direct = false; // do not auto-send client to server
   count = 0;
   old_prompt = sess->get_prompt();
+  Boxes box(40, 1, 4);
+  to_client("\n\r");
+  to_client(box.top());
+  std::string output = "Proxy ACTIVE! Welcome!";
+  while (output.length() < 40)
+    output.append(" ");
+
+  to_client(box.row(output));
+  to_client(box.bottom());
   to_client("\n\r\x1b[1;34mWELCOME!  You are now in the proxy zone...\n\r");
   id.prompt = "Proxy Main :=> ";
   id.max_length = 25;

+ 19 - 21
dispatchers.h

@@ -41,8 +41,6 @@ public:
   void to_client(const std::string &send);
 };
 
-
-
 class InputDispatch : public Dispatch {
 private:
   DispatchSettings ds;
@@ -63,14 +61,31 @@ public:
   void client_input(const std::string &cinput);
 };
 
+class MenuDispatch : public Dispatch {
+private:
+  DispatchSettings ds;
+
+public:
+  MenuDispatch(Session *);
+
+  int choice;
+
+  void activate(void) override;
+  void deactivate(void) override;
+
+  // optional here
+  void server_line(const std::string &line);
+  // void server_prompt(const std::string &prompt);
+  void client_input(const std::string &cinput);
+};
 
 /**
  * The main/first proxy Dispatcher.
- * 
+ *
  * Don't follow this as an example.  On disable,
  * it resets everything back to nothing active.
  * (Which is likely not what you want.)
- * 
+ *
  */
 
 class MainDispatch : public Dispatch {
@@ -95,23 +110,6 @@ private:
   std::string old_prompt;
 };
 
-
-
-class MenuDispatch : public Dispatch {
-public:
-  MenuDispatch(Session *);
-
-  void activate(void) override;
-  void deactivate(void) override;
-
-  // optional here
-  void server_line(const std::string &line);
-  void server_prompt(const std::string &prompt);
-  void client_input(const std::string &input);
-};
-
-
-
 class CoreDispatch : public Dispatch {
 public:
   CoreDispatch(Session *);

+ 18 - 0
session_common.h

@@ -0,0 +1,18 @@
+#ifndef SESSION_COMMON_H
+#define SESSION_COMMON_H
+
+#include <functional>
+#include <string>
+
+typedef std::function<void(const std::string &)> StringFunc;
+typedef std::function<void(void)> notifyFunc;
+
+struct DispatchSettings {
+  StringFunc server_line;
+  StringFunc server_prompt;
+  StringFunc client_input;
+  bool show_client;
+  bool talk_direct;
+};
+
+#endif