forked from fictive-kin/webshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsreadline.js
233 lines (197 loc) · 6.35 KB
/
wsreadline.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
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
var readline = require('readline'),
exec = require('child_process').exec,
tty = require('tty'),
fs = require('fs');
module.exports = readline;
var getCols = function() {
return tty.getWindowSize(process.stdin);
};
readline.Interface.prototype.cursorToEnd = function() {
// place the cursor at the end of the current line
var bufferOk = this.output.write(
'\x1b[0G\x1b['
+ (this._promptLength + this.line.length)
+ 'C'
);
this.cursor = this.line.length;
return bufferOk;
}
readline.Interface.prototype.completeHistory = function() {
return this.complete(this.history);
}
readline.Interface.prototype.complete = function(input) {
var line = this.line;
var matches = [];
// find all matching items (but avoid duplicates)
input.map(function (val) {
if (val.substring(0, line.length) == line
&& matches.indexOf(val) == -1) {
matches.push(val);
}
});
if (matches.length > 1) {
// more than one match, print matching lines
console.log("\r");
matches.map(function (val) {
console.log(val + "\r");
});
// populate the line with as much of the matches as we can
// (the common part)
var common = matches[0];
matches.map(function(v) {
for (var i=0; i<common.length; i++) {
if (v.charAt(i) != common.charAt(i)) {
common = common.substring(0, i);
return;
}
}
});
this.line = common;
this.prompt();
this.cursorToEnd();
return false; // did not complete, but matches found
} else if (matches.length == 1) {
// exactly one match, so fill the line
this.line = matches[0];
this.prompt();
this.cursorToEnd();
return true; // completed
}
// if we haven't returned yet, that means that there are no matches
return null;
}
readline.Interface.prototype.outputWrite = function (msg) {
return this.output.write(msg);
};
readline.Interface.prototype.node_ttyWrite = readline.Interface.prototype._ttyWrite;
readline.Interface.prototype._ttyWrite = function (s, key) {
this._hardClosed = false;
key = key || {};
if (key.ctrl) {
switch (key.name) {
case 'c': // control-c
this.output.write("^C\r\n");
if (this.cursor === 0 && this.line.length === 0) { // only at start
this._hardClosed = true;
} else {
this.line = '';
this.cursor = 0;
this._refreshLine();
return;
}
break;
case 'd': // control-d, delete right or EOF
if (this.cursor === 0 && this.line.length === 0) { // only at start
this.output.write("^D\r\n");
}
break;
case 'l': // CTRL-L
// clear screen
this.output.write('\x1b[2J');
this.output.write('\x1b[0;0H');
this._refreshLine();
return;
break;
}
} else {
switch (key.name) {
case 'enter': // enter
this._prevLineParams = null; // reset prevLineParams
// write the rest of the current line (this ensures that the cursor is at
// the end of the current command)
this.output.write(this.line.slice(this.cursor));
// no return; pass through to normal "enter" handler
break;
}
}
/*
// THIS BROKE ON NODE 0.3 (0.4); it might no longer be necessary
case 27: // escape sequence
if (b[1] === 91 && b[2] === 67) { // right arrow
if (this.cursor != this.line.length) {
this.cursor++;
// if we're at the first character of a new line:
if (((this.cursor + this._promptLength) % getCols()) == 0) {
// cursor to the left, down one line
this.output.write('\x1b[0G\x1b[1B');
} else {
// otherwise, just move the cursor one char to the right
this.output.write('\x1b[0C');
}
this._renegotiatePrevLineParams();
}
return;
} else if (b[1] === 91 && b[2] === 68) { // left arrow
if (this.cursor > 0) {
this.cursor--;
this.output.write('\x1b[0D');
this._renegotiatePrevLineParams();
}
return;
}
break;
}
*/
// unhandled, so let the original method handle it
this.node_ttyWrite(s, key);
}
// overloading the _addHistory method to up the history size to 1000
var kHistorySize = 1000;
readline.Interface.prototype._addHistory = function () {
if (this.line.length === 0) return "";
this.history.unshift(this.line);
this.line = "";
this.historyIndex = -1;
this.cursor = 0;
// Only store so many
if (this.history.length > kHistorySize) this.history.pop();
return this.history[0];
};
readline.Interface.prototype._renegotiatePrevLineParams = function () {
if (this._prevLineParams) {
this._prevLineParams.cursorPos = (this._promptLength + this.cursor) % getCols();
this._prevLineParams.cursorRow = Math.floor((this._promptLength + this.cursor) / getCols());
}
};
readline.Interface.prototype._prevLineParams = null;
readline.Interface.prototype._refreshLine = function () {
if (this._closed) return;
tty.setRawMode(true);
var lineLen = this.line.length + this._promptLength;
var rows = Math.floor(lineLen / getCols());
var cursorPos = (this._promptLength + this.cursor) % getCols();
var cursorRow = Math.floor((this.cursor + this._promptLength) / getCols());
var cursorDiff = rows - cursorRow;
// Cursor to left edge.
this.output.write('\x1b[0G');
// Erase to the right
this.output.write('\x1b[0K');
// if we have a previous line, then clear what was written
if (null !== this._prevLineParams) {
// delete any additional lines in the terminal
this.output.write('\x1b[' + this._prevLineParams.cursorDiff + 'M');
// up one row, cursor to left, clear to right
for (var i=0; i < this._prevLineParams.cursorRow; i++) {
this.output.write('\x1b[1A\x1b[0G\x1b[0K');
}
}
// store previous line params
this._prevLineParams = {
rows: rows,
cursorPos: cursorPos,
cursorRow: cursorRow,
cursorDiff: cursorDiff
};
// Write the prompt and the current buffer content.
this.output.write(this._prompt);
this.output.write(this.line);
// Erase to right.
this.output.write('\x1b[0K');
// Move cursor to original position.
if (rows > 0 && cursorDiff > 0) {
// cursor up {cursorDiff} lines
this.output.write('\x1b[' + cursorDiff + 'A');
}
this.output.write('\x1b[0G\x1b[' + cursorPos + 'C');
};
// vim: ts=2 sw=2 et