-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealmain.cc
More file actions
2030 lines (1786 loc) · 49.5 KB
/
realmain.cc
File metadata and controls
2030 lines (1786 loc) · 49.5 KB
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
/* -*- mode: C++; c-basic-offset: 4; tab-width: 8; -*-
* vi: set shiftwidth=4 tabstop=8:
* :indentSize=4:tabSize=8:
*/
#include <wchar.h>
#include <getopt.h>
#include <sysexits.h>
#include <iostream>
#include <cassert>
#include <stdint.h>
#include <stdlib.h>
#include <memory>
#include <regex>
#include <map>
#include <fstream>
#include <thread>
#include <iterator>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if defined(__unix__)
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <cstring>
#endif
#include "file_index.h"
#include "regex_index.h"
#include "error.h"
#include "display_info.h"
#include "normalize_regex.h"
#include "curses_attr.h"
#include "history.h"
#include "foreach.h"
#include "getRSS.h"
#include "to_wide.h"
#include "event.h"
#include "intersect.h"
#include "search.h"
#include "temporary_file.h"
#include "console.h"
#include "errno_str.h"
#include "click_link.h"
#include "command.h"
#include "maximize_window.h"
#include "merge_command_line.h"
#include "tokenize_command_line.h"
#include "getenv_str.h"
#include "color.h"
#include "timeGetTime.h"
#include "complete_filename.h"
#include "word_set.h"
#undef max
/// verbosity level
unsigned verbose = 0;
#define lightgray_on_black color(COLOR_WHITE, COLOR_BLACK)
#define white_on_black (lightgray_on_black | A_BOLD | A_STANDOUT)
#define gray_on_black (lightgray_on_black | A_DIM)
namespace {
/// file name of line edit history
const char* line_edit_history_rc = ".few.history";
/// the filename of the currently executed process (argv[0]).
std::string argv0;
/// the filename as used on the command line that is displayed.
std::string command_line_filename = "-";
/// the filename that is read. Typically the same as command_line_filename, but can be a temporary file name for reading from STDIN.
std::string real_filename;
/// the info string which is shown in the lower right corner
std::string info;
/// maximum number of regular expressions. 10 for keys 1..0, 12 for F1..F12.
const unsigned max_regex_num = 10 + 12;
/// minimum screen height
const unsigned min_screen_height = 1 // lines
+ max_regex_num
+ 1 // search
;
/// minimum screen width
const unsigned min_screen_width = 16;
/// width of screen in characters
#define screen_width static_cast<unsigned>(COLS)
/// height of screen in characters
#define screen_height static_cast<unsigned>(LINES)
/// width of a tab character in characters
unsigned tab_width = 8;
/// current search regular expression string
std::string search_str;
/// compiled search regular expression
std::wregex search_rgx;
/// error string if search regular expression could not be compiled
std::string search_err;
/// the y position of the search window
unsigned search_y;
/// regular expression to match links
std::wregex link_rgx(L"(ht|f)tps?://[a-zA-Z0-9/~&=%_.-]+", std::regex::ECMAScript | std::regex::optimize | std::regex::icase);
typedef std::pair<unsigned, unsigned> coordinate_t;
std::map<coordinate_t, std::wstring> link;
/// regular expression to match emails
/// see http://www.regular-expressions.info/email.html
std::wregex email_rgx(L"\\b[a-z0-9._%+-]+\\@[a-z0-9.-]+\\.[a-z]{2,4}\\b", std::regex::ECMAScript | std::regex::optimize | std::regex::icase);
std::map<coordinate_t, std::wstring> email;
/// the file that is displayed
file_index::ptr_t f_idx;
/// object to manage displayed lines
DisplayInfo::ptr_t display_info;
/// height of the lines window
unsigned w_lines_height;
/// line number displayed at the middle of the lines window
line_number_t middle_line_number = 0;
/// the y position of the lines filter regex window
unsigned filter_y;
struct regex_container_t
{
/// the regular expression string
std::string rgx_;
/// an error string if rgx_ is invalid
std::string err_;
/// object used for lines filter
std::shared_ptr<regex_index> ri_;
///@{
/// regex object used for replace display filter
std::shared_ptr<std::regex> replace_df_rgx_;
/// replace display filter replacement text
std::string replace_df_text_;
///@}
///@{
/// regex object used for the attribute display filter
std::shared_ptr<std::wregex> attribute_df_rgx_;
/// attribute display filter curses attributes
curses_attr_t attribute_df_attr_;
///@}
};
typedef std::vector<std::shared_ptr<regex_container_t>> regex_vec_t;
/**
* type of a regex_container_t cache.
* key is the regular expression string and has to be equal to value->rgx_.
* value is a shared pointer to the regex container object.
*/
typedef std::map<std::string, std::shared_ptr<regex_container_t>> regex_cache_t;
/// the regular expressions for the display and filter regex
regex_vec_t regex_vec;
void regex_vec_resize(const unsigned num)
{
while (regex_vec.size() < num) {
regex_vec.push_back(std::make_shared<regex_container_t>());
}
}
/// the filter regex cache
regex_cache_t filter_cache;
/// @return number of digits in i.
int digits(uint64_t i)
{
if (i < 10llu) return 1;
if (i < 100llu) return 2;
if (i < 1000llu) return 3;
if (i < 10000llu) return 4;
if (i < 100000llu) return 5;
if (i < 1000000llu) return 6;
if (i < 10000000llu) return 7;
if (i < 100000000llu) return 8;
if (i < 1000000000llu) return 9;
if (i < 10000000000llu) return 10;
if (i < 100000000000llu) return 11;
if (i < 1000000000000llu) return 12;
if (i < 10000000000000llu) return 13;
if (i < 100000000000000llu) return 14;
if (i < 1000000000000000llu) return 15;
if (i < 10000000000000000llu) return 16;
if (i < 100000000000000000llu) return 17;
if (i < 1000000000000000000llu) return 18;
if (i < 10000000000000000000llu) return 19;
return 20;
}
/// fill the row y between x and screen_width with space characters.
void fill(unsigned y, unsigned x)
{
while (x < screen_width) {
mvaddch(y, x++, ' ');
}
}
unsigned print_line_prefix(const unsigned y, const unsigned line_num, const unsigned line_len, const unsigned line_num_width)
{
unsigned x = 0;
curses_attr a(A_REVERSE | color(COLOR_WHITE, COLOR_BLACK));
const unsigned line_len_w = digits(line_len) + 1;
const unsigned line_num_w = digits(line_num) + 1;
// is there enough space to print the line length?
if (line_len_w + line_num_w <= line_num_width) {
curses_attr a(A_BOLD);
mvprintw(y, x, "%u ", line_len);
x += line_len_w;
}
// print spaces to separate the numbers
for (; x < line_num_width - line_num_w; ++x) {
mvaddch(y, x, ' ');
}
mvprintw(y, x, "%u ", line_num);
x += line_num_w;
return x;
}
void mvaddwch(unsigned y, unsigned x, wchar_t c)
{
wchar_t wc[2] = { c, 0 };
mvaddwstr(y, x, wc);
}
void refresh_info()
{
if (info.empty()) {
return;
}
if (info.size() > screen_width) {
info.resize(screen_width);
}
curses_attr a(use_color() ? color(COLOR_RED, COLOR_BLACK) : A_BOLD);
mvprintw(w_lines_height - 1, screen_width - info.size(), "%s", info.c_str());
}
void refresh_lines_window()
{
assert(tab_width > 0);
link.clear();
email.clear();
clear_word_set();
middle_line_number = 0;
unsigned y = 0;
if (display_info->start()) {
while (y < w_lines_height) {
const line_number_t current_line_num = display_info->current();
if (y < w_lines_height / 2) {
middle_line_number = current_line_num;
}
line_t line = f_idx->line(current_line_num);
assert(current_line_num == line.num_);
unsigned line_num_width = digits(current_line_num);
if (line_num_width < tab_width) {
line_num_width = tab_width;
}
if (line_num_width < 8) {
line_num_width = 8;
}
// apply Replace Display Filters
if (! line.empty()) {
static std::string l;
l = line.to_string();
for (auto df : regex_vec) {
if (df->replace_df_rgx_) {
l = std::regex_replace(l, *(df->replace_df_rgx_), df->replace_df_text_);
}
}
line.assign(l);
}
// handle empty line
if (line.empty()) {
unsigned x = print_line_prefix(y, line.num_, 0, line_num_width);
fill(y, x);
// are we at the end of the lines window?
if (++y >= w_lines_height) {
return;
}
// do we have another line to display?
if (display_info->next()) {
continue; // there is a next line to display
}
else {
break; // last line displayed
}
}
add_to_word_set(line.to_string());
auto wline = to_wide(line.to_string());
// map of pointers into the line and a corresponding curses attribute for the character
std::map<std::wstring::iterator, curses_attr_t> character_attr;
// apply Attribute Display Filters
for(auto df : regex_vec) {
if (df->attribute_df_rgx_) {
for(auto it = std::wsregex_iterator(wline.begin(), wline.end(), *(df->attribute_df_rgx_)), it_end = std::wsregex_iterator(); it != it_end; ++it) {
std::wstring::iterator b = wline.begin() + it->position();
std::wstring::iterator e = b + it->length();
assert(b <= e);
// set character attribute for all matched characters
for (std::wstring::iterator i = b; i != e; ++i) {
character_attr[i] &= ~A_COLOR; // clear any previous color
character_attr[i] |= df->attribute_df_attr_; // set new attribute and color
}
}
}
}
// apply search?
if (search_err.empty()) {
// apply search regex to line
for (auto it = std::wsregex_iterator(wline.begin(), wline.end(), search_rgx); it != std::wsregex_iterator(); ++it) {
std::wstring::iterator b = wline.begin() + it->position();
std::wstring::iterator e = b + it->length();
assert(b <= e);
// set character attribute for all matched characters
for (std::wstring::iterator i = b; i != e; ++i) {
character_attr[i] &= ~A_COLOR; // clear any previous color
character_attr[i] |= (use_color() ? (color(COLOR_GREEN, COLOR_BLACK) | A_BOLD) : A_REVERSE);
}
}
}
// look for links
std::map<std::wstring::iterator, std::wstring> iterator2link;
for (auto it = std::wsregex_iterator(wline.begin(), wline.end(), link_rgx); it != std::wsregex_iterator(); ++it) {
std::wstring::iterator b = wline.begin() + it->position();
std::wstring::iterator e = b + it->length();
assert(b <= e);
std::wstring l(b, e);
// set character attribute for all matched characters
for (std::wstring::iterator i = b; i != e; ++i) {
character_attr[i] |= A_UNDERLINE;
iterator2link[i] = l;
}
}
// look for emails
std::map<std::wstring::iterator, std::wstring> iterator2email;
for (auto it = std::wsregex_iterator(wline.begin(), wline.end(), email_rgx); it != std::wsregex_iterator(); ++it) {
std::wstring::iterator b = wline.begin() + it->position();
std::wstring::iterator e = b + it->length();
assert(b <= e);
std::wstring l(b, e);
// set character attribute for all matched characters
for (std::wstring::iterator i = b; i != e; ++i) {
character_attr[i] |= A_UNDERLINE;
iterator2email[i] = l;
}
}
// print the current line
auto it = wline.begin();
while (it != wline.end() && y < w_lines_height) {
unsigned x = 0;
// block to print left info column
{
// are we at the start of the line?
if (it == wline.begin()) {
// print line number
x += print_line_prefix(y, line.num_, wline.size(), line_num_width);
}
else {
// print empty space
curses_attr a(A_REVERSE | color(COLOR_WHITE, COLOR_BLACK));
for (; x < line_num_width; ++x) {
mvaddch(y, x, ' ');
}
}
// on the upper line, print percentage of position into file
if (y == 0) {
curses_attr a(A_REVERSE | A_BOLD | color(COLOR_WHITE, COLOR_BLACK));
mvprintw(y, 0, "%u%%", static_cast<unsigned>(current_line_num * 100llu / display_info->lastLineNum()));
}
}
// print line in chunks of screen width
curses_attr a(gray_on_black);
for (; it != wline.end() && x < screen_width; ++it) {
// check for link
auto i = iterator2link.find(it);
if (i != iterator2link.end()) {
link.emplace(std::make_pair(x, y), i->second);
}
// check for email
i = iterator2email.find(it);
if (i != iterator2email.end()) {
email.emplace(std::make_pair(x, y), i->second);
}
auto c = *it;
// handle tab character
if (c == '\t') {
do {
mvaddch(y, x++, ' ');
} while (x % tab_width);
}
else {
// replace non printable characters with a space
if (iswprint(c)) {
curses_attr a(character_attr[it]);
mvaddwch(y, x, c);
}
else {
mvaddwch(y, x, L'\uFFFD');
}
++x;
}
}
fill(y, x);
// are we at the end of the lines window?
if (++y >= w_lines_height) {
return;
}
}
// did we display the full line?
if (it == wline.end()) {
// do we have another line to display?
if (display_info->next()) {
continue; // there is a next line to display
}
else {
break; // last line displayed
}
}
}
}
while (y < w_lines_height) {
fill(y++, 0);
}
}
/**
* print the string s at the screen at position x,y.
* The string does not print beyond screen_width.
* @param y vertical coordinate.
* @param x horizontal coordinate.
* @param s string.
* @return number of characters printed.
*/
unsigned print_string(const unsigned y, const unsigned x, std::string s)
{
if (x >= screen_width) {
return 0;
}
if (y >= screen_height) {
return 0;
}
if (s.empty()) {
return 0;
}
if (s.size() >= screen_width - x) {
s.resize(screen_width - x);
}
assert(x + s.size() <= screen_width);
mvprintw(y, x, "%s", s.c_str());
return s.size();
}
void refresh_regex_window(unsigned y)
{
unsigned cnt = 0;
for(auto c : regex_vec) {
++cnt;
std::string s = c->rgx_;
std::string title = "regex";
curses_attr_t attr = A_REVERSE;
if (c->err_.empty()) {
attr |= (cnt & 1) ? gray_on_black : lightgray_on_black;
if (c->ri_) {
title = (cnt < 10) ? "filtr" : "filt";
} else if (c->replace_df_rgx_) {
title = (cnt < 10) ? "disft" : "disf";
} else if (c->attribute_df_rgx_) {
title = (cnt < 10) ? "dfatr" : "dfat";
} else {
title = (cnt < 10) ? " " : " ";
}
} else {
attr |= color(COLOR_RED, COLOR_BLACK);
s += " : ";
s += c->err_;
title = (cnt < 10) ? "error" : "err ";
}
curses_attr a(attr);
unsigned X = 0;
{
curses_attr a(A_BOLD);
mvprintw(y, X, "%s %u ", title.c_str(), cnt);
X += 8;
}
if (!s.empty()) {
X += print_string(y, X, s);
if (c->ri_) {
curses_attr a(use_color() ? 0 : A_BOLD);
s = " (";
const uint64_t num = c->ri_->size();
s += std::to_string(num);
s += " match";
if (num != 1) {
s += "es";
}
s += ", " + std::to_string(num * 100llu / f_idx->size()) + "%)";
X += print_string(y, X, s);
}
}
fill(y, X);
++y;
}
}
void print_centered(const std::string s, int y_offset = 0)
{
int x = screen_width / 2 - s.size() / 2;
if (x < 0) { x = 0; }
int y = screen_height / 2 + y_offset;
if (y < 0) { y = 0; }
mvprintw(y, x, "%s", s.c_str());
}
void refresh_windows()
{
if (screen_height >= min_screen_height && screen_width >= min_screen_width) {
refresh_lines_window();
refresh_regex_window(filter_y);
if (!search_str.empty()) {
curses_attr a(A_BOLD);
mvprintw(search_y, 0, "Search: %s %s", search_str.c_str(), search_err.c_str());
fill(search_y, 8 + search_str.size());
}
}
else if (screen_height < min_screen_height) {
erase();
print_centered("Minimum screen height is " + std::to_string(min_screen_height) + " lines.");
print_centered("Current screen height is " + std::to_string(screen_height) + " lines.", 1);
}
else if (screen_width < min_screen_width) {
erase();
print_centered("Min Width: " + std::to_string(min_screen_width));
print_centered("Cur Width: " + std::to_string(screen_width), 1);
}
else {
// this case should not happen
assert(false);
}
refresh();
}
void calculate_window_sizes()
{
if (screen_height < min_screen_height) {
return;
}
w_lines_height = screen_height;
w_lines_height -= regex_vec.size();
if (!search_str.empty()) {
--w_lines_height;
}
assert(w_lines_height > 0);
// lines window
unsigned y = w_lines_height;
if (regex_vec.size() == 0) {
filter_y = 0;
}
else {
assert(regex_vec.size() <= 10+12);
filter_y = y;
y += regex_vec.size();
}
search_y = screen_height - 1;
}
}
void create_windows()
{
calculate_window_sizes();
refresh_windows();
}
bool initialize_curses()
{
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, false);
keypad(stdscr, true);
halfdelay(3);
mousemask(BUTTON1_CLICKED, nullptr);
curs_set(0); // disable cursor
if (use_color()) {
use_color(has_colors() ? true : false);
}
if (use_color()) {
start_color();
}
create_windows();
return true;
}
void close_curses()
{
endwin();
}
namespace {
void key_up()
{
if (display_info->isFirstLineDisplayed()) {
info = "moved to top";
} else {
display_info->up();
}
refresh_lines_window();
refresh();
}
void key_down()
{
if (display_info->isLastLineDisplayed()) {
info = "moved to bottom";
} else {
display_info->down();
}
refresh_lines_window();
refresh();
}
void key_npage()
{
if (display_info->isLastLineDisplayed()) {
info = "moved to bottom";
} else {
display_info->page_down();
}
refresh_lines_window();
refresh();
}
void key_ppage()
{
if (! display_info->start()) {
info = "nothing to display";
} else if (display_info->isFirstLineDisplayed()) {
info = "moved to top";
} else {
const line_number_t oldTopLineNum = display_info->current();
// scroll up one page, even if it is too far.
for(unsigned i = 0; i < w_lines_height - 1; ++i) {
display_info->up();
}
// render lines
refresh_lines_window();
// now scroll down, until old top line is at the bottom
while(display_info->bottomLineNum() < oldTopLineNum) {
display_info->down();
refresh_lines_window();
}
}
refresh_lines_window();
refresh();
}
// position display on top line
void key_g()
{
if (display_info->isFirstLineDisplayed()) {
info = "moved to top";
} else {
display_info->top();
}
refresh_lines_window();
refresh();
}
// position display on bottom line
void key_G()
{
if (! display_info->start()) {
info = "nothing to display";
} else if (display_info->isLastLineDisplayed()) {
info = "moved to bottom";
} else {
const line_number_t lastLineNum = display_info->lastLineNum();
display_info->go_to(lastLineNum);
// scroll up until we don't print the last line any more
while (!display_info->isFirstLineDisplayed()) {
display_info->up();
refresh_lines_window();
if (display_info->bottomLineNum() != lastLineNum) {
break;
}
}
// now scroll down again one line, so we see the last line
display_info->down();
}
refresh_lines_window();
refresh();
}
void key_d()
{
if (middle_line_number == 0) {
return;
}
display_info->go_to(middle_line_number);
refresh_lines_window();
refresh();
}
void key_u()
{
if (middle_line_number == 0) {
return;
}
// scroll up until the old middle line number is the bottom line
const line_number_t old_mln = middle_line_number;
while (!display_info->isFirstLineDisplayed() && display_info->bottomLineNum() > old_mln) {
display_info->up();
refresh_lines_window();
}
refresh();
}
// \todo maybe background search?
void key_n()
{
info = "searching...";
refresh_lines_window();
refresh_info();
refresh();
if (! search_next(search_rgx, display_info, f_idx)) {
info = "did not find any next search match";
} else {
info = "next match found";
}
refresh_lines_window();
refresh_info();
refresh();
}
void key_N()
{
info = "searching...";
refresh_lines_window();
refresh_info();
refresh();
if (! search_prev(search_rgx, display_info, f_idx)) {
info = "did not find any next search match";
} else {
info = "prev match found";
}
refresh_lines_window();
refresh_info();
refresh();
}
void key_h()
{
#if defined(__unix__)
// try to render a text version of the manpage
try {
TemporaryFile tmp;
if (! run_command("MANWIDTH=" + std::to_string(screen_width - 10) + " man few > " + tmp.filename() + " 2> /dev/null", info)) {
return;
}
run_program(argv0 + " " + tmp.filename(), info, false);
} catch (std::exception& e) {
info = "could not show help: ";
info += e.what();
} catch (...) {
info = "could not show help: unknown exception";
}
#else
info = "help not supported on this platform";
#endif
}
void key_M()
{
maximize_window(info);
}
void key_mouse()
{
MEVENT e;
if (getmouse(&e) != OK) {
return;
}
if (e.bstate & BUTTON1_CLICKED) {
// check for link
auto it = link.find(std::make_pair(static_cast<unsigned>(e.x), static_cast<unsigned>(e.y)));
if (it != link.end()) {
const std::string l = to_utf8(it->second);
if (click_link(l, info)) {
info = "opened " + l + " in browser";
}
refresh_lines_window();
refresh();
}
// check for emails
it = email.find(std::make_pair(static_cast<unsigned>(e.x), static_cast<unsigned>(e.y)));
if (it != email.end()) {
const std::string e = to_utf8(it->second);
if (click_email(e, command_line_filename, info)) {
info = "created email to " + e;
}
refresh_lines_window();
refresh();
}
}
}
/**
* provision the display_info object.
* use the lines from f_idx and filter with the regular expression vector filter_vec.
*/
void intersect_regex(ProgressFunctor *func)
{
// set up a vector regex_index lineNum_vector iterator pairs
std::shared_ptr<regex_index> ri;
lineNum_vector_intersect_vector_t v;
for(auto c : regex_vec) {
if (c->ri_) {
ri = c->ri_;
const auto& s = ri->lineNum_vector();
v.push_back(std::make_pair(s.begin(), s.end()));
}
}
lineNum_vector_t s;
// if there are no regex_index objects found, show the complete file
if (v.empty()) {
s = f_idx->lineNum_vector();
} else if (v.size() == 1) {
// if there is only a single regex_index object, use that one
s = ri->lineNum_vector();
} else {
multiple_set_intersect(v.begin(), v.end(), std::back_insert_iterator<lineNum_vector_t>(s));
}
display_info->assign(std::move(s));
}
void intersect_regex_curses()
{
CursesProgressFunctor func(screen_height / 2, screen_width / 2 - 10, A_REVERSE|A_BOLD, " intersect regex: ");
intersect_regex(&func);
}
class CursesCursorHelper
{
const int prev_;
public:
CursesCursorHelper(int state) :
prev_(curs_set(state))
{}
~CursesCursorHelper()
{
if (prev_ != ERR) {
curs_set(prev_);
}
}
};
History::ptr_t line_edit_history;
/// the function pointer type of an autocomplete function.
typedef std::set<std::string> (*autocomplete_f)(std::string& path, std::string& err);
/**
* read an input string with curses.
* The input windows is positioned at coordinates x,y and has a maximum width of max_width.
* If you press the enter/return key the function finishes and returns the currently edited string.
* If you press the escape key or CTRL+g the function finishes and returns the input string.
*
* If the function pointer autocomplete_f is set and the tab key
* is pressed, the function will call the autocomplete function
* with the current edited string.
*
* @param y vertical coordinate.
* @param x horizontal coordinate.
* @param input initial string.
* @param max_width maximum width of edit window. This will also limit the size of the returned string.
* @param autocomplete_func function pointer to an auto complete function, can be NULL.
* @return edited string.
*/
std::string line_edit(const unsigned y,
const unsigned x,
const std::string& input,
const unsigned max_width,
autocomplete_f autocomplete_func)
{
static std::string killring;
if (max_width < 1) {
return input;
}
std::string s = input;
if (s.size() > max_width) {
s.resize(max_width);
}
History::iterator_t history_it;
// the cursor position
unsigned X = s.size();
// set curses cursor to very visible
CursesCursorHelper cch(2);
// an info string displayed on the edit line
std::string line_edit_info;
// \todo remove the debug stuff
#define LINEEDITDEBUG 0
#if LINEEDITDEBUG
int doj=-1, dojkey=-1;
#endif
while(true) {
#if LINEEDITDEBUG
mvprintw(0,0,"'%s' %i doj=%i key=%i X+%i ", s.c_str(), s.size(), doj, dojkey, X);
mvprintw(1,0,"'%s' %i ", line_edit_info.c_str(), line_edit_info.size());
#endif
// print current line, filling up with spaces to max_width
std::string displayed_line = s + line_edit_info;
for(unsigned i = 0; i < max_width; ++i) {
char c = ' ';
if (i < displayed_line.size()) {
c = displayed_line[i];
}
mvaddch(y, x + i, c);
}
move(y, x+X); // position cursor
refresh();
int key = getch();
if (key == '\t' && autocomplete_func) {