-
Notifications
You must be signed in to change notification settings - Fork 2
/
display.js
519 lines (449 loc) · 11.7 KB
/
display.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
// TODO: Wired.com break something
// TODO: ibm.com too much empty lines
function urlView(url) {
CLS();
clearline(0);
iplot(0, 0, 'Loading '+url+' ...');
let start = Date.now();
wget(
url,
function(h, err, nid, url) {
showPage(h, err, nid, url,
Date.now()-start);
});
}
function showPage(h, err, nid, url, loadms){
clearline(0);
iplot(0, 0, 'load='+loadms);
let parsems = Date.now();
let txt = HTML2TELETEXT(nid, h);
parsems = Date.now() - parsems;
let isink = c7=>(c7 <= 7);
let ispaper = c7=>(16 <= c7 && c7 <= 24);
// fast go pages back
let pageindex = [];
pageindex.push([0,0]);
let lastcleanindex = 0;
// state
let i, screenstartindex, step,
cink, cpaper, cstack,
lastwasink, lastwaspaper, lastwasspace,
rowcounter, lastrow,
startTime,
forwardms, renderms,
lastDummy;
i = 0;
step = SCREEN_COLS; //SCREEN_COLS; // !
step = SCREEN_ROWS*SCREEN_COLS/1;
function reset(atindex=-1, lci=-1) {
// This controls how many chars to print
// per tick.
// 1 = 300 baud (feels like it, readble)
// 4 = 1200 baud
// SCREEN_COLS = about 1 page / second
// set-up screen
CLS();
iplot(0, 0,
(url || '').substring(0, SCREEN_COLS));
startTime = Date.now();
screenstartindex = atindex;
forwardms = -Date.now();
if (i <= atindex) {
// we're good, were already on the way
// enable the runner
inputMode = undefined;
} else {
// we're starting from beginning!
i = 0;
if (atindex >= lastcleanindex) {
i = lastcleanindex;
}
if (lci !== -1 &&
atindex >= lci) {
lastcleanindex = i = lci;
}
screenstartindex = atindex>=0 ? atindex : 0;
// charcodes of current inc/paper
cink = ink;
cpaper = paper + 16;
// color stack
cstack = [];
lastwasink = false;
lastwaspaper = false;
lastwasspace = (ch == 32);
rowcounter = 0;
lastrow = 0;
}
}
function redraw() {
reset(screenstartindex);
}
reset();
// TODO: make it the default
if (cink !== 7 || cpaper !== 0+16) {
column = 2;
}
function putc(ch) {
let lastrow = row;
window.putc(ch);
// if not time to show, just care about the attributes
if (i <= screenstartindex) {
// TODO: we just keep (over) writing it at the same row!
if (row > 1) {
row = 1;
screen(1, 0, paper+16);
screen(1, 1, ink);
}
} else {
if (forwardms < 0) {
forwardms = Date.now() + forwardms;
renderms = -Date.now();
}
}
};
function myputc(ch) {
// enable for debug
if (0) {
lastwasspace = false;
lastwasink = false;
lastwaspaper = false;
}
if (row != lastrow) {
rowcounter++;
lastrow = row;
}
if (cink == 7 && cpaper == 0+16 && col <= column && (lastwasink || lastwaspaper || lastwasspace)) {
col = 0;
column = 0;
}
// TODO: make it the default
if (cink !== 7 || cpaper !== 0+16) {
column = 2;
if (col < column) col = column;
}
//console.log('putc '+JSON.stringify({ c: String.fromCharCode(ch), ch, col, cpaper, cink, lastwasink, lastwaspaper, lastwasspace }));
// horunge (bastad childs)
// TOOD: fix ugly word breaks
// TODO: cheat at .,:,? at EOL?
let c7 = ch & 0x7f;
// optimize corner cases
// if at beginning of line and space
if (col == column && (ch == 32 || ch == 10)) {
// ignore such space!
return;
} else if (col == column
&& (isink(c7) || ispaper(c7))){
// save space by using protected
//let cont = (ch & 0x0F00 != 0x0300);
if (isink(c7)) {
screen(row, 1, cink = c7);
} else if (ispaper(c7)) {
screen(row, 0, cpaper = c7);
}
// it can only be overwritten if column
lastwasink = false;
lastwaspaper = false;
lastwasspace = false
return; // done here
} else if (ch == 32 && lastwasspace) {
// ignore soft spaces
return;
}
if (ch == 32 && (lastwasspace || lastwasink || lastwaspaper))
return;
// escape control codes
if (ch != 10 && ch != 27 && ch < 32) {
// TODO: some bug if enable lastwassapce
// HTML.COM/TABLES losses some bgcol restore
// this is because, they may come in different order, the end attribute actually just triggers restore of cink/cpaper...
if ((lastwasspace && 0) ||
(lastwasink && isink(c7)) ||
(lastwaspaper && ispaper(c7))) {
// how about more levels... lol
if (col > 2) col--; // BackSpace
}
if (lastwasspace && col > 0 &&
screen(row, col-1) === ' ') {
col--; // BackSpace
}
// update
if (isink(c7)) {
cink = c7;
lastwasink = true;
lastwaspaper = false;
lastwasspace = false;
}
if (ispaper(c7)) {
cpaper = c7;
lastwasink = false;
lastwaspaper = true;
lastwasspace = false;
}
// TODO: 107 is start white ink
// how about inverse?
putc(ESC);
putc(c7+64);
// TODO: if write at last pos...?
lastwasspace = true;
// no return as this might have wrapped line and we need fall through for cols
} else {
// finally print it!
lastwasink = false;
lastwaspaper = false;
lastwasspace = (ch == 32);
// hard newline (pre, br)
if (ch == 0x030a) ch = 10;
// hard space (pre)
if (ch == 0x0320) ch = 32;
// paper 0 ink 7
// (<h1>) can just move cursor to col=0!
if (col == column && (lastwasink || lastwasspace || lastwaspaper) && cink == 7 && cpaper == 0+16) {
col = 0;
}
//lastwasspace = false;
putc(ch);
//putc('.'.charCodeAt(0));
//if (ch > 128 || ch < 32) princ('['+ch+']');
}
// at newline need continue color
if (col == column) {
// but only if default is different
if (ink != cink)
myputc(cink);
if (paper != cpaper)
myputc(cpaper);
}
}
function navigate() {
renderms = Date.now() + renderms;
// stats line
clearline(0);
iplot(0,0,'l'+loadms+' f'+forwardms+' r'+renderms+' c'+lastcleanindex+' i'+i+'/'+txt.length+' h'+h.length);
renderms = -Date.now();
let percent = INT(100 * screenstartindex /txt.length);
let percent2 = INT(100 * i /txt.length);
let page = pageindex.length;
let pages = INT(100 * pageindex.length / percent2 +0.5);
let posmsg = '\x00\x13'+page+'/'+pages+' '+percent2+'%';
iplot(SCREEN_COLS-posmsg.length, 0, posmsg);
// help line
iplot(0, SCREEN_ROWS-1, // bottom row
'\x13\x04WWW\x03\x03\x14 SPC B < + - ESC C-U 319 \x13\x04??? ');
inputMode = function navigation(k) {
if (k < 0) return;
let c = String.fromCharCode(k).toLowerCase();
//iplot(0,0, 'k='+k+' c='+c+' ');
// less style commands?
switch(c) {
case 'f':
case ' ': { // forward page
inputMode = undefined;
// continue where we left off
let g = lastcleanindex; // i-50;
pageindex.push([g,g]);
reset(g, g);
break; }
case 'b': { // back page
iplot(0,SCREEN_ROWS-1,'back............');
inputMode = undefined;
// continue where we left off
if (!pageindex.length)
pageindex.push([0,0]);
let g;
[g, lastcleanindex] = pageindex.pop();
if (!pageindex.length)
pageindex.push([0,0]);
reset(g, lastcleanindex);
break; }
case 'q':
case ESC: { // quit
inputMode = undefined;
CLS(); // TODO: restore screen?
lastKey = -1;
stop();
return true;
break; }
case '<': { // beginning
inputMode = undefined;
//step = txt.length;
reset(0);
break; }
case '/': // search in page
inputMode = undefined;
// fall through to next
case 'n': { // next match
inputMode = undefined;
reset(0);
break; }
case 'p': { // prev match
inputMode = undefined;
reset(0);
break; }
case '+': { // zoom: less rows/cols
inputMode = undefined;
// how about state? (parse from beginning?)
//step = txt.length;
TEXT(INT(SCREEN_ROWS/1.4+1),
INT(SCREEN_COLS/1.1));
redraw();
break; }
case '-': { // zoom out: more rows/cols
inputMode = undefined;
// how about state? (parse from beginning?)
//step = txt.length;
TEXT(INT(SCREEN_ROWS*1.4),
INT(SCREEN_COLS*1.1));
redraw();
break; }
case '3': // 300 baud!
step = SCREEN_COLS/20; redraw(); break;
case '1': // 1200 baud!
step = SCREEN_COLS; redraw(); break;
case '9': // full speed!
step = SCREEN_COLS*SCREEN_ROWS*10; redraw(); break;
default: { // pass it on (arrows etc)
return false;
break; }
}
// eat up keystroke
lastKey = -1;
end = i + step;
return true;
};
//row = toprow;
//col = column;
return;
}
RUN.interval = setInterval(
function printSlow() {
// TOOD: generalize these runners
// for LIST, RUN, CLOAD, WWW
if (scstop) return;
if (Date.now() < waittill) return;
if (inputMode) return;
let end = i + step;
for(; i<end; i++) {
// it's a clean state! - remember!
if (cstack.length == 0 &&
col == 2 &&
cink == ink && cpaper == paper+16) {
lastcleanindex = i;
}
if (row >= SCREEN_ROWS-2)
return navigate();
if (i > txt.length) {
if (col !== column) print();
print('-- ' + url);
// TODO: not accurate as involve waiting time for next page etc
//print('(Rendering '+(Date.now()-startTime)+' ms)');
return navigate();
}
let c = txt[i];
let ch = txt.charCodeAt(i);
let c7 = ch & 0x7f;
// output whole word unbroken
// or split word according to hyphen.
// (this already avoids already hypnen)
// word-limit length == 25...
let word;
if (1)
//if (c === '"' || c === "'" ||
//(c >= 'a' && c <= 'z'))
{
word = txt.substr(i, 25)
.match(/^['"]{0,1}[\w]+[,\.\?:;!'"]{0,1}/);
}
if (word) {
word = word[0];
let l = word.length;
//myputc('['.charCodeAt(0));
if (col + l < SCREEN_COLS) {
// fits!
for(let j=0; j<l; j++)
myputc(word.charCodeAt(j));
//myputc('~'.charCodeAt(0));
} else if (1) {
// doesn't fit
let parts = hyphen(word).split(' ');
parts.forEach((p,i)=>{
let islast = parts.length === i+1;
let pl = p.length;
// newline if not fit, and not too big
if (islast &&
col + pl <= SCREEN_COLS){
// last does fit!
; // fine!
} else {
// isn't last, or didn't fit
if (i)
myputc('-'.charCodeAt(0));
// '-' could have moved us!
if (col !== column) {
myputc(0x030a);
//myputc('&'.charCodeAt(0));
}
}
for(let j=0; j<pl; j++)
myputc(p.charCodeAt(j));
//myputc('&'.charCodeAt(0));
});
}
//myputc(']'.charCodeAt(0));
i += l - 1; // cont will inc by one!
//end += 2;
continue;
}
if (ch >= 0x0300) {
// continue color (delimiter)
myputc(ch);
} else if (ch >= 0x0200) {
// end color
let t = cstack.pop();
if (!t) {
// ingore unmatched
//myputc(32);
myputc('?'.charCodeAt(0));
continue;
}
if (0) {
// allow reverse in any order
// (trust end attribute)
myputc(c7);
} else {
// reverse in matching order
// resore color
if (cpaper != t.cpaper) {
myputc(t.cpaper);
//myputc('P'.charCodeAt(0));
}
if (cink != t.cink) {
myputc(t.cink);
//myputc('C'.charCodeAt(0));
}
}
if (0) {
// TODO: graphics ???
myputc(32);
}
//princ(`[i:${t.cink}p:${t.cpaper}]`);
} else if (ch >= 0x0100) {
// start color
cstack.push({c7, cink, cpaper});
if (c7 == cink || c7 == cpaper) {
// same
myputc(32);
} else {
// hack: color correction:
// (<h1><a>... => blue on black :-(
// change to cyan!
if (cpaper == 0+16 && c7 == 4)
c7 = 6;
myputc(c7);
}
//myputc('v'.charCodeAt(0));
} else {
myputc(ch);
}
} // for
}, 0); // set interval
} //