-
Notifications
You must be signed in to change notification settings - Fork 0
/
tuxedo.js
113 lines (108 loc) · 3.33 KB
/
tuxedo.js
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
function initialize(element)
{
var ui = require("./ui");
var commandManager = require("./command_manager");
var commandline = require("./commandline");
var env = require("./environment");
var autoComplete = require("./auto_complete");
var screen = ui.create(element, onAutoComplete);
screen.appendCommandPrompt(env.commandPrompt());
var prevCmd = null;
function onAutoComplete() {
var focused = screen.autoCompleteWindow.focused();
if (focused) {
// clear current arg
var index = screen.cursor.index();
var cmd = commandline.parse(screen.cursor.text(), index);
if (cmd.current !== undefined) {
screen.cursor.backspace(index - cmd.argsmap[cmd.current][0]);
screen.cursor.del(cmd.argsmap[cmd.current][1] - index);
}
// insert hint
screen.cursor.insert(focused);
screen.autoCompleteWindow.hide();
}
}
function initAutoComplete() {
screen.autoCompleteWindow.clear();
screen.autoCompleteWindow.show();
prevCmd = commandline.parse(screen.cursor.text(), screen.cursor.index());
autoComplete.init(prevCmd, env, screen.autoCompleteWindow);
updateAutoComplete();
}
function updateAutoComplete() {
if (!screen.autoCompleteWindow.active()) return;
var index = screen.cursor.index();
var cmd = commandline.parse(screen.cursor.text(), index);
if (prevCmd.virtualCurrent == cmd.virtualCurrent) {
autoComplete.update(screen.cursor.text(), index);
} else {
screen.autoCompleteWindow.hide();
}
prevCmd = cmd;
}
screen.keydown(function(c) {
switch (c) {
case 9: // tab
if (!screen.autoCompleteWindow.active()) initAutoComplete();
else onAutoComplete();
break;
case 27: // esc
if (screen.autoCompleteWindow.visible()) screen.autoCompleteWindow.hide();
else screen.cursor.clear();
break;
case 8: // backspace
screen.cursor.backspace();
updateAutoComplete();
break;
case 46: // delete
screen.cursor.del();
updateAutoComplete();
break;
case 37: // left
screen.cursor.left();
updateAutoComplete();
break;
case 39: // right
screen.cursor.right();
updateAutoComplete();
break;
case 36: // home
screen.cursor.home();
updateAutoComplete();
break;
case 35: // end
screen.cursor.end();
updateAutoComplete();
break;
case 38: // up
if (screen.autoCompleteWindow.visible()) screen.autoCompleteWindow.focusPrev();
break;
case 40: // down
if (screen.autoCompleteWindow.visible()) screen.autoCompleteWindow.focusNext();
break;
}
screen.scrollToBottom();
});
screen.keypress(function(c) {
if (c == 13) {
if (screen.autoCompleteWindow.visible()) {
// auto complete
onAutoComplete();
} else {
// execute command
var output = screen.appendOutput();
var text = screen.cursor.text();
screen.cursor.detach();
commandManager.exec(text, env, output, function() {
screen.appendCommandPrompt(env.commandPrompt());
screen.scrollToBottom();
});
}
} else {
screen.cursor.insert(String.fromCharCode(c));
screen.scrollToBottom();
updateAutoComplete();
}
});
}