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 :(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
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;
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 ¦ ¦
105 ¦ ¦ if (Display_row < Last_printed_row) ++Display_row;
106 ¦ }
107 ¦ if (key == 'k' || key == TB_KEY_ARROW_UP) {
108 ¦ ¦
109 ¦ ¦ if (Display_row > 0) --Display_row;
110 ¦ }
111 ¦ if (key == 't') {
112 ¦ ¦
113 ¦ ¦ Display_row = 0;
114 ¦ }
115 ¦ if (key == 'c') {
116 ¦ ¦
117 ¦ ¦ Display_row = tb_height()/2;
118 ¦ }
119 ¦ if (key == 'b') {
120 ¦ ¦
121 ¦ ¦ Display_row = tb_height()-1;
122 ¦ }
123 ¦ if (key == 'T') {
124 ¦ ¦
125 ¦ ¦ Top_of_screen = get(Trace_index, Display_row);
126 ¦ ¦ Display_row = 0;
127 ¦ ¦ refresh_screen_rows();
128 ¦ }
129 ¦ if (key == 'h' || key == TB_KEY_ARROW_LEFT) {
130 ¦ ¦
131 ¦ ¦ --Left_of_screen;
132 ¦ }
133 ¦ if (key == 'l' || key == TB_KEY_ARROW_RIGHT) {
134 ¦ ¦
135 ¦ ¦ ++Left_of_screen;
136 ¦ }
137 ¦ if (key == 'H') {
138 ¦ ¦
139 ¦ ¦ Left_of_screen -= (tb_width() - 5);
140 ¦ ¦ if (Left_of_screen < 0) Left_of_screen = 0;
141 ¦ }
142 ¦ if (key == 'L') {
143 ¦ ¦
144 ¦ ¦ Left_of_screen += (tb_width() - 5);
145 ¦ }
146 ¦ if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) {
147 ¦ ¦
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 ¦ ¦
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();
169 ¦ }
170 ¦ if (key == 'G' || key == TB_KEY_END) {
171 ¦ ¦
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 ¦ ¦
181 ¦ ¦ Display_row = Last_printed_row;
182 ¦ ¦ refresh_screen_rows();
183 ¦ }
184 ¦ if (key == TB_KEY_CARRIAGE_RETURN) {
185 ¦ ¦
186 ¦ ¦ assert(contains_key(Trace_index, Display_row));
187 ¦ ¦ int start_index = get(Trace_index, Display_row);
188 ¦ ¦ int index = 0;
189 ¦ ¦
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 ¦ ¦
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 ¦ ¦
209 ¦ ¦ assert(contains_key(Trace_index, Display_row));
210 ¦ ¦ int start_index = get(Trace_index, Display_row);
211 ¦ ¦ int index = 0;
212 ¦ ¦
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 ¦ ¦
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
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 ¦
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 ¦ ¦
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
263 Last_printed_row = screen_row-1;
264 for (; screen_row < tb_height(); ++screen_row) {
265 ¦ render_line(screen_row, "~", false);
266 }
267
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);
285 ¦ if (c == '\n') c = ';';
286 ¦
287 ¦ if (c == '\1') { color = 1; c = ' '; }
288 ¦ if (c == '\2') { color = TB_WHITE; c = ' '; }
289 ¦ tb_change_cell(col, screen_row, c, color, highlight ? 240 : TB_BLACK);
290 }
291 for (; col < tb_width(); ++col)
292 ¦ tb_change_cell(col, screen_row, ' ', TB_WHITE, highlight ? 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 }