-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcolour.c
More file actions
182 lines (158 loc) · 5.15 KB
/
colour.c
File metadata and controls
182 lines (158 loc) · 5.15 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright Nico Sonack <nsonack@herrhotzenplotz.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <gcli/cmd/cmd.h>
#include <gcli/cmd/colour.h>
#include <gcli/cmd/cmdconfig.h>
#include <gcli/port/string.h>
#include <gcli/port/util.h>
#include <stdlib.h>
#include <strings.h>
static struct {
uint32_t code;
char *sequence;
} colour_table[1024];
static size_t colour_table_size;
static void
clean_colour_table(void)
{
for (size_t i = 0; i < colour_table_size; ++i)
free(colour_table[i].sequence);
}
static char const *
colour_cache_lookup(uint32_t const code)
{
for (size_t i = 0; i < colour_table_size; ++i) {
if (colour_table[i].code == code)
return colour_table[i].sequence;
}
return NULL;
}
static void
colour_cache_insert(uint32_t const code, char *sequence)
{
colour_table[colour_table_size].code = code;
colour_table[colour_table_size].sequence = sequence;
colour_table_size++;
}
char const *
gcli_setcolour256(uint32_t const code)
{
char *result = NULL;
char const *oldresult = NULL;
if (!gcli_config_have_colours(g_clictx))
return "";
if (colour_table_size == 0)
atexit(clean_colour_table);
oldresult = colour_cache_lookup(code);
if (oldresult)
return oldresult;
result = gcli_asprintf("\033[48;2;%02d;%02d;%02dm",
(code & 0xFF000000) >> 24,
(code & 0x00FF0000) >> 16,
(code & 0x0000FF00) >> 8);
colour_cache_insert(code, result);
return result;
}
const char *
gcli_resetcolour(void)
{
if (!gcli_config_have_colours(g_clictx))
return "";
return "\033[m";
}
const char *
gcli_setcolour(int code)
{
if (!gcli_config_have_colours(g_clictx))
return "";
switch (code) {
case GCLI_COLOR_BLACK: return "\033[30m";
case GCLI_COLOR_RED: return "\033[31m";
case GCLI_COLOR_GREEN: return "\033[32m";
case GCLI_COLOR_YELLOW: return "\033[33m";
case GCLI_COLOR_BLUE: return "\033[34m";
case GCLI_COLOR_MAGENTA: return "\033[35m";
case GCLI_COLOR_CYAN: return "\033[36m";
case GCLI_COLOR_WHITE: return "\033[37m";
case GCLI_COLOR_DEFAULT: return "\033[39m";
default:
gcli_notreached;
}
return NULL;
}
char const *
gcli_setbold(void)
{
if (!gcli_config_have_colours(g_clictx))
return "";
else
return "\033[1m";
}
char const *
gcli_resetbold(void)
{
if (!gcli_config_have_colours(g_clictx))
return "";
else
return "\033[22m";
}
static const struct { char const *name; int code; }
state_colour_table[] =
{
{ .name = "open", .code = GCLI_COLOR_GREEN },
{ .name = "Open", .code = GCLI_COLOR_GREEN },
{ .name = "active", .code = GCLI_COLOR_GREEN },
{ .name = "success", .code = GCLI_COLOR_GREEN },
{ .name = "APPROVED", .code = GCLI_COLOR_GREEN },
{ .name = "merged", .code = GCLI_COLOR_MAGENTA },
{ .name = "closed", .code = GCLI_COLOR_RED },
{ .name = "Closed", .code = GCLI_COLOR_RED },
{ .name = "failed", .code = GCLI_COLOR_RED },
{ .name = "canceled", .code = GCLI_COLOR_RED }, /* orthography has left the channel */
{ .name = "failure", .code = GCLI_COLOR_RED },
{ .name = "running", .code = GCLI_COLOR_BLUE },
{ .name = "created", .code = GCLI_COLOR_BLUE },
{ .name = "New", .code = GCLI_COLOR_BLUE },
{ .name = "COMMENTED", .code = GCLI_COLOR_BLUE },
{ .name = "pending", .code = GCLI_COLOR_CYAN },
{ .name = "In Progress", .code = GCLI_COLOR_CYAN },
{ .name = "CHANGES_REQUESTED", .code = GCLI_COLOR_RED },
};
char const *
gcli_state_colour_str(char const *it)
{
if (it) {
for (size_t i = 0; i < ARRAY_SIZE(state_colour_table); ++i) {
size_t n = strlen(state_colour_table[i].name);
if (strncasecmp(it, state_colour_table[i].name, n) == 0)
return gcli_setcolour(state_colour_table[i].code);
}
}
return gcli_setcolour(GCLI_COLOR_DEFAULT);
}