-
-
Notifications
You must be signed in to change notification settings - Fork 570
/
less.js
236 lines (236 loc) · 9.08 KB
/
less.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
234
235
236
/**@license
* __ _____ ________ __
* / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / /
* __ / // // // // // _ // _// // / / // _ // _// // // \/ // _ \/ /
* / / // // // // // ___// / / // / / // ___// / / / / // // /\ // // / /__
* \___//____ \\___//____//_/ _\_ / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/
* \/ /____/
* http://terminal.jcubic.pl
*
* This is example of how to create less like command for jQuery Terminal
* the code is based on the one from leash shell and written as jQuery plugin
*
* Copyright (c) 2018 Jakub Jankiewicz <http://jcubic.pl/me>
* Released under the MIT license
*
*/
/* global jQuery */
(function($) {
function less(term, text, exit) {
var export_data = term.export_view();
var cols, rows;
var pos = 0;
var original_lines;
var lines;
var prompt;
var left = 0;
function print() {
term.clear();
if (lines.length-pos > rows-1) {
prompt = ':';
} else {
prompt = '[[;;;inverted](END)]';
}
term.set_prompt(prompt);
var to_print = lines.slice(pos, pos+rows-1);
var format_start_re = /^(\[\[[!gbiuso]*;[^;]*;[^\]]*\])/i;
to_print = to_print.map(function(line, line_index) {
return $.terminal.substring(line, left, left+cols-1);
});
if (to_print.length < rows-1) {
while (rows-1 > to_print.length) {
to_print.push('~');
}
}
term.echo(to_print.join('\n'));
}
function quit() {
term.pop().import_view(export_data);
//term.off('mousewheel', wheel);
if ($.isFunction(exit)) {
exit();
}
}
function refresh_view() {
cols = term.cols();
rows = term.rows();
if ($.isFunction(text)) {
text(cols, function(new_lines) {
original_lines = new_lines;
lines = original_lines.slice();
print();
});
} else {
original_lines = text.split('\n');
lines = original_lines.slice();
print();
}
}
refresh_view();
var scroll_by = 3;
//term.on('mousewheel', wheel);
var in_search = false, last_found, search_string;
function search(start, reset) {
var escape = $.terminal.escape_brackets(search_string),
flag = search_string.toLowerCase() == search_string ? 'i' : '',
start_re = new RegExp('^(' + escape + ')', flag),
regex = new RegExp(escape, flag),
index = -1,
prev_format = '',
formatting = false,
in_text = false;
lines = original_lines.slice();
if (reset) {
index = pos = 0;
}
for (var i=0; i<lines.length; ++i) {
var line = lines[i];
for (var j=0, jlen=line.length; j<jlen; ++j) {
if (line[j] === '[' && line[j+1] === '[') {
formatting = true;
in_text = false;
start = j;
} else if (formatting && line[j] === ']') {
if (in_text) {
formatting = false;
in_text = false;
} else {
in_text = true;
prev_format = line.substring(start, j+1);
}
} else if (formatting && in_text || !formatting) {
if (line.substring(j).match(start_re)) {
var rep;
if (formatting && in_text) {
rep = '][[;;;inverted]$1]' +
prev_format;
} else {
rep = '[[;;;inverted]$1]';
}
line = line.substring(0, j) +
line.substring(j).replace(start_re, rep);
j += rep.length-2;
if (i > pos && index === -1) {
index = pos = i;
}
}
}
}
lines[i] = line;
}
print();
term.set_command('');
term.set_prompt(prompt);
return index;
}
term.push($.noop, {
resize: refresh_view,
mousewheel: function(event, delta) {
if (delta > 0) {
pos -= scroll_by;
if (pos < 0) {
pos = 0;
}
} else {
pos += scroll_by;
if (pos-1 > lines.length-rows) {
pos = lines.length-rows+1;
}
}
print();
return false;
},
name: 'less',
keydown: function(e) {
var command = term.get_command();
if (term.get_prompt() !== '/') {
if (e.which == 191) {
term.set_prompt('/');
} else if (in_search &&
$.inArray(e.which, [78, 80]) != -1) {
if (e.which == 78) { // search_string
if (last_found != -1) {
last_found = search(last_found+1);
}
} else if (e.which == 80) { // P
last_found = search(0, true);
}
} else if (e.which == 81) { //Q
quit();
} else if (e.which == 39) { // right
left+=Math.round(cols/2);
print();
} else if (e.which == 37) { // left
left-=Math.round(cols/2);
if (left < 0) {
left = 0;
}
print();
} else {
// scroll
if (lines.length > rows) {
if (e.which === 38) { //up
if (pos > 0) {
--pos;
print();
}
} else if (e.which === 40) { //down
if (pos <= lines.length-rows) {
++pos;
print();
}
} else if (e.which === 34) {
// Page up
pos += rows;
if (pos > lines.length-rows+1) {
pos = lines.length-rows+1;
}
print();
} else if (e.which === 33) {
//Page Down
pos -= rows;
if (pos < 0) {
pos = 0;
}
print();
}
}
}
if (!e.ctrlKey && !e.alKey) {
return false;
}
} else {
// search
if (e.which === 8 && command === '') {
// backspace
term.set_prompt(prompt);
} else if (e.which == 13) { // enter
// basic search find only first
if (command.length > 0) {
in_search = true;
pos = 0;
search_string = command;
last_found = search(0);
}
return false;
}
}
},
prompt: prompt
});
}
$.fn.less = function(text, options) {
var settings = $.extend({
onExit: $.noop
}, options);
if (!this.terminal) {
throw new Error('This plugin require jQuery Terminal');
}
var term = this.terminal();
if (!term) {
throw new Error('You need to invoke this plugin on selector that have ' +
'jQuery Terminal or on jQuery Terminal instance');
}
less(term, text, settings.onExit);
};
})(jQuery);