about summary refs log tree commit diff stats
path: root/html/apps
diff options
context:
space:
mode:
Diffstat (limited to 'html/apps')
0 files changed, 0 insertions, 0 deletions
t;vc@akkartik.com> 2017-03-06 09:06:58 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2017-03-06 09:06:58 -0800 3757 - more permissive trace browser UI' href='/akkartik/mu/commit/100trace_browser.cc?h=main&id=1d2d6cf5544ce260203d6ad2c52e7b41b9471e42'>1d2d6cf5 ^
215cf3c2 ^
1d2d6cf5 ^
215cf3c2 ^

1d2d6cf5 ^
215cf3c2 ^

06f207f3 ^



0668b1b2 ^

ab53600b ^

215cf3c2 ^
1d2d6cf5 ^

215cf3c2 ^
caf17905 ^


b0ef6d58 ^
9aa2fd34 ^
cd723793 ^



4b487ef5 ^


a819afb0 ^

4b487ef5 ^
a819afb0 ^




890f9b61 ^

8f249677 ^
4243d710 ^


795f5244 ^
166e3c0d ^



4243d710 ^





8f249677 ^







4243d710 ^
b24eb476 ^

ab53600b ^
b24eb476 ^

9aa2fd34 ^
cd723793 ^



4243d710 ^



bf458036 ^
b24eb476 ^
4886e4e9 ^
3d8e5101 ^
3d8e5101 ^

bf458036 ^
3d8e5101 ^
4886e4e9 ^
293dbd7e ^
3d8e5101 ^

4243d710 ^
537ad74c ^
4243d710 ^
341239c9 ^

4243d710 ^

29f1b21e ^
1d2d6cf5 ^
b1ce29dd ^
1725a858 ^
22d1a275 ^

ab146dd2 ^
1725a858 ^
22d1a275 ^

ab146dd2 ^
1725a858 ^
9463d417 ^

ab146dd2 ^
1725a858 ^
9463d417 ^
053ffdc6 ^

9463d417 ^
ab146dd2 ^
1725a858 ^
9463d417 ^
053ffdc6 ^

9463d417 ^
ab146dd2 ^

b0ef6d58 ^



ab146dd2 ^
b734ab6b ^

0668b1b2 ^
ab146dd2 ^
b734ab6b ^
0668b1b2 ^

ab146dd2 ^
8bec2907 ^
ab53600b ^


ab146dd2 ^
8bec2907 ^
ab53600b ^

ab146dd2 ^
1725a858 ^
9463d417 ^
f22250a1 ^
9463d417 ^


ab146dd2 ^
1725a858 ^
4886e4e9 ^
9463d417 ^

f3760b0f ^
9463d417 ^
9463d417 ^
4d03f69c ^
9463d417 ^

ab146dd2 ^
1d2d6cf5 ^




ab146dd2 ^
63030ea7 ^

4886e4e9 ^
63030ea7 ^

f3760b0f ^
63030ea7 ^






ab146dd2 ^
1725a858 ^
f3760b0f ^
b24eb476 ^

293dbd7e ^

4886e4e9 ^
f3760b0f ^
293dbd7e ^
293dbd7e ^


b24eb476 ^
293dbd7e ^
4886e4e9 ^
293dbd7e ^

293dbd7e ^


293dbd7e ^

ab146dd2 ^
1725a858 ^
f3760b0f ^
b24eb476 ^

ff1f5976 ^

4886e4e9 ^
f3760b0f ^
12f93e4a ^
ff1f5976 ^
12f93e4a ^
b24eb476 ^
12f93e4a ^
4886e4e9 ^
ff1f5976 ^
12f93e4a ^


9aa2fd34 ^
cd723793 ^





9aa2fd34 ^

4b487ef5 ^
cd723793 ^
9aa2fd34 ^

4b487ef5 ^
cd723793 ^
9aa2fd34 ^
4243d710 ^



cd723793 ^
9aa2fd34 ^


fcffe1d7 ^
9aa2fd34 ^
537ad74c ^

9aa2fd34 ^



cd723793 ^
4b487ef5 ^
cd723793 ^

4b487ef5 ^




fcffe1d7 ^



fcffe1d7 ^





fcffe1d7 ^




fcffe1d7 ^



