-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgr.c
148 lines (143 loc) · 3.24 KB
/
sgr.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "esc.h"
#include <string.h>
#include <unistd.h>
struct named_style {
const char *name;
int num;
};
struct color_offset {
const char *name;
int offset;
};
struct named_style named_styles[] = {
{"bold", 1},
{"faint", 2},
{"italic", 3},
{"underline", 4},
{"blink", 5},
{"flash", 6},
{"negative", 7},
{"conceal", 8},
{"cross", 9},
{"font", 10},
{"normalfont", 10},
{"font1", 11},
{"font2", 12},
{"font3", 13},
{"font4", 14},
{"font5", 15},
{"font6", 16},
{"font7", 17},
{"font8", 18},
{"font9", 19},
{"fraktur", 20},
{"dunderline", 21},
{"doubleunderline", 21},
{"resetcolor", 22},
{"noitalicfraktur", 23},
{"nounderline", 24},
{"steady", 25},
{"positive", 27},
{"reveal", 28},
{"nocross", 29},
// colors are skipped
{"frame", 51},
{"circle", 52},
{"overline", 53},
{"noframecircle", 54},
{"nooverline", 55},
{NULL, 0},
};
// technically don't need the struct here
// but it's nicer to target this way
struct color_offset color_offsets[] = {
{"black", 0},
{"red", 1},
{"green", 2},
{"yellow", 3},
{"blue", 4},
{"magenta", 5},
{"cyan", 6},
{"white", 7},
{"reset", 9},
{"default", 9},
{NULL, 0},
};
int esc_sgr(int argc, char *argv[]) {
/* TODO:
* -S: integer style: any integer
* used directly for truecolor and co
* -s: named style: bold, faint, italic, underline,
* blink (slow blink), flash (fast blink),
* negative, conceal, cross, dunderline (double),
* frame, encircle, overline
* -i: short for -s italic
* -b: short for -s bold
* -n: normal color
* -N: normal background
* -c: color black, red, green, yellow, blue, magenta, cyan, white
* -C: background black, red, green, yellow, blue, magenta, cyan, white
* -r: reset all
* -t: table color (0 to 255)
* -T: true color (r;g;b)
*/
int c, errflag = 0, first = 0;
setbuf(stdout, NULL);
printf(CSI);
while ((c = getopt(argc, argv, "S:s:ibnNc:C:rt:T:")) != -1) {
if (first++) putchar(';');
switch (c) {
case 'S': // use as-is
printf("%s", optarg);
break;
case 's': { // named style
struct named_style *ptr;
for (ptr = named_styles; ptr->name; ptr++) {
if (!strcmp(ptr->name, optarg)) {
printf("%d", ptr->num);
break;
}
}
if (!ptr->name) errflag++;
break;
}
case 'i': // italic
putchar('3');
break;
case 'b': // bold
putchar('1');
break;
case 'r': // reset
putchar('0');
break;
case 'n': // normal foreground
printf("39");
break;
case 'N': // normal background
printf("49");
break;
case 'c':
case 'C': {
int base = 30;
if (c == 'C') base += 10;
struct color_offset *ptr;
for (ptr = color_offsets; ptr->name; ptr++) {
if (!strcmp(ptr->name, optarg)) {
printf("%d", base + ptr->offset);
break;
}
}
if (!ptr->name) errflag++;
break;
}
case 't':
printf("38;5;%s", optarg);
break;
case 'T':
printf("38;2;%s", optarg);
break;
}
}
printf("\x6d");
return 0;
}