-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.ts
92 lines (64 loc) · 2.62 KB
/
input.ts
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
import { openSync, readSync } from "fs";
import { Shell } from ".";
/*
Position the Cursor: \033[<L>;<C>H or \033[<L>;<C>f (puts the cursor at line L and column C)
Move the cursor up N lines: \033[<N>A
Move the cursor down N lines: \033[<N>B
Move the cursor forward N columns: \033[<N>C
Move the cursor backward N columns: \033[<N>D
Clear the screen, move to (0,0): \033[2J
Erase to end of line: \033[K
Save cursor position: \033[s
Restore cursor position: \033[u
*/
export class Prompt {
public static prompt(){
var log_str:string = ""
var log_cursor:number = 0
var fd:number = openSync('/dev/tty', 'rs');
var buffer:Buffer = Buffer.alloc(3)
Shell.write("SUPERSHELL>")
process.stdin.setRawMode(true)
while(1){
var read = readSync(fd,buffer,0,3,null);
if (read > 0){
var char = buffer[read-1];
var out = String.fromCharCode(char)
var bufstr = buffer.toString()
if (read > 1){
if (bufstr == "\u001b[A"){ // UP
out = "\u001b[1A"
log_cursor += 1
}
if (bufstr == "\u001b[B"){ // DOWN
out = "\u001b[1B"
}
if (bufstr == "\u001b[D"){ // LEFT
out = "\u001b[1D"
log_cursor -= (log_cursor) ? 1 : 0
}
if (bufstr == "\u001b[C"){ // RIGHT
out = "\u001b[1C"
}
}
if (char == 9) out = "Autocomplete is not avalible."
if (char == 3) process.exit(130); // ^C
if (char == 13) {
return log_str
}
if (char == 127 || char == 8) {
out = '\u001b[1D'
log_cursor -= 1;
}
if (out == String.fromCharCode(buffer[read-1])){ // default case
var before = log_str.slice(0,log_cursor)
var after = log_str.slice(log_cursor)
log_str = before + String.fromCharCode(char) + ((after.length > 0) ? after.slice(1) : after)
log_cursor += 1;
}
Shell.write(out)
}
}
process.stdin.setRawMode(false)
}
}