CLI v1.0.0
Custom cli console for creating commands executable at runtime
Loading...
Searching...
No Matches
cli.hpp
1#pragma once
2#include <cmath>
3#include <deque>
4#include <functional>
5#include <memory>
6#include <optional>
7#include <string>
8#include <termios.h>
9#include <unistd.h>
10#include <unordered_map>
11
12namespace cli {
13
21class Console {
22public:
23 class Node {
24 friend class Console;
25
26 private:
27 std::unordered_map<std::string, std::shared_ptr<Node>> children;
28
29 public:
33 std::function<void(const std::vector<std::string> &)> function = nullptr;
34
35 public:
50 Console::Node &operator[](const std::string &key) {
51 std::shared_ptr<Node> &ret = children[key];
52 if (!ret)
53 ret = std::make_shared<Node>();
54 return *ret;
55 }
56 };
57
63 bool stop = false;
67 struct {
74 bool newLineAutoComplete = true;
80 int histSize = 10;
82
83private:
84 std::unordered_map<std::string, std::shared_ptr<Node>> nodes;
85 std::string buf = "";
86 std::deque<std::string> hist;
87 int histIndex = -1;
88
89public:
105 Node &operator[](const std::string &key);
106
112 void Run();
113
128 void Run(std::string filename);
129
130private:
131 void SetRawMode_(bool enable);
132
133 std::optional<char> ReadChar_();
134
135 bool Input_available_(int fd, int timeout_ms);
136
137 void HandleEscapeChar_();
138
139 void AutoComplete_();
140
141 void SaveToHist_();
142
143 void ExecuteCommand_();
144
145 int HandleInput_(char ch);
146
147 bool VerifyHelper_(const std::shared_ptr<Node> &node);
148
149 bool Verify_();
150
151 void SetupDefaultCommands_();
152};
153} // namespace cli
Definition cli.hpp:23
Console::Node & operator[](const std::string &key)
Used to add a new command.
Definition cli.hpp:50
std::function< void(const std::vector< std::string > &)> function
A function to call when executing a command.
Definition cli.hpp:33
Console represents the customisable console.
Definition cli.hpp:21
Node & operator[](const std::string &key)
Used to add a new command.
Definition cli.cpp:8
struct cli::Console::@0 settings
Console settings.
bool stop
Manages if the main loop should stop.
Definition cli.hpp:63
void Run()
Starts the custom console.
Definition cli.cpp:269