From 9b68d7b60a306acf79e5b00496ebaf4e2b13cbf5 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 10 Mar 2017 00:58:06 -0800 Subject: 3784 --- html/100trace_browser.cc.html | 804 +++++++++++++++++++++++++----------------- 1 file changed, 487 insertions(+), 317 deletions(-) diff --git a/html/100trace_browser.cc.html b/html/100trace_browser.cc.html index 1f08868c..5f481652 100644 --- a/html/100trace_browser.cc.html +++ b/html/100trace_browser.cc.html @@ -59,326 +59,496 @@ if ('onhashchange' in window) {
   1 //: A debugging helper that lets you zoom in/out on a trace.
-  2 //:
-  3 //: To try it out, first create an example trace:
-  4 //:   mu --trace nqueens.mu
-  5 //: Then to browse the trace, which was stored in a file called 'last_run':
-  6 //:   mu browse-trace last_run
-  7 //:
-  8 //: You should now find yourself in a UI showing a subsequence of lines from
-  9 //: the trace, each line starting with a numeric depth, and ending with a
- 10 //: parenthetical count of trace lines hidden after it with greater depths.
- 11 //:
- 12 //: For example, this line:
- 13 //:   2 app: line1 (30)
- 14 //: indicates that it was logged with depth 2, and that 30 following lines
- 15 //: have been hidden at a depth greater than 2.
- 16 //:
- 17 //: As an experiment, hidden counts of 1000 or more are in red to highlight
- 18 //: where you might be particularly interested in expanding.
- 19 //:
- 20 //: The UI provides the following hotkeys:
- 21 //:
- 22 //:   `q` or `ctrl-c`: Quit.
- 23 //:
- 24 //:   `Enter`: 'Zoom into' this line. Expand some or all of the hidden lines
- 25 //:   at the next higher level, updating parenthetical counts of hidden lines.
- 26 //:
- 27 //:   `Backspace`: 'Zoom out' on a line after zooming in, collapsing expanded
- 28 //:   lines below by some series of <Enter> commands.
- 29 //:
- 30 //:   `j` or `down-arrow`: Move/scroll cursor down one line.
- 31 //:   `k` or `up-arrow`: Move/scroll cursor up one line.
- 32 //:   `J` or `ctrl-f` or `page-down`: Scroll cursor down one page.
- 33 //:   `K` or `ctrl-b` or `page-up`: Scroll cursor up one page.
- 34 //:   `h` or `left-arrow`: Scroll cursor left one character.
- 35 //:   `l` or `right-arrow`: Scroll cursor right one character.
- 36 //:   `H`: Scroll cursor left one screen-width.
- 37 //:   `L`: Scroll cursor right one screen-width.
- 38 //:
- 39 //:   `g` or `home`: Move cursor to start of trace.
- 40 //:   `G` or `end`: Move cursor to end of trace.
- 41 //:
- 42 //:   `t`: Move cursor to top line on screen.
- 43 //:   `c`: Move cursor to center line on screen.
- 44 //:   `b`: Move cursor to bottom line on screen.
- 45 //:   `T`: Scroll line at cursor to top of screen.
- 46 
- 47 :(before "End Primitive Recipe Declarations")
- 48 _BROWSE_TRACE,
- 49 :(before "End Primitive Recipe Numbers")
- 50 put(Recipe_ordinal, "$browse-trace", _BROWSE_TRACE);
- 51 :(before "End Primitive Recipe Checks")
- 52 case _BROWSE_TRACE: {
- 53   break;
- 54 }
- 55 :(before "End Primitive Recipe Implementations")
- 56 case _BROWSE_TRACE: {
- 57   start_trace_browser();
- 58   break;
- 59 }
- 60 
- 61 //: browse a trace loaded from a file
- 62 :(after "Commandline Parsing")
- 63 if (argc == 3 && is_equal(argv[1], "browse-trace")) {
- 64   load_trace(argv[2]);
- 65   start_trace_browser();
- 66   return 0;
- 67 }
- 68 
- 69 :(before "End Globals")
- 70 set<int> Visible;
- 71 int Top_of_screen = 0;
- 72 int Left_of_screen = 0;
- 73 int Last_printed_row = 0;
- 74 map<int, int> Trace_index;  // screen row -> trace index
- 75 
- 76 :(code)
- 77 void start_trace_browser() {
- 78   if (!Trace_stream) return;
- 79   cerr << "computing min depth to display\n";
- 80   int min_depth = 9999;
- 81   for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
- 82   ¦ trace_line& curr_line = Trace_stream->past_lines.at(i);
- 83   ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
- 84   }
- 85   cerr << "min depth is " << min_depth << '\n';
- 86   cerr << "computing lines to display\n";
- 87   for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
- 88   ¦ if (Trace_stream->past_lines.at(i).depth == min_depth)
- 89   ¦ ¦ Visible.insert(i);
- 90   }
- 91   tb_init();
- 92   Display_row = Display_column = 0;
- 93   tb_event event;
- 94   Top_of_screen = 0;
- 95   refresh_screen_rows();
- 96   while (true) {
- 97   ¦ render();
- 98   ¦ do {
- 99   ¦ ¦ tb_poll_event(&event);
-100   ¦ } while (event.type != TB_EVENT_KEY);
-101   ¦ int key = event.key ? event.key : event.ch;
-102   ¦ if (key == 'q' || key == 'Q' || key == TB_KEY_CTRL_C) break;
-103   ¦ if (key == 'j' || key == TB_KEY_ARROW_DOWN) {
-104   ¦ ¦ // move cursor one line down
-105   ¦ ¦ if (Display_row < Last_printed_row) ++Display_row;
-106   ¦ }
-107   ¦ if (key == 'k' || key == TB_KEY_ARROW_UP) {
-108   ¦ ¦ // move cursor one line up
-109   ¦ ¦ if (Display_row > 0) --Display_row;
-110   ¦ }
-111   ¦ if (key == 't') {
-112   ¦ ¦ // move cursor to top of screen
-113   ¦ ¦ Display_row = 0;
-114   ¦ }
-115   ¦ if (key == 'c') {
-116   ¦ ¦ // move cursor to center of screen
-117   ¦ ¦ Display_row = tb_height()/2;
-118   ¦ }
-119   ¦ if (key == 'b') {
-120   ¦ ¦ // move cursor to bottom of screen
-121   ¦ ¦ Display_row = tb_height()-1;
-122   ¦ }
-123   ¦ if (key == 'T') {
-124   ¦ ¦ // move cursor _row_ to top of screen
-125   ¦ ¦ Top_of_screen = get(Trace_index, Display_row);
+  2 //: Warning: this tool has zero automated tests.
+  3 //:
+  4 //: To try it out, first create an example trace:
+  5 //:   mu --trace nqueens.mu
+  6 //: Then to browse the trace, which was stored in a file called 'last_run':
+  7 //:   mu browse-trace last_run
+  8 //:
+  9 //: You should now find yourself in a UI showing a subsequence of lines from
+ 10 //: the trace, each line starting with a numeric depth, and ending with a
+ 11 //: parenthetical count of trace lines hidden after it with greater depths.
+ 12 //:
+ 13 //: For example, this line:
+ 14 //:   2 app: line1 (30)
+ 15 //: indicates that it was logged with depth 2, and that 30 following lines
+ 16 //: have been hidden at a depth greater than 2.
+ 17 //:
+ 18 //: As an experiment, hidden counts of 1000 or more are in red to highlight
+ 19 //: where you might be particularly interested in expanding.
+ 20 //:
+ 21 //: The UI provides the following hotkeys:
+ 22 //:
+ 23 //:   `q` or `ctrl-c`: Quit.
+ 24 //:
+ 25 //:   `Enter`: 'Zoom into' this line. Expand some or all of the hidden lines
+ 26 //:   at the next higher level, updating parenthetical counts of hidden lines.
+ 27 //:
+ 28 //:   `Backspace`: 'Zoom out' on a line after zooming in, collapsing expanded
+ 29 //:   lines below by some series of <Enter> commands.
+ 30 //:
+ 31 //:   `j` or `down-arrow`: Move/scroll cursor down one line.
+ 32 //:   `k` or `up-arrow`: Move/scroll cursor up one line.
+ 33 //:   `J` or `ctrl-f` or `page-down`: Scroll cursor down one page.
+ 34 //:   `K` or `ctrl-b` or `page-up`: Scroll cursor up one page.
+ 35 //:   `h` or `left-arrow`: Scroll cursor left one character.
+ 36 //:   `l` or `right-arrow`: Scroll cursor right one character.
+ 37 //:   `H`: Scroll cursor left one screen-width.
+ 38 //:   `L`: Scroll cursor right one screen-width.
+ 39 //:
+ 40 //:   `g` or `home`: Move cursor to start of trace.
+ 41 //:   `G` or `end`: Move cursor to end of trace.
+ 42 //:
+ 43 //:   `t`: Move cursor to top line on screen.
+ 44 //:   `c`: Move cursor to center line on screen.
+ 45 //:   `b`: Move cursor to bottom line on screen.
+ 46 //:   `T`: Scroll line at cursor to top of screen.
+ 47 //:
+ 48 //:   `/`: Search for a pattern.
+ 49 //:   `n`: Jump to next instance of search pattern.
+ 50 //:   `N`: Jump to previous instance of search pattern.
+ 51 //:
+ 52 //:   After hitting `/`, a small editor on the bottom-most line supports the
+ 53 //:   following hotkeys:
+ 54 //:     ascii characters: add the key to the pattern.
+ 55 //:     `Enter`: search for the pattern.
+ 56 //:     `Esc` or `ctrl-c`: cancel the current search, setting the screen back
+ 57 //:       to its state before the search.
+ 58 //:     `left-arrow`: move cursor left.
+ 59 //:     `right-arrow`: move cursor right.
+ 60 //:     `ctrl-a` or `home`: move cursor to start of search pattern.
+ 61 //:     `ctrl-e` or `end`: move cursor to end of search pattern.
+ 62 
+ 63 :(before "End Primitive Recipe Declarations")
+ 64 _BROWSE_TRACE,
+ 65 :(before "End Primitive Recipe Numbers")
+ 66 put(Recipe_ordinal, "$browse-trace", _BROWSE_TRACE);
+ 67 :(before "End Primitive Recipe Checks")
+ 68 case _BROWSE_TRACE: {
+ 69   break;
+ 70 }
+ 71 :(before "End Primitive Recipe Implementations")
+ 72 case _BROWSE_TRACE: {
+ 73   start_trace_browser();
+ 74   break;
+ 75 }
+ 76 
+ 77 //: browse a trace loaded from a file
+ 78 :(after "Commandline Parsing")
+ 79 if (argc == 3 && is_equal(argv[1], "browse-trace")) {
+ 80   load_trace(argv[2]);
+ 81   start_trace_browser();
+ 82   return 0;
+ 83 }
+ 84 
+ 85 :(before "End Globals")
+ 86 set<int> Visible;
+ 87 int Top_of_screen = 0;
+ 88 int Left_of_screen = 0;
+ 89 int Last_printed_row = 0;
+ 90 map<int, int> Trace_index;  // screen row -> trace index
+ 91 string Current_search_pattern = "";
+ 92 
+ 93 :(code)
+ 94 void start_trace_browser() {
+ 95   if (!Trace_stream) return;
+ 96   cerr << "computing min depth to display\n";
+ 97   int min_depth = 9999;
+ 98   for (int i = 0;  i < SIZE(Trace_stream->past_lines);  ++i) {
+ 99   ¦ trace_line& curr_line = Trace_stream->past_lines.at(i);
+100   ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
+101   }
+102   cerr << "min depth is " << min_depth << '\n';
+103   cerr << "computing lines to display\n";
+104   for (int i = 0;  i < SIZE(Trace_stream->past_lines);  ++i) {
+105   ¦ if (Trace_stream->past_lines.at(i).depth == min_depth)
+106   ¦ ¦ Visible.insert(i);
+107   }
+108   tb_init();
+109   Display_row = Display_column = 0;
+110   Top_of_screen = 0;
+111   refresh_screen_rows();
+112   while (true) {
+113   ¦ render();
+114   ¦ int key = read_key();
+115   ¦ if (key == 'q' || key == 'Q' || key == TB_KEY_CTRL_C) break;
+116   ¦ if (key == 'j' || key == TB_KEY_ARROW_DOWN) {
+117   ¦ ¦ // move cursor one line down
+118   ¦ ¦ if (Display_row < Last_printed_row) ++Display_row;
+119   ¦ }
+120   ¦ else if (key == 'k' || key == TB_KEY_ARROW_UP) {
+121   ¦ ¦ // move cursor one line up
+122   ¦ ¦ if (Display_row > 0) --Display_row;
+123   ¦ }
+124   ¦ else if (key == 't') {
+125   ¦ ¦ // move cursor to top of screen
 126   ¦ ¦ Display_row = 0;
-127   ¦ ¦ refresh_screen_rows();
-128   ¦ }
-129   ¦ if (key == 'h' || key == TB_KEY_ARROW_LEFT) {
-130   ¦ ¦ // pan screen one character right
-131   ¦ ¦ --Left_of_screen;
-132   ¦ }
-133   ¦ if (key == 'l' || key == TB_KEY_ARROW_RIGHT) {
-134   ¦ ¦ // pan screen one character left
-135   ¦ ¦ ++Left_of_screen;
-136   ¦ }
-137   ¦ if (key == 'H') {
-138   ¦ ¦ // pan screen one screen-width left
-139   ¦ ¦ Left_of_screen -= (tb_width() - 5);
-140   ¦ ¦ if (Left_of_screen < 0) Left_of_screen = 0;
-141   ¦ }
-142   ¦ if (key == 'L') {
-143   ¦ ¦ // pan screen one screen-width right
-144   ¦ ¦ Left_of_screen += (tb_width() - 5);
+127   ¦ }
+128   ¦ else if (key == 'c') {
+129   ¦ ¦ // move cursor to center of screen
+130   ¦ ¦ Display_row = tb_height()/2;
+131   ¦ ¦ while (!contains_key(Trace_index, Display_row))
+132   ¦ ¦ ¦ --Display_row;
+133   ¦ }
+134   ¦ else if (key == 'b') {
+135   ¦ ¦ // move cursor to bottom of screen
+136   ¦ ¦ Display_row = tb_height()-1;
+137   ¦ ¦ while (!contains_key(Trace_index, Display_row))
+138   ¦ ¦ ¦ --Display_row;
+139   ¦ }
+140   ¦ else if (key == 'T') {
+141   ¦ ¦ // scroll line at cursor to top of screen
+142   ¦ ¦ Top_of_screen = get(Trace_index, Display_row);
+143   ¦ ¦ Display_row = 0;
+144   ¦ ¦ refresh_screen_rows();
 145   ¦ }
-146   ¦ if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) {
-147   ¦ ¦ // page-down
-148   ¦ ¦ if (Trace_index.find(tb_height()-1) != Trace_index.end()) {
-149   ¦ ¦ ¦ Top_of_screen = get(Trace_index, tb_height()-1) + 1;
-150   ¦ ¦ ¦ refresh_screen_rows();
-151   ¦ ¦ }
-152   ¦ }
-153   ¦ if (key == 'K' || key == TB_KEY_PGUP || key == TB_KEY_CTRL_B) {
-154   ¦ ¦ // page-up is more convoluted
-155   ¦ ¦ for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) {
-156   ¦ ¦ ¦ --Top_of_screen;
-157   ¦ ¦ ¦ if (Top_of_screen <= 0) break;
-158   ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
-159   ¦ ¦ ¦ ¦ --Top_of_screen;
-160   ¦ ¦ }
-161   ¦ ¦ if (Top_of_screen >= 0)
-162   ¦ ¦ ¦ refresh_screen_rows();
-163   ¦ }
-164   ¦ if (key == 'g' || key == TB_KEY_HOME) {
-165   ¦ ¦ ¦ Top_of_screen = 0;
-166   ¦ ¦ ¦ Last_printed_row = 0;
-167   ¦ ¦ ¦ Display_row = 0;
-168   ¦ ¦ ¦ refresh_screen_rows();
+146   ¦ else if (key == 'h' || key == TB_KEY_ARROW_LEFT) {
+147   ¦ ¦ // pan screen one character left
+148   ¦ ¦ if (Left_of_screen > 0) --Left_of_screen;
+149   ¦ }
+150   ¦ else if (key == 'l' || key == TB_KEY_ARROW_RIGHT) {
+151   ¦ ¦ // pan screen one character right
+152   ¦ ¦ ++Left_of_screen;
+153   ¦ }
+154   ¦ else if (key == 'H') {
+155   ¦ ¦ // pan screen one screen-width left
+156   ¦ ¦ Left_of_screen -= (tb_width() - 5);
+157   ¦ ¦ if (Left_of_screen < 0) Left_of_screen = 0;
+158   ¦ }
+159   ¦ else if (key == 'L') {
+160   ¦ ¦ // pan screen one screen-width right
+161   ¦ ¦ Left_of_screen += (tb_width() - 5);
+162   ¦ }
+163   ¦ else if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) {
+164   ¦ ¦ // page-down
+165   ¦ ¦ if (Trace_index.find(tb_height()-1) != Trace_index.end()) {
+166   ¦ ¦ ¦ Top_of_screen = get(Trace_index, tb_height()-1) + 1;
+167   ¦ ¦ ¦ refresh_screen_rows();
+168   ¦ ¦ }
 169   ¦ }
-170   ¦ if (key == 'G' || key == TB_KEY_END) {
-171   ¦ ¦ // go to bottom of screen; largely like page-up, interestingly
-172   ¦ ¦ Top_of_screen = SIZE(Trace_stream->past_lines)-1;
-173   ¦ ¦ for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) {
-174   ¦ ¦ ¦ --Top_of_screen;
-175   ¦ ¦ ¦ if (Top_of_screen <= 0) break;
-176   ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
-177   ¦ ¦ ¦ ¦ --Top_of_screen;
-178   ¦ ¦ }
-179   ¦ ¦ refresh_screen_rows();
-180   ¦ ¦ // move cursor to bottom
-181   ¦ ¦ Display_row = Last_printed_row;
-182   ¦ ¦ refresh_screen_rows();
-183   ¦ }
-184   ¦ if (key == TB_KEY_CARRIAGE_RETURN) {
-185   ¦ ¦ // expand lines under current by one level
-186   ¦ ¦ assert(contains_key(Trace_index, Display_row));
-187   ¦ ¦ int start_index = get(Trace_index, Display_row);
-188   ¦ ¦ int index = 0;
-189   ¦ ¦ // simultaneously compute end_index and min_depth
-190   ¦ ¦ int min_depth = 9999;
-191   ¦ ¦ for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) {
-192   ¦ ¦ ¦ if (contains_key(Visible, index)) break;
-193   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
-194   ¦ ¦ ¦ assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth);
-195   ¦ ¦ ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
-196   ¦ ¦ }
-197   ¦ ¦ int end_index = index;
-198   ¦ ¦ // mark as visible all intervening indices at min_depth
-199   ¦ ¦ for (index = start_index; index < end_index; ++index) {
-200   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
-201   ¦ ¦ ¦ if (curr_line.depth == min_depth) {
-202   ¦ ¦ ¦ ¦ Visible.insert(index);
-203   ¦ ¦ ¦ }
-204   ¦ ¦ }
-205   ¦ ¦ refresh_screen_rows();
-206   ¦ }
-207   ¦ if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
-208   ¦ ¦ // collapse all lines under current
-209   ¦ ¦ assert(contains_key(Trace_index, Display_row));
-210   ¦ ¦ int start_index = get(Trace_index, Display_row);
-211   ¦ ¦ int index = 0;
-212   ¦ ¦ // end_index is the next line at a depth same as or lower than start_index
-213   ¦ ¦ int initial_depth = Trace_stream->past_lines.at(start_index).depth;
-214   ¦ ¦ for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) {
-215   ¦ ¦ ¦ if (!contains_key(Visible, index)) continue;
-216   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
-217   ¦ ¦ ¦ if (curr_line.depth <= initial_depth) break;
-218   ¦ ¦ }
-219   ¦ ¦ int end_index = index;
-220   ¦ ¦ // mark as visible all intervening indices at min_depth
-221   ¦ ¦ for (index = start_index+1; index < end_index; ++index) {
-222   ¦ ¦ ¦ Visible.erase(index);
-223   ¦ ¦ }
-224   ¦ ¦ refresh_screen_rows();
-225   ¦ }
-226   }
-227   tb_shutdown();
-228 }
-229 
-230 // update Trace_indices for each screen_row on the basis of Top_of_screen and Visible
-231 void refresh_screen_rows() {
-232   int screen_row = 0, index = 0;
-233   Trace_index.clear();
-234   for (screen_row = 0, index = Top_of_screen; screen_row < tb_height() && index < SIZE(Trace_stream->past_lines); ++screen_row, ++index) {
-235   ¦ // skip lines without depth for now
-236   ¦ while (!contains_key(Visible, index)) {
-237   ¦ ¦ ++index;
-238   ¦ ¦ if (index >= SIZE(Trace_stream->past_lines)) goto done;
-239   ¦ }
-240   ¦ assert(index < SIZE(Trace_stream->past_lines));
-241   ¦ put(Trace_index, screen_row, index);
-242   }
-243 done:;
-244 }
-245 
-246 void render() {
-247   int screen_row = 0;
-248   for (screen_row = 0; screen_row < tb_height(); ++screen_row) {
-249   ¦ if (!contains_key(Trace_index, screen_row)) break;
-250   ¦ trace_line& curr_line = Trace_stream->past_lines.at(get(Trace_index, screen_row));
-251   ¦ ostringstream out;
-252   ¦ out << std::setw(4) << curr_line.depth << ' ' << curr_line.label << ": " << curr_line.contents;
-253   ¦ if (screen_row < tb_height()-1) {
-254   ¦ ¦ int delta = lines_hidden(screen_row);
-255   ¦ ¦ // home-brew escape sequence for red
-256   ¦ ¦ if (delta > 999) out << static_cast<char>(1);
-257   ¦ ¦ out << " (" << delta << ")";
-258   ¦ ¦ if (delta > 999) out << static_cast<char>(2);
-259   ¦ }
-260   ¦ render_line(screen_row, out.str(), screen_row == Display_row);
-261   }
-262   // clear rest of screen
-263   Last_printed_row = screen_row-1;
-264   for (; screen_row < tb_height(); ++screen_row) {
-265   ¦ render_line(screen_row, "~", /*highlight?*/false);
-266   }
-267   // move cursor back to display row at the end
-268   tb_set_cursor(0, Display_row);
-269   tb_present();
-270 }
-271 
-272 int lines_hidden(int screen_row) {
-273   assert(contains_key(Trace_index, screen_row));
-274   if (!contains_key(Trace_index, screen_row+1))
-275   ¦ return SIZE(Trace_stream->past_lines) - get(Trace_index, screen_row);
-276   else
-277   ¦ return get(Trace_index, screen_row+1) - get(Trace_index, screen_row);
-278 }
-279 
-280 void render_line(int screen_row, const string& s, bool highlight) {
-281   int col = 0;
-282   int color = TB_WHITE;
-283   for (col = 0; col < tb_width() && col+Left_of_screen < SIZE(s); ++col) {
-284   ¦ char c = s.at(col+Left_of_screen);  // todo: unicode
-285   ¦ if (c == '\n') c = ';';  // replace newlines with semi-colons
-286   ¦ // escapes. hack: can't start a line with them.
-287   ¦ if (c == '\1') { color = /*red*/1; c = ' '; }
-288   ¦ if (c == '\2') { color = TB_WHITE; c = ' '; }
-289   ¦ tb_change_cell(col, screen_row, c, color, highlight ? /*subtle grey*/240 : TB_BLACK);
-290   }
-291   for (; col < tb_width(); ++col)
-292   ¦ tb_change_cell(col, screen_row, ' ', TB_WHITE, highlight ? /*subtle grey*/240 : TB_BLACK);
-293 }
-294 
-295 void load_trace(const char* filename) {
-296   ifstream tin(filename);
-297   if (!tin) {
-298   ¦ cerr << "no such file: " << filename << '\n';
-299   ¦ exit(1);
-300   }
-301   Trace_stream = new trace_stream;
-302   while (has_data(tin)) {
-303   ¦ tin >> std::noskipws;
-304   ¦ ¦ skip_whitespace_but_not_newline(tin);
-305   ¦ ¦ if (!isdigit(tin.peek())) {
-306   ¦ ¦ ¦ string dummy;
-307   ¦ ¦ ¦ getline(tin, dummy);
-308   ¦ ¦ ¦ continue;
-309   ¦ ¦ }
-310   ¦ tin >> std::skipws;
-311   ¦ int depth;
-312   ¦ tin >> depth;
-313   ¦ string label;
-314   ¦ tin >> label;
-315   ¦ if (*--label.end() == ':') label.erase(--label.end());
-316   ¦ string line;
-317   ¦ getline(tin, line);
-318   ¦ Trace_stream->past_lines.push_back(trace_line(depth, label, line));
-319   }
-320   cerr << "lines read: " << Trace_stream->past_lines.size() << '\n';
-321 }
+170   ¦ else if (key == 'K' || key == TB_KEY_PGUP || key == TB_KEY_CTRL_B) {
+171   ¦ ¦ // page-up is more convoluted
+172   ¦ ¦ for (int screen_row = tb_height();  screen_row > 0 && Top_of_screen > 0;  --screen_row) {
+173   ¦ ¦ ¦ --Top_of_screen;
+174   ¦ ¦ ¦ if (Top_of_screen <= 0) break;
+175   ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
+176   ¦ ¦ ¦ ¦ --Top_of_screen;
+177   ¦ ¦ }
+178   ¦ ¦ if (Top_of_screen >= 0)
+179   ¦ ¦ ¦ refresh_screen_rows();
+180   ¦ }
+181   ¦ else if (key == 'g' || key == TB_KEY_HOME) {
+182   ¦ ¦ ¦ Top_of_screen = 0;
+183   ¦ ¦ ¦ Last_printed_row = 0;
+184   ¦ ¦ ¦ Display_row = 0;
+185   ¦ ¦ ¦ refresh_screen_rows();
+186   ¦ }
+187   ¦ else if (key == 'G' || key == TB_KEY_END) {
+188   ¦ ¦ // go to bottom of screen; largely like page-up, interestingly
+189   ¦ ¦ Top_of_screen = SIZE(Trace_stream->past_lines)-1;
+190   ¦ ¦ for (int screen_row = tb_height();  screen_row > 0 && Top_of_screen > 0;  --screen_row) {
+191   ¦ ¦ ¦ --Top_of_screen;
+192   ¦ ¦ ¦ if (Top_of_screen <= 0) break;
+193   ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
+194   ¦ ¦ ¦ ¦ --Top_of_screen;
+195   ¦ ¦ }
+196   ¦ ¦ refresh_screen_rows();
+197   ¦ ¦ // move cursor to bottom
+198   ¦ ¦ Display_row = Last_printed_row;
+199   ¦ ¦ refresh_screen_rows();
+200   ¦ }
+201   ¦ else if (key == TB_KEY_CARRIAGE_RETURN) {
+202   ¦ ¦ // expand lines under current by one level
+203   ¦ ¦ assert(contains_key(Trace_index, Display_row));
+204   ¦ ¦ int start_index = get(Trace_index, Display_row);
+205   ¦ ¦ int index = 0;
+206   ¦ ¦ // simultaneously compute end_index and min_depth
+207   ¦ ¦ int min_depth = 9999;
+208   ¦ ¦ for (index = start_index+1;  index < SIZE(Trace_stream->past_lines);  ++index) {
+209   ¦ ¦ ¦ if (contains_key(Visible, index)) break;
+210   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
+211   ¦ ¦ ¦ assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth);
+212   ¦ ¦ ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
+213   ¦ ¦ }
+214   ¦ ¦ int end_index = index;
+215   ¦ ¦ // mark as visible all intervening indices at min_depth
+216   ¦ ¦ for (index = start_index;  index < end_index;  ++index) {
+217   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
+218   ¦ ¦ ¦ if (curr_line.depth == min_depth) {
+219   ¦ ¦ ¦ ¦ Visible.insert(index);
+220   ¦ ¦ ¦ }
+221   ¦ ¦ }
+222   ¦ ¦ refresh_screen_rows();
+223   ¦ }
+224   ¦ else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
+225   ¦ ¦ // collapse all lines under current
+226   ¦ ¦ assert(contains_key(Trace_index, Display_row));
+227   ¦ ¦ int start_index = get(Trace_index, Display_row);
+228   ¦ ¦ int index = 0;
+229   ¦ ¦ // end_index is the next line at a depth same as or lower than start_index
+230   ¦ ¦ int initial_depth = Trace_stream->past_lines.at(start_index).depth;
+231   ¦ ¦ for (index = start_index+1;  index < SIZE(Trace_stream->past_lines);  ++index) {
+232   ¦ ¦ ¦ if (!contains_key(Visible, index)) continue;
+233   ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
+234   ¦ ¦ ¦ if (curr_line.depth <= initial_depth) break;
+235   ¦ ¦ }
+236   ¦ ¦ int end_index = index;
+237   ¦ ¦ // mark as visible all intervening indices at min_depth
+238   ¦ ¦ for (index = start_index+1;  index < end_index;  ++index) {
+239   ¦ ¦ ¦ Visible.erase(index);
+240   ¦ ¦ }
+241   ¦ ¦ refresh_screen_rows();
+242   ¦ }
+243   ¦ else if (key == '/') {
+244   ¦ ¦ if (start_search_editor())
+245   ¦ ¦ ¦ search_next(Current_search_pattern);
+246   ¦ }
+247   ¦ else if (key == 'n') {
+248   ¦ ¦ if (!Current_search_pattern.empty())
+249   ¦ ¦ ¦ search_next(Current_search_pattern);
+250   ¦ }
+251   ¦ else if (key == 'N') {
+252   ¦ ¦ if (!Current_search_pattern.empty())
+253   ¦ ¦ ¦ search_previous(Current_search_pattern);
+254   ¦ }
+255   }
+256   tb_shutdown();
+257 }
+258 
+259 bool start_search_editor() {
+260   const int bottom_screen_line = tb_height()-1;
+261   // run a little editor just in the last line of the screen
+262   clear_line(bottom_screen_line);
+263   tb_set_cursor(0, bottom_screen_line);
+264   int col = 0;  // screen column of cursor on bottom line. also used to update pattern.
+265   tb_change_cell(col, bottom_screen_line, '/', TB_WHITE, TB_BLACK);
+266   ++col;
+267   tb_set_cursor(col, bottom_screen_line);
+268   tb_present();
+269   string pattern;
+270   while (true) {
+271   ¦ int key = read_key();
+272   ¦ if (key == TB_KEY_ENTER) {
+273   ¦ ¦ if (!pattern.empty())
+274   ¦ ¦ ¦ Current_search_pattern = pattern;
+275   ¦ ¦ return true;
+276   ¦ }
+277   ¦ else if (key == TB_KEY_ESC || key == TB_KEY_CTRL_C) {
+278   ¦ ¦ return false;
+279   ¦ }
+280   ¦ else if (key == TB_KEY_ARROW_LEFT) {
+281   ¦ ¦ if (col > /*slash*/1) {
+282   ¦ ¦ ¦ --col;
+283   ¦ ¦ ¦ tb_set_cursor(col, bottom_screen_line);
+284   ¦ ¦ ¦ tb_present();
+285   ¦ ¦ }
+286   ¦ }
+287   ¦ else if (key == TB_KEY_ARROW_RIGHT) {
+288   ¦ ¦ if (col-/*slash*/1 < SIZE(pattern)) {
+289   ¦ ¦ ¦ ++col;
+290   ¦ ¦ ¦ tb_set_cursor(col, bottom_screen_line);
+291   ¦ ¦ ¦ tb_present();
+292   ¦ ¦ }
+293   ¦ }
+294   ¦ else if (key == TB_KEY_HOME || key == TB_KEY_CTRL_A) {
+295   ¦ ¦ col = /*skip slash*/1;
+296   ¦ ¦ tb_set_cursor(col, bottom_screen_line);
+297   ¦ ¦ tb_present();
+298   ¦ }
+299   ¦ else if (key == TB_KEY_END || key == TB_KEY_CTRL_E) {
+300   ¦ ¦ col = SIZE(pattern)+/*skip slash*/1;
+301   ¦ ¦ tb_set_cursor(col, bottom_screen_line);
+302   ¦ ¦ tb_present();
+303   ¦ }
+304   ¦ else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
+305   ¦ ¦ if (col > /*slash*/1) {
+306   ¦ ¦ ¦ --col;
+307   ¦ ¦ ¦ // update pattern
+308   ¦ ¦ ¦ pattern.erase(col-/*slash*/1, /*len*/1);
+309   ¦ ¦ ¦ // update screen
+310   ¦ ¦ ¦ if (col > SIZE(pattern)) {
+311   ¦ ¦ ¦ ¦ tb_change_cell(col, bottom_screen_line, ' ', TB_WHITE, TB_BLACK);
+312   ¦ ¦ ¦ }
+313   ¦ ¦ ¦ else {
+314   ¦ ¦ ¦ ¦ assert(col <= SIZE(pattern));
+315   ¦ ¦ ¦ ¦ for (int x = col;  x < SIZE(pattern)+/*skip slash*/1;  ++x)
+316   ¦ ¦ ¦ ¦ ¦ tb_change_cell(x, bottom_screen_line, pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK);
+317   ¦ ¦ ¦ ¦ tb_change_cell(SIZE(pattern)+/*skip slash*/1, bottom_screen_line, ' ', TB_WHITE, TB_BLACK);
+318   ¦ ¦ ¦ }
+319   ¦ ¦ ¦ tb_set_cursor(col, bottom_screen_line);
+320   ¦ ¦ ¦ tb_present();
+321   ¦ ¦ }
+322   ¦ }
+323   ¦ else if (key < 128) {  // ascii only
+324   ¦ ¦ // update pattern
+325   ¦ ¦ char c = static_cast<char>(key);
+326   ¦ ¦ pattern.insert(col-/*slash*/1, /*num*/1, c);
+327   ¦ ¦ // update screen
+328   ¦ ¦ for (int x = col;  x < SIZE(pattern)+/*skip slash*/1;  ++x)
+329   ¦ ¦ ¦ tb_change_cell(x, bottom_screen_line, pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK);
+330   ¦ ¦ ++col;
+331   ¦ ¦ tb_set_cursor(col, bottom_screen_line);
+332   ¦ ¦ tb_present();
+333   ¦ }
+334   }
+335 }
+336 
+337 void search_next(const string& pat) {
+338   for (int trace_index = get(Trace_index, Display_row)+1;  trace_index < SIZE(Trace_stream->past_lines);  ++trace_index) {
+339   ¦ if (!contains_key(Visible, trace_index)) continue;
+340   ¦ const trace_line& line = Trace_stream->past_lines.at(trace_index);
+341   ¦ if (line.label.find(pat) == string::npos && line.contents.find(pat) == string::npos) continue;
+342   ¦ Top_of_screen = trace_index;
+343   ¦ Display_row = 0;
+344   ¦ refresh_screen_rows();
+345   ¦ return;
+346   }
+347 }
+348 
+349 void search_previous(const string& pat) {
+350   for (int trace_index = get(Trace_index, Display_row)-1;  trace_index >= 0;  --trace_index) {
+351   ¦ if (!contains_key(Visible, trace_index)) continue;
+352   ¦ const trace_line& line = Trace_stream->past_lines.at(trace_index);
+353   ¦ if (line.label.find(pat) == string::npos && line.contents.find(pat) == string::npos) continue;
+354   ¦ Top_of_screen = trace_index;
+355   ¦ Display_row = 0;
+356   ¦ refresh_screen_rows();
+357   ¦ return;
+358   }
+359 }
+360 
+361 void clear_line(int screen_row) {
+362   for (int col = 0;  col < tb_width();  ++col)
+363   ¦ tb_change_cell(col, screen_row, ' ', TB_WHITE, TB_BLACK);
+364 }
+365 
+366 // update Trace_indices for each screen_row on the basis of Top_of_screen and Visible
+367 void refresh_screen_rows() {
+368   int screen_row = 0, index = 0;
+369   Trace_index.clear();
+370   for (screen_row = 0, index = Top_of_screen;  screen_row < tb_height() && index < SIZE(Trace_stream->past_lines);  ++screen_row, ++index) {
+371   ¦ // skip lines without depth for now
+372   ¦ while (!contains_key(Visible, index)) {
+373   ¦ ¦ ++index;
+374   ¦ ¦ if (index >= SIZE(Trace_stream->past_lines)) goto done;
+375   ¦ }
+376   ¦ assert(index < SIZE(Trace_stream->past_lines));
+377   ¦ put(Trace_index, screen_row, index);
+378   }
+379 done:;
+380 }
+381 
+382 void render() {
+383   int screen_row = 0;
+384   for (screen_row = 0;  screen_row < tb_height();  ++screen_row) {
+385   ¦ if (!contains_key(Trace_index, screen_row)) break;
+386   ¦ trace_line& curr_line = Trace_stream->past_lines.at(get(Trace_index, screen_row));
+387   ¦ ostringstream out;
+388   ¦ out << std::setw(4) << curr_line.depth << ' ' << curr_line.label << ": " << curr_line.contents;
+389   ¦ if (screen_row < tb_height()-1) {
+390   ¦ ¦ int delta = lines_hidden(screen_row);
+391   ¦ ¦ // home-brew escape sequence for red
+392   ¦ ¦ if (delta > 999) out << static_cast<char>(1);
+393   ¦ ¦ out << " (" << delta << ")";
+394   ¦ ¦ if (delta > 999) out << static_cast<char>(2);
+395   ¦ }
+396   ¦ render_line(screen_row, out.str(), screen_row == Display_row);
+397   }
+398   // clear rest of screen
+399   Last_printed_row = screen_row-1;
+400   for (;  screen_row < tb_height();  ++screen_row)
+401   ¦ render_line(screen_row, "~", /*cursor_line?*/false);
+402   // move cursor back to display row at the end
+403   tb_set_cursor(0, Display_row);
+404   tb_present();
+405 }
+406 
+407 int lines_hidden(int screen_row) {
+408   assert(contains_key(Trace_index, screen_row));
+409   if (!contains_key(Trace_index, screen_row+1))
+410   ¦ return SIZE(Trace_stream->past_lines) - get(Trace_index, screen_row);
+411   else
+412   ¦ return get(Trace_index, screen_row+1) - get(Trace_index, screen_row);
+413 }
+414 
+415 void render_line(int screen_row, const string& s, bool cursor_line) {
+416   int col = 0;
+417   int color = TB_WHITE;
+418   int background_color = cursor_line ? /*subtle grey*/240 : TB_BLACK;
+419   vector<pair<size_t, size_t> > highlight_ranges = find_all_occurrences(s, Current_search_pattern);
+420   for (col = 0;  col < tb_width() && col+Left_of_screen < SIZE(s);  ++col) {
+421   ¦ char c = s.at(col+Left_of_screen);  // todo: unicode
+422   ¦ if (c == '\n') c = ';';  // replace newlines with semi-colons
+423   ¦ // escapes. hack: can't start a line with them.
+424   ¦ if (c == '\1') { color = /*red*/1;  c = ' '; }
+425   ¦ if (c == '\2') { color = TB_WHITE;  c = ' '; }
+426   ¦ if (in_range(highlight_ranges, col+Left_of_screen))
+427   ¦ ¦ tb_change_cell(col, screen_row, c, TB_BLACK, /*yellow*/11);
+428   ¦ else
+429   ¦ ¦ tb_change_cell(col, screen_row, c, color, background_color);
+430   }
+431   for (;  col < tb_width();  ++col)
+432   ¦ tb_change_cell(col, screen_row, ' ', TB_WHITE, background_color);
+433 }
+434 
+435 vector<pair<size_t, size_t> > find_all_occurrences(const string& s, const string& pat) {
+436   vector<pair<size_t, size_t> > result;
+437   if (pat.empty()) return result;
+438   size_t idx = 0;
+439   while (true) {
+440   ¦ size_t next_idx = s.find(pat, idx);
+441   ¦ if (next_idx == string::npos) break;
+442   ¦ result.push_back(pair<size_t, size_t>(next_idx, next_idx+SIZE(pat)));
+443   ¦ idx = next_idx+SIZE(pat);
+444   }
+445   return result;
+446 }
+447 
+448 bool in_range(const vector<pair<size_t, size_t> >& highlight_ranges, size_t idx) {
+449   for (int i = 0;  i < SIZE(highlight_ranges);  ++i) {
+450   ¦ if (idx >= highlight_ranges.at(i).first && idx < highlight_ranges.at(i).second)
+451   ¦ ¦ return true;
+452   ¦ if (idx < highlight_ranges.at(i).second) break;
+453   }
+454   return false;
+455 }
+456 
+457 void load_trace(const char* filename) {
+458   ifstream tin(filename);
+459   if (!tin) {
+460   ¦ cerr << "no such file: " << filename << '\n';
+461   ¦ exit(1);
+462   }
+463   Trace_stream = new trace_stream;
+464   while (has_data(tin)) {
+465   ¦ tin >> std::noskipws;
+466   ¦ ¦ skip_whitespace_but_not_newline(tin);
+467   ¦ ¦ if (!isdigit(tin.peek())) {
+468   ¦ ¦ ¦ string dummy;
+469   ¦ ¦ ¦ getline(tin, dummy);
+470   ¦ ¦ ¦ continue;
+471   ¦ ¦ }
+472   ¦ tin >> std::skipws;
+473   ¦ int depth;
+474   ¦ tin >> depth;
+475   ¦ string label;
+476   ¦ tin >> label;
+477   ¦ if (*--label.end() == ':') label.erase(--label.end());
+478   ¦ string line;
+479   ¦ getline(tin, line);
+480   ¦ Trace_stream->past_lines.push_back(trace_line(depth, label, line));
+481   }
+482   cerr << "lines read: " << Trace_stream->past_lines.size() << '\n';
+483 }
+484 
+485 int read_key() {
+486   tb_event event;
+487   do {
+488   ¦ tb_poll_event(&event);
+489   } while (event.type != TB_EVENT_KEY);
+490   return event.key ? event.key : event.ch;
+491 }
 
-- cgit 1.4.1-2-gfad0