-
-
Notifications
You must be signed in to change notification settings - Fork 576
/
Copy pathless.js
565 lines (565 loc) · 22.2 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**@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-2024 Jakub Jankiewicz <https://jcubic.pl/me>
* Released under the MIT license
*
*/
/* global define */
(function(factory, undefined) {
var root;
if (typeof window !== 'undefined') {
root = window;
} else if (typeof self !== 'undefined') {
root = self;
} else if (typeof global !== 'undefined') {
root = global;
} else {
throw new Error('Unknow context');
}
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
// istanbul ignore next
define(['jquery', 'jquery.terminal'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = function(root, jQuery) {
if (jQuery === undefined) {
// require('jQuery') returns a factory that requires window to
// build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop
// if it's defined (how jquery works)
if (window !== undefined) {
jQuery = require('jquery');
} else {
jQuery = require('jquery')(root);
}
}
if (!jQuery.fn.terminal) {
if (window !== undefined) {
require('jquery.terminal');
} else {
require('jquery.terminal')(jQuery);
}
}
factory(jQuery);
return jQuery;
};
} else {
// Browser
// istanbul ignore next
factory(root.jQuery);
}
})(function($) {
var img_split_re = /(\[\[(?:[^;]*@[^;]*);[^;]*;[^\]]*\]\s*\])/;
var img_re = /\[\[(?:[^;]*@[^;]*);[^;]*;[^;]*;([^;]*);([^;]*)(?:;[^;]+)?\]([^\]]*)\]/;
// -------------------------------------------------------------------------
function find(arr, fn) {
for (var i in arr) {
if (fn(arr[i])) {
return arr[i];
}
}
}
// -------------------------------------------------------------------------
// $.when is always async we don't want that for normal non images
// -------------------------------------------------------------------------
function unpromise(args, fn) {
var found = find(args, function(arg) {
return typeof arg.then === 'function';
});
if (found) {
return $.when.apply($, args).then(fn);
} else {
return fn.apply(null, args);
}
}
// -------------------------------------------------------------------------
// slice images into terminal lines - each line is unique blob url
function slice_image(img_data, width, y1, y2) {
// render slice on canvas and get Blob Data URI
var canvas = new OffscreenCanvas(width, y2 - y1);
var ctx = canvas.getContext('2d', {willReadFrequently: true});
ctx.putImageData(img_data, 0, 0);
return canvas.convertToBlob().then(function(blob) {
if (blob === null) {
return null;
} else {
return URL.createObjectURL(blob);
}
});
}
function slice(src, options) {
var settings = $.extend({
width: null,
line_height: null
}, options);
var img = new Image();
var defer = $.Deferred();
var slices = [];
img.onload = function() {
var height, width;
if (settings.width < img.width) {
height = Math.floor((img.height * settings.width) / img.width);
width = settings.width;
} else {
height = img.height;
width = img.width;
}
var canvas = new OffscreenCanvas(width, height);
var ctx = canvas.getContext('2d', {willReadFrequently: true});
// scale the image to fit the terminal
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height);
(function recur(start) {
// loop over slices
if (start < height) {
var y1 = start, y2 = start + settings.line_height;
if (y2 > height) {
y2 = height;
}
var img_data = ctx.getImageData(0, y1, width, y2);
slice_image(img_data, width, y1, y2).then(function(uri) {
slices.push(uri);
recur(y2);
});
} else {
defer.resolve(slices);
}
})(0);
};
img.onerror = function() {
defer.reject('Error loading the image: ' + src);
};
// images need to have CORS if on different server,
// without this it will throw error
img.crossOrigin = "anonymous";
img.src = src;
return defer.promise();
}
// -----------------------------------------------------------------------------------
function less(term, text, options) {
var export_data = term.export_view();
var cols, rows;
var pos = 0;
var original_lines;
var lines;
var prompt = '';
var left = 0;
var $output = term.find('.terminal-output');
var had_cache = term.option('useCache');
if (!had_cache) {
term.option('useCache', true);
}
var cmd = term.cmd();
var scroll_by = 3;
//term.on('mousewheel', wheel);
var in_search = false, last_found, search_string;
// -------------------------------------------------------------------------------
function print() {
// performance optimization
term.find('.terminal-output').css('visibilty', 'hidden');
term.clear();
if (lines.length - pos > rows - 1) {
prompt = ':';
} else {
prompt = '[[;;;cmd-inverted](END)]';
}
term.set_prompt(prompt);
var to_print = lines.slice(pos, pos + rows - 1);
var should_substring = options.wrap ? false : to_print.filter(function(line) {
var len = $.terminal.length(line);
return len > cols;
}).length;
if (should_substring) {
to_print = to_print.map(function(line) {
return $.terminal.substring(line, left, left + cols - 1);
});
}
if (to_print.length < rows - 1) {
while (rows - 1 > to_print.length) {
to_print.push('~');
}
}
var finalize;
if (options.ansi) {
finalize = function(div) {
div.addClass('ansi');
};
}
term.echo(to_print.join('\n'), {
finalize: finalize,
externalPause: false
});
if (term.find('.terminal-output').is(':empty')) {
// sometimes the output is not flushed not idea why
// TODO: investigate
term.flush();
}
}
// -------------------------------------------------------------------------------
function quit() {
term.pop().import_view(export_data);
clear_cache();
term.removeClass('terminal-less');
$output.css('height', '');
var exit = options.exit || options.onExit;
if (typeof exit === 'function') {
exit();
}
}
// -------------------------------------------------------------------------------
var cache = {};
function clear_cache() {
if (!had_cache) {
term.option('useCache', false).clear_cache();
}
Object.keys(cache).forEach(function(width) {
Object.keys(cache[width]).forEach(function(img) {
cache[width][img].forEach(function(uri) {
URL.revokeObjectURL(uri);
});
});
});
cache = {};
}
// -------------------------------------------------------------------------------
function fixed_output() {
// this will not change on resize, but the font size may change
var height = cmd.outerHeight(true);
term.addClass('terminal-less');
$output.css('height', 'calc(100% - ' + height + 'px)');
}
// -------------------------------------------------------------------------------
function refresh_view() {
cols = term.cols();
rows = term.rows();
fixed_output();
function cont(l) {
original_lines = process_optional_wrap(l);
lines = original_lines.slice();
if (in_search) {
search(last_found);
} else {
print();
}
}
function process_optional_wrap(arg) {
if (arg instanceof Array) {
if (options.wrap) {
arg = arg.join('\n');
return $.terminal.split_equal(arg, cols, options.keepWords);
}
return arg;
} else if (options.wrap) {
return $.terminal.split_equal(arg, cols, options.keepWords);
} else {
return [arg];
}
}
function run(arg) {
var text;
if (arg instanceof Array) {
if (options.formatters) {
text = arg.join('\n');
} else {
original_lines = arg;
}
} else {
text = arg;
}
if (text) {
if (options.formatters) {
text = $.terminal.apply_formatters(text);
} else {
// prism text will be boken when there are nestings (xml)
// and empty formattings
text = $.terminal.nested_formatting(text);
text = $.terminal.normalize(text);
}
unpromise([image_formatter(text)], cont);
} else {
unpromise(original_lines.map(image_formatter), function() {
var l = Array.prototype.concat.apply([], arguments);
cont(l);
});
}
}
if (typeof text === 'function') {
text(cols, run);
} else {
run(text);
}
}
// -------------------------------------------------------------------------------
function cursor_size() {
var cursor = term.find('.cmd-cursor')[0];
return cursor.getBoundingClientRect();
}
// -------------------------------------------------------------------------------
function image_formatter(text) {
var defer = $.Deferred();
if (!text.match(img_re)) {
return text.split('\n');
}
var parts = text.split(img_split_re).filter(Boolean);
var width = term.find('.terminal-output').width();
var result = [];
(function recur() {
function concat_slices(slices) {
cache[width][img] = slices;
result = result.concat(slices.map(function(uri) {
return '[[@;;;' + cls + ';' + uri + ']' + alt + ']';
}));
recur();
}
if (!parts.length) {
return defer.resolve(result);
}
var part = parts.shift();
var m = part.match(img_re);
if (m) {
var img = m[2];
var cls = m[1].trim();
if (cls) {
cls += ' terminal-less';
} else {
cls = 'terminal-less';
}
var alt = m[3];
var rect = cursor_size();
var opts = {
width: width,
line_height: Math.floor(rect.height)
};
cache[width] = cache[width] || {};
if (cache[width][img]) {
concat_slices(cache[width][img]);
} else {
slice(img, opts).then(concat_slices).catch(function() {
var msg = $.terminal.escape_brackets('[BROKEN IMAGE]');
var cls = 'terminal-broken-image';
result.push('[[;#c00;;' + cls + ']' + msg + ']');
recur();
});
}
} else {
if (part !== '\n') {
result = result.concat(part.split('\n'));
}
recur();
}
})();
return defer.promise();
}
// -------------------------------------------------------------------------------
function search(start, reset) {
var escape = $.terminal.escape_brackets(search_string);
var flag = search_string.toLowerCase() === search_string ? 'i' : '';
var start_re = new RegExp('^(' + escape + ')', flag);
var index = -1;
var prev_format = '';
var formatting = false;
var in_text = false;
var count = 0;
lines = original_lines.slice();
if (reset) {
index = pos = 0;
}
for (var i = start; 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) {
var style = prev_format.match(/\[\[([^;]+)/);
var new_format = ';;;terminal-inverted';
style = style ? style[1] : '';
if (style.match(/!/)) {
new_format = style + new_format + ';';
new_format += prev_format.replace(/]$/, '')
.split(';').slice(4).join(';');
}
rep = '][[' + new_format + ']$1]' + prev_format;
} else {
rep = '[[;;;terminal-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;
}
count++;
}
}
}
lines[i] = line;
}
print();
term.set_command('');
term.set_prompt(prompt);
if (count === 1) {
return -1;
}
return index;
}
// -------------------------------------------------------------------------------
function scroll(delta, scroll_by) {
if (delta > 0) {
pos -= scroll_by;
if (pos < 0) {
pos = 0;
}
} else if (pos <= lines.length - rows) {
pos += scroll_by;
if (pos - 1 > lines.length - rows) {
pos = lines.length - rows + 1;
}
}
print();
return false;
}
term.push($.noop, {
onResize: refresh_view,
touchscroll: function(event, delta) {
var offset = Math.abs(delta);
scroll(delta, Math.round(offset / 14));
return false;
},
onPaste: function() {
if (term.get_prompt() !== '/') {
return false;
}
},
mousewheel: function(event, delta) {
return scroll(delta, scroll_by);
},
name: 'less',
keydown: function(e) {
var command = term.get_command();
var key = e.key.toUpperCase();
if (term.get_prompt() !== '/') {
if (key === '/') {
term.set_prompt('/');
} else if (in_search &&
$.inArray(e.which, [78, 80]) !== -1) {
if (key === 'N') { // search_string
if (last_found !== -1) {
var ret = search(last_found + 1);
if (ret !== -1) {
last_found = ret;
}
}
} else if (key === 'P') {
last_found = search(0, true);
}
} else if (key === 'Q') {
quit();
} else if (key === 'ARROWRIGHT') {
if (!options.wrap) {
left += Math.round(cols / 2);
print();
}
} else if (key === 'ARROWLEFT') {
if (!options.wrap) {
left -= Math.round(cols / 2);
if (left < 0) {
left = 0;
}
print();
// scroll
}
} else if (lines.length > rows) {
if (key === 'ARROWUP') { //up
if (pos > 0) {
--pos;
print();
}
} else if (key === 'ARROWDOWN') { //down
if (pos <= lines.length - rows) {
++pos;
print();
}
} else if (key === 'PAGEDOWN') {
pos += rows - 1;
var limit = lines.length - rows + 1;
if (pos > limit) {
pos = limit;
}
print();
} else if (key === 'PAGEUP') {
//Page Down
pos -= rows - 1;
if (pos < 0) {
pos = 0;
}
print();
}
}
if (!e.ctrlKey && !e.alKey) {
return false;
}
// search
} else 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);
}
// this will disable history
return false;
}
},
prompt: prompt
});
// -------------------------------------------------------------------------------
refresh_view();
}
// -----------------------------------------------------------------------------------
$.fn.less = function(text, options) {
var settings = $.extend({
onExit: $.noop,
ansi: false,
formatters: false
}, options);
if (!(this instanceof $.fn.init && 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);
return term;
};
});