-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.cpp
More file actions
350 lines (294 loc) · 8.53 KB
/
console.cpp
File metadata and controls
350 lines (294 loc) · 8.53 KB
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
#include <curses.h>
#include "console.h"
#include "orasql.h"
#include <string>
#include <stdlib.h>
#include <stdio.h>
using std::string;
bool Console::parse(const string& line)
{
stringstream ss;
try
{
interpreter_.parse_input(line);
// it bellow function do not throw exception, it calls specified function
// which should return results as string.
return true;
}
catch (example::NotAFunction& error)
{
return sql_->statement(line);
}
catch (std::runtime_error &error)
{
return false;
}
return true;
}
Console::Console(OraSql* sql, const string& prompt) : sql_(sql), prompt_(prompt)
{
interpreter_.register_function("clear", &Console::clear, this);
interpreter_.register_function("quit", &Console::quit, this);
interpreter_.register_function("exit", &Console::quit, this);
initscr();
cbreak();
keypad(stdscr, TRUE);
noecho();
}
Console::~Console()
{
endwin();
}
int Console::exec()
{
int ch;
// current row and column - defines cursor position.
int row(0), col(0);
// keeps track how many times tab key was pressed (up key was pressed)
// based on this parameters completion and history is implemented.
int tabhit(0), uphit(0);
size_t found(0);
string word;
// only those keywords which fits typed word.
vector<string> completions;
clear();
while(1)
{
ch = getch();
if (ch == KEY_BACKSPACE ) // || ch == 0x08)
//if (ch == '\a')
{
deleteChar();
}
else if (ch == KEY_UP)
{
if (uphit >= 0 && !history_.empty())
{
deleteToPosition(prompt_.size());
getyx(stdscr, row, col);
mvprintw(row, (int)prompt_.size(), history_[uphit].c_str());
msg_ = history_[uphit];
--uphit;
}
}
else if (ch == KEY_DOWN)
{
if (uphit != (history_.size() -1))
{
++uphit;
deleteToPosition(prompt_.size());
getyx(stdscr, row, col);
mvprintw(row, (int)prompt_.size(), history_[uphit].c_str());
msg_ = history_[uphit];
}
}
else if (ch == KEY_LEFT)
{
getyx(stdscr, row, col);
if (col > (int)prompt_.size())
move(row, col-1);
}
else if (ch == KEY_RIGHT)
{
getyx(stdscr, row, col);
if (col < ((int)msg_.size() + (int)prompt_.size()))
move(row, col+1);
}
// tab key == 0x09
else if (ch == 0x09)
{
// delete char '\t'
delch();
// do some completion.
found = msg_.rfind(" ");
if (found != string::npos)
word = msg_.substr(found+1);
else
word = msg_;
searchWord(word, completions);
if (!completions.empty())
{
deleteToPosition(prompt_.size()+found+1);
mvprintw(row, prompt_.size()+found+1, completions[tabhit].c_str());
++tabhit;
if (tabhit == (int)completions.size()) tabhit = 0;
}
}
else if (ch == ' ')
{
// check if completion was called before space.
if (!completions.empty())
{
if (tabhit == 0)
msg_ = msg_.substr(0, found+1) + completions[completions.size()-1];
else
msg_ = msg_.substr(0, found+1) + completions[tabhit-1];
}
// clear completion variable - job is done!.
completions.clear();
tabhit = 0;
insertChar(ch);
}
// enter key
else if (ch == '\n' || ch == KEY_ENTER)
{
screenbuffer_.push_back(prompt_ + msg_);
int nlines(0);
// do something with string processing.
if (!msg_.empty())
{
bool isOk = parse(msg_);
// whenever was a result, remember last statement.
history_.push_back(msg_);
if (isOk) updateKeywords(msg_);
// reset to last history element.
uphit = history_.size()-1;
string out = sql_->result();
// print whatever it cames out: error or result.
getyx(stdscr, row, col);
nlines = pushToBuffer(out);
if ((row + nlines) > LINES-2)
repaintScreen();
else
{
++row;
int startpos = (int)screenbuffer_.size() - nlines;
for (int k = 0; k < nlines; ++k, ++row)
mvprintw(row, 0, screenbuffer_[startpos+k].c_str());
mvprintw(row, 0, prompt_.c_str());
}
// and if user just pressed "enter" without entring anything
}
else
{
getyx(stdscr, row, col);
if (row > LINES-2)
repaintScreen();
else
{
++row;
mvprintw(row, 0, prompt_.c_str());
}
}
// prepare to start new statment.
msg_.clear();
}
else
{
// inserts or appends char to the message strig and shows on console.
insertChar(ch);
}
refresh();
} // end of while
return 0;
} // end of operator()
void Console::clear()
{
clearScreen();
screenbuffer_.clear();
screenbuffer_.push_back(prompt_);
mvprintw(0,0,prompt_.c_str());
refresh();
}
void Console::repaintScreen()
{
for (int i = 0; i < (screenbuffer_.size() - LINES+1); ++i)
screenbuffer_.pop_front();
clearScreen();
int row, col;
getyx(stdscr, row, col);
for (int k = 0; k < (int)screenbuffer_.size(); ++k, ++row)
mvprintw(row, 0, screenbuffer_[k].c_str());
mvprintw(row, 0, prompt_.c_str());
// refresh();
}
// return number of lines required for the output
int Console::pushToBuffer(const string& out)
{
int nlines(0);
if (out.empty())
return nlines;
// search for line termminator '\n' in output string and print to console. NOTE
std::stringstream ss(out);
std::string line;
while ( std::getline(ss, line))
{
screenbuffer_.push_back(line);
++nlines;
}
return nlines;
}
void Console::deleteChar()
{
int row, col;
getyx(stdscr, row, col);
if (col > (int)prompt_.size())
{
int pos = col - (int)prompt_.size() - 1;
msg_.erase(msg_.begin() + pos);
deleteToPosition(0);
string line = prompt_ + msg_;
mvprintw(row, 0, line.c_str());
move(row, --col);
}
}
void Console::insertChar(char ch)
{
int row, col;
getyx(stdscr, row, col);
msg_.insert(col - (int)prompt_.size(), 1, ch);
deleteToPosition(0);
string line = prompt_ + msg_;
mvprintw(row, 0, line.c_str());
move(row, ++col);
}
void Console::updateKeywords(const string& str)
{
// if parsing succeedes - save statement to history(it could be possible recall in the futurei with UP
// and DOWN keys)
// such statement will also be split into keywords for completion.
istringstream iss(str);
std::copy(istream_iterator<string>(iss), istream_iterator<string>(), std::inserter(keywords_, keywords_.begin()));
}
void Console::quit()
{
endwin();
exit(0);
}
void Console::clearScreen()
{
// clean all terminal
int r, c;
getyx(stdscr, r, c);
move(r, 0);
while(r != 0)
{
deleteln(); move(--r, 0);
}
deleteln();
refresh();
}
void Console::deleteToPosition(int colnr)
{
int c, r;
getyx(stdscr, r, c);
while(c > colnr)
{
mvdelch(r, --c);
}
refresh();
}
void Console::searchWord(const string& str, vector<string>& result)
{
result.clear();
if (str.empty())
return;
std::set<string>::const_iterator cit;
for (cit = keywords_.begin(); cit != keywords_.end(); ++cit)
{
string tmp = *cit;
int res = tmp.compare(0, str.size(), str);
if (res == 0)
result.push_back(tmp);
}
}