-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
205 lines (159 loc) · 7.06 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <regex>
#include <iomanip>
#include <iostream>
#include "cpp-readline/src/Console.hpp"
#include "commands.cpp"
#include "replxx_utils.h"
#include "hashidsxx/hashids.h"
#define DEFAULT_DB "walletDB"
using Replxx = replxx::Replxx;
namespace cr = CppReadline;
using ret = cr::Console::ReturnCode;
int main(int argc, char *argv[]) {
cr::Console c("\x1b[1;32mcb-cli\x1b[0m> ");
// TODO: error handling
std::string wallet_name;
if(argc < 2)
wallet_name = DEFAULT_DB;
else
wallet_name = argv[1];
// init the hashidsxx
hashidsxx::Hashids id_generator("salt", 16, "abcdefghijklmnopqrstuvwxyz");
// this counter is used to create unique ID's for each request
unsigned request_counter = 1;
// registering commands
c.registerCommand(".info", info, "this command shows the manual for this application.");
c.registerCommand(".get_accounts", get_accounts, "this command will return list of wallet accounts.");
c.registerCommand(".get_confidential_address", get_confidential_address, "this command will return address of confidential account");
c.registerCommand(".get_balance", get_balance, "this command will return balances of wallet.");
c.registerCommand(".get_notes", get_notes, "this command will return list of wallet notes.");
c.registerCommand(".get_registered_accounts", get_registered_accounts, "this command will return list of wallet registered accounts.");
c.registerCommand(".purge_accounts", purge_accounts, "this command will delete all of wallet accounts.");
c.registerCommand(".add_account", add_account, "this command will create new account in wallet.");
c.registerCommand(".register_account", register_account, "this command will register provided account in wallet.");
c.registerCommand(".get_details", get_details, "this command will get details of provided account.");
c.registerCommand(".transaction", transaction, "this command will perform transactions.");
c.registerCommand(".confidential_tx_mint", confidential_tx_mint, "this command will perform confidential transactions.[mint]"); //TODO
c.registerCommand(".confidential_tx_pour", confidential_tx_pour, "this command will perform confidential transactions.[pour]"); //TODO
c.registerCommand(".test", test, "this method is a dummy command for debugging purposes.");
// words to be completed
std::vector<std::string> examples = {".clear"};
// highlight specific words
// a regex string, and a color
// the order matters, the last match will take precedence
using cl = Replxx::Color;
std::vector<std::pair<std::string, cl>> regex_color {
// single chars
{"\\`", cl::BRIGHTCYAN},
{"\\'", cl::BRIGHTBLUE},
{"\\\"", cl::BRIGHTBLUE},
{"\\-", cl::BRIGHTBLUE},
{"\\+", cl::BRIGHTBLUE},
{"\\=", cl::BRIGHTBLUE},
{"\\/", cl::BRIGHTBLUE},
{"\\*", cl::BRIGHTBLUE},
{"\\^", cl::BRIGHTBLUE},
{"\\.", cl::BRIGHTMAGENTA},
{"\\(", cl::BRIGHTMAGENTA},
{"\\)", cl::BRIGHTMAGENTA},
{"\\[", cl::BRIGHTMAGENTA},
{"\\]", cl::BRIGHTMAGENTA},
{"\\{", cl::BRIGHTMAGENTA},
{"\\}", cl::BRIGHTMAGENTA},
// color keywords
{"color_black", cl::BLACK},
{"color_red", cl::RED},
{"color_green", cl::GREEN},
{"color_brown", cl::BROWN},
{"color_blue", cl::BLUE},
{"color_magenta", cl::MAGENTA},
{"color_cyan", cl::CYAN},
{"color_lightgray", cl::LIGHTGRAY},
{"color_gray", cl::GRAY},
{"color_brightred", cl::BRIGHTRED},
{"color_brightgreen", cl::BRIGHTGREEN},
{"color_yellow", cl::YELLOW},
{"color_brightblue", cl::BRIGHTBLUE},
{"color_brightmagenta", cl::BRIGHTMAGENTA},
{"color_brightcyan", cl::BRIGHTCYAN},
{"color_white", cl::WHITE},
{"color_normal", cl::NORMAL},
// commands
{"\\.help", cl::BRIGHTMAGENTA},
{"\\.history", cl::BRIGHTMAGENTA},
{"\\.quit", cl::BRIGHTMAGENTA},
{"\\.exit", cl::BRIGHTMAGENTA},
{"\\.clear", cl::BRIGHTMAGENTA},
{"\\.prompt", cl::BRIGHTMAGENTA},
// numbers
{"[\\-|+]{0,1}[0-9]+", cl::YELLOW}, // integers
{"[\\-|+]{0,1}[0-9]*\\.[0-9]+", cl::YELLOW}, // decimals
{"[\\-|+]{0,1}[0-9]+e[\\-|+]{0,1}[0-9]+", cl::YELLOW}, // scientific notation
// strings
{"\".*?\"", cl::BRIGHTGREEN}, // double quotes
{"\'.*?\'", cl::BRIGHTGREEN}, // single quotes
};
for(auto k: c.getRegisteredCommands()) {
examples.push_back(k);
regex_color.push_back({k, cl::BRIGHTMAGENTA});
}
// init the replxx
Replxx rx;
rx.install_window_change_handler();
// the path to the history file
std::string history_file {"./replxx_history.txt"};
// load the history file if it exists
rx.history_load(history_file);
// set the max history size
rx.set_max_history_size(12);
// set the max input line size
rx.set_max_line_size(512);
// set the max number of hint rows to show
rx.set_max_hint_rows(10);
// set the callbacks
rx.set_completion_callback(hook_completion, static_cast<void*>(&examples));
rx.set_highlighter_callback(hook_color, static_cast<void*>(®ex_color));
rx.set_hint_callback(hook_hint, static_cast<void*>(&examples));
// display initial welcome message
std::cout
<< "Welcome to confidential-bank client\n"
<< "version[confidential-bank]:CB-0.0.1\n"
<< "Press 'tab' to view autocompletions\n"
<< "Type '.help' for help \n"
<< "Type '.quit' or '.exit' to exit\n"
<< "wallet name: " << wallet_name << "\n\n";
// set the repl prompt
std::string prompt {"\x1b[1;32mcb-cli\x1b[0m> "};
int retCode;
// main repl loop
for (;;) {
// display the prompt and retrieve input from the user
char const *cinput = rx.input(prompt);
std::string input{cinput};
if (cinput == nullptr) {
// reached EOF
std::cout << "\n";
//break;
} else if (input.compare(0, 6, ".clear") == 0) {
// clear the screen
rx.clear_screen();
rx.history_add(input);
continue;
} else {
retCode = c.readLine(cinput, id_generator.encode(request_counter), wallet_name);
request_counter++;
if (retCode == ret::Quit) {
rx.history_add(std::string{cinput});
break;
} else {
if (retCode >= 1)
std::cout << "Error in reading commands. use '.help' command for more information." << std::endl;
rx.history_add(std::string{cinput});
continue;
}
}
}
//save the history
rx.history_save(history_file);
std::cout << "\nExiting cb-cli\n";
}