fcffe1d7 ^
4b487ef5 ^
fcffe1d7 ^
537ad74c ^
4b487ef5 ^
1485ee1b ^


537ad74c ^



4b487ef5 ^
4b487ef5 ^
9aa2fd34 ^
890f9b61 ^


537ad74c ^
890f9b61 ^
537ad74c ^
890f9b61 ^
890f9b61 ^



537ad74c ^

890f9b61 ^
537ad74c ^
890f9b61 ^
537ad74c ^

890f9b61 ^
4b487ef5 ^
fcffe1d7 ^
9aa2fd34 ^
537ad74c ^

fcffe1d7 ^


537ad74c ^
9aa2fd34 ^

9aa2fd34 ^



cd723793 ^









9aa2fd34 ^
























537ad74c ^
9aa2fd34 ^
537ad74c ^

9aa2fd34 ^

341239c9 ^

b24eb476 ^
341239c9 ^
4886e4e9 ^
4243d710 ^
f3760b0f ^
4243d710 ^



f22250a1 ^
341239c9 ^




b24eb476 ^
4886e4e9 ^
f3760b0f ^
f22250a1 ^
4243d710 ^

1164aefc ^
b24eb476 ^
1164aefc ^
9be4a67f ^




1164aefc ^
8ccf992d ^
4243d710 ^
4243d710 ^
22d1a275 ^
c01f82b0 ^
77f88e8c ^
4243d710 ^

4243d710 ^

b24eb476 ^
f3760b0f ^

f22250a1 ^
1164aefc ^
f22250a1 ^
1164aefc ^

77f88e8c ^
b24eb476 ^
1164aefc ^
77f88e8c ^

537ad74c ^
4886e4e9 ^
ab53600b ^
4243d710 ^
1164aefc ^
4886e4e9 ^

77f88e8c ^
537ad74c ^
77f88e8c ^
537ad74c ^
4243d710 ^
4886e4e9 ^
537ad74c ^
77f88e8c ^





















4243d710 ^
8f249677 ^








bf458036 ^







8f249677 ^








