1 
2 #ifndef _CONSOLE_H
3 #define _CONSOLE_H
4 
5 #define CONSOLE_MAXCMDLEN 48
6 #define CONSOLE_MAXRESPONSELEN 48
7 
8 // how many commands to remember in the backbuffer
9 #define CONSOLE_MAX_BACK 8
10 #include <string>
11 #include <vector>
12 #include <spdlog/fmt/fmt.h>
13 
14 struct CommandEntry
15 {
16   std::string name;
17   void (*handler)(std::vector<std::string> *args, int num);
18   unsigned int minArgs, maxArgs;
19   std::string help;
20 };
21 
22 class DebugConsole
23 {
24 public:
25   DebugConsole();
26 
27   void SetVisible(bool newstate);
28   bool IsVisible();
29 
30   bool HandleKey(int key);
31   void HandleKeyRelease(int key);
32   void Draw();
33 
34   bool Execute(std::string& line);
35 
Print(const std::string & format,Args...args)36   template<typename... Args> void Print(const std::string& format, Args... args)
37   {
38     fResponse.push_back(fmt::format(format, args...));
39     fResponseTimer = 60;
40   }
41 
getCommands()42   std::vector<CommandEntry>& getCommands()
43   {
44     return commands;
45   }
46 
47 private:
48   void DrawDebugText(const std::string& text, int y = 16);
49   void MatchCommand(const std::string& cmd, std::vector<CommandEntry>& matches);
50   std::string SplitCommand(const std::string& line_in, std::vector<std::string>& args);
51   void ExpandCommand();
52 
53   std::string fLine = "";
54   int fKeyDown = 0;
55   int fRepeatTimer = 0;
56 
57   std::string fLineToExpand = "";
58   bool fBrowsingExpansion;
59   unsigned int fExpandIndex;
60 
61   std::vector<std::string> fResponse;
62   int fResponseTimer = 0;
63 
64   int fCursorTimer = 0;
65   bool fVisible = false;
66 
67   // up-down last-command buffer
68   int fBackIndex;
69   std::vector<std::string> fBackBuffer;
70   std::vector<CommandEntry> commands;
71 };
72 
73 extern DebugConsole console;
74 
75 #endif
76