dte test coverage


Directory: ./
File: src/palette.c
Date: 2025-05-08 15:05:54
Exec Total Coverage
Lines: 36 36 100.0%
Functions: 3 3 100.0%
Branches: 8 8 100.0%

Line Branch Exec Source
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include "palette.h"
4
5 18 static void seq (
6 const char *sgr_prefix,
7 unsigned int seq_start,
8 unsigned int seq_end,
9 int width, // Space-padded width of printed number
10 const char *suffix,
11 bool sgr_starts_at_zero
12 ) {
13
2/2
✓ Branch 0 (7→3) taken 270 times.
✓ Branch 1 (7→8) taken 18 times.
288 for (unsigned int i = seq_start; i <= seq_end; i++) {
14
2/2
✓ Branch 0 (3→4) taken 15 times.
✓ Branch 1 (3→5) taken 255 times.
270 unsigned int sgr = sgr_starts_at_zero ? i - seq_start : i;
15 270 fprintf(stdout, "\033[%s%um %*u%s", sgr_prefix, sgr, width, i, suffix);
16 }
17 18 }
18
19 34 static void str(const char *s)
20 {
21 34 fputs(s, stdout);
22 34 }
23
24 // Print a visual representation of the XTerm style color palette to
25 // stdout. No effort is made to limit the emitted SGR sequences to the
26 // set supported by the terminal, or even to detect whether stdout is
27 // in fact a terminal.
28 1 ExitCode print_256_color_palette(void)
29 {
30 // Row 1: basic ECMA-48 palette colors
31 1 str("\033[0m\n ");
32 1 seq("4", 0, 6, 2, " ", false);
33 1 str("\033[30;47m 7 \033[0m");
34 1 seq("3", 0, 7, 2, " ", false);
35 1 str("\033[0m Default \033[0m\n ");
36
37 // Row 2: aixterm-style "bright" palette colors
38 1 seq("10", 8, 14, 2, " ", true);
39 1 str("\033[30;107m 15 \033[0m");
40 1 seq("9", 8, 15, 2, " ", true);
41 1 str("\033[0;2m Faint\033[0m\n\n");
42
43 // First half of color cube (light foreground)
44 1 str("\033[38;5;253m");
45
2/2
✓ Branch 0 (17→13) taken 6 times.
✓ Branch 1 (17→18) taken 1 times.
7 for (unsigned int i = 0; i < 6; i++) {
46 6 unsigned int start = 16 + (i * 36); // Sequence: 16 52 88 124 160 196
47 6 str(" ");
48 6 seq("48;5;", start, start + 17, 3, "", false);
49 6 str("\033[49m\n");
50 }
51
52 // Second half of color cube (dark foreground)
53 1 str("\033[38;5;235m");
54
2/2
✓ Branch 0 (24→20) taken 6 times.
✓ Branch 1 (24→25) taken 1 times.
7 for (unsigned int i = 0; i < 6; i++) {
55 6 unsigned int start = 34 + (i * 36); // Sequence: 34 70 106 142 178 214
56 6 str(" ");
57 6 seq("48;5;", start, start + 17, 3, "", false);
58 6 str("\033[49m\n");
59 }
60
61 // First row of grayscales (light foreground)
62 1 str("\033[0;38;5;253m\n ");
63 1 seq("48;5;", 232, 243, 4, " ", false);
64
65 // Second row of grayscales (dark foreground)
66 1 str("\033[0;38;5;235m\n ");
67 1 seq("48;5;", 244, 255, 4, " ", false);
68
69 1 str("\033[0m\n\n");
70 1 return EC_OK;
71 }
72