-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
572 lines (502 loc) · 22.4 KB
/
main.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
566
567
568
569
570
571
'use strict';
var torrent_search = require("torrentflix/lib/torrent_search.js");
var terminalWidgets = require("terminal-widgets");
var chalk = require('chalk');
var fileSizeParser = require('filesize-parser');
var dateParser = require("date.js");
var util = require("util");
var fs = require("fs");
var domain = require("domain");
var path = require("path");
var torrent_sources = {
"kickass": { name: 'Kickass', url: "https://kat.cr" },
"limetorrents": { name: 'LimeTorrents', url: "http://limetorrents.cc" },
"extratorrent": { name: 'ExtraTorrent', url: "http://extratorrent.cc" },
"sky": { name: 'Skytorrents', url: "https://skytorrents.in" },
"strike": { name: 'GetStrike', url: "https://getstrike.net" },
"yts": { name: 'YTS', url: "https://yts.ag" },
"tpb": { name: 'The Pirate Bay', url: "https://thepiratebay.la" },
"btdigg": { name: 'BTDigg', url: "https://btdigg.org" },
"seedpeer": { name: 'Seedpeer', url: "http://seedpeer.eu" },
"leetx": { name: '1337x', url: "https://1337x.to" },
"nyaa": { name: 'Nyaa', url: "http://www.nyaa.se" },
"tokyotosho": { name: 'Tokiotosho', url: "https://www.tokyotosho.info" },
"cpasbien": { name: 'Cpasbien', url: "http://www.cpasbien.tv" },
"eztv": { name: 'Eztv', url: "https://www.eztv.ag" },
"rarbg": { name: 'Rarbg', url: "https://torrentapi.org" },
"xbit": { name: 'Xbit', url: "https://xbit.pw" },
"zooqle": { name: 'Zooqle', url: "https://zooqle.com" },
"torrentproject": { name: 'TorrentProject', url: "https://www.torrentproject.se" }
}
// extract sites urls
try {
var script_config_vars = fs.readFileSync("node_modules/torrentflix/lib/cli.js", "utf-8");
var str_config_vars = script_config_vars.match(/var config_vars = ({[\s\S]*?});/);
var config_vars = eval("(" + str_config_vars[1] + ")");
torrent_sources = config_vars.torrent_sources;
} catch(e) {
console.log(chalk.yellow("Warning: Could not extract torrent sources urls from torrentflix, using hardcoded urls instead"));
}
var scrapers = [];
scrapers = [
function() { var obj = require("torrentflix/lib/limetorrents.js"); return { name: "limetorrents", search: function(query) { return obj.search(query, torrent_sources["limetorrents"].url); } } }(),
function() { var obj = require("torrentflix/lib/thepiratebay.js"); return { name: "thepiratebay", search: function(query) { return obj.search(query, torrent_sources["tpb"].url); } } }(),
function() { var obj = require("torrentflix/lib/yts.js"); return { name: "yts", search: function(query) { return obj.search(query, torrent_sources["yts"].url); } } }(),
function() { var obj = require("torrentflix/lib/1337x.js"); return { name: "1337x", search: function(query) { return obj.search(query, torrent_sources["leetx"].url); } } }(),
function() { var obj = require("torrentflix/lib/nyaa.js"); return { name: "nyaa", search: function(query) { return obj.search(query, torrent_sources["nyaa"].url); } } }(),
function() { var obj = require("torrentflix/lib/kickass.js"); return { name: "kickass", search: function(query) { return obj.search(query, torrent_sources["kickass"].url); } } }(),
function() { var obj = require("torrentflix/lib/tokyotosho.js"); return { name: "tokyotosho", search: function(query) { return obj.search(query, torrent_sources["tokyotosho"].url); } } }(),
function() { var obj = require("torrentflix/lib/eztv.js"); return { name: "eztv", search: function(query) { return obj.search(query, torrent_sources["eztv"].url); } } }(),
function() { var obj = require("torrentflix/lib/xbit.js"); return { name: "xbit", search: function(query) { return obj.search(query, torrent_sources["xbit"].url); } } }(),
function() { var obj = require("torrentflix/lib/zooqle.js"); return { name: "zooqle", search: function(query) { return obj.search(query, torrent_sources["zooqle"].url); } } }(),
function() { var obj = require("torrentflix/lib/skytorrents.js"); return { name: "sky", search: function(query) { return obj.search(query, torrent_sources["sky"].url); } } }(),
function() { var obj = require("torrentflix/lib/rarbg.js"); return { name: "rarbg", search: function(query) { return obj.search(query, torrent_sources["rarbg"].url); } } }()
];
//scrapers = [
// scrapers[scrapers.length - 1]
//];
if (false) { // plugins developments disabled for now
var qbittorrent_search = function(query, engine) {
// https://github.com/qbittorrent/qBittorrent/wiki/How-to-write-a-search-plugin
return new Promise((resolve, reject) => {
const spawn = require('child_process').spawn;
const cmd = spawn('python2', ['plugins/qbittorrent/nova/nova2.py', engine, 'all', query]);
const readline = require('readline');
const rl = readline.createInterface({
input: cmd.stdout
});
var data = [];
rl.on('line', (input) => {
// format: link|name|size|seeds|leech|engine_url
var splits = input.split('|');
data.push({
torrent_link: splits[0], title: splits[1], size: splits[2], seeds: splits[3], leechs: splits[4]
});
});
cmd.stderr.on('data', (data) => {
//console.log(`stderr: ${data}`);
});
cmd.on('close', (code) => {
//console.log(`child process exited with code ${code}`);
if(code >= 0)
resolve(data);
else
reject("error code " + code);
});
})
}
// load search plugins
var pluginsPath = path.join(__dirname, "plugins");
/* === disabled for now
var torrentflixPluginsPath = path.join(pluginsPath, "torrentflix");
require("fs").readdirSync(torrentflixPluginsPath).forEach(function(filename) {
var obj = require(path.join(torrentflixPluginsPath, filename));
var parsedPath = path.parse(filename);
if(torrent_sources[parsedPath.name] === undefined) {
console.log(chalk.yellow("Warning: unknown url for torrentflix plugin '" + parsedPath.name + "', plugin skipped"));
return;
}
scrapers.push({
name: parsedPath.name,
search: function(query) {
return obj.search(query, torrent_sources[parsedPath.name].url);
}
})
});
*/
/*
Installation:
mkdir plugins
cd plugins
mkdir qbittorrent
cd qbittorrent
svn export "https://github.com/qbittorrent/qBittorrent.git/trunk/src/searchengine/nova"
svn export --force "https://github.com/qbittorrent/search-plugins.git/trunk/nova/engines"
*/
var qbittorrentPluginsPath = path.join(pluginsPath, "qbittorrent/nova/engines");
try {
require("fs").readdirSync(qbittorrentPluginsPath).forEach(function(filename) {
var parsedPath = path.parse(filename);
if(parsedPath.ext === ".py" && parsedPath.name != "__init__") {
scrapers.push({
name: parsedPath.name,
search: function(query) {
return qbittorrent_search(query, parsedPath.name);
}
})
}
});
} catch(e) {}
}
//scrapers = [ scrapers[0] ];
//scrapers = [ ];
var results = [], allResults = [];
// ============
// UI
// ============
var widgetContext = new terminalWidgets.WidgetContext();
var uiWidth = function() {
if(overrideUiWidth) return overrideUiWidth;
if(process.platform.startsWith("win")) return Math.min(100, process.stdout.columns); // on windows process.stdout.columns return the number of columns of the buffer, not the number of visible columns
else return process.stdout.columns;
};
var resultsMenuItemStyle = function(selected, focused) {
if(selected && focused) return chalk.black.bgWhite;
else if(selected && !focused) return chalk.gray.bgWhite;
else return function(str){ return str; };
}
var scraperStyle = function(status) {
switch(status) {
case "ok": return chalk.green;
case "error": return chalk.red;
}
return function(str){ return str; };
}
var nostyle = function(str) { return str; };
var printable = function(str) {
return (str || "").toString().trim().replace(/[^\x20-\x7E]/g, ".");
}
var scrapersStatusLabel = new terminalWidgets.Label({
width: function() { return Math.floor(uiWidth()*80/100); },
height: function() { return scrapers.length; },
render: function(item, width) {
return scraperStyle(scrapers[item].status) (terminalWidgets.padRight(scrapers[item].name + ": " + scrapers[item].status + ", " + scrapers[item].status_message, width));
}
});
var resultsMenuMaxTextScroll = 0, newResultsMenuMaxTextScroll = 0;
var resultsMenuRedrawTimeout = null;
var resultsMenu = new terminalWidgets.VMenu({
width: function() { return uiWidth() - resultsMenuVScrollBar.callback.width(); },
height: function() { return 10 - resultsMenuHScrollBar.callback.height(); },
itemsCount: function() { return results.length; },
maxTextScroll: function() { return resultsMenuMaxTextScroll; },
render: function(item, line, current, width, hScroll) {
var torrent = results[item];
var columnWidths = [ Math.floor(width*5/100), 1, 0, 1, Math.floor(width*5/100), 1, Math.floor(width*15/100), 1, Math.floor(width*15/100), 1, Math.floor(width*5/100), 1, Math.floor(width*5/100) ];
columnWidths[2] = width - columnWidths.reduce(function(a,b) { return a + b; });
newResultsMenuMaxTextScroll = Math.max(newResultsMenuMaxTextScroll, printable(torrent.title).length + width - columnWidths[2]);
if(resultsMenuMaxTextScroll != newResultsMenuMaxTextScroll && !resultsMenuRedrawTimeout) {
resultsMenuRedrawTimeout = setTimeout(function() {
resultsMenuMaxTextScroll = newResultsMenuMaxTextScroll;
widgetContext.draw();
resultsMenuRedrawTimeout = null;
}, 0);
} // async redraw (we can't redraw here)
var line = terminalWidgets.padRightStop(torrent.scraper.name, columnWidths[0], hScroll) + "|"
+ terminalWidgets.padRightStop(printable(torrent.title), columnWidths[2], hScroll) + "|"
+ terminalWidgets.padRightStop(printable(torrent.torrent_verified), columnWidths[4], hScroll) + "|"
+ terminalWidgets.padRightStop(printable(torrent.date_added), columnWidths[6], hScroll) + "|"
+ terminalWidgets.padRightStop(printable(torrent.size), columnWidths[8], hScroll) + "|"
+ terminalWidgets.padRightStop(printable(torrent.seeds), columnWidths[10], hScroll) + "|"
+ terminalWidgets.padRightStop(printable(torrent.leechs), columnWidths[12], hScroll);
return resultsMenuItemStyle(current, widgetContext.focusedWidget === resultsMenu) (line);
},
itemSelected: function(item) {
if(results[item])
processTorrent(results[item], function(success, message) {
widgetContext.setWidget(layout, false); // keep output of the command
if(success) {
} else {
logMessage("ERROR", "processTorrent", String(message), encodeURI(results[item].torrent_link || results[item].torrent_site));
}
widgetContext.draw();
});
},
handleKeyEvent: function(key) {
if(key >= "\x20") { // printable + backspace
widgetContext.setFocus( searchFieldInput );
searchFieldInput.handleKeyEvent(key);
return true;
}
return false;
}
});
var resultsMenuVScrollBar = resultsMenu.newVScrollBar({
height: function() { return resultsMenu.callback.height(); },
width: function() { return (resultsMenu.callback.itemsCount() > resultsMenu.callback.height()) ? 1 : 0; },
render: function(component, line, width) {
return (component === 0 ? chalk.bgBlue : nostyle)(terminalWidgets.padRight("", width));
}
});
var resultsMenuHScrollBar = resultsMenu.newHScrollBar({
height: function() { return (resultsMenu.callback.maxTextScroll() > 0) ? 1 : 0; },
width: function() { return resultsMenu.callback.width(); },
render: function(component, line, width) {
return (component === 0 ? chalk.bgBlue : nostyle)(terminalWidgets.padRight("", width));
}
});
var parseDate = function(str) {
var ret;
// cleanup
str = (str || "").toString().trim()
.replace(/[^\x20-\x7E]/g, ".");
ret = new Date(str);
//console.log("Date.parse(\"" + str + "\") = " + ret );
if(!isNaN(ret)) {
return ret;
}
if(str === "" || str.toLowerCase() === (new Date("")).toString().toLowerCase()) // "Invalid Date" // "Invalid Date"
return new Date(-8640000000000000); // http://stackoverflow.com/questions/11526504/minimum-and-maximum-date
str = str
.replace(/^a /g, "1 ");
try {
ret = dateParser(str);
} catch(e) {
logMessage("ERROR", "parseDate", e, str);
return new Date(-8640000000000000); // http://stackoverflow.com/questions/11526504/minimum-and-maximum-date
}
var currentDate = new Date();
if(ret > currentDate) ret = currentDate - (ret - currentDate); // sometimes the date is an age
return ret;
}
var parseFileSize = function(str) {
try {
// cleanup
str = (str || "").toString().trim()
.replace(/[^\x20-\x7E]/g, "")
.replace(/[^:]*:/,"")
.replace(/\.([a-zA-Z])/g,"$1")
.replace(/([kmg])[o]/gi, "$1B")
.replace(/(byte)($|[^s])/gi, "$1s$2");
return fileSizeParser(str);
} catch(e) {
logMessage("ERROR", "parseFileSize", e, str);
return 0;
}
}
var parseCount = function(str) {
str = (str || "").toString().trim()
.replace(/,/g, "");
return Number(str);
}
var sortOptionsMenuItemStyle = function(selected, focused) {
if(selected && focused) return chalk.black.bgWhite;
else return function(str){ return str; };
}
var sortOptionSelected = -1;
var sortOptions = [
{ name: "seeds", compareFunction: function(a, b) { return parseCount(b.seeds) - parseCount(a.seeds); } },
{ name: "age", compareFunction: function(a, b) { return parseDate(b.date_added || "") - parseDate(a.date_added || ""); } },
{ name: "size", compareFunction: function(a, b) { return parseFileSize(b.size || "") - parseFileSize(a.size || "");} }
];
var refreshResults = function() {
results = allResults.filter(function(item) {
return item.title.toLowerCase().indexOf(searchFieldInputText[0].toLowerCase()) >= 0;
});
if(sortOptionSelected >= 0)
results.sort( sortOptions[sortOptionSelected].compareFunction );
resultsMenu.moveCurrentItem({ row: 0, col: 0 }); // refresh the view if needed
}
var sortOptionsMenu = new terminalWidgets.VMenu({
width: function() { return uiWidth() - scrapersStatusLabel.callback.width(); },
height: function() { return sortOptions.length; },
itemsCount: function() { return sortOptions.length; },
render: function(item, line, current, width, hScroll) {
return sortOptionsMenuItemStyle(current, widgetContext.focusedWidget === sortOptionsMenu)(terminalWidgets.padBoth((item == sortOptionSelected ? "*" : "") + sortOptions[item].name, width));
},
itemSelected: function(item) {
sortOptionSelected = item;
refreshResults();
widgetContext.setFocus(resultsMenu);
widgetContext.draw();
}
});
var searchFieldHeaderText = "Search: ";
var searchFieldHeaderLabel = new terminalWidgets.Label({
width: function() { return searchFieldHeaderText.length; },
height: function() { return 1; },
render: function(item, width) {
return (widgetContext.focusedWidget === searchFieldInput ? chalk.white : chalk.gray)(searchFieldHeaderText);
}
});
var searchFieldInputText = [];
var searchFieldInput = new terminalWidgets.Input(searchFieldInputText, {
width: function() { return Math.max(0, uiWidth() - searchFieldHeaderText.length); },
height: function() { return 1; },
textMaxLines: function() { return 1 },
render: function(component, line, start, width) {
return (widgetContext.focusedWidget === searchFieldInput ? chalk.white : chalk.gray)((component === 0 ? chalk.bgGreen : nostyle)(terminalWidgets.padRight(searchFieldInputText[line].substr(start, width), width)));
},
textModified : function() {
refreshResults();
},
handleKeyEvent: function(key) {
if(key < "\x20") { // not printable
widgetContext.setFocus( resultsMenu );
resultsMenu.handleKeyEvent(key);
return true;
}
return false;
}
});
var debugMessages = Array(3);
var debugMessagesTop = 0, debugMessagesCurrent = 0;
var logMessage = function(level, module, message, data) {
if(debugMessages[debugMessagesCurrent]) debugMessagesTop = (debugMessagesTop+1) % debugMessages.length;
debugMessages[debugMessagesCurrent] = { date: new Date(), module: module, message: message, data: data };
debugMessagesCurrent = (debugMessagesCurrent+1) % debugMessages.length;
}
var debugLabel = new terminalWidgets.Label({
width: function() { return uiWidth(); },
height: function() { return (debugMessages[debugMessagesTop] === undefined)? 0 : debugMessages.length; },
render: function(item, width) {
var line = debugMessages[(debugMessagesTop+item) % debugMessages.length] ? debugMessages[(debugMessagesTop+item) % debugMessages.length].message.replace(/\n/,"") : "";
return chalk.yellow(terminalWidgets.padRight(line, width));
}
});
var resultsMenuScrollBarPad = new terminalWidgets.Label({
width: function() { return resultsMenuVScrollBar.callback.width(); },
height: function() { return resultsMenuHScrollBar.callback.height(); },
render: function(item, width) {
return terminalWidgets.padRight(" ", width);
}
});
var layout = new terminalWidgets.VBoxLayout([
new terminalWidgets.HBoxLayout([ scrapersStatusLabel, sortOptionsMenu ]),
new terminalWidgets.HBoxLayout([ resultsMenu, resultsMenuVScrollBar ]),
new terminalWidgets.HBoxLayout([ resultsMenuHScrollBar, resultsMenuScrollBarPad ]),
//resultsMenuHScrollBar,
new terminalWidgets.HBoxLayout([ searchFieldHeaderLabel, searchFieldInput ]),
debugLabel
]);
widgetContext.setWidget(layout);
widgetContext.setFocus(resultsMenu);
var tabOrder = [ resultsMenu, sortOptionsMenu, searchFieldInput ];
// ===================
// Main
// ===================
var externalCommand = [];
var query = "";
var verbose = false;
var help = false;
var overrideUiWidth = null;
for(var i = 2 ; i < process.argv.length ; ++i) {
if(process.argv[i].startsWith("--exec")) {
var separator = process.argv[i].substr("--exec".length) || ";";
while(++i < process.argv.length && process.argv[i] != separator) {
externalCommand.push(process.argv[i]);
}
}
else if(process.argv[i] == "-v") verbose = true;
else if(process.argv[i] == "-c" && i < process.argv.length-1) {
overrideUiWidth = parseInt(process.argv[++i]);
}
else if(["-h", "-?", "--help"].indexOf(process.argv[i]) >= 0) {
console.log("Usage: " + process.argv[0] + " " + process.argv[1] + " query-string [--exec command [initial-arguments]]");
console.log(" --exec command: command to start when selecting a torrent");
console.log(" the command will be started with initial-arguments list, with {} replaced by the torrent link");
console.log(" --help: this help");
console.log(" --version: version number");
console.log(" -c [NUM]: number of columns in the UI");
process.exit(0); // EX_OK
}
else if(["-v", "--version"].indexOf(process.argv[i]) >= 0) {
console.log(require("./package.json").version);
process.exit(0); // EX_OK
}
else query = process.argv[i];
}
if(query === "") {
console.error("Missing query-string, check usage with -h");
process.exit(64); // EX_USAGE
}
if(externalCommand.length === 0) externalCommand = [ "echo", "{}" ];
var i;
for (i in scrapers) { (function() {
var scraper = scrapers[i];
var d = domain.create();
scraper.status = "pending";
scraper.status_message = "request sent";
d.on('error', function(err) {
scraper.status = "error";
scraper.status_message = err;
widgetContext.draw();
});
d.run( function() {
scraper.search(query).then(
function (data) {
scraper.status = "ok";
scraper.status_message = "" + data.length + " torrents found";
data.forEach( function(item) { item.title = item.title || "<UNKNOW>"; item.scraper = scraper; } );
allResults.push.apply(allResults, data);
refreshResults();
widgetContext.draw();
}, function (err) {
throw err;
}
);
});
})(); }
var resolveTorrentLink = function(torrent, callback) {
if(torrent.torrent_link) {
//console.log(encodeURI(torrent.torrent_link));
setTimeout( function() { callback(true, torrent.torrent_link); }, 0 );
} else if (torrent.torrent_site) {
torrent_search.torrentSearch(encodeURI(torrent.torrent_site)).then(
function(data) {
torrent.torrent_link = data;
callback(true);
}, function(err) {
callback(false, err);
}
);
} else {
callback(false, "", "don't know how to download this torrent");
}
}
var processTorrent = function(torrent, callback) {
if(!torrent.torrent_link) {
logMessage("INFO", "parseTorrent", "Resolving torrent link...");
resolveTorrentLink(torrent, function(success, err){
if(success) processTorrent(torrent, callback);
else callback(false, err);
});
return;
}
executeExternalCommand(torrent, callback);
}
var externalCommandRunning = false;
var ignoreSignal = function() {};
var executeExternalCommand = function(torrent, callback) {
var spawn = require('child_process').spawn;
process.stdin.setRawMode(false);
var translatedCommand = externalCommand.map( function(item) {
switch(item) {
case "{}": return torrent.torrent_link;
default: return item;
}
});
logMessage("INFO", "executeExternalCommand", "Starting ext command...");
widgetContext.draw();
setTimeout(function() { // delay execution so that the widget finishes impending drawing
var child = spawn(translatedCommand[0], translatedCommand.slice(1), { stdio: "inherit" } );
process.on('SIGINT', ignoreSignal);
externalCommandRunning = true;
child.on('close', function(code, signal) {
console.log("");i // add extra "\n" to the child output else we will overwrite the last line
logMessage("INFO", "executeExternalCommand", "Ext command result: " + (signal || code));
externalCommandRunning = false;
process.stdin.setRawMode(true);
process.removeListener('SIGINT', ignoreSignal);
callback(true);
});
},0);
}
process.stdout.write("\u001b[?7l"); // disable line wrap (linewrap causes problems when resizing the window quickly: the widget is rendred with a size assumption but upon display it does not match the window size anymore)
process.on("exit", function() { process.stdout.write("\u001b[?7h"); }); // reenable linewrap on exit
process.stdin.setRawMode(true);
var stdinListener = function() {
if(externalCommandRunning) return; // child process will process the input
var key;
while((key = process.stdin.read()) != null) {
if(key.compare(new Buffer.from([ 3 ])) == 0) process.exit(0); // EX_OK
else if(key.compare(new Buffer.from([ 9 ])) == 0) {
widgetContext.setFocus( tabOrder[ (tabOrder.indexOf(widgetContext.focusedWidget) + 1) % tabOrder.length ] );
}
else widgetContext.handleKeyEvent(key);
widgetContext.draw();
}
};
process.stdin.on('readable', stdinListener);
process.stdout.on('resize', function() { if(externalCommandRunning) return; searchFieldInput.moveCursor({line: 0, col: 0}); widgetContext.draw(); });
widgetContext.draw();