NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
dump.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include <stdio.h>
32#include "mutt/lib.h"
33#include "config/lib.h"
34#include "core/lib.h"
35#include "gui/lib.h"
36#include "dump.h"
37#include "pager/lib.h"
38#include "attr.h"
39#include "color.h"
40#include "parse_color.h"
41#include "quoted.h"
42#include "regex4.h"
43#include "simple2.h"
44
52void color_log_color_attrs(struct AttrColor *ac, struct Buffer *swatch)
53{
54 buf_reset(swatch);
55
56 if (ac->attrs & A_BLINK)
57 buf_add_printf(swatch, "\033[5m");
58 if (ac->attrs & A_BOLD)
59 buf_add_printf(swatch, "\033[1m");
60 if (ac->attrs & A_ITALIC)
61 buf_add_printf(swatch, "\033[3m");
62 if (ac->attrs == A_NORMAL)
63 buf_add_printf(swatch, "\033[0m");
64 if (ac->attrs & A_REVERSE)
65 buf_add_printf(swatch, "\033[7m");
66 if (ac->attrs & A_STANDOUT)
67 buf_add_printf(swatch, "\033[1m");
68 if (ac->attrs & A_UNDERLINE)
69 buf_add_printf(swatch, "\033[4m");
70
71 if (ac->fg.color >= 0)
72 {
73 switch (ac->fg.type)
74 {
75 case CT_SIMPLE:
76 {
77 buf_add_printf(swatch, "\033[%dm", 30 + ac->fg.color);
78 break;
79 }
80
81 case CT_PALETTE:
82 {
83 buf_add_printf(swatch, "\033[38;5;%dm", ac->fg.color);
84 break;
85 }
86
87 case CT_RGB:
88 {
89 int r = (ac->fg.color >> 16) & 0xff;
90 int g = (ac->fg.color >> 8) & 0xff;
91 int b = (ac->fg.color >> 0) & 0xff;
92 buf_add_printf(swatch, "\033[38;2;%d;%d;%dm", r, g, b);
93 break;
94 }
95 }
96 }
97
98 if (ac->bg.color >= 0)
99 {
100 switch (ac->bg.type)
101 {
102 case CT_SIMPLE:
103 {
104 buf_add_printf(swatch, "\033[%dm", 40 + ac->bg.color);
105 break;
106 }
107
108 case CT_PALETTE:
109 {
110 buf_add_printf(swatch, "\033[48;5;%dm", ac->bg.color);
111 break;
112 }
113
114 case CT_RGB:
115 {
116 int r = (ac->bg.color >> 16) & 0xff;
117 int g = (ac->bg.color >> 8) & 0xff;
118 int b = (ac->bg.color >> 0) & 0xff;
119 buf_add_printf(swatch, "\033[48;2;%d;%d;%dm", r, g, b);
120 break;
121 }
122 }
123 }
124
125 buf_addstr(swatch, "XXXXXX\033[0m");
126}
127
135const char *color_log_attrs_list(int attrs)
136{
137 static char text[64];
138
139 text[0] = '\0';
140 int pos = 0;
141 // We can ignore the A_NORMAL case
142 if (attrs & A_BLINK)
143 pos += snprintf(text + pos, sizeof(text) - pos, "blink ");
144 if (attrs & A_BOLD)
145 pos += snprintf(text + pos, sizeof(text) - pos, "bold ");
146 if (attrs & A_ITALIC)
147 pos += snprintf(text + pos, sizeof(text) - pos, "italic ");
148 if (attrs & A_REVERSE)
149 pos += snprintf(text + pos, sizeof(text) - pos, "reverse ");
150 if (attrs & A_STANDOUT)
151 pos += snprintf(text + pos, sizeof(text) - pos, "standout ");
152 if (attrs & A_UNDERLINE)
153 pos += snprintf(text + pos, sizeof(text) - pos, "underline ");
154
155 return text;
156}
157
165const char *color_log_name(char *buf, int buflen, struct ColorElement *elem)
166{
167 if (elem->color < 0)
168 return "default";
169
170 switch (elem->type)
171 {
172 case CT_SIMPLE:
173 {
174 const char *prefix = NULL;
175 switch (elem->prefix)
176 {
178 prefix = "alert";
179 break;
181 prefix = "bright";
182 break;
184 prefix = "light";
185 break;
186 default:
187 prefix = "";
188 break;
189 }
190
191 const char *name = mutt_map_get_name(elem->color, ColorNames);
192 snprintf(buf, buflen, "%s%s", prefix, name);
193 break;
194 }
195
196 case CT_PALETTE:
197 {
198 if (elem->color < 256)
199 snprintf(buf, buflen, "color%d", elem->color);
200 else
201 snprintf(buf, buflen, "BAD:%d", elem->color); // LCOV_EXCL_LINE
202 break;
203 }
204
205 case CT_RGB:
206 {
207 int r = (elem->color >> 16) & 0xff;
208 int g = (elem->color >> 8) & 0xff;
209 int b = (elem->color >> 0) & 0xff;
210 snprintf(buf, buflen, "#%02x%02x%02x", r, g, b);
211 break;
212 }
213 }
214
215 return buf;
216}
217
222void quoted_colors_dump(struct Buffer *buf)
223{
224 if (NumQuotedColors == 0)
225 return;
226
227 struct Buffer *swatch = buf_pool_get();
228 char color_fg[64] = { 0 };
229 char color_bg[64] = { 0 };
230
231 buf_addstr(buf, _("# Quoted Colors\n"));
232 for (int i = 0; i < NumQuotedColors; i++)
233 {
234 struct AttrColor *ac = quoted_colors_get(i);
235 if (!ac)
236 continue; // LCOV_EXCL_LINE
237
238 color_log_color_attrs(ac, swatch);
239 buf_add_printf(buf, "color quoted%d %-20s %-16s %-16s # %s\n", i,
241 color_log_name(color_fg, sizeof(color_fg), &ac->fg),
242 color_log_name(color_bg, sizeof(color_bg), &ac->bg),
243 buf_string(swatch));
244 }
245
246 buf_addstr(buf, "\n");
247 buf_pool_release(&swatch);
248}
249
254void regex_colors_dump(struct Buffer *buf)
255{
256 struct Buffer *swatch = buf_pool_get();
257 struct Buffer *pattern = buf_pool_get();
258 char color_fg[64] = { 0 };
259 char color_bg[64] = { 0 };
260
261 for (enum ColorId cid = MT_COLOR_NONE; cid != MT_COLOR_MAX; cid++)
262 {
263 if (cid == MT_COLOR_STATUS)
264 continue;
265
266 if (!mutt_color_has_pattern(cid))
267 continue;
268
269 struct RegexColorList *rcl = regex_colors_get_list(cid);
270 if (STAILQ_EMPTY(rcl))
271 continue;
272
273 const char *name = mutt_map_get_name(cid, ColorFields);
274 if (!name)
275 continue; // LCOV_EXCL_LINE
276
277 buf_add_printf(buf, _("# Regex Color %s\n"), name);
278
279 struct RegexColor *rc = NULL;
280 STAILQ_FOREACH(rc, rcl, entries)
281 {
282 struct AttrColor *ac = &rc->attr_color;
283
284 buf_reset(pattern);
285 pretty_var(rc->pattern, pattern);
286 color_log_color_attrs(ac, swatch);
287 buf_add_printf(buf, "color %-16s %-20s %-16s %-16s %-30s # %s\n", name,
289 color_log_name(color_fg, sizeof(color_fg), &ac->fg),
290 color_log_name(color_bg, sizeof(color_bg), &ac->bg),
291 buf_string(pattern), buf_string(swatch));
292 }
293 buf_addstr(buf, "\n");
294 }
295
296 buf_pool_release(&swatch);
297 buf_pool_release(&pattern);
298}
299
304void simple_colors_dump(struct Buffer *buf)
305{
306 struct Buffer *swatch = buf_pool_get();
307 char color_fg[64] = { 0 };
308 char color_bg[64] = { 0 };
309
310 int count = 0;
311 for (enum ColorId cid = MT_COLOR_NONE + 1; cid < MT_COLOR_MAX; cid++)
312 {
313 if ((cid == MT_COLOR_QUOTED) || (cid == MT_COLOR_STATUS))
314 continue;
315
316 struct AttrColor *ac = simple_color_get(cid);
317 if (attr_color_is_set(ac))
318 count++;
319 }
320
321 if (count > 0)
322 {
323 buf_addstr(buf, _("# Simple Colors\n"));
324 for (enum ColorId cid = MT_COLOR_NONE + 1; cid < MT_COLOR_MAX; cid++)
325 {
326 if ((cid == MT_COLOR_QUOTED) || (cid == MT_COLOR_STATUS))
327 continue;
328
329 struct AttrColor *ac = simple_color_get(cid);
330 if (!attr_color_is_set(ac))
331 continue;
332
333 const char *name = mutt_map_get_name(cid, ColorFields);
334 if (!name)
335 continue;
336
337 color_log_color_attrs(ac, swatch);
338 buf_add_printf(buf, "color %-18s %-20s %-16s %-16s # %s\n", name,
340 color_log_name(color_fg, sizeof(color_fg), &ac->fg),
341 color_log_name(color_bg, sizeof(color_bg), &ac->bg),
342 buf_string(swatch));
343 }
344 buf_addstr(buf, "\n");
345 }
346
347 count = 0;
348 for (int i = 0; ComposeColorFields[i].name; i++)
349 {
350 enum ColorId cid = ComposeColorFields[i].value;
351
352 struct AttrColor *ac = simple_color_get(cid);
353 if (attr_color_is_set(ac))
354 count++;
355 }
356
357 if (count > 0)
358 {
359 buf_addstr(buf, _("# Compose Colors\n"));
360 for (int i = 0; ComposeColorFields[i].name; i++)
361 {
362 const char *name = ComposeColorFields[i].name;
363 enum ColorId cid = ComposeColorFields[i].value;
364
365 struct AttrColor *ac = simple_color_get(cid);
366 if (!attr_color_is_set(ac))
367 continue;
368
369 color_log_color_attrs(ac, swatch);
370 buf_add_printf(buf, "color compose %-18s %-20s %-16s %-16s # %s\n", name,
372 color_log_name(color_fg, sizeof(color_fg), &ac->fg),
373 color_log_name(color_bg, sizeof(color_bg), &ac->bg),
374 buf_string(swatch));
375 }
376 buf_addstr(buf, "\n");
377 }
378
379 buf_pool_release(&swatch);
380}
381
386void status_colors_dump(struct Buffer *buf)
387{
388 struct Buffer *swatch = buf_pool_get();
389 struct Buffer *pattern = buf_pool_get();
390 char color_fg[64] = { 0 };
391 char color_bg[64] = { 0 };
392
393 bool set = false;
394
395 const enum ColorId cid = MT_COLOR_STATUS;
396 struct AttrColor *ac = simple_color_get(cid);
397 if (attr_color_is_set(ac))
398 set = true;
399
400 struct RegexColorList *rcl = regex_colors_get_list(cid);
401 if (!STAILQ_EMPTY(rcl))
402 set = true;
403
404 if (set)
405 {
406 buf_addstr(buf, _("# Status Colors\n"));
407
408 color_log_color_attrs(ac, swatch);
409 buf_add_printf(buf, "color status %-20s %-16s %-16s # %s\n",
411 color_log_name(color_fg, sizeof(color_fg), &ac->fg),
412 color_log_name(color_bg, sizeof(color_bg), &ac->bg),
413 buf_string(swatch));
414
415 struct RegexColor *rc = NULL;
416 STAILQ_FOREACH(rc, rcl, entries)
417 {
418 ac = &rc->attr_color;
419
422 color_log_color_attrs(ac, swatch);
423 if (rc->match == 0)
424 {
425 buf_add_printf(buf, "color status %-20s %-16s %-16s %-30s # %s\n",
427 color_log_name(color_fg, sizeof(color_fg), &ac->fg),
428 color_log_name(color_bg, sizeof(color_bg), &ac->bg),
429 buf_string(pattern), buf_string(swatch));
430 }
431 else
432 {
433 buf_add_printf(buf, "color status %-20s %-16s %-16s %-28s %d # %s\n",
435 color_log_name(color_fg, sizeof(color_fg), &ac->fg),
436 color_log_name(color_bg, sizeof(color_bg), &ac->bg),
437 buf_string(pattern), rc->match, buf_string(swatch));
438 }
439 }
440 buf_addstr(buf, "\n");
441 }
442
443 buf_pool_release(&swatch);
445}
446
450void color_dump(void)
451{
452 struct Buffer *tmp_file = buf_pool_get();
453
454 buf_mktemp(tmp_file);
455 FILE *fp = mutt_file_fopen(buf_string(tmp_file), "w");
456 if (!fp)
457 {
458 // LCOV_EXCL_START
459 // L10N: '%s' is the file name of the temporary file
460 mutt_error(_("Could not create temporary file %s"), buf_string(tmp_file));
461 buf_pool_release(&tmp_file);
462 return;
463 // LCOV_EXCL_STOP
464 }
465
466 struct Buffer *buf = buf_pool_get();
467
472
473#ifdef USE_DEBUG_COLOR
475 ansi_colors_dump(buf);
478#endif
479
481 buf_pool_release(&buf);
482 mutt_file_fclose(&fp);
483
484 struct PagerData pdata = { 0 };
485 struct PagerView pview = { &pdata };
486
487 pdata.fname = buf_string(tmp_file);
488
489 pview.banner = "color";
490 pview.flags = MUTT_SHOWCOLOR;
491 pview.mode = PAGER_MODE_OTHER;
492
493 mutt_do_pager(&pview, NULL);
494 buf_pool_release(&tmp_file);
495}
bool attr_color_is_set(const struct AttrColor *ac)
Is the object coloured?
Definition: attr.c:179
Colour and attributes.
@ COLOR_PREFIX_ALERT
"alert" colour prefix
Definition: attr.h:47
@ COLOR_PREFIX_LIGHT
"light" colour prefix
Definition: attr.h:49
@ COLOR_PREFIX_BRIGHT
"bright" colour prefix
Definition: attr.h:48
@ CT_SIMPLE
Simple colour, e.g. "Red".
Definition: attr.h:36
@ CT_PALETTE
Palette colour, e.g. "color207".
Definition: attr.h:37
@ CT_RGB
True colour, e.g. "#11AAFF".
Definition: attr.h:38
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:204
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:76
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
void simple_colors_dump(struct Buffer *buf)
Dump all the Simple colours.
Definition: dump.c:304
void color_log_color_attrs(struct AttrColor *ac, struct Buffer *swatch)
Get a colourful string to represent a colour in the log.
Definition: dump.c:52
const char * color_log_name(char *buf, int buflen, struct ColorElement *elem)
Get a string to represent a colour name.
Definition: dump.c:165
void quoted_colors_dump(struct Buffer *buf)
Dump all the Quoted colours.
Definition: dump.c:222
void regex_colors_dump(struct Buffer *buf)
Dump all the Regex colours.
Definition: dump.c:254
void color_dump(void)
Display all the colours in the Pager.
Definition: dump.c:450
const char * color_log_attrs_list(int attrs)
Get a string to represent some attributes in the log.
Definition: dump.c:135
void status_colors_dump(struct Buffer *buf)
Dump all the Status colours.
Definition: dump.c:386
struct RegexColorList * regex_colors_get_list(enum ColorId cid)
Return the RegexColorList for a colour id.
Definition: regex.c:182
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition: simple.c:88
bool mutt_color_has_pattern(enum ColorId cid)
Check if a color object supports a regex pattern.
Definition: color.c:97
Color and attribute parsing.
const struct Mapping ComposeColorFields[]
Mapping of compose colour names to their IDs.
Definition: command.c:111
const struct Mapping ColorFields[]
Mapping of colour names to their IDs.
Definition: command.c:56
ColorId
List of all colored objects.
Definition: color.h:40
@ MT_COLOR_QUOTED
Pager: quoted text.
Definition: color.h:63
@ MT_COLOR_MAX
Definition: color.h:94
@ MT_COLOR_STATUS
Status bar (takes a pattern)
Definition: color.h:75
@ MT_COLOR_NONE
Definition: color.h:41
size_t pretty_var(const char *str, struct Buffer *buf)
Escape and stringify a config item value.
Definition: dump.c:85
Dump all the config.
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
void ansi_colors_dump(struct Buffer *buf)
Dump all the ANSI colours.
Definition: debug.c:84
void curses_colors_dump(struct Buffer *buf)
Dump all the Curses colours.
Definition: debug.c:144
void merged_colors_dump(struct Buffer *buf)
Dump all the Merged colours.
Definition: debug.c:177
int mutt_do_pager(struct PagerView *pview, struct Email *e)
Display some page-able text to the user (help or attachment)
Definition: do_pager.c:122
size_t mutt_file_save_str(FILE *fp, const char *str)
Save a string to a file.
Definition: file.c:1679
#define mutt_file_fclose(FP)
Definition: file.h:138
#define mutt_file_fopen(PATH, MODE)
Definition: file.h:137
#define mutt_error(...)
Definition: logging2.h:92
Convenience wrapper for the gui headers.
#define log_multiline(LEVEL, STRING)
Definition: logging2.h:96
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
#define A_ITALIC
Definition: mutt_curses.h:49
GUI display a file/email/help in a viewport with paging.
#define MUTT_SHOWCOLOR
Show characters in color otherwise don't show characters.
Definition: lib.h:62
@ PAGER_MODE_OTHER
Pager is invoked via 3rd path. Non-email content is likely to be shown.
Definition: lib.h:142
const struct Mapping ColorNames[]
Mapping between a colour name and an ncurses colour.
Definition: parse_color.c:41
Parse colour commands.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_EMPTY(head)
Definition: queue.h:348
struct AttrColor * quoted_colors_get(int q)
Return the color of a quote, cycling through the used quotes.
Definition: quoted.c:92
int NumQuotedColors
Number of colours for quoted email text.
Definition: quoted.c:44
Quoted-Email colours.
Regex Colour.
Simple colour.
A curses colour and its attributes.
Definition: attr.h:66
struct ColorElement bg
Background colour.
Definition: attr.h:68
struct ColorElement fg
Foreground colour.
Definition: attr.h:67
int attrs
Text attributes, e.g. A_BOLD.
Definition: attr.h:69
String manipulation buffer.
Definition: buffer.h:36
One element of a Colour.
Definition: attr.h:56
enum ColorType type
Type of Colour.
Definition: attr.h:58
color_t color
Colour.
Definition: attr.h:57
enum ColorPrefix prefix
Optional Colour Modifier.
Definition: attr.h:59
int value
Integer value.
Definition: mapping.h:35
const char * name
String value.
Definition: mapping.h:34
Data to be displayed by PagerView.
Definition: lib.h:161
const char * fname
Name of the file to read.
Definition: lib.h:165
Paged view into some data.
Definition: lib.h:172
struct PagerData * pdata
Data that pager displays. NOTNULL.
Definition: lib.h:173
enum PagerMode mode
Pager mode.
Definition: lib.h:174
PagerFlags flags
Additional settings to tweak pager's function.
Definition: lib.h:175
const char * banner
Title to display in status bar.
Definition: lib.h:176
A regular expression and a color to highlight a line.
Definition: regex4.h:36
struct AttrColor attr_color
Colour and attributes to apply.
Definition: regex4.h:37
char * pattern
Pattern to match.
Definition: regex4.h:38
int match
Substring to match, 0 for old behaviour.
Definition: regex4.h:40
#define buf_mktemp(buf)
Definition: tmp.h:33