-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputLine.cc
More file actions
582 lines (516 loc) · 20.2 KB
/
InputLine.cc
File metadata and controls
582 lines (516 loc) · 20.2 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// The input line
#include "InputLine.h"
#include "misc.h"
#include "TTY.h"
#include "Interpreter.h"
#include "EmbeddedInterpreter.h"
#include "Screen.h"
#include "Config.h"
#include "Color.h"
#include "Selection.h"
#include "Session.h"
#include "OutputWindow.h"
#include "StatusLine.h"
#include "StaticBuffer.h"
#include "MessageBox.h"
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <assert.h>
History::History(int _id) : id (_id), current(0) {
max_history = config->getOption(opt_histsize);
strings = new string[max_history];
timestamps = new time_t[max_history];
}
History::~History() {
delete[] strings;
delete[] timestamps;
}
void History::add(const string& s,time_t t) {
// Don't store multiple strings that are exactly the same
if (current > 0 && s == strings[(current-1) % max_history])
return;
strings[current % max_history] = s;
timestamps[current % max_history] = t;
current++;
}
// getting number 1 gets you the LAST line
const char * History::get(int count, time_t* timestamp) {
if (count > min(current, max_history))
return NULL;
if (timestamp)
*timestamp = timestamps[(current - count) % max_history];
return strings[(current - count) % max_history].c_str();
}
HistorySet::HistorySet() : hist_list(hi_search_scrollback+1) {
for(int i=hi_none;i<=hi_search_scrollback;i++) {
hist_list[i] = new History(i);
}
}
HistorySet::~HistorySet() {
for (hist_list_t::iterator it = hist_list.begin(); it != hist_list.end(); it++) {
delete *it;
hist_list.erase(it);
}
}
void HistorySet::saveHistory() {
if (config->getOption(opt_save_history)) {
FILE *fp = fopen(Sprintf("%s/.dirt/history", getenv("HOME")), "w");
if (fp) {
const char *s;
time_t t;
fchmod(fileno(fp), 0600);
for(hist_list_t::iterator it = hist_list.begin(); it != hist_list.end(); it++)
for (int i = config->getOption(opt_histsize) ; i > 0; i--)
if ((s = get((history_id)(*it)->id,i, &t)))
fprintf(fp, "%d %ld %s\n", (*it)->id, t, s);
fclose(fp);
}
}
}
void HistorySet::loadHistory() {
if (config->getOption(opt_save_history)) {
FILE *fp = fopen(Sprintf("%s/.dirt/history", getenv("HOME")), "r");
if (fp) {
int id;
time_t t;
char buf[4096];
while (3 == fscanf(fp, "%d %ld %1024[^\n]", &id, &t, buf))
hist_list[id]->add(buf,t);
fclose(fp);
}
/*
ifstream hin(Sprintf("%s/.dirt/history", getenv("HOME")));
if (hin.is_open()) {
int id;
time_t t;
char buf[4096], c;
//while(in.peek() != in.eof()) {
while(!hin.eof()) {
hin.getline(buf, 4096); // Dies right here...never returns...
// cout << "Got history line: " << buf << endl;
istringstream sbuf(buf);
sbuf >> id >> t;
sbuf.get(c); // skip the leading space.
sbuf.getline(buf,4096);
hist_list[id]->add(buf, t);
}
hin.close();
}
*/
}
}
class InputHistorySelection : public Selection{
public:
InputHistorySelection(Window *parent, int w, int h, int x, int y, InputLine& _input, bool _enterExecutes, history_id _historyId) : Selection(parent,w,h,x,y),
inputLine(_input), enterExecutes(_enterExecutes), historyId(_historyId) {
const char *s;
for (int i = 1; (s = history->get(historyId,i, NULL)); i++)
prepend_string(s);
}
virtual void idle() {
doSelect(getSelection());
force_update();
}
virtual void doSelect(int no) {
time_t t;
char buf[64];
assert(history->get(historyId, getCount() - no, &t));
strftime(buf, sizeof(buf), "%H:%M:%S", localtime(&t));
const char *unit = "second";
int n = current_time - t;
if (n > 120) {
n /= 60;
unit = "minute";
if (n > 120) {
n /= 60;
unit = "hour";
if (n > 48) {
n /= 24;
unit = "day";
}
}
}
set_bottom_message(Sprintf("Executed on: %s (%d %s%s ago)", buf, n, unit, n == 1 ? "" : "s"));
}
virtual void doChoose(int no, int key) {
const char *s = history->get(historyId, getCount() - no,NULL);
assert(s != NULL);
inputLine.set(s);
inputLine.keypress(key);
die();
}
virtual bool keypress(int key) { // adds editing keys as valid selectors
dirty = true;
if(selection >= 0 && (key == '\n' || key == '\r' || key == key_arrow_right
|| key == key_arrow_left || key == key_ctrl_h || key == key_backspace
|| key == key_ctrl_a || key == key_ctrl_j || key == key_ctrl_k
|| key == key_ctrl_e || key == key_ctrl_w || key == key_delete
|| key == key_kp_enter || key == ' ' || key == key_tab)) {
doChoose(selection, key);
return true;
} else {
return Selection::keypress(key);
}
}
private:
InputLine& inputLine; // Input line to do the changes on
bool enterExecutes; // Enter sends \r to the input line too
history_id historyId; // History to get the data from
};
InputLine::InputLine(Window *_parent, int _w, int _h, Style _style, int _x, int _y, history_id _id)
: Window(_parent, _w, _h, _style, _x, _y),
cursor_pos(0), max_pos(0), left_pos(0), ready(false), id(_id), history_pos(0)
{
set_default_prompt();
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_a", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_a, "", &InputLine::keypress_ctrl_a, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_c", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_c, "", &InputLine::keypress_ctrl_c, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_k", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_k, "", &InputLine::keypress_ctrl_k, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_j", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_j, "", &InputLine::keypress_ctrl_k, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_escape", vector<string>(1, "Dirt keys"),
"", "", key_escape, "", &InputLine::keypress_escape, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_backspace", vector<string>(1, "Dirt keys"),
"", "", key_backspace, "", &InputLine::keypress_backspace, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_h", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_h, "", &InputLine::keypress_backspace, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_e", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_e, "", &InputLine::keypress_ctrl_e, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_u", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_u, "", &InputLine::keypress_ctrl_u, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_ctrl_w", vector<string>(1, "Dirt keys"),
"", "", key_ctrl_w, "", &InputLine::keypress_ctrl_w, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_delete", vector<string>(1, "Dirt keys"),
"", "", key_delete, "", &InputLine::keypress_delete, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_enter", vector<string>(1, "Dirt keys"),
"", "", key_enter, "", &InputLine::keypress_enter, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_kp_enter", vector<string>(1, "Dirt keys"),
"", "", key_kp_enter, "", &InputLine::keypress_enter, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_arrow_left", vector<string>(1, "Dirt keys"),
"", "", key_arrow_left, "", &InputLine::keypress_arrow_left, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_arrow_right", vector<string>(1, "Dirt keys"),
"", "", key_arrow_right, "", &InputLine::keypress_arrow_right, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_arrow_up", vector<string>(1, "Dirt keys"),
"", "", key_arrow_up, "", &InputLine::keypress_arrow_up, (void*)this));
hook.add(KEYPRESS, new KeypressHookStub(-1, 1.0, -1, false, true, true,
"__DIRT_BUILTIN_InputLine::keypress_arrow_down", vector<string>(1, "Dirt keys"),
"", "", key_arrow_down, "", &InputLine::keypress_arrow_down, (void*)this));
}
void InputLine::set_default_prompt() {
strcpy(prompt_buf, ">");
adjust();
dirty = true;
}
void InputLine::set(const char *s) {
strcpy (input_buf, s);
max_pos = strlen(input_buf);
cursor_pos = max_pos;
left_pos = 0;
adjust();
dirty = true;
}
void MainInputLine::set (const char *s) {
InputLine::set(s);
if (*s == NUL) { // clear, go back to standard size
move(0, parent->height-1);
resize(width, 1);
outputWindow->move(0,0);
}
}
// Handle a keypress
bool InputLine::keypress(int key) {
lastkey = key;
if (ready)
return true;
embed_interp->set("Key", key);
dirty = true; // let's just assume this to make things easier
// report("calling hook.run(KEYPRESS, %s)", input_buf);
hook.run(KEYPRESS, input_buf); // FIXME MOVE to TTY::check_fdset
// report("returned from hook.run(KEYPRESS, %s)", input_buf);
max_pos = strlen(input_buf);
cursor_pos = min(max_pos, max(cursor_pos, (unsigned int) 0));
adjust();
// set Key to 0 if keypress handled. Ugh FIXME: make all of the below hooks.
if ((key = embed_interp->get_int("Key")) == 0)
return true;
if (key >= ' ' && key < key_backspace) { // Normal key. Just insert
// if (key >= ' ' && key < 127) { // Normal key. Just insert
if (max_pos < MAX_INPUT_BUF-1) {
if (cursor_pos == max_pos) { // We are already at EOL
input_buf[max_pos++] = key;
cursor_pos++;
} else { // We are inserting somewhere in the middle
memmove(input_buf + cursor_pos +1, input_buf + cursor_pos, max_pos - cursor_pos);
max_pos++;
input_buf[cursor_pos++] = key;
}
adjust();
}
else
status->setf ("The input buffer is full");
input_buf[max_pos] = NUL;
return true;
}
return false;
}
void InputLine::redraw() {
int prompt_len = strlen(prompt_buf);
gotoxy(0,0);
set_color(config->getOption(opt_inputcolor));
input_buf[max_pos] = NUL;
if (config->getOption(opt_multiinput) && isExpandable()) {
printf("%s%s%*s", prompt_buf, input_buf, (height*width)-prompt_len-max_pos, "");
if (is_focused())
set_cursor((cursor_pos+prompt_len)%width, (cursor_pos+prompt_len)/width);
} else {
printf("%s%s%-*.*s", *prompt_buf != '\0' ? prompt_buf : "", left_pos ? "<" : "",
width-1-prompt_len + (left_pos ? 0 : 1),
width-1-prompt_len + (left_pos ? 0 : 1),
input_buf+left_pos);
if (is_focused())
set_cursor(cursor_pos+prompt_len-left_pos + (left_pos ? 1 : 0) ,0);
}
dirty = false;
}
// If an input line is ready, move it over to buf
bool InputLine::getline(char *buf, bool fForce)
{
if (!ready && !fForce)
return false;
else
{
ready = false;
if (fForce)
input_buf[max_pos] = NUL;
strcpy (buf, input_buf);
max_pos = left_pos = 0;
return true;
}
}
void InputLine::adjust() {
if (config->getOption(opt_multiinput) && isExpandable()) {
int newheight = 1 + (strlen(prompt_buf) + max_pos)/width;
int adjust = newheight - height;
status->setf("height: %d, adjust = %d, 1-(height+adjust)=%d", height, adjust, 1-(height+adjust));
if(adjust) {
// FIXME: This doesn't work.
// There is some strange interactions going on at the Window level when I resize things.
// For instance, after the outputWindow->move() call, the HEIGHT of the InputLine is changed.
move(parent_x, parent_y - adjust);
report("move(%d, %d)", parent_x, parent_y-adjust);
outputWindow->move(0, 1-(height+adjust));
report("outputWindow->move(0, %d)", 1-(height+adjust));
resize(width, height+adjust);
report("resize(%d, %d) height: %d, adjust: %d", width, height+adjust, height, adjust);
//outputWindow->resize(outputWindow->width, outputWindow->height - adjust); // causes segfault.
//parent->resize(parent->width, parent->height - adjust);
}
}
else
while (1 + (unsigned int) strlen (prompt_buf) + cursor_pos - left_pos >= (unsigned)width)
left_pos++;
}
void InputLine::set_prompt (const char *s) {
const char *in;
char *out;
for (in = s, out = prompt_buf; *in && out-prompt_buf < MAX_PROMPT_BUF-1; in++)
if (*in == (signed char) SET_COLOR)
in++;
else if (*in == '\n' || *in == '\r')
*out++ = ' ';
else
*out++ = *in;
*out++ = NUL;
dirty = true;
}
// go to beginning of line.
bool InputLine::keypress_ctrl_a(string&, void* mt) {
InputLine* mythis = (InputLine*)mt;
mythis->cursor_pos = mythis->left_pos = 0;
return true;
}
// save line to history but don't execute
bool InputLine::keypress_ctrl_c(string& inputline, void* mt) {
InputLine* mythis = (InputLine*)mt;
if(inputline.length()) {
history->add (mythis->id, mythis->input_buf);
inputline = "";
status->setf("Line added to history but not sent");
}
return true;
}
// delete until EOL
bool InputLine::keypress_ctrl_k(string& inputline, void* mt) {
InputLine* mythis = (InputLine*)mt;
if(mythis->cursor_pos <= (unsigned int)inputline.length())
inputline.erase(mythis->cursor_pos);
return true;
}
// erase input line
bool InputLine::keypress_escape(string& inputline, void*) {
inputline = "";
return true;
}
// delete one character to left
bool InputLine::keypress_backspace(string& inputline, void* mt) {
InputLine* mythis = (InputLine*)mt;
if (mythis->max_pos != 0 && mythis->cursor_pos != 0) {
if(mythis->cursor_pos <= (unsigned int)inputline.length()+1)
inputline.erase(--mythis->cursor_pos, 1);
mythis->left_pos = max((int) 0,(int)mythis->left_pos-1);
}
return true;
}
// go to end of line.
bool InputLine::keypress_ctrl_e(string&, void* mt) {
InputLine* mythis = (InputLine*)mt;
mythis->cursor_pos = mythis->max_pos;
return true;
}
// Delete one word
bool InputLine::keypress_ctrl_w(string& inputline, void* mt) {
InputLine* mythis = (InputLine*)mt;
// How long is the word?
int bow = mythis->cursor_pos - 1;
while (bow > 0 && isspace(inputline[bow])) bow--; // Scan to end of word (skip space)
while (bow > 0 && !isspace(inputline[bow])) bow--; // Scan to beginning of word
if (bow > 0) bow++; // Don't eat the space
if (bow >= 0 ) {
inputline.erase(bow, mythis->cursor_pos - bow);
mythis->cursor_pos = bow;
}
return true;
}
// delete to beginning of line
bool InputLine::keypress_ctrl_u(string& inputline, void* mt) {
InputLine* mythis = (InputLine*)mt;
if(mythis->cursor_pos <= (unsigned int)inputline.length())
inputline.erase(0, mythis->cursor_pos);
mythis->cursor_pos = 0;
return true;
}
// delete one char to the right
bool InputLine::keypress_delete(string& inputline, void* mt) {
InputLine* mythis = (InputLine*)mt;
if (mythis->cursor_pos != mythis->max_pos)
inputline = inputline.substr(0,mythis->cursor_pos)
+ inputline.substr(mythis->cursor_pos+1, mythis->max_pos);
return true;
}
// execute
bool InputLine::keypress_enter(string& inputline, void* mt) {
InputLine* mythis = (InputLine*)mt;
mythis->ready = true;
if(inputline.length() >= (unsigned)config->getOption(opt_histwordsize)) {
history->add (mythis->id, mythis->input_buf);
}
mythis->history_pos = 0; // Reset history cycling
mythis->cursor_pos = 0;
mythis->left_pos = 0;
mythis->ready = false;
if(currentSession)
currentSession->userInput(inputline.c_str()); // deal with prompt stuff
hook.run(USERINPUT, inputline);
if(inputline[0] == interpreter.getCommandCharacter())
hook.run(COMMAND, inputline);
else hook.run(SEND, inputline);
// FIXME interpreter.add(inputline);
inputline = "";
return true;
}
bool InputLine::keypress_arrow_left(string&, void* mt) {
InputLine* mythis = (InputLine*)mt;
if (mythis->cursor_pos == 0)
status->setf ("Already at the far left of the input line.");
else
{
mythis->cursor_pos--;
mythis->left_pos = max((int) 0,(int)mythis->left_pos-1);
}
return true;
}
bool InputLine::keypress_arrow_right(string&, void* mt) {
InputLine* mythis = (InputLine*)mt;
if (mythis->cursor_pos == mythis->max_pos)
status->setf ("Already at the end of the input line.");
else
{
mythis->cursor_pos++;
if (mythis->cursor_pos > (unsigned int)7*(unsigned int)mythis->width/(unsigned int)8) // scroll only when we are approaching right margin
mythis->adjust();
}
return true;
}
bool InputLine::keypress_arrow_up(string&, void* mt) {
InputLine* mythis = (InputLine*)mt;
int lines=0;
if (mythis->id == hi_none)
status->setf ("No history available");
else if (mythis->id != hi_generic && (lines = config->getOption(opt_historywindow)))
{
lines = min (mythis->parent->height-4, lines);
if (!history->get(mythis->id,1, NULL))
status->setf ("There are no previous commands");
else
{
if (lines > 3)
(void)new InputHistorySelection(mythis->parent, mythis->width,
lines, 0, -(lines+2), *mythis, true, mythis->id);
// Window is to small; we need to cycle history in the input box
else {
(void)new MessageBox(screen, "Sorry, no history available", 0);
}
}
}
else {
const char *s;
if (!(s = history->get(mythis->id, mythis->history_pos+1, NULL)))
status->setf ("No previous history");
else {
mythis->set(s);
mythis->history_pos++;
}
}
return true;
}
bool InputLine::keypress_arrow_down(string&, void* mt) {
InputLine* mythis = (InputLine*)mt;
const char *s;
if (mythis->id == hi_none)
status->setf("No history available");
else if (mythis->history_pos <= 1 || !(s = history->get(mythis->id,mythis->history_pos-1, NULL)))
{
status->setf ("No next history");
mythis->history_pos = 0;
mythis->set("");
}
else
{
mythis->set(s);
mythis->history_pos--;
}
return true;
}
MainInputLine::MainInputLine()
:InputLine(screen, wh_full, 1, None, 0, -1, hi_main_input) {
parent->focus(this);
}