about summary refs log tree commit diff stats
path: root/077trace_browser.cc
blob: 133ad1bc2e4bf728eabd3ae63b94fcb83fb3d35d (plain) (blame)
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
:(before "End Primitive Recipe Declarations")
_BROWSE_TRACE,
:(before "End Primitive Recipe Numbers")
Recipe_number["$browse-trace"] = _BROWSE_TRACE;
:(before "End Primitive Recipe Implementations")
case _BROWSE_TRACE: {
  start_trace_browser();
  break;
}

:(before "End Globals")
set<long long int> Visible;
long long int Top_of_screen = 0;
long long int Last_printed_row = 0;
map<int, long long int> Trace_index;  // screen row -> trace index

:(code)
void start_trace_browser() {
  if (!Trace_stream) return;
  cerr << "computing depth to display\n";
  long long int min_depth = 9999;
  for (long long int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
    trace_line& curr_line = Trace_stream->past_lines.at(i);
    if (curr_line.depth == 0) continue;
    if (curr_line.depth < min_depth) min_depth = curr_line.depth;
  }
  cerr << "depth is " << min_depth << '\n';
  cerr << "computing lines to display\n";
  for (long long int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
    if (Trace_stream->past_lines.at(i).depth < 110 && Trace_stream->past_lines.at(i).depth) //== min_depth)
      Visible.insert(i);
  }
  tb_init();
  Display_row = Display_column = 0;
  struct tb_event event;
  Top_of_screen = 0;
  refresh_screen_rows();
  while (true) {
    render();
    do {
      tb_poll_event(&event);
    } while (event.type != TB_EVENT_KEY);
    long long int key = event.key ? event.key : event.ch;
    if (key == 'q' || key == 'Q') break;
    if (key == 'j') {
      if (Display_row < Last_printed_row) ++Display_row;
    }
    if (key == 'k') {
      if (Display_row > 0) --Display_row;
    }
    if (key == 'H') {
      Display_row = 0;
    }
    if (key == 'M') {
      Display_row = tb_height()/2;
    }
    if (key == 'L') {
      Display_row = tb_height()-1;
    }
    if (key == 'J') {
      if (Trace_index.find(tb_height()-1) != Trace_index.end()) {
        Top_of_screen = Trace_index[tb_height()-1]+1;
        refresh_screen_rows();
      }
    }
    if (key == 'K') {
      int max = tb_height();
//?       tb_shutdown(); //? 1
//?       cerr << "page-up: Top_of_screen is currently " << Top_of_screen << '\n'; //? 1
      // page-up is more convoluted
      for (int screen_row = max; screen_row > 0 && Top_of_screen > 0; --screen_row) {
        --Top_of_screen;
        if (Top_of_screen <= 0) break;
        while (Top_of_screen > 0 && Visible.find(Top_of_screen) == Visible.end())
          --Top_of_screen;
//?         cerr << "now " << Top_of_screen << '\n'; //? 1
      }
//?       exit(0); //? 1
      if (Top_of_screen > 0)
        refresh_screen_rows();
    }
  }
  tb_shutdown();
}

// update Trace_indices for each screen_row on the basis of Top_of_screen and Visible
void refresh_screen_rows() {
  long long 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 (Visible.find(index) == Visible.end()) {
      ++index;
      if (index >= SIZE(Trace_stream->past_lines)) goto done;
    }
    assert(index < SIZE(Trace_stream->past_lines));
    Trace_index[screen_row] = index;
  }
done:;
}

void render() {
  long long int screen_row = 0;
  for (screen_row = 0; screen_row < tb_height(); ++screen_row) {
    if (Trace_index.find(screen_row) == Trace_index.end()) break;
    trace_line& curr_line = Trace_stream->past_lines.at(Trace_index[screen_row]);
    ostringstream out;
    out << std::setw(4) << curr_line.depth << ' ' << curr_line.label << ": " << curr_line.contents;
    render_line(screen_row, out.str());
  }
  // clear rest of screen
  Last_printed_row = screen_row-1;
  for (; screen_row < tb_height(); ++screen_row) {
    render_line(screen_row, "~");
  }
  // move cursor back to display row at the end
  tb_set_cursor(0, Display_row);
  tb_present();
}

void render_line(int screen_row, const string& s) {
  long long int col = 0;
  for (col = 0; col < tb_width() && col < SIZE(s); ++col) {
    char c = s.at(col);
    if (c == '\n') c = ';';  // replace newlines with semi-colons
    tb_change_cell(col, screen_row, c, TB_WHITE, TB_DEFAULT);
  }
  for (; col < tb_width(); ++col) {
    tb_change_cell(col, screen_row, ' ', TB_WHITE, TB_DEFAULT);
  }
}