-
Notifications
You must be signed in to change notification settings - Fork 0
/
escape.js
129 lines (115 loc) · 3.24 KB
/
escape.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
function cursor_position(param) {
param = param.split(';');
// set row
cursor.row = (parseInt(param[0]) || 1) - 1;
// clamp
if (cursor.row < 0) {
cursor.row = 0;
}
if (cursor.row >= screen.length) {
cursor.row = screen.length - 1;
}
var current_line = screen[cursor.row];
// set column
cursor.col = (parseInt(param[1]) || 1) - 1;
// clamp
if (cursor.col < 0) {
cursor.col = 0;
}
if (cursor.col > current_line.length) {
cursor.col = current_line.length - 1;
}
}
function cursor_up(param) {
cursor.row -= parseInt(param) || 1;
if (cursor.row < 0) {
cursor.row = 0;
}
}
function cursor_down(param) {
cursor.row += parseInt(param) || 1;
if (cursor.row >= screen.length) {
cursor.row = screen.length - 1;
}
}
function cursor_forward(param) {
var current_line = screen[cursor.row];
cursor.col += parseInt(param) || 1;
if (cursor.col >= current_line.length) {
cursor.col = current_line.length - 1;
}
}
function cursor_backward(param) {
cursor.col -= parseInt(param) || 1;
if (cursor.col < 0) {
cursor.col = 0;
}
}
function save_cursor_state(_) {
copy(cursor, saved_cursor);
}
function restore_cursor_state(_) {
copy(saved_cursor, cursor);
}
function erase_display(param) {
param = parseInt(param) || 0;
if (param == 0) {
screen.splice(cursor.row+1);
} else if (param == 1) {
for (var i = 0; i < cursor.row; i++) {
screen[i].splice(0);
}
} else if (param == 2) {
screen = [[]];
}
}
function erase_line(param) {
param = parseInt(param) || 0;
var current_line = screen[cursor.row] || [];
if (param == 0) { // clear line from cursor right
current_line.splice(cursor.col);
} else if (param == 1) { // clear line from cursor left
var stop = cursor.col;
for (cursor.col = 0; cursor.col <= stop; ) {
write_char(' ');
}
} else if (param == 2) { // clear entire line
current_line.splice(0);
}
}
// terminal attributes
function set_graphics_mode(attributes) {
if (!attributes) {
reset_attributes();
return;
}
// parse attributes to set
attributes.split(';').forEach(function(attribute) {
var code = parseInt(attribute);
if (false);
else if (attribute == '') { reset_attributes(); }
else if (code == 0) { reset_attributes(); }
else if (code == 1) { cursor.bold = true; }
else if (code == 2) { cursor.dim = true; }
else if (code == 4) { cursor.underline = true; }
else if (code == 5) { cursor.blinking = true; }
else if (code == 7) { cursor.reverse = true; }
else if (code == 8) { cursor.invisible = true; }
else if (code < 30) { }
else if (code < 40) { cursor.foreground = code - 30; }
else if (code < 48) { cursor.background = code - 40; }
});
}
escape_sequences = {
'H': cursor_position,
'f': cursor_position,
'A': cursor_up,
'B': cursor_down,
'C': cursor_forward,
'D': cursor_backward,
's': save_cursor_state,
'u': restore_cursor_state,
'J': erase_display,
'K': erase_line,
'm': set_graphics_mode,
}