bf458036 ^
8f249677 ^
29f1b21e ^







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
                                                            
                                                



















                                                                            
                            
   
                                                                            

                                                                              
                                                                             

                                                     



                                                                  

                                                              

                                                
   

                                                   
   


                                                
                                                  
   



                                                                    


                                                                            

                                                     
                                                                             




                                                                   

                                                          
 


                                             
                                                    



                                       





                                                







                                                     
                       

                      
                       

                                                        
                                   



                                                    



                            
                                             
                       
                                                              
                                                           

                                                                 
                                               
                                         
                                                              
                                                          

                        
            
             
                                   

                        

                
                         
                                                                
                                                 
                                  

                                                        
                                                    
                                

                                         
                          
                                     

                      
                          
                                        
                                  

                                                     
     
                          
                                        
                                  

                                                     
     

                                               



                                                    
                                                      

                                               
     
                                                       
                                       

                       
                          
                                         


                                                 
                          
                                          

                                         
                                                                        
                  
                                                                 
                                                            


                              
                                                                        
                                   
                                                                                               

                                      
                                                                          
                          
       
                             

                              
                                                




                              
                                               

                                                                    
                                                                                               

                                      
                                                                          






                                     
                                             
                                                
                                                     

                                                      

                                                       
                                                                                      
                                                
                                                                   


                                                                                 
                            
                                                             
                                                               

                                                                   


                                

                            
                                                                   
                                         
                                                     

                                                      

                                                                                
                                                                                      
                                                    
                                                                   
                                                    
       
                            
                                                             
                                                                 
                             


                            
                          





                                                                 

                          
                                          
                                                                 

                          
                                          
                                                                           
     



                
                                                


                                                            
                                                                                       
                                         

                                    



                              
                             
                                         

                                       




                                                         



                                               





                                               




                                                          



                                                         
     
                                                                   
                             
                                       
              


                                                



                                                                   
                                               
       
     


                                                                      
                                             
                                                               
                                          
                                             



                                           

                                             
                                                                        
                                                               
                                                                                         

                                             
     
                                        
                       
                                      

                                     


                                                                 
                                                               

                                             



     









                                                      
























                                                                                                                          
                               
                                              

                                      

 

                                                                                     
                                
                      
                                                                                                                                            
                                       
                                           



                                                             
                                        




               
                     
                                                                  
                                                      
                                                                                      

                                                                                                   
                                     
                                           
                                          




                                                     
     
                                                                  
   
                         
                                  
                                                  
                                                        

                                               

 
                                  

                                                
                                                                         
      
                                                                         

 
                                                                     
              
                       

                                                                                                   
                               
                                                                            
                                                        
                                                                 
                                                   

                                                  
                                                       
                                          
        
                                           
   
                                   
                                              





















                                                                                        
 








                                                 







                                           








                                                                       
                                                                    
 







                                          
//: A debugging helper that lets you zoom in/out on a trace.
//: Warning: this tool has zero automated tests.
//:
//: To try it out, first create an example trace:
//:   mu --trace nqueens.mu
//: Then to browse the trace, which was stored in a file called 'last_run':
//:   mu browse-trace last_run
//:
//: You should now find yourself in a UI showing a subsequence of lines from
//: the trace, each line starting with a numeric depth, and ending with a
//: parenthetical count of trace lines hidden after it with greater depths.
//:
//: For example, this line:
//:   2 app: line1 (30)
//: indicates that it was logged with depth 2, and that 30 following lines
//: have been hidden at a depth greater than 2.
//:
//: As an experiment, hidden counts of 1000 or more are in red to highlight
//: where you might be particularly interested in expanding.
//:
//: The UI provides the following hotkeys:
//:
//:   `q` or `ctrl-c`: Quit.
//:
//:   `Enter`: 'Zoom into' this line. Expand some or all of the hidden lines
//:   at the next higher level, updating parenthetical counts of hidden lines.
//:
//:   `Backspace`: 'Zoom out' on a line after zooming in, collapsing expanded
//:   lines below by some series of <Enter> commands.
//:
//:   `j` or `down-arrow`: Move/scroll cursor down one line.
//:   `k` or `up-arrow`: Move/scroll cursor up one line.
//:   `J` or `ctrl-f` or `page-down`: Scroll cursor down one page.
//:   `K` or `ctrl-b` or `page-up`: Scroll cursor up one page.
//:   `h` or `left-arrow`: Scroll cursor left one character.
//:   `l` or `right-arrow`: Scroll cursor right one character.
//:   `H`: Scroll cursor left one screen-width.
//:   `L`: Scroll cursor right one screen-width.
//:
//:   `g` or `home`: Move cursor to start of trace.
//:   `G` or `end`: Move cursor to end of trace.
//:
//:   `t`: Move cursor to top line on screen.
//:   `c`: Move cursor to center line on screen.
//:   `b`: Move cursor to bottom line on screen.
//:   `T`: Scroll line at cursor to top of screen.
//:
//:   `/`: Search forward for a pattern.
//:   '?': Search backward for a pattern.
//:   `n`: Repeat the previous '/' or '?'.
//:   `N`: Repeat the previous '/' or '?' in the opposite direction.
//:
//:   After hitting `/`, a small editor on the bottom-most line supports the
//:   following hotkeys:
//:     ascii characters: add the key to the pattern.
//:     `Enter`: search for the pattern.
//:     `Esc` or `ctrl-c`: cancel the current search, setting the screen back
//:       to its state before the search.
//:     `left-arrow`: move cursor left.
//:     `right-arrow`: move cursor right.
//:     `ctrl-a` or `home`: move cursor to start of search pattern.
//:     `ctrl-e` or `end`: move cursor to end of search pattern.
//:     `ctrl-u`: clear search pattern before cursor
//:     `ctrl-k`: clear search pattern at and after cursor

:(before "End Primitive Recipe Declarations")
_BROWSE_TRACE,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "$browse-trace", _BROWSE_TRACE);
:(before "End Primitive Recipe Checks")
case _BROWSE_TRACE: {
  break;
}
:(before "End Primitive Recipe Implementations")
case _BROWSE_TRACE: {
  start_trace_browser();
  break;
}

//: browse a trace loaded from a file
:(after "Commandline Parsing")
if (argc == 3 && is_equal(argv[1], "browse-trace")) {
  load_trace(argv[2]);
  start_trace_browser();
  return 0;
}

