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 :(before "End Primitive Recipe Declarations")
44 _BROWSE_TRACE,
45 :(before "End Primitive Recipe Numbers")
46 put(Recipe_ordinal, "$browse-trace", _BROWSE_TRACE);
47 :(before "End Primitive Recipe Checks")
48 case _BROWSE_TRACE: {
49 break;
50 }
51 :(before "End Primitive Recipe Implementations")
52 case _BROWSE_TRACE: {
53 start_trace_browser();
54 break;
55 }
56
57
58 :(after "Commandline Parsing")
59 if (argc == 3 && is_equal(argv[1], "browse-trace")) {
60 load_trace(argv[2]);
61 start_trace_browser();
62 return 0;
63 }
64
65 :(before "End Globals")
66 set<int> Visible;
67 int Top_of_screen = 0;
68 int Last_printed_row = 0;
69 map<int, int> Trace_index;
70
71 :(code)
72 void start_trace_browser() {
73 if (!Trace_stream) return;
74 cerr << "computing min depth to display\n";
75 int min_depth = 9999;
76 for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
77 ¦ trace_line& curr_line = Trace_stream->past_lines.at(i);
78 ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
79 }
80 cerr << "min depth is " << min_depth << '\n';
81 cerr << "computing lines to display\n";
82 for (int i = 0; i < SIZE(Trace_stream->past_lines); ++i) {
83 ¦ if (Trace_stream->past_lines.at(i).depth == min_depth)
84 ¦ ¦ Visible.insert(i);
85 }
86 tb_init();
87 Display_row = Display_column = 0;
88 tb_event event;
89 Top_of_screen = 0;
90 refresh_screen_rows();
91 while (true) {
92 ¦ render();
93 ¦ do {
94 ¦ ¦ tb_poll_event(&event);
95 ¦ } while (event.type != TB_EVENT_KEY);
96 ¦ int key = event.key ? event.key : event.ch;
97 ¦ if (key == 'q' || key == 'Q' || key == TB_KEY_CTRL_C) break;
98 ¦ if (key == 'j' || key == TB_KEY_ARROW_DOWN) {
99 ¦ ¦
100 ¦ ¦ if (Display_row < Last_printed_row) ++Display_row;
101 ¦ }
102 ¦ if (key == 'k' || key == TB_KEY_ARROW_UP) {
103 ¦ ¦
104 ¦ ¦ if (Display_row > 0) --Display_row;
105 ¦ }
106 ¦ if (key == 't') {
107 ¦ ¦
108 ¦ ¦ Display_row = 0;
109 ¦ }
110 ¦ if (key == 'c') {
111 ¦ ¦
112 ¦ ¦ Display_row = tb_height()/2;
113 ¦ }
114 ¦ if (key == 'b') {
115 ¦ ¦
116 ¦ ¦ Display_row = tb_height()-1;
117 ¦ }
118 ¦ if (key == 'T') {
119 ¦ ¦ Top_of_screen = get(Trace_index, Display_row);
120 ¦ ¦ Display_row = 0;
121 ¦ ¦ refresh_screen_rows();
122 ¦ }
123 ¦ if (key == 'J' || key == TB_KEY_PGDN || key == TB_KEY_CTRL_F) {
124 ¦ ¦
125 ¦ ¦ if (Trace_index.find(tb_height()-1) != Trace_index.end()) {
126 ¦ ¦ ¦ Top_of_screen = get(Trace_index, tb_height()-1) + 1;
127 ¦ ¦ ¦ refresh_screen_rows();
128 ¦ ¦ }
129 ¦ }
130 ¦ if (key == 'K' || key == TB_KEY_PGUP || key == TB_KEY_CTRL_B) {
131 ¦ ¦
132 ¦ ¦ for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) {
133 ¦ ¦ ¦ --Top_of_screen;
134 ¦ ¦ ¦ if (Top_of_screen <= 0) break;
135 ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
136 ¦ ¦ ¦ ¦ --Top_of_screen;
137 ¦ ¦ }
138 ¦ ¦ if (Top_of_screen >= 0)
139 ¦ ¦ ¦ refresh_screen_rows();
140 ¦ }
141 ¦ if (key == 'g' || key == TB_KEY_HOME) {
142 ¦ ¦ ¦ Top_of_screen = 0;
143 ¦ ¦ ¦ Last_printed_row = 0;
144 ¦ ¦ ¦ Display_row = 0;
145 ¦ ¦ ¦ refresh_screen_rows();
146 ¦ }
147 ¦ if (key == 'G' || key == TB_KEY_END) {
148 ¦ ¦
149 ¦ ¦ Top_of_screen = SIZE(Trace_stream->past_lines)-1;
150 ¦ ¦ for (int screen_row = tb_height(); screen_row > 0 && Top_of_screen > 0; --screen_row) {
151 ¦ ¦ ¦ --Top_of_screen;
152 ¦ ¦ ¦ if (Top_of_screen <= 0) break;
153 ¦ ¦ ¦ while (Top_of_screen > 0 && !contains_key(Visible, Top_of_screen))
154 ¦ ¦ ¦ ¦ --Top_of_screen;
155 ¦ ¦ }
156 ¦ ¦ refresh_screen_rows();
157 ¦ ¦
158 ¦ ¦ Display_row = Last_printed_row;
159 ¦ ¦ refresh_screen_rows();
160 ¦ }
161 ¦ if (key == TB_KEY_CARRIAGE_RETURN) {
162 ¦ ¦
163 ¦ ¦ assert(contains_key(Trace_index, Display_row));
164 ¦ ¦ int start_index = get(Trace_index, Display_row);
165 ¦ ¦ int index = 0;
166 ¦ ¦
167 ¦ ¦ int min_depth = 9999;
168 ¦ ¦ for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) {
169 ¦ ¦ ¦ if (contains_key(Visible, index)) break;
170 ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
171 ¦ ¦ ¦ assert(curr_line.depth > Trace_stream->past_lines.at(start_index).depth);
172 ¦ ¦ ¦ if (curr_line.depth < min_depth) min_depth = curr_line.depth;
173 ¦ ¦ }
174 ¦ ¦ int end_index = index;
175 ¦ ¦
176 ¦ ¦ for (index = start_index; index < end_index; ++index) {
177 ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
178 ¦ ¦ ¦ if (curr_line.depth == min_depth) {
179 ¦ ¦ ¦ ¦ Visible.insert(index);
180 ¦ ¦ ¦ }
181 ¦ ¦ }
182 ¦ ¦ refresh_screen_rows();
183 ¦ }
184 ¦ if (key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2) {
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 initial_depth = Trace_stream->past_lines.at(start_index).depth;
191 ¦ ¦ for (index = start_index+1; index < SIZE(Trace_stream->past_lines); ++index) {
192 ¦ ¦ ¦ if (!contains_key(Visible, index)) continue;
193 ¦ ¦ ¦ trace_line& curr_line = Trace_stream->past_lines.at(index);
194 ¦ ¦ ¦ if (curr_line.depth <= initial_depth) break;
195 ¦ ¦ }
196 ¦ ¦ int end_index = index;
197 ¦ ¦
198 ¦ ¦ for (index = start_index+1; index < end_index; ++index) {
199 ¦ ¦ ¦ Visible.erase(index);
200 ¦ ¦ }
201 ¦ ¦ refresh_screen_rows();
202 ¦ }
203 }
204 tb_shutdown();
205 }
206
207
208 void refresh_screen_rows() {
209 int screen_row = 0, index = 0;
210 Trace_index.clear();
211 for (screen_row = 0, index = Top_of_screen; screen_row < tb_height() && index < SIZE(Trace_stream->past_lines); ++screen_row, ++index) {
212 ¦
213 ¦ while (!contains_key(Visible, index)) {
214 ¦ ¦ ++index;
215 ¦ ¦ if (index >= SIZE(Trace_stream->past_lines)) goto done;
216 ¦ }
217 ¦ assert(index < SIZE(Trace_stream->past_lines));
218 ¦ put(Trace_index, screen_row, index);
219 }
220 done:;
221 }
222
223 void render() {
224 int screen_row = 0;
225 for (screen_row = 0; screen_row < tb_height(); ++screen_row) {
226 ¦ if (!contains_key(Trace_index, screen_row)) break;
227 ¦ trace_line& curr_line = Trace_stream->past_lines.at(get(Trace_index, screen_row));
228 ¦ ostringstream out;
229 ¦ out << std::setw(4) << curr_line.depth << ' ' << curr_line.label << ": " << curr_line.contents;
230 ¦ if (screen_row < tb_height()-1) {
231 ¦ ¦ int delta = lines_hidden(screen_row);
232 ¦ ¦
233 ¦ ¦ if (delta > 999) out << static_cast<char>(1);
234 ¦ ¦ out << " (" << delta << ")";
235 ¦ ¦ if (delta > 999) out << static_cast<char>(2);
236 ¦ }
237 ¦ render_line(screen_row, out.str(), screen_row == Display_row);
238 }
239
240 Last_printed_row = screen_row-1;
241 for (; screen_row < tb_height(); ++screen_row) {
242 ¦ render_line(screen_row, "~", false);
243 }
244
245 tb_set_cursor(0, Display_row);
246 tb_present();
247 }
248
249 int lines_hidden(int screen_row) {
250 assert(contains_key(Trace_index, screen_row));
251 if (!contains_key(Trace_index, screen_row+1))
252 ¦ return SIZE(Trace_stream->past_lines) - get(Trace_index, screen_row);
253 else
254 ¦ return get(Trace_index, screen_row+1) - get(Trace_index, screen_row);
255 }
256
257 void render_line(int screen_row, const string& s, bool highlight) {
258 int col = 0;
259 int color = TB_WHITE;
260 for (col = 0; col < tb_width() && col < SIZE(s); ++col) {
261 ¦ char c = s.at(col);
262 ¦ if (c == '\n') c = ';';
263 ¦
264 ¦ if (c == '\1') { color = 1; c = ' '; }
265 ¦ if (c == '\2') { color = TB_WHITE; c = ' '; }
266 ¦ tb_change_cell(col, screen_row, c, color, highlight ? 240 : TB_BLACK);
267 }
268 for (; col < tb_width(); ++col)
269 ¦ tb_change_cell(col, screen_row, ' ', TB_WHITE, highlight ? 240 : TB_BLACK);
270 }
271
272 void load_trace(const char* filename) {
273 ifstream tin(filename);
274 if (!tin) {
275 ¦ cerr << "no such file: " << filename << '\n';
276 ¦ exit(1);
277 }
278 Trace_stream = new trace_stream;
279 while (has_data(tin)) {
280 ¦ tin >> std::noskipws;
281 ¦ ¦ skip_whitespace_but_not_newline(tin);
282 ¦ ¦ if (!isdigit(tin.peek())) {
283 ¦ ¦ ¦ string dummy;
284 ¦ ¦ ¦ getline(tin, dummy);
285 ¦ ¦ ¦ continue;
286 ¦ ¦ }
287 ¦ tin >> std::skipws;
288 ¦ int depth;
289 ¦ tin >> depth;
290 ¦ string label;
291 ¦ tin >> label;
292 ¦ if (*--label.end() == ':') label.erase(--label.end());
293 ¦ string line;
294 ¦ getline(tin, line);
295 ¦ Trace_stream->past_lines.push_back(trace_line(depth, label, line));
296 }
297 cerr << "lines read: " << Trace_stream->past_lines.size() << '\n';
298 }