Logo

index : track

---

  • summary
  • about
  • tree
  • log
  • branches
<< path: root/public/track.git/html/src/display.jai blob: 269841fc78e7b3fde48ec594f8e58f0e12fe8352 [raw] [clear marker]

        
0
1
2display :: (
3 todo: [..]Token,
4 note: [..]Token,
5 idea: [..]Token,
6 cont: [..]Token,
7 other: [..]Token,
8 file_name: string
9) {
10 push_allocator(temp);
11 term := terminal_info_size();
12
13 // Hack. See TODO @TERMINAL_MIN_WIDTH
14 if term.col < TERMINAL_MIN_WIDTH then term.col = TERMINAL_MIN_WIDTH;
15
16 section_title(file_name, term);
17
18 tokens_print(todo, " TODO ", term);
19 tokens_print(note, " NOTE ", term);
20 tokens_print(idea, " IDEA ", term);
21 tokens_print(cont, " CONTINUE ", term);
22 tokens_print(other, " OTHER ", term);
23}
24
25
26#scope_file
27
28
29PADDING :: 4;
30
31
32/** TODO(adam): This is a fugly hack. Below 88 columns I see on _my_ terminal
33 bazillions of hyphens. This is worth investigating.
34*/
35TERMINAL_MIN_WIDTH :: 88;
36
37COLUMN_LABEL_COLOR_LINE: Color_Foreground: .MAGENTA;
38COLUMN_LABEL_COLOR_PRIORITY: Color_Foreground: .GREEN;
39COLUMN_LABEL_COLOR_AUTHOR: Color_Foreground: .BLUE;
40COLUMN_LABEL_COLOR_KEYWORD: Color_Foreground: .RED;
41
42COLUMN_LABEL_LINE :: " Line ";
43COLUMN_LABEL_PRIORITY :: " Priority ";
44COLUMN_LABEL_AUTHOR :: " Author ";
45
46COLUMN_PADDING_LINE :: 3;
47COLUMN_PADDING_PRIORITY :: 1;
48COLUMN_PADDING_AUTHOR :: 5;
49
50COLUMN_WIDTH_LINE :: #run COLUMN_LABEL_LINE.count + COLUMN_PADDING_LINE;
51COLUMN_WIDTH_PRIORITY :: #run COLUMN_LABEL_PRIORITY.count + COLUMN_PADDING_PRIORITY;
52COLUMN_WIDTH_AUTHOR :: #run COLUMN_LABEL_AUTHOR.count + COLUMN_PADDING_AUTHOR;
53
54SEPARATOR_FIRST :: "|";
55SEPARATOR_CORNER :: "+-";
56SEPARATOR :: " | ";
57
58HLINE :: "-";
59
60
61#no_reset table_header_visual_count: int;
62
63TABLE_HEADER :: #run -> string {
64
65 create_column_title :: (
66 label: string,
67 padding: int,
68 fgc: Color_Foreground,
69 bgc: Color_Background
70 )
71 -> vcount: int #expand
72 {
73 append(*`buf, SEPARATOR_CORNER);
74 out, vcount := paint(
75 label,
76 fg = fgc,
77 bg = bgc
78 );
79 append(*`buf, out);
80
81 for 0..padding append(*`buf, HLINE);
82
83 return vcount;
84 }
85
86 buf: String_Builder;
87 init_string_builder(*buf);
88
89 vcount_line := create_column_title(
90 COLUMN_LABEL_LINE,
91 COLUMN_PADDING_LINE - 1,
92 COLUMN_LABEL_COLOR_LINE,
93 .BLACK
94 );
95
96 vcount_prio := create_column_title(
97 COLUMN_LABEL_PRIORITY,
98 COLUMN_PADDING_PRIORITY,
99 COLUMN_LABEL_COLOR_PRIORITY,
100 .BLACK
101 );
102
103 vcount_author := create_column_title(
104 COLUMN_LABEL_AUTHOR,
105 COLUMN_PADDING_AUTHOR,
106 COLUMN_LABEL_COLOR_AUTHOR,
107 .BLACK
108 );
109
110 append(*buf, SEPARATOR_CORNER);
111
112 table_header_visual_count += vcount_line
113 + vcount_prio
114 + vcount_author;
115
116 return builder_to_string(*buf);
117}
118
119
120Winsize :: struct {
121 row: u16 = 0; // not in use
122 col: u16 = TERMINAL_MIN_WIDTH;
123 xpixel: u16 = 0; // not in use
124 ypixel: u16 = 0; // not in use
125}
126
127
128section_title :: (label: string, term: Winsize) {
129 title, vcount_title := paint(
130 tprint(" File: % ", label),
131 .ITALIC,
132 fg = .CYAN,
133 bg = .BLACK
134 );
135
136 pref, vcount_pref := paint("o-", fg = .CYAN);
137
138 header := string_pad_right(
139 tprint("%%", pref, title),
140 term.col + vcount_title + vcount_pref - (PADDING - 1),
141 paint(HLINE, fg = .CYAN, no_termination = true)
142 );
143
144 print("\n");
145 print(header);
146 print(paint_reset());
147 print("\n");
148}
149
150tokens_print :: (tokens: [..]Token, label: string, term: Winsize) {
151 if !tokens return;
152 header_print(label, term);
153
154 for tokens print("%\n", lines_assemble(it, term));
155 print("\n");
156}
157
158header_print :: (label: string, term: Winsize) {
159 title, title_vcount := paint(
160 tprint("%", label),
161 fg = COLUMN_LABEL_COLOR_KEYWORD,
162 bg = .BLACK
163 );
164
165 width_adapted := table_header_visual_count + title_vcount;
166
167 header := string_pad_right(
168 title,
169 term.col - PADDING - TABLE_HEADER.count + width_adapted + 1,
170 HLINE
171 );
172
173 print("%%\n", TABLE_HEADER, header);
174}
175
176word_wrap_and_append :: (
177 buf: *String_Builder,
178 field: string,
179 columns_count: int,
180 current_width: int
181) {
182 tab_to_keyword_column :: () #expand {
183 append(`buf, "\n");
184 for 0..`columns_count-1 append(`buf, " ");
185 `current_line_width = `columns_count;
186 }
187
188 find_whitespace :: (s: string, pos: int) -> int {
189 c: int;
190 for i: pos..s.count-1 {
191 c += 1;
192 if is_space(s[i]) then return c;
193 }
194 return c;
195 }
196
197 comment := field;
198
199 legal_term_width := current_width - PADDING;
200 idx_column: int = columns_count;
201
202 idx_field: int;
203 while idx_field < comment.count {
204 ws_pos := find_whitespace(comment, idx_field);
205 word_length := (idx_field + ws_pos) - idx_field;
206
207 if idx_column + word_length >= legal_term_width {
208 append(buf, "\n");
209 for 0..columns_count-1 append(buf, " ");
210 idx_column = columns_count;
211 }
212
213 if idx_column + word_length >= legal_term_width {
214 for idx_column..legal_term_width {
215 append(buf, comment[idx_field]);
216 idx_field += 1;
217 idx_column += 1;
218 }
219 }
220
221 append(buf, comment[idx_field]);
222 idx_field += 1;
223 idx_column += 1;
224 }
225}
226
227lines_assemble :: (token: Token, terminal: Winsize) -> string {
228
229 make_string :: (
230 elem: Any,
231 filler: string,
232 fgc: Color_Foreground,
233 width: int,
234 pad_proc: (string, int, string) -> string
235 )
236 -> string, int
237 {
238 str, vcount := paint(tprint("%", elem), fg = fgc);
239 out := pad_proc(str, width + vcount, filler);
240 return out, vcount;
241 }
242
243
244 buf: String_Builder;
245 init_string_builder(*buf);
246
247 line, vcount_line := make_string(
248 token.line,
249 ".",
250 COLUMN_LABEL_COLOR_LINE,
251 COLUMN_WIDTH_LINE,
252 string_pad_left
253 );
254
255 priority, vcount_prio := make_string(
256 token.priority,
257 " ",
258 COLUMN_LABEL_COLOR_PRIORITY,
259 COLUMN_WIDTH_PRIORITY,
260 string_pad_left
261 );
262
263 author, vcount_author := make_string(
264 token.author,
265 " ",
266 COLUMN_LABEL_COLOR_AUTHOR,
267 COLUMN_WIDTH_AUTHOR,
268 string_pad_right
269 );
270
271 current_width := terminal.col - PADDING;
272
273 columns_author_count := SEPARATOR_FIRST.count
274 + (SEPARATOR.count * 2)
275 + abs(line.count - vcount_line)
276 + abs(priority.count - vcount_prio);
277
278 columns_comment_count := columns_author_count
279 + SEPARATOR.count
280 + abs(author.count - vcount_author);
281
282 append(*buf, SEPARATOR_FIRST);
283 append(*buf, line);
284 append(*buf, SEPARATOR);
285 append(*buf, priority);
286 append(*buf, SEPARATOR);
287 append(*buf, author);
288 append(*buf, SEPARATOR);
289
290 word_wrap_and_append(
291 *buf,
292 token.comment,
293 columns_comment_count,
294 current_width
295 );
296
297 return builder_to_string(*buf);
298}
299
300terminal_info_size :: () -> term: Winsize {
301 w: Winsize;
302 error_code := ioctl(STDOUT_FILENO, TIOCGWINSZ, *w);
303
304 if error_code != 0 {
305 log_error(
306 "[Warning]: Could not get terminal information. Falling back to defaults."
307 );
308 return {};
309 }
310
311 return w;
312}
313
314
Copyright 2026  E766CB298A6D1E64 | Git-Thing heavily inspired by cgit