:(before "End Globals")
set<int> Visible;
int Top_of_screen = 0;
int Left_of_screen = 0;
int Last_printed_row = 0;
map<int, int> Trace_index;  // screen row -> trace index
string Current_search_pattern = "";
:(before "End Types")
enum search_direction { FORWARD, BACKWARD };
:(before "End Globals")
search_direction Current_search_direction = FORWARD;

:(code)
void start_trace_browser() {
  if (!Trace_stream) return;
  cerr << "computing min depth to display\n";
  int min_depth = 9999;
  for (int i = 0;  i < SIZE(Trace_stream->past_lines);  ++i) {
    trace_line& curr_line = Trace_stream->past_lines.at(i);
    if (curr_line.depth < min_depth) min_depth = curr_line.depth;
  }
  cerr << "min depth is " << min_depth << '\n';
  cerr << "computing lines to display\n";
  for (int i = 0;  i < SIZE(Trace_stream->past_lines);  ++i) {
    if (Trace_stream->past_lines.at(i).depth == min_depth)
      Visible.insert(i);
  }
  tb_init();
  tb_clear();
  Display_row = Display_column = 0;
  Top_of_screen = 0;
  refresh_screen_rows();
  while (true) {
    render();
    int key = read_key();
    if (key == 'q' || key == 'Q' || key == TB_KEY_CTRL_C) break;
    if (key == 'j' || key == TB_KEY_ARROW_DOWN) {
      // move cursor one line down
      if (Display_row < Last_printed_row) ++Display_row;
    }
    else if (key == 'k' || key == TB_KEY_ARROW_UP) {
      // move cursor one line up
      if (Display_row > 0) --Display_row;
    }
    else if (key == 't') {
      // move cursor to top of screen
      Display_row = 0;
    }
    else if (key == 'c') {
      // move cursor to center of screen
      Display_row = tb_height()/2;
      while (!contains_key(Trace_index, Display_row))
        --Display_row;
    }
    else if (key == 'b') {
      // move cursor to bottom of screen
      Display_row = tb_height()-1;
      while (!contains_key(Trace_index, Display_row))
        --Display_row;
    }
    else if (key == 'T') {
      // scroll line at cursor to top of screen
      Top_of_screen = get(Trace_index, Display_row);
      Display_row = 0;
      refresh_screen_rows();
    }
    else if (key == 'h' || key == TB_KEY_ARROW_LEFT) {
      // pan screen one character left
      if (Left_of_screen > 0) --Left_of_screen;
    }
    else if (key == 'l' || key == TB_KEY_ARROW_RIGHT) {
      // pan screen one character right
      ++Left_of_screen;
    }
    else if (key == 'H') {
      // pan screen one screen-width left
      Left_of_screen -= (tb_width() - 5);
      if (Left_of_screen < 0) Left_of_screen = 0;
    }
    else if (key == 'L') {
      // pan screen one screen-width right
      Left_of_screen += (tb_width() - 5);
    }
    else if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) {
      // page-down
      if (Trace_index.find(tb_height()-1) != Trace_index.end()) {
        Top_of_screen = get(Trace_index, tb_height()-1) + 1;
        refresh_screen_rows();
      }
    }
    else if (key == 'K' || key == TB_KEY_PGUP || key == TB_KEY_CTRL_B) {
      // page-up is more convoluted
      for (int screen_row = tb_height();  screen_row > 0 && Top_of_screen > 0;  --screen_row) {
        --Top_of_screen;
        if (Top_of_screen <= 0) break;
        while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
          --Top_of_screen;
      }
      if (Top_of_screen >= 0)
        refresh_screen_rows();
    }
    else if (key == 'g' || key == TB_KEY_HOME) {
        Top_of_screen = 0;
        Last_printed_row = 0;
        Display_row = 0;
        refresh_screen_rows();
    }
    else if (key == 'G' || key == TB_KEY_END) {
      // go to bottom of screen; largely like page-up, interestingly
      Top_of_screen = SIZE(Trace_stream->past_lines)-1;
      for (int screen_row = tb_height();  screen_row > 0 && Top_of_screen > 0;  --screen_row) {
        --Top_of_screen;
        if (Top_of_screen <= 0) break;
        while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
          --Top_of_screen;
      }
      refresh_screen_rows();
      // move cursor to bottom
      Display_row = Last_printed_row;
      refresh_screen_rows();
    }
    else if (key == TB_KEY_CARRIAGE_RETURN) {
      // expand lines under current by one level
      assert(contains_key(Trace_index, Display_row));
      int start_index = get(Trace_index, Display_row);
      int index = 0;
      // simultaneously compute end_index and min_depth
      int min_depth = 9999;
      for (index = start_index+1;  index < SIZE(Trace_stream->past_lines);  ++index) {
        if (contains_key(Visible, index)) break;
        trace_line& curr_line = Trace_stream->past_lines.at(index);
        assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth);
        if (curr_line.depth < min_depth) min_depth = curr_line.depth;
      }
      int end_index = index;
      // mark as visible all intervening indices at min_depth
      for (index = start_index;  index < end_index;  ++index) {
        trace_line& curr_line = Trace_stream->past_lines.at(index);
        if (curr_line.depth == min_depth) {
          Visible.insert(index);
        }
      }
      refresh_screen_rows();
    }
    else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
      // collapse all lines under current
      assert(contains_key(Trace_index, Display_row));
      int start_index = get(Trace_index, Display_row);
      int index = 0;
      // end_index is the next line at a depth same as or lower than start_index
      int initial_depth = Trace_stream->past_lines.at(start_index).depth;
      for (index = start_index+1;  index < SIZE(Trace_stream->past_lines);  ++index) {
        if (!contains_key(Visible, index)) continue;
        trace_line& curr_line = Trace_stream->past_lines.at(index);
        if (curr_line.depth <= initial_depth) break;
      }
      int end_index = index;
      // mark as visible all intervening indices at min_depth
      for (index = start_index+1;  index < end_index;  ++index) {
        Visible.erase(index);
      }
      refresh_screen_rows();
    }
    else if (key == '/') {
      if (start_search_editor(FORWARD))
        search(Current_search_pattern, Current_search_direction);
    }
    else if (key == '?') {
      if (start_search_editor(BACKWARD))
        search(Current_search_pattern, Current_search_direction);
    }
    else if (key == 'n') {
      if (!Current_search_pattern.empty())
        search(Current_search_pattern, Current_search_direction);
    }
    else if (key == 'N') {
      if (!Current_search_pattern.empty())
        search(Current_search_pattern, opposite(Current_search_direction));
    }
  }
  tb_shutdown();
}

