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
|
/*
* sysobj - https://github.com/bp0/verbose-spork
* Copyright (C) 2018 Burt P. <pburt0@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "format_early.h"
#include "vendor.h"
#define ANSI_COLOR_RESET "\x1b[0m"
const gchar *color_lookup(int ansi_color) {
static struct { int ansi; const gchar *html; } tab[] = {
{ 30, "#010101" }, { 31, "#de382b" }, { 32, "#39b54a" }, { 33, "#ffc706" },
{ 34, "#006fb8" }, { 35, "#762671" }, { 36, "#2cb5e9" }, { 37, "#cccccc" },
{ 90, "#808080" }, { 91, "#ff0000" }, { 92, "#00ff00" }, { 93, "#ffff00" },
{ 94, "#0000ff" }, { 95, "#ff00ff" }, { 96, "#00ffff" }, { 97, "#ffffff" },
{ 40, "#010101" }, { 41, "#de382b" }, { 42, "#39b54a" }, { 43, "#ffc706" },
{ 44, "#006fb8" }, { 45, "#762671" }, { 46, "#2cb5e9" }, { 47, "#cccccc" },
{ 100, "#808080" }, { 101, "#ff0000" }, { 102, "#00ff00" }, { 103, "#ffff00" },
{ 104, "#0000ff" }, { 105, "#ff00ff" }, { 106, "#00ffff" }, { 107, "#ffffff" },
};
for (int i = 0; i<(int)G_N_ELEMENTS(tab); i++)
if (tab[i].ansi == ansi_color)
return tab[i].html;
return NULL;
}
gchar *safe_ansi_color(gchar *ansi_color, gboolean free_in) {
if (!ansi_color) return NULL;
gchar *ret = NULL;
gchar **codes = g_strsplit(ansi_color, ";", -1);
if (free_in)
g_free(ansi_color);
int len = g_strv_length(codes);
for(int i = 0; i < len; i++) {
int c = atoi(codes[i]);
if (c == 0 || c == 1
|| ( c >= 30 && c <= 37)
|| ( c >= 40 && c <= 47)
|| ( c >= 90 && c <= 97)
|| ( c >= 100 && c <= 107) ) {
ret = appf(ret, ";", "%s", codes[i]);
}
}
g_strfreev(codes);
return ret;
}
gchar *format_with_ansi_color(const gchar *str, const gchar *ansi_color, int fmt_opts) {
gchar *ret = NULL;
gchar *safe_color = g_strdup(ansi_color);
util_strstrip_double_quotes_dumb(safe_color);
if (fmt_opts & FMT_OPT_ATERM) {
safe_color = safe_ansi_color(safe_color, TRUE);
ret = g_strdup_printf("\x1b[%sm%s" ANSI_COLOR_RESET, safe_color, str);
goto format_with_ansi_color_end;
}
if (fmt_opts & FMT_OPT_PANGO || fmt_opts & FMT_OPT_HTML) {
int fgc = 37, bgc = 40;
gchar **codes = g_strsplit(safe_color, ";", -1);
int len = g_strv_length(codes);
for(int i = 0; i < len; i++) {
int c = atoi(codes[i]);
if ( (c >= 30 && c <= 37)
|| ( c >= 90 && c <= 97 ) ) {
fgc = c;
}
if ( (c >= 40 && c <= 47)
|| ( c >= 100 && c <= 107) ) {
bgc = c;
}
}
g_strfreev(codes);
const gchar *html_color_fg = color_lookup(fgc);
const gchar *html_color_bg = color_lookup(bgc);
if (fmt_opts & FMT_OPT_PANGO)
ret = g_strdup_printf("<span background=\"%s\" color=\"%s\"><b> %s </b></span>", html_color_bg, html_color_fg, str);
else if (fmt_opts & FMT_OPT_HTML)
ret = g_strdup_printf("<span style=\"background-color: %s; color: %s;\"><b> %s </b></span>", html_color_bg, html_color_fg, str);
}
format_with_ansi_color_end:
g_free(safe_color);
if (!ret)
ret = g_strdup(str);
return ret;
}
void tag_vendor(gchar **str, guint offset, const gchar *vendor_str, const char *ansi_color, int fmt_opts) {
if (!str || !*str) return;
if (!vendor_str || !ansi_color) return;
gchar *work = *str, *new = NULL;
if (g_str_has_prefix(work + offset, vendor_str)
|| strncasecmp(work + offset, vendor_str, strlen(vendor_str)) == 0) {
gchar *cvs = format_with_ansi_color(vendor_str, ansi_color, fmt_opts);
*(work+offset) = 0;
new = g_strdup_printf("%s%s%s", work, cvs, work + offset + strlen(vendor_str) );
g_free(work);
*str = new;
g_free(cvs);
}
}
gchar *vendor_match_tag(const gchar *vendor_str, int fmt_opts) {
const Vendor *v = vendor_match(vendor_str, NULL);
if (v) {
gchar *ven_tag = v->name_short ? g_strdup(v->name_short) : g_strdup(v->name);
tag_vendor(&ven_tag, 0, ven_tag, v->ansi_color, fmt_opts);
return ven_tag;
}
return NULL;
}
|