dte test coverage


Directory: ./
File: src/status.c
Date: 2025-11-12 12:04:10
Coverage Exec Excl Total
Lines: 80.6% 162 1 202
Functions: 92.9% 13 0 14
Branches: 76.0% 76 0 100

Line Branch Exec Source
1 #include <stdbool.h>
2 #include <stdint.h>
3 #include <string.h>
4 #include "status.h"
5 #include "cmdline.h"
6 #include "search.h"
7 #include "selection.h"
8 #include "util/debug.h"
9 #include "util/macros.h"
10 #include "util/numtostr.h"
11 #include "util/utf8.h"
12 #include "util/xsnprintf.h"
13
14 typedef struct {
15 char *buf;
16 size_t size;
17 size_t pos;
18 size_t separator;
19 const Window *window;
20 const GlobalOptions *opts;
21 const ModeHandler *mode;
22 } Formatter;
23
24 enum {
25 SHORT_SEPARATOR_SIZE = 1, // For STATUS_SEPARATOR
26 LONG_SEPARATOR_SIZE = 3, // For STATUS_SEPARATOR_LONG
27 SEPARATOR_WRITE_SIZE = 4, // See add_separator()
28 };
29
30 typedef enum {
31 STATUS_INVALID = 0,
32 STATUS_ESCAPED_PERCENT,
33 STATUS_ENCODING,
34 STATUS_MISC,
35 STATUS_IS_CRLF,
36 STATUS_SEPARATOR_LONG,
37 STATUS_CURSOR_COL_BYTES,
38 STATUS_TOTAL_ROWS,
39 STATUS_BOM,
40 STATUS_FILENAME,
41 STATUS_INPUT_MODE,
42 STATUS_MODIFIED,
43 STATUS_LINE_ENDING,
44 STATUS_OVERWRITE,
45 STATUS_SCROLL_POSITION,
46 STATUS_READONLY,
47 STATUS_SEPARATOR,
48 STATUS_FILETYPE,
49 STATUS_UNICODE,
50 STATUS_CURSOR_COL,
51 STATUS_CURSOR_ROW,
52 } FormatSpecifierType;
53
54 397 static FormatSpecifierType lookup_format_specifier(unsigned char ch)
55 {
56
21/21
✓ Branch 2 → 3 taken 15 times.
✓ Branch 2 → 4 taken 19 times.
✓ Branch 2 → 5 taken 6 times.
✓ Branch 2 → 6 taken 4 times.
✓ Branch 2 → 7 taken 14 times.
✓ Branch 2 → 8 taken 4 times.
✓ Branch 2 → 9 taken 15 times.
✓ Branch 2 → 10 taken 15 times.
✓ Branch 2 → 11 taken 2 times.
✓ Branch 2 → 12 taken 15 times.
✓ Branch 2 → 13 taken 17 times.
✓ Branch 2 → 14 taken 15 times.
✓ Branch 2 → 15 taken 14 times.
✓ Branch 2 → 16 taken 17 times.
✓ Branch 2 → 17 taken 70 times.
✓ Branch 2 → 18 taken 15 times.
✓ Branch 2 → 19 taken 14 times.
✓ Branch 2 → 20 taken 3 times.
✓ Branch 2 → 21 taken 15 times.
✓ Branch 2 → 22 taken 104 times.
✓ Branch 2 → 23 taken 4 times.
397 switch (ch) {
57 case '%': return STATUS_ESCAPED_PERCENT;
58 15 case 'E': return STATUS_ENCODING;
59 19 case 'M': return STATUS_MISC;
60 6 case 'N': return STATUS_IS_CRLF;
61 4 case 'S': return STATUS_SEPARATOR_LONG;
62 14 case 'X': return STATUS_CURSOR_COL_BYTES;
63 4 case 'Y': return STATUS_TOTAL_ROWS;
64 15 case 'b': return STATUS_BOM;
65 15 case 'f': return STATUS_FILENAME;
66 2 case 'i': return STATUS_INPUT_MODE;
67 15 case 'm': return STATUS_MODIFIED;
68 17 case 'n': return STATUS_LINE_ENDING;
69 15 case 'o': return STATUS_OVERWRITE;
70 14 case 'p': return STATUS_SCROLL_POSITION;
71 17 case 'r': return STATUS_READONLY;
72 70 case 's': return STATUS_SEPARATOR;
73 15 case 't': return STATUS_FILETYPE;
74 14 case 'u': return STATUS_UNICODE;
75 3 case 'x': return STATUS_CURSOR_COL;
76 15 case 'y': return STATUS_CURSOR_ROW;
77 }
78 104 return STATUS_INVALID;
79 }
80
81 #define add_status_literal(f, s) add_status_bytes(f, s, STRLEN(s))
82
83 8 static void add_ch(Formatter *f, char ch)
84 {
85 8 f->buf[f->pos++] = ch;
86 8 }
87
88 40 static void add_separator(Formatter *f)
89 {
90 40 size_t len = MIN(f->separator, f->size - f->pos);
91 40 BUG_ON(len > LONG_SEPARATOR_SIZE);
92 40 char *dest = f->buf + f->pos;
93 40 f->pos += len;
94 40 f->separator = 0;
95
96 // The extra buffer space set aside by sf_format() allows us to write
97 // 4 bytes unconditionally here, to allow this memset() call to be
98 // optimized into a simple move instruction
99 40 memset(dest, ' ', SEPARATOR_WRITE_SIZE);
100 40 }
101
102 7 static void add_status_str(Formatter *f, const char *str)
103 {
104 7 BUG_ON(!str);
105
1/2
✓ Branch 4 → 5 taken 7 times.
✗ Branch 4 → 13 not taken.
7 if (unlikely(!str[0])) {
106 return;
107 }
108
109 7 add_separator(f);
110 7 char *buf = f->buf;
111 7 size_t pos = f->pos;
112
113
3/4
✓ Branch 10 → 11 taken 43 times.
✗ Branch 10 → 12 not taken.
✓ Branch 11 → 7 taken 36 times.
✓ Branch 11 → 12 taken 7 times.
43 for (size_t i = 0, size = f->size; pos < size && str[i]; ) {
114 36 pos += u_set_char(buf + pos, u_str_get_char(str, &i));
115 }
116
117 7 f->pos = pos;
118 }
119
120 25 static void add_status_bytes(Formatter *f, const char *str, size_t len)
121 {
122
1/2
✓ Branch 2 → 3 taken 25 times.
✗ Branch 2 → 7 not taken.
25 if (unlikely(len == 0)) {
123 return;
124 }
125
126 25 add_separator(f);
127
1/2
✓ Branch 4 → 5 taken 25 times.
✗ Branch 4 → 7 not taken.
25 if (f->pos >= f->size) {
128 return;
129 }
130
131 25 size_t avail = f->size - f->pos;
132 25 f->pos += copystrn(f->buf + f->pos, str, MIN(len, avail));
133 }
134
135 PRINTF(2)
136 static void add_status_format(Formatter *f, const char *format, ...)
137 {
138 char buf[1024];
139 va_list ap;
140 va_start(ap, format);
141 size_t len = xvsnprintf(buf, sizeof(buf), format, ap);
142 va_end(ap);
143 add_status_bytes(f, buf, len);
144 }
145
146 6 static void add_status_umax(Formatter *f, uintmax_t x)
147 {
148 6 char buf[DECIMAL_STR_MAX(x)];
149 6 size_t len = buf_umax_to_str(x, buf);
150 6 add_status_bytes(f, buf, len);
151 6 }
152
153 7 static void add_status_bool(Formatter *f, bool state, const char *on, const char *off)
154 {
155
2/2
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 4 taken 4 times.
7 const char *str = state ? on : off;
156 7 add_status_bytes(f, str, strlen(str));
157 7 }
158
159 1 static void add_status_unicode(Formatter *f, const BlockIter *cursor)
160 {
161 1 CodePoint u;
162
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 10 taken 1 time.
1 if (unlikely(!block_iter_get_char(cursor, &u))) {
163 1 return;
164 }
165
166 if (!u_is_unicode(u)) {
167 add_status_literal(f, "Invalid");
168 return;
169 }
170
171 char str[STRLEN("U+10FFFF") + 1];
172 str[0] = 'U';
173 str[1] = '+';
174 size_t ndigits = buf_umax_to_hex_str(u, str + 2, 4);
175 add_status_bytes(f, str, 2 + ndigits);
176 }
177
178 1 static void add_status_pos(Formatter *f)
179 {
180 1 size_t lines = f->window->view->buffer->nl;
181 1 int h = f->window->edit_h;
182 1 long pos = f->window->view->vy;
183
1/2
✓ Branch 2 → 3 taken 1 time.
✗ Branch 2 → 6 not taken.
1 if (lines <= h) {
184
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
1 if (pos) {
185 add_status_literal(f, "Bot");
186 } else {
187 1 add_status_literal(f, "All");
188 }
189 } else if (pos == 0) {
190 add_status_literal(f, "Top");
191 } else if (pos + h - 1 >= lines) {
192 add_status_literal(f, "Bot");
193 } else {
194 unsigned int d = lines - (h - 1);
195 unsigned int percent = (pos * 100 + d / 2) / d;
196 BUG_ON(percent > 100);
197 char buf[4];
198 size_t len = buf_uint_to_str(percent, buf);
199 buf[len++] = '%';
200 add_status_bytes(f, buf, len);
201 }
202 1 }
203
204 6 static void add_misc_status(Formatter *f)
205 {
206 6 static const struct {
207 const char NONSTRING str[24];
208 size_t len;
209 } css_strs[] = {
210 [CSS_FALSE] = {STRN("[case-sensitive = false]")},
211 [CSS_TRUE] = {STRN("[case-sensitive = true]")},
212 [CSS_AUTO] = {STRN("[case-sensitive = auto]")},
213 };
214
215
2/2
✓ Branch 2 → 3 taken 5 times.
✓ Branch 2 → 6 taken 1 time.
6 if (f->mode->cmds == &search_mode_commands) {
216 5 SearchCaseSensitivity css = f->opts->case_sensitive_search;
217 5 BUG_ON(css >= ARRAYLEN(css_strs));
218 5 add_status_bytes(f, css_strs[css].str, css_strs[css].len);
219 11 return;
220 }
221
222 1 const View *view = f->window->view;
223
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 18 taken 1 time.
1 if (view->selection == SELECT_NONE) {
224 return;
225 }
226
227 SelectionInfo si = init_selection(view);
228 bool is_lines = (view->selection == SELECT_LINES);
229 size_t n = is_lines ? get_nr_selected_lines(&si) : get_nr_selected_chars(&si);
230 const char *unit = is_lines ? "line" : "char";
231 const char *plural = unlikely(n == 1) ? "" : "s";
232 add_status_format(f, "[%zu %s%s]", n, unit, plural);
233 }
234
235 59 static void expand_format_specifier(Formatter *f, FormatSpecifierType type)
236 {
237 59 const View *view = f->window->view;
238 59 const Buffer *buffer = view->buffer;
239
20/21
✓ Branch 2 → 3 taken 3 times.
✓ Branch 2 → 5 taken 2 times.
✓ Branch 2 → 7 taken 1 time.
✓ Branch 2 → 9 taken 2 times.
✓ Branch 2 → 12 taken 4 times.
✓ Branch 2 → 16 taken 2 times.
✓ Branch 2 → 18 taken 2 times.
✓ Branch 2 → 20 taken 1 time.
✓ Branch 2 → 22 taken 1 time.
✓ Branch 2 → 26 taken 1 time.
✓ Branch 2 → 28 taken 2 times.
✓ Branch 2 → 30 taken 6 times.
✓ Branch 2 → 32 taken 4 times.
✓ Branch 2 → 34 taken 4 times.
✓ Branch 2 → 36 taken 3 times.
✓ Branch 2 → 38 taken 3 times.
✓ Branch 2 → 39 taken 13 times.
✓ Branch 2 → 40 taken 2 times.
✓ Branch 2 → 42 taken 1 time.
✓ Branch 2 → 44 taken 2 times.
✗ Branch 2 → 47 not taken.
59 switch (type) {
240 3 case STATUS_BOM:
241
2/2
✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 49 taken 1 time.
3 if (buffer->bom) {
242 2 add_status_literal(f, "BOM");
243 }
244 return;
245 2 case STATUS_FILENAME:
246 2 add_status_str(f, buffer_filename(buffer));
247 2 return;
248 1 case STATUS_INPUT_MODE:
249 1 add_status_str(f, f->mode->name);
250 1 return;
251 2 case STATUS_MODIFIED:
252
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 49 taken 2 times.
2 if (buffer_modified(buffer)) {
253 add_separator(f);
254 add_ch(f, '*');
255 }
256 return;
257 4 case STATUS_READONLY:
258
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 4 times.
4 if (buffer->readonly) {
259 add_status_literal(f, "RO");
260
2/2
✓ Branch 14 → 15 taken 2 times.
✓ Branch 14 → 49 taken 2 times.
4 } else if (buffer->temporary) {
261 2 add_status_literal(f, "TMP");
262 }
263 return;
264 2 case STATUS_CURSOR_ROW:
265 2 add_status_umax(f, view->cy + 1);
266 2 return;
267 2 case STATUS_TOTAL_ROWS:
268 2 add_status_umax(f, buffer->nl);
269 2 return;
270 1 case STATUS_CURSOR_COL:
271 1 add_status_umax(f, view->cx_display + 1);
272 1 return;
273 1 case STATUS_CURSOR_COL_BYTES:
274 1 add_status_umax(f, view->cx_char + 1);
275
1/2
✗ Branch 23 → 24 not taken.
✓ Branch 23 → 49 taken 1 time.
1 if (view->cx_display != view->cx_char) {
276 add_ch(f, '-');
277 add_status_umax(f, view->cx_display + 1);
278 }
279 return;
280 1 case STATUS_SCROLL_POSITION:
281 1 add_status_pos(f);
282 1 return;
283 2 case STATUS_ENCODING:
284 2 add_status_str(f, buffer->encoding);
285 2 return;
286 6 case STATUS_MISC:
287 6 add_misc_status(f);
288 6 return;
289 4 case STATUS_IS_CRLF:
290
2/2
✓ Branch 32 → 33 taken 2 times.
✓ Branch 32 → 49 taken 2 times.
4 if (buffer->crlf_newlines) {
291 2 add_status_literal(f, "CRLF");
292 }
293 return;
294 4 case STATUS_LINE_ENDING:
295 4 add_status_bool(f, buffer->crlf_newlines, "CRLF", "LF");
296 4 return;
297 3 case STATUS_OVERWRITE:
298 3 add_status_bool(f, buffer->options.overwrite, "OVR", "INS");
299 3 return;
300 3 case STATUS_SEPARATOR_LONG:
301 3 f->separator = LONG_SEPARATOR_SIZE;
302 3 return;
303 13 case STATUS_SEPARATOR:
304 13 f->separator = SHORT_SEPARATOR_SIZE;
305 13 return;
306 2 case STATUS_FILETYPE:
307 2 add_status_str(f, buffer->options.filetype);
308 2 return;
309 1 case STATUS_UNICODE:
310 1 add_status_unicode(f, &view->cursor);
311 1 return;
312 2 case STATUS_ESCAPED_PERCENT:
313 2 add_separator(f);
314 2 add_ch(f, '%');
315 2 return;
316 case STATUS_INVALID:
317 break;
318 }
319
320 BUG("should be unreachable, due to validate_statusline_format()");
321 }
322
323 26 size_t sf_format (
324 const Window *window,
325 const GlobalOptions *opts,
326 const ModeHandler *mode,
327 char *buf, // NOLINT(readability-non-const-parameter)
328 size_t size,
329 const char *format
330 ) {
331 26 BUG_ON(size < 16);
332 26 Formatter f = {
333 .window = window,
334 .opts = opts,
335 .mode = mode,
336 .buf = buf,
337 26 .size = size - SEPARATOR_WRITE_SIZE - U_SET_CHAR_MAXLEN - 1,
338 };
339
340
4/4
✓ Branch 11 → 12 taken 90 times.
✓ Branch 11 → 13 taken 1 time.
✓ Branch 12 → 5 taken 65 times.
✓ Branch 12 → 13 taken 25 times.
91 while (f.pos < f.size && *format) {
341 65 unsigned char ch = *format++;
342
2/2
✓ Branch 5 → 6 taken 6 times.
✓ Branch 5 → 9 taken 59 times.
65 if (ch != '%') {
343 6 add_separator(&f);
344 6 add_ch(&f, ch);
345 6 continue;
346 }
347 59 FormatSpecifierType type = lookup_format_specifier(*format++);
348 59 expand_format_specifier(&f, type);
349 }
350
351 26 f.buf[f.pos] = '\0';
352 26 return u_str_width(f.buf);
353 }
354
355 // Returns the offset of the first invalid format specifier, or 0 if
356 // the whole format string is valid. It's safe to use 0 to indicate
357 // "no errors", since it's not possible for there to be an error at
358 // the very start of the string.
359 147 size_t statusline_format_find_error(const char *str)
360 {
361
2/2
✓ Branch 7 → 3 taken 507 times.
✓ Branch 7 → 8 taken 43 times.
550 for (size_t i = 0; str[i]; ) {
362
2/2
✓ Branch 3 → 4 taken 169 times.
✓ Branch 3 → 5 taken 338 times.
507 if (str[i++] != '%') {
363 169 continue;
364 }
365
2/2
✓ Branch 5 → 6 taken 234 times.
✓ Branch 5 → 8 taken 104 times.
338 if (lookup_format_specifier(str[i++]) == STATUS_INVALID) {
366 return i - 1;
367 }
368 }
369 return 0;
370 }
371