bool start_search_editor(search_direction dir) {
  const int bottom_screen_line = tb_height()-1;
  // run a little editor just in the last line of the screen
  clear_line(bottom_screen_line);
  int col = 0;  // screen column of cursor on bottom line. also used to update pattern.
  tb_set_cursor(col, bottom_screen_line);
  tb_print('/', TB_WHITE, TB_BLACK);
  ++col;
  string pattern;
  while (true) {
    int key = read_key();
    if (key == TB_KEY_ENTER) {
      if (!pattern.empty()) {
        Current_search_pattern = pattern;
        Current_search_direction = dir;
      }
      return true;
    }
    else if (key == TB_KEY_ESC || key == TB_KEY_CTRL_C) {
      return false;
    }
    else if (key == TB_KEY_ARROW_LEFT) {
      if (col > /*slash*/1) {
        --col;
        tb_set_cursor(col, bottom_screen_line);
      }
    }
    else if (key == TB_KEY_ARROW_RIGHT) {
      if (col-/*slash*/1 < SIZE(pattern)) {
        ++col;
        tb_set_cursor(col, bottom_screen_line);
      }
    }
    else if (key == TB_KEY_HOME || key == TB_KEY_CTRL_A) {
      col = /*skip slash*/1;
      tb_set_cursor(col, bottom_screen_line);
    }
    else if (key == TB_KEY_END || key == TB_KEY_CTRL_E) {
      col = SIZE(pattern)+/*skip slash*/1;
      tb_set_cursor(col, bottom_screen_line);
    }
    else if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
      if (col > /*slash*/1) {
        assert(col <= SIZE(pattern)+1);
        --col;
        // update pattern
        pattern.erase(col-/*slash*/1, /*len*/1);
        // update screen
        tb_set_cursor(col, bottom_screen_line);
        for (int x = col;  x < SIZE(pattern)+/*skip slash*/1;  ++x)
          tb_print(pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK);
        tb_print(' ', TB_WHITE, TB_BLACK);
        tb_set_cursor(col, bottom_screen_line);
      }
    }
    else if (key == TB_KEY_CTRL_K) {
      int old_pattern_size = SIZE(pattern);
      pattern.erase(col-/*slash*/1, SIZE(pattern) - (col-/*slash*/1));
      tb_set_cursor(col, bottom_screen_line);
      for (int x = col;  x < old_pattern_size+/*slash*/1;  ++x)
        tb_print(' ', TB_WHITE, TB_BLACK);
      tb_set_cursor(col, bottom_screen_line);
    }
    else if (key == TB_KEY_CTRL_U) {
      int old_pattern_size = SIZE(pattern);
      pattern.erase(0, col-/*slash*/1);
      col = /*skip slash*/1;
      tb_set_cursor(col, bottom_screen_line);
      for (int x = /*slash*/1;  x < SIZE(pattern)+/*skip slash*/1;  ++x)
        tb_print(pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK);
      for (int x = SIZE(pattern)+/*slash*/1;  x < old_pattern_size+/*skip slash*/1;  ++x)
        tb_print(' ', TB_WHITE, TB_BLACK);
      tb_set_cursor(col, bottom_screen_line);
    }
    else if (key < 128) {  // ascii only
      // update pattern
      char c = static_cast<char>(key);
      assert(col-1 >= 0);
      assert(col-1 <= SIZE(pattern));
      pattern.insert(col-/*slash*/1, /*num*/1, c);
      // update screen
      for (int x = col;  x < SIZE(pattern)+/*skip slash*/1;  ++x)
        tb_print(pattern.at(x-/*slash*/1), TB_WHITE, TB_BLACK);
      ++col;
      tb_set_cursor(col, bottom_screen_line);
    }
  }
}

