-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtinycon.cpp
364 lines (328 loc) · 7.3 KB
/
tinycon.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "tinycon.h"
/*
* Terminal Console
* Usage: tinyConsole tc("prompt>");
* tc.run();
*
* When a command is entered, it will be passed to the trigger method for
* processing. This is a blocking function.
*
* tinyConsole::trigger can be over-ridden in a derivative class to
* change its functionality.
*
* Additionally, a check to the 'hotkeys' method is called for each
* keypress, which can also be over-ridden in a dirivative class.
*
*/
// On Unix-like systems, add getch support without curses
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32)
int getch ()
{
struct termios oldt, newt;
int ch;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
if(ch == ESC)
{
ch = getchar();
if(ch == 91)
{
ch = KEY_CTRL1;
}
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
#endif
tinyConsole::tinyConsole ()
{
_max_history = MAX_HISTORY;
_quit = false;
pos = -1;
line_pos = 0;
skip_out = 0;
}
tinyConsole::tinyConsole (std::string s)
{
_max_history = MAX_HISTORY;
_quit = false;
_prompt = s;
pos = -1;
line_pos = 0;
skip_out = 0;
}
std::string tinyConsole::version ()
{
return TINYCON_VERSION;
}
void tinyConsole::pause ()
{
getch();
}
void tinyConsole::quit ()
{
_quit = true;
}
int tinyConsole::trigger (std::string cmd)
{
if (cmd == "exit") {
_quit = true;
} else {
std::cout << cmd << std::endl;
}
return 0;
}
int tinyConsole::hotkeys (char c)
{
return 0;
}
void tinyConsole::setMaxHistory (int max)
{
_max_history = max;
}
std::string tinyConsole::getLine ()
{
return getLine(M_LINE, "");
}
std::string tinyConsole::getLine (int mode)
{
return getLine(mode, "");
}
std::string tinyConsole::getLine (int mode = M_LINE, std::string delimeter = "")
{
std::string line;
char c;
for (;;)
{
c = getch();
if ( c == NEWLINE)
{
std::cout << std::endl;
return line;
} else if ((int) c == BACKSPACE ) {
if (line.length())
{
line = line.substr(0,line.size()-1);
if (mode != M_PASSWORD)
{
std::cout << "\b \b";
}
}
} else {
line += c;
if (mode != M_PASSWORD)
{
std::cout << c;
}
}
}
}
std::string tinyConsole::getBuffer ()
{
return std::string(buffer.begin(),buffer.end());
}
void tinyConsole::setBuffer (std::string s)
{
buffer.assign(s.begin(),s.end());
line_pos = buffer.size();
}
void tinyConsole::run ()
{
//show prompt
std::cout << _prompt;
// grab input
for (;;)
{
c = getch();
if(!hotkeys(c))
switch (c)
{
case ESC:
// TODO escape is only detected if double-pressed. this should be fixed
std::cout << "(Esc)";
break;
case KEY_CTRL1: // look for arrow keys
switch (c = getch())
{
case UP_ARROW:
if (!history.size()) break;
if (pos == -1)
{
// store current command
unused = "";
unused.assign(buffer.begin(), buffer.end());
}
// clear line
for (int i = 0; i < line_pos; i++)
{
std::cout << "\b \b";
}
// clean buffer
buffer.erase(buffer.begin(), buffer.end());
pos++;
if (pos > (history.size() - 1)) pos = history.size() - 1;
// store in buffer
for (int i = 0; i < history[pos].size(); i++)
{
buffer.push_back(history[pos][i]);
}
line_pos = buffer.size();
// output to screen
std::cout << history[pos];
break;
case DOWN_ARROW:
if (!history.size()) break;
// clear line
for (int i = 0; i < line_pos; i++)
{
std::cout << "\b \b";
}
// clean buffer
buffer.erase(buffer.begin(), buffer.end());
pos--;
if (pos<-1) pos = -1;
if (pos >= 0) {
std::cout << history[pos];
// store in buffer
for (int i = 0; i < history[pos].size(); i++)
{
buffer.push_back(history[pos][i]);
}
} else {
if (buffer.size())
{
std::cout << unused;
// store in buffer
for (int i = 0; i < unused.size(); i++)
{
buffer.push_back(unused[i]);
}
}
}
line_pos = buffer.size();
break;
case LEFT_ARROW:
// if there are characters to move left over, do so
if (line_pos)
{
std::cout << "\b";
line_pos--;
}
break;
case RIGHT_ARROW:
// if there are characters to move right over, do so
if (line_pos < buffer.size())
{
std::cout << buffer[line_pos];
line_pos++;
}
break;
case DEL:
if (line_pos < buffer.size())
{
skip_out = 1;
buffer.erase(buffer.begin()+line_pos);
// update screen after current position
for (int i = line_pos; i < buffer.size(); i++ )
{
std::cout << buffer[i];
}
// erase last char
std::cout << " ";
for (int i = line_pos; i < buffer.size(); i++ )
{
std::cout << "\b";
}
// make-up for erase position
std::cout << "\b";
//std::cout << "(DEL)";
}
break;
default:
skip_out = 1;
//std::cout << "(" << (int) c << ")" << std::endl;
}
break;
case BACKSPACE:
if (line_pos == 0) break;
// move cursor back, blank char, and move cursor back again
std::cout << "\b \b";
// don't forget to clean the buffer and update line position
if (line_pos == buffer.size()) {
buffer.pop_back();
line_pos--;
} else {
line_pos--;
buffer.erase(buffer.begin()+line_pos);
// update screen after current position
for (int i = line_pos; i < buffer.size(); i++ ) {
std::cout << buffer[i];
}
// erase last char
std::cout << " ";
for (int i = line_pos+1; i < buffer.size(); i++ ) {
std::cout << "\b";
}
// make-up for erase position and go to new position
std::cout << "\b\b";
}
break;
case TAB:
break;
// print history
for (int i = 0; i < history.size(); i++) {
std::cout << history[i] << std::endl;
}
break;
case NEWLINE:
// store in string
s.assign(buffer.begin(), buffer.end());
// save command to history
// trimming of command should be done in callback function
if(s.length())
history.push_front(s);
// run command
//(*callbackFunc)(s.c_str());
std::cout << std::endl;
trigger(s);
// check for exit command
if(_quit == true) {
return;
}
if (history.size() > _max_history) history.pop_back();
// clean buffer
buffer.erase(buffer.begin(), buffer.end());
// print prompt. new line should be added from callback function
std::cout << _prompt;
// reset position
pos = -1;
line_pos = 0;
break;
default:
if (skip_out) {
skip_out = 0;
break;
}
std::cout << c;
if(line_pos == buffer.size()) {
// line position is at the end of the buffer, just append
buffer.push_back(c);
} else {
// line position is not at end. Insert new char
buffer.insert(buffer.begin()+line_pos, c);
// update screen after current position
for (int i = line_pos+1; i < buffer.size(); i++ ) {
std::cout << buffer[i];
}
for (int i = line_pos+1; i < buffer.size(); i++ ) {
std::cout << "\b";
}
}
line_pos++;
break;
} // end switch
}
}