dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 83.1% 162 / 1 / 196
Functions: 92.9% 13 / 0 / 14
Branches: 77.6% 76 / 10 / 108

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