void search(const string& pat, search_direction dir) {
  if (dir == FORWARD) search_next(pat);
  else search_previous(pat);
}

search_direction opposite(search_direction dir) {
  if (dir == FORWARD) return BACKWARD;
  else return FORWARD;
}

void search_next(const string& pat) {
  for (int trace_index = get(Trace_index, Display_row)+1;  trace_index < SIZE(Trace_stream->past_lines);  ++trace_index) {
    if (!contains_key(Visible, trace_index)) continue;
    const trace_line& line = Trace_stream->past_lines.at(trace_index);
    if (line.label.find(pat) == string::npos && line.contents.find(pat) == string::npos) continue;
    Top_of_screen = trace_index;
    Display_row = 0;
    refresh_screen_rows();
    return;
  }
}

void search_previous(const string& pat) {
  for (int trace_index = get(Trace_index, Display_row)-1;  trace_index >= 0;  --trace_index) {
    if (!contains_key(Visible, trace_index)) continue;
    const trace_line& line = Trace_stream->past_lines.at(trace_index);
    if (line.label.find(pat) == string::npos && line.contents.find(pat) == string::npos) continue;
    Top_of_screen = trace_index;
    Display_row = 0;
    refresh_screen_rows();
    return;
  }
}

void clear_line(int screen_row) {
  tb_set_cursor(0, screen_row);
  for (int col = 0;  col < tb_width();  ++col)
    tb_print(' ', TB_WHITE, TB_BLACK);
  tb_set_cursor(0, screen_row);
}

// update Trace_indices for each screen_row on the basis of Top_of_screen and Visible
void refresh_screen_rows() {
  int screen_row = 0, index = 0;
  Trace_index.clear();
  for (screen_row = 0, index = Top_of_screen;  screen_row < tb_height() && index < SIZE(Trace_stream->past_lines);  ++screen_row, ++index) {
    // skip lines without depth for now
    while (!contains_key(Visible, index)) {
      ++index;
      if (index >= SIZE(Trace_stream->past_lines)) goto done;
    }
    assert(index < SIZE(Trace_stream->past_lines));
    put(Trace_index, screen_row, index);
  }
done:;
}

