-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwless.c
2436 lines (2055 loc) · 56.4 KB
/
wless.c
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// welss.c
//
// browser UI
//
//
// (>) 2021 Jonas S Karlsson
// jsk@yesco.org
// A "Command Line First" Project
//
// This file implements a command line
// browser, it's still a full TUI app,
// but it's fully controllable from the
// command line.
//
// This not to say that there are other
// ways to interact with the browser,
// but there are no irritating GUI menus
// that provide all functionality.
//
// Instead, we have a small "irritating"
// Termux/Xterm enabled TUI, mostly focused
// on scrolling the current page, or going
// back and forth in browser history.
//
// Implementation details:
// - structured to have no unique
// non-persistent state
// - state is directlly persisted
// - state is read from disk at each
// action (yes! it's fast enough!
// use LEFT/RIGHT to go back and
// forth between seen pages...)
// - pages are always rendered from disk
// file (.ANSI) - it is fast!
// - at each action the screen completely
// redrawn (unless supressed)
//
// - it's a fast hack - you've been warned!
// - it has many experimental UI-functions
// many will be removed when done/failed
//
// - the code has little state, but it's
// global.
//
// 0. start_tab, tab, top
// 1. current history line =>
// (hit, url, file)
// 2. the command line (cmd)
// 3. searching metadata (_search, _only,
// _click_r, _click_c)
// TODO: tail -f "feature"?
//
// - http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <limits.h>
#include <stdarg.h>
#include "jio.h"
// lazy include no .h
#include "graphics.c"
// TODO: move to INI
#define LOADING_FILE ".w/Cache/loading.html.ANSI"
#define INTERNET_SEARCH "https://duckduckgo.com/html?q=%s"
// Text only retro-info text search-engine
//#define INTERNET_SEARCH "https://search.marginalia.nu/search?query=foo"
// generic functions
void message(char* format, ...) {
va_list argp;
va_start(argp, format);
save();
// last line
gotorc(screen_rows-2, 0);
int sbg= B(black), sfg= C(green);
vprintf(format, argp);
B(sbg); C(sfg);
clearend();
restore();
fflush(stdout);
}
// set k=NO_REDRAW to not update screen
// (good for showing temporary information like menus/hilite, CTRL-L redraws
#define NO_REDRAW -1
#define REDRAW (CTRL+'P')
// --- limits
int nlines= 0; // number of lines in file
// number of newly opened tabs + last
int start_tab=0, ntab= 1;
int rows;
// --- "global" browser state
FILE *fhistory, *fbookmarks;
int top, tab;
// searching and matching
char *_search= NULL;
int _only= 0;
int _click_r=-100, _click_c=-100;
char *hit= NULL; // FREE!
// DONT free (pointers into hit)
char *file= NULL, *url= NULL;
dstr *cmd= NULL;
void clearcmd() {
if (cmd) cmd->s[0]= 0;
}
int incdec(int v, int k, int ikey, int dkey, int min, int max, int min2val, int max2val) {
if (k==ikey) v++;
if (k==dkey) v--;
// fix
if (v<min) v= min2val;
if (v>max) v= max2val;
return v;
}
#define COUNT(var, ikey, dkey, limit) var= incdec(var, k, ikey, dkey, 0, limit-1, 0, limit-1)
#define COUNT_WRAP(var, ikey, dkey, limit) var= incdec(var, k, ikey, dkey, 0, limit-1, limit-1, 0)
////////////////////////////////////////
// bookmarks + links
// TODO: dynamic array? or read from file and match on every keystroke?
// (some newspapers have 26*26*5++ links! [cee] )
#define LINKS_MAX 26*26*26
int nlinks= 0;
char *links[LINKS_MAX] = {0};
// free old links after n
void trunclinks(int n) {
while(nlinks > n) {
char *l= links[--nlinks];
if (l) free(l);
links[nlinks]= NULL;
}
}
// open fname (.FOO) but with ext .BAR
FILE *fopenext(char *fname, char *ext, char *mode) {
char tmp[strlen(fname)+1+strlen(ext)+1];
strcpy(tmp, fname);
char *ldot= strrchr(tmp, '.');
if (ldot)
strcpy(ldot, ext);
else
strcat(tmp, ext);
return fopen(tmp, mode);
}
// Load bookmarks/keyboards from FILE
// Returns number of items added
int loadshortcuts(char *file) {
if (!file) return 0;
FILE *flinks;
// .ansi file - extract links
if (endswith(file, ".ANSI")) {
flinks= fopenext(file, ".LINKS", "r");
} else {
flinks= fopen(file, "r");
}
// TODO: display error message?
if (!flinks) return 0;
// TODO: fgetline doesn't work with popen
char *lk;
while (lk=fgetline(flinks)) {
if (nlinks<LINKS_MAX)
links[nlinks++]= lk;
else if (LINKS_MAX>=nlinks) {
message("loadshortcuts: LINKS_MAX reached!");
}
}
fclose(flinks);
}
// Generic fuzzy string matcher
// (match link/desc A with cmd shortcut B)
// Usage: match("foo bar", "f b")
// Returns 0 if no match
// INT_MAX if equal (case match)
// IMT_MAX/2 if equal (case not)
// 10000 if it's a prefix
// 1000+ if substring (>= 1000)
// n prefixwords matched
// score of string A matched to B
// INT_MAX "FOO" to "FOO"
// INT_MAX/2 "foo" to "FOO"
// 10000 "foo" to "fo"
// 9993 "foo bar" to "bar"
// 9990 "fo ba bar" to "bar"
// 1 "foo" "foO"
// 2 "fie foo fum" "foo fum"
// 2 "fie foo foo bar" "foo bar"
//
int match(char* a, char* b) {
// equal
if (!strcmp(a, b)) return INT_MAX;
if (!strcasecmp(a, b)) return INT_MAX/2;
// prefix (TODO: case)
int l= lprefix(a, b);
if (l==strlen(b)) return 10000;
// substring
char* m= strcasestr(a, b);
// but start of word
if (m && m-a>0 && isalnum(*(m-1))) return 0;
if (m) return 9999- (m-a);
// matches prefixes to words in order
char* s= strchr(b, ' ');
int n= s? s-b : strlen(b);
int r= 0;
while (a && *a && b && *b) {
char* wd= NULL;
int l= 0;
// TODO: malloc/free expensive?
sscanf(b, "%ms%n", &wd, &l);
// find it
m= strcasestr(a, wd);
if (wd) free(wd);
if (!m) return 0;
// but start of word
if (m && m-a>0 && isalnum(*(m-1))) return 0;
// matched one
r++;
// next word
while(*b && *b!=' ') b++;
while(*b && *b==' ') b++;
if (!*b) return r;
a= m+l;
while(*a && isalnum(*a)) a++;
while(*a && *a==' ') a++;
if (!*a) return 0;
}
return r;
}
// TODO: search prefixkey, or string
keycode listshortcuts() {
clear();
printf("\n=== ShortCuts/Links ===\n\n");
for(int i=0; i<nlinks; i++)
printf("%s\n", links[i]);
// try clever matching of command line
if (cmd->s[0]) {
for(int i=0; i<nlinks; i++) {
int m= match(links[i], cmd->s);
if (0) ;
else if (m>10000) C(green);
else if (m>1000) C(yellow);
else if (m) C(cyan);
else continue;
printf("==> %5d %s\n", m, links[i]);
}
C(white);
}
printf("\n\n(press CTRL-L to see browser page)\n");
return NO_REDRAW;
}
// TODO: research universal XML bookmarks format called XBEL, also supported by e.g. Galeon, various "always-have-my-bookmarks" websites and number of universal bookmark converters.
// k=='^' == ^note or ^todo and no log url!
void logbookmark(int k, char *s) {
//log(bms, url, offset, top, s);
gotorc(screen_rows-1, 0);
cleareos();
FLOGF(fbookmarks, "%s %d %d %c%s\n",
k!='^'? url: "\"\"", -1, top, k, s);
message("Saved bookmark '%c' = '%s'", k, s);
}
void loghistory(char *url, int ulen, char *file) {
FLOGF(fhistory, "#=W %.*s %s\n",
ulen, url, file);
}
#define FAILIF(exp, msg...) if(exp){message(msg); return NO_REDRAW;}
keycode follow(char *txt) {
if (!txt || !*txt) txt= url;
FAILIF(!url || !*url, "No URL");
// -- follow
FILE *f= fopen(".wfollow", "a+");
// assume it's an URL
FLOGF(f, "%s\n", txt);
message("Followed %s", txt);
fclose(f);
return NO_REDRAW;
}
keycode listbookmarks(char *url, char *s) {
char *line;
clear();
C(black); B(white);
if (url)
printf("./wbookmarks %s", url);
else if (s && *s)
printf("./wbookmarks %s", s);
else
printf("./wbookmarks");
clearend(); printf("\n\n");
C(white); B(black);
fseek(fbookmarks, 0, SEEK_SET);
cursoron();
int mcount= 0;
while(line= fgetline(fbookmarks)) {
char *m=NULL;
if ((!url || !*url || strstr(line, url))
&& (!s || !*s || (m= strcasestr(line, s)))) {
// it's a match!
//printf("= %s\n", line);
char *date, *u, *data;
int offset, top;
int n= sscanf(line, "%ms %ms %d %d %m[^]",
&date, &u, &offset, &top, &data);
// simplify url to show
char *simple= u;
simple= sskip(simple, "https://");
simple= sskip(simple, "http://");
simple= sskip(simple, "www.");
simple= strunc(simple, "?");
simple= sdel(simple, ".html");
simple= sdel(simple, ".htm");
//printf("matched=%d (5)\n", n);
if (n == 5 && data[0]!='=') {
mcount++;
// --- got a matched result
const int right= 16;
int cols= screen_cols-right-2;
if (strlen(data)<=cols)
printf("%-*s ", cols, data);
else
printf("%-*s ", cols, "");
// print right column
if (url) {
C(yellow);
printf("%.16s\n", isoago(date));
} else {
C(cyan);
if (strlen(simple)>right)
printf("%.13s...\n", simple);
else
printf("%.16s\n", simple);
}
C(white);
if (strlen(data)>cols)
printf("%s\n", data);
}
free(line);
free(date); free(u); free(data);
} else {
free(line);
}
}
printf("\n%d Matching Lines\n", mcount);
printf("(CTRL-L to redraw)\n");
return NO_REDRAW;
}
keycode bookmark(int k, char *text) {
int cpos= -1; // TODO
if (k=='*' || k==CTRL+'D') {
k= '*'; text= "";
}
// log all including searches!
logbookmark(k, text);
// search
if (k=='=') listbookmarks(NULL, text);
return NO_REDRAW;
}
// search is performed in printAnsiLines()
void search(int k, char* text) {
if (!text || !strlen(text)) {
FREE(_search);
} else {
_search= strdup(text);
}
}
// start download in background
#define FORCE_RENDER 1
#define FORCE_RELOAD 2
void download(char* url, int force, int dolog) {
if (!url || !*url) return;
char *end= strpbrk(url, " \t\n");
int ulen= end? end-url : strlen(url);
// quote & log
dstr *file= dstrncat(NULL, ".w/Cache/", -1);
file= dstrncaturi(file, url, ulen);
file= dstrncat(file, ".ANSI", -1);
if (dolog)
loghistory(url, ulen, file->s);
FILE *f= force ? NULL : fopen(file, "r");
free(file);
// if .ANSI file exists, exit
if (f) {
fclose(f);
} else {
dstr *run= dstrprintf(NULL, "./wdownload %s \"%.*s\" %d %d &",
force>=FORCE_RELOAD?"-d":"", ulen, url, screen_rows, screen_cols);
system(run->s);
free(run);
// wait a little for .TMP to be created
// TODO: fix this timing issue, create the .TMP file?
usleep(300*1000);
}
}
void reload(char* url) {
download(url, FORCE_RELOAD, 0);
}
void rerender(char* url) {
download(url, FORCE_RENDER, 0);
}
int netErr() {
// net down?
FILE *f= fopen(".wnetdown", "r");
if (f) {
gtoasterr("Net Down");
fclose(f);
return 1;
}
return 0;
}
// opens ansi file, or reloads it and waits, any key will return
FILE *openOrWaitReloadAnsi() {
// wait for open of ANSI file
FILE *fansi= fopen(file, "r");
FILE *ftmp= fopenext(file, ".TMP", "r");
//
if (!fansi && !ftmp) {
FILE *ferr= fopenext(file, ".ERR", "r");
// TODO: easy way to remov all this code: on error generate an .ANSI with error?
// error previously - report again
// this red error message is too much in the face?
if (ferr) {
wclear();
char buf[80];
fgets(buf, sizeof(buf), ferr);
char *err= strstr(buf, "ERROR ");
if (err) {
char *end= strchr(err, '.');
if (end) *end= 0;
gtoasterr(err);
} else {
gtoasterr("Load Err");
}
fclose(ferr);
} else {
gotorc(1,0);
// file missing in cache
gtoast(" Download? ");
// if waited 1s then reload
if (keywait(1000)>1000) {
gtoast(" Loading ");
gotorc(1, 0); // place of >>>
reload(url);
keywait(300);
ftmp= fopenext(file, ".TMP", "r");
}
}
}
// wait if have .TMP till not there
// that signals the end of .ANSI created
gotorc(1, 0);
int zlast= 0;
while (ftmp && !haskey()) {
usleep(300*1000);
fseek(ftmp, 0, SEEK_END);
int z= ftell(ftmp);
if (z!=zlast)
printf(" %d ", z);
zlast= z;
fclose(ftmp);
putchar('>'); fflush(stdout);
// TODO: ?
if (netErr()) return NULL;
ftmp= fopenext(file, ".TMP", "r");
}
if (ftmp) fclose(ftmp);
fansi= fopen(file?file:".stdout", "r");
if (!fansi && netErr()) return NULL;
// -- no difference in speed...
// wc: takes 0.01s
// cat: takes 0.11s
//
// static char *buffer= NULL;
// static int *size= 100*100*3;
// if (!buffer) buffer= malloc(size);
// if (fansi) setbuffer(fansi, buffer, size); // _IOFBF, size);
// it's an upper boundary
// each ANSI line has an @offset
// but occasionaly #metadata
nlines= fansi? flines(fansi)/2 : -1;
return fansi;
}
int findlink(char *ln) {
// We find beginning of link by hidden text
char *p= strcasestr(ln,"\e]:A:");
if (!p) return 0;
// TODO: find better:
// end of link: end underline, LOL
char *end= strcasestr(ln, "\e[24m");
int len= !end? 0xff : end-p;
return ((p-ln)<<8) + MIN(0xff, len);
}
// limited to matching 255 chars
// Returns: pos*256 + len
// 0 if no match
// if len==0xff -> end of string
int matchfinder(char *ln, char *pat) {
if (!ln || !*ln || !pat || !*pat) return 0;
if (!strcmp(pat, "LINKS"))
return findlink(ln);
char *p= strcasestr(ln, pat);
if (!p) return 0;
int len= strlen(pat);
return ((p-ln)<<8) + MIN(0xff, len);
}
int visCol(char *ln, char *end) {
if (!ln) return -100;
int col= 0;
char c;
while ((c= *ln) && ln++<=end) {
if (c=='\r' || c=='\n') col= 0; // ^M
else if (c=='\e') {
if ((c= *ln)==']') {
// skip hidden text
while ((c= *ln++) && c!='\e');
c=*ln++; // skip \\
} else {
// skip till letter
while ((c= *ln++) && !isalpha(c));
}
} else if (c<32) ;
else if (c>127) {
// TODO: utf-8 fullwidth
if (isstartutf8(c)) col++;
} else {
if (c>=' ') col++;
}
}
return col;
}
// inject "codes" to rest color (hilite) after any code terminal co
int _screen_top= 0;
int _screen_left= 0;
int _curx= 0;
// poor mans clear end for limited width window!
void _clearend() {
spaces(screen_cols-_curx);
_curx= screen_cols;
}
void printansi(int len, char *ln, char *codes) {
// poor mans (don't scroll sideways)
if (0) {
printf("%.*s", len, ln);
return;
}
static int ch= 0, chk= 0; // utf-8 decode
if (!ln || len<=0) return;
char c;
_curx= 0;
while ((c= *ln++) && len-->0) {
if (c=='\n' || c=='\r') {
_curx= 0; ch= 0; chk= 0;
printf("\e[%dG", _screen_left);
printf("\e[24;0m"); // reset
continue;
}
if (_curx>= screen_cols) continue;
// decode utf-8 inline!
if (isstartutf8(c)) {
putchar(c);
ch= c;
chk= 0;
int m=0x80;
while(m & ch) {
ch &= ~m;
m>>= 1;
chk++;
}
chk--;
} else if (isinsideutf8(c)) {
putchar(c); ch= (ch<<6) + (c & 0x3f);
if (--chk==0 && isfullwidth(ch))
_curx++;
} else {
putchar(c); ch= 0;
}
if (c>31 && !isinsideutf8(c))
_curx++;
else if (c=='\r' || c=='\n')
_curx= 0;
else if (c=='\e') {
c= *ln++;
if (!c) return;
putchar(c);
if (c==']') {
// TODO: not needed? - delete?
// hidden text
while ((c= *ln++) && c!='\e')
putchar(c);
if (!c) return;
putchar(c);
c= *ln++; // read \\
if (!c) return;
putchar(c);
} else {
// ansi codes-skip till letter
while ((c= *ln++) && (!isalpha(c) && c!='\\' && c!=7))
putchar(c);
if (!c) return;
// if "clear" ignore...
if (c=='K') {
// cancel clear (K)
// TODO: not sure how this works, but just a single space makes us
// loose on next char! So we rewrite "\eK" to "\e\e "!
// This may not be portable?
printf("\e ");
} else {
putchar(c);
}
}
// after possible change reapply codes
if (codes) printf("%s", codes);
}
}
}
int _clicked= 0;
char *diffcodes= NULL;
// forwards
int click(char *keys);
keycode deltab();
int newtab(char* url);
// print ansi line and hilite matches
int printansiln(char *ln, int n, int matchLink) {
char *s= ln;
int m= matchLink? findlink(s) : matchfinder(s, _search);
char *f= m ? s+(m>>8) : NULL;
int len= m & 0xff;
int found= !!f;
// trunacte if only and no match
if (_only && !f) *s= 0;
// reset codes before each text char!
char *codes= -n==screen_rows/2-1?"\e[27m":"";
codes= "";
codes= diffcodes? diffcodes: "";
// find and hilite each match
while(f) {
printansi(f-s, s, codes);
// -- print match
// test if click on
if (_search && !strcmp(_search, "LINKS")) {
int r= -n-1+1, c= visCol(ln, f);
//printf("c(%d ? %d %d)", _click_c, c, f-ln);
int vend= visCol(ln, f+len);
if (_click_r==r && c<=_click_c && _click_c<=vend) {
char *p= sskip(f, "\e]:A:{");
// copy "shortcut" to cmd line
cmd->s[0]= 0;
while(*p && !isspace(*p))
cmd= dstrncat(cmd, p++, 1);
// go!
tab= click(cmd);
_clicked= 1;
// make click pos inactive!
// todo: cleaner?
FREE(_search);
B(red); C(white);
} else {
// missed it, hilite anyway!
B(red); C(white);
}
} else if (_search) {
// hilite every match
B(red); C(white);
}
//printf("\e ");
printansi(len, f, codes);
printf("\e[24;0m"); // reset
if (len==0xff) { f=NULL; break; } // all
s= f + len;
// assume color (todo search back?)
B(white); C(black);
// find next match
m= matchLink? findlink(s) : matchfinder(s, _search);
len= m & 0xff;
f= len ? s+(m>>8) : NULL;
}
// print remainder
// (this will always be called, even for lines not to be displayed)
printansi(strlen(s), s, codes);
diffcodes= NULL;
return found;
}
// read chars till next nl (keeep nl)
void fskiprest(FILE *f) {
int c;
while((c= fgetc(f))!=EOF && c!='\n');
ungetc(c, f);
}
int page_offset= -1;
// Return: true if clicked (askin redo page)
int printAnsiLines(FILE *fansi, int top, int rows) {
_clicked= 0;
page_offset= -1;
int matchLink = _search && !strcmp(_search, "LINKS");
int c, n=top;
rows-=1;
fseek(fansi, 0, SEEK_SET);
dstr *ln= dstrncat(NULL, NULL, 160);
int found= 0;
while(c= fgetc(fansi)) {
// TODO: cleanup when make the hidden lines simplier...
if (c=='\n' || c==EOF) {
// -- print accumulated line
if (ln->s[0]) {
gotorc(_screen_top-n, _screen_left);
found= printansiln(ln->s, n, matchLink);
ln->s[0]= 0;
}
if (c==EOF) break;
c= fgetc(fansi);
// handle diff output
if (c==' ') c= fgetc(fansi);
if (c=='\\') { // \ No newline ...
fskiprest(fansi);
continue;
}
// diff adds one char, remove space
if (c=='@') {
// extract (html soruce) offset
int num=-1;
if (1==fscanf(fansi, "%d", &num)) {
if (page_offset==-1)
page_offset= num;
continue;
}
}
// diff adds one char, detect
// (any leading space we've removed or quoted if in _pre-mode)
if (strchr(" +-!%<>@", c)) {
// if inverted 3x otherise 4x
if (c=='+' || c=='>') diffcodes="\e[48;5;34m"; // added: dark green 28, 22, 34
if (c=='-' || c=='<') diffcodes="\e[41;1m"; // removed: red
if (c=='!' || c=='%') diffcodes="\e[48;5;18m"; // changed: dark blue
if (c=='@') { // diff output
diffcodes= "";
fskiprest(fansi);
continue;
}
c= fgetc(fansi);
if (c=='@') { // at offset
diffcodes= ""; // reset
fskiprest(fansi);
continue;
}
}
if (c!='\n' && c!='#') {
// count of lines printed
if (n<0 && (!_only || found || matchLink)) {
_clearend();
putchar('\n');
}
if (n>=0 || !_only || found || matchLink)
n--;
if (n<-rows) break;
} else {
// skip comment line(s)
while(c=='\n' || c=='#') {
if (c=='#')
while((c= fgetc(fansi)) != EOF && c!='\n');
c= fgetc(fansi);
if (c==EOF) break;
}
}
_clearend();
}
// accumulate actual char to print
if (n<0) {
char ch= c;
ln= dstrncat(ln, &c, 1);
}
}
// mark click position
if (matchLink) {
save();
gotorc(_click_r, _click_c);
B(red); C(white);
putchar('*');
restore();
}
reset();
fflush(stdout);
return _clicked;
}
void displayScrollbar(int vis) {
int v= MAX(0, (100-vis-10)*4/100)+1;
gclear(); gbg=white; gfg=rgb(v,v,v);
int pc= 100*screen_rows/nlines;
int nr= MAX(1, pc*gsizey/100);
int y0= gsizey*top/nlines+2;
for(int y=0; y<nr; y++) {
if (y0+y>=gsizey-2) break;
gset(gsizex-1, y0+y, gfg);
gset(gsizex-2, y0+y, gfg);
}
gupdate();
}
void displayPageNum() {
// show page numbers
gclear();
char parts[15];
// TODO: same calculation as display()
// (but here no " L4711 ")
snprintf(parts, sizeof(parts), " %d/%d ", (top+2)/(rows-4)+1, (nlines-rows+2)/(rows-4)+1);
drawCenteredText(parts);
gupdate();
//TODO: too much obtrustive
//keywait(300);
}
// Returns a malloced string (no-NULL)
char* getTitle(char *url, char *file) {
if (!file || file==LOADING_FILE) return strdup("");
char *r= NULL;
FILE *f= fopenext(file, ".TITLE", "r");
if (f) {
r= fgetline(f);
fclose(f);
}
// try use URL (hostname)
if (!r && url) {
// get hostname
r= sskip(url, "http://");
r= sskip(r, "file://");
char *end= r= sskip(r, "https://");
if (1) {
// full path
end+= strlen(end);
} else {
// just hostname
while(*end && *end!='/') end++;
}
r= strndup(r, end-r);
}
return r ? r : strdup("");
}
void displayTabInfo(keycode k) {
gclear();
char buf[10]; sprintf(buf, " Tab%+d ", tab);
// print Tab-3 center white o black
// TODO: a bit too much on the nose?
if (0) {
gy= 10;
gx= (gsizex-strlen(buf)*8)/2;
gputs(buf);
gnl(); gy+= 4;
int sgy= gy;
gupdate();
if (url) {
// print host center black on white
gclear(); gbg= white; gfg= black;
gy= sgy;
// clear previous host
for(int i=gsizex/8; i; i--) gputc(' ');
// TODO: use fullwidth? or small font
char *u= url, *end;
u= sskip(u, "http://");
u= end= sskip(u, "https://");
while(*end && *end!='/') end++;
gx= (gsizex-8*(end-u))/2; gx= MAX(0, gx);
while(*u && *u!='/' && gx<gsizex) gputc(*u++);
// print path
//if (*u) gputc(*u++);
//gnl();
//gputs(u);
//while(gy<gsizey) gputc(' ');
}
// line of <<<<< or >>>>>
gy= (gsizey-8)/2; // middle line
gx= (gsizex%8)/2; // center text
for(int i=gsizex/8; i; i--)
gputc(k==LEFT?'<':'>');