void render() {
  int screen_row = 0;
  for (screen_row = 0;  screen_row < tb_height();  ++screen_row) {
    if (!contains_key(Trace_index, screen_row)) break;
    trace_line& curr_line = Trace_stream->past_lines.at(get(Trace_index, screen_row));
    ostringstream out;
    out << std::setw(4) << curr_line.depth << ' ' << curr_line.label << ": " << curr_line.contents;
    if (screen_row < tb_height()-1) {
      int delta = lines_hidden(screen_row);
      // home-brew escape sequence for red
      if (delta > 1) {
        if (delta > 999) out << static_cast<char>(1);
        out << " (" << delta << ")";
        if (delta > 999) out << static_cast<char>(2);
      }
    }
    render_line(screen_row, out.str(), screen_row == Display_row);
  }
  // clear rest of screen
  Last_printed_row = screen_row-1;
  for (;  screen_row < tb_height();  ++screen_row)
    render_line(screen_row, "~", /*cursor_line?*/false);
  // move cursor back to display row at the end
  tb_set_cursor(0, Display_row);
}

int lines_hidden(int screen_row) {
  assert(contains_key(Trace_index, screen_row));
  if (!contains_key(Trace_index, screen_row+1))
    return SIZE(Trace_stream->past_lines) - get(Trace_index, screen_row);
  else
    return get(Trace_index, screen_row+1) - get(Trace_index, screen_row);
}

void render_line(int screen_row, const string& s, bool cursor_line) {
  int col = 0;
  int color = TB_WHITE;
  int background_color = cursor_line ? /*subtle grey*/240 : TB_BLACK;
  vector<pair<size_t, size_t> > highlight_ranges = find_all_occurrences(s, Current_search_pattern);
  tb_set_cursor(0, screen_row);
  for (col = 0;  col < tb_width() && col+Left_of_screen < SIZE(s);  ++col) {
    char c = s.at(col+Left_of_screen);  // todo: unicode
    if (c == '\n') c = ';';  // replace newlines with semi-colons
    // escapes. hack: can't start a line with them.
    if (c == '\1') { color = /*red*/1;  c = ' '; }
    if (c == '\2') { color = TB_WHITE;  c = ' '; }
    if (in_range(highlight_ranges, col+Left_of_screen))
      tb_print(c, TB_BLACK, /*yellow*/11);
    else
      tb_print(c, color, background_color);
  }
  for (;  col < tb_width();  ++col)
    tb_print(' ', TB_WHITE, background_color);
}

vector<pair<size_t, size_t> > find_all_occurrences(const string& s, const string& pat) {
  vector<pair<size_t, size_t> > result;
  if (pat.empty()) return result;
  size_t idx = 0;
  while (true) {
    size_t next_idx = s.find(pat, idx);
    if (next_idx == string::npos) break;
    result.push_back(pair<size_t, size_t>(next_idx, next_idx+SIZE(pat)));
    idx = next_idx+SIZE(pat);
  }
  return result;
}

bool in_range(const vector<pair<size_t, size_t> >& highlight_ranges, size_t idx) {
  for (int i = 0;  i < SIZE(highlight_ranges);  ++i) {
    if (idx >= highlight_ranges.at(i).first && idx < highlight_ranges.at(i).second)
      return true;
    if (idx < highlight_ranges.at(i).second) break;
  }
  return false;
}

void load_trace(const char* filename) {
  ifstream tin(filename);
  if (!tin) {
    cerr << "no such file: " << filename << '\n';
    exit(1);
  }
  Trace_stream = new trace_stream;
  while (has_data(tin)) {
    tin >> std::noskipws;
      skip_whitespace_but_not_newline(tin);
      if (!isdigit(tin.peek())) {
        string dummy;
        getline(tin, dummy);
        continue;
      }
    tin >> std::skipws;
    int depth;
    tin >> depth;
    string label;
    tin >> label;
    if (*--label.end() == ':') label.erase(--label.end());
    string line;
    getline(tin, line);
    Trace_stream->past_lines.push_back(trace_line(depth, label, line));
  }
  cerr << "lines read: " << Trace_stream->past_lines.size() << '\n';
}

int read_key() {
  tb_event event;
  do {
    tb_poll_event(&event);
  } while (event.type != TB_EVENT_KEY);
  return event.key ? event.key : event.ch;
}