NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
ansi.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stdbool.h>
31#include "mutt/lib.h"
32#include "gui/lib.h"
33#include "ansi.h"
34#include "attr.h"
35#include "color.h"
36#include "curses2.h"
37#include "parse_ansi.h"
38#include "simple2.h"
39
41
49static void ansi_color_list_add(struct AttrColorList *acl, struct AnsiColor *ansi)
50{
51 if (!acl || !ansi)
52 return;
53
54 if ((ansi->fg.color == COLOR_DEFAULT) && (ansi->bg.color == COLOR_DEFAULT))
55 {
56 switch (ansi->attrs)
57 {
58 case A_NORMAL:
59 return;
60 case A_BOLD:
62 return;
63#ifdef A_ITALIC_DEFINED
64 case A_ITALIC:
66 return;
67#endif
68 case A_UNDERLINE:
70 return;
71 }
72 }
73
74 color_t fg = ansi->fg.color;
75 color_t bg = ansi->bg.color;
76
77#ifdef NEOMUTT_DIRECT_COLORS
78 if ((ansi->fg.type == CT_SIMPLE) || (ansi->fg.type == CT_PALETTE))
80 else if (fg < 8)
81 fg = 8;
82 if ((ansi->bg.type == CT_SIMPLE) || (ansi->bg.type == CT_PALETTE))
84 else if (bg < 8)
85 bg = 8;
86#endif
87
88 struct AttrColor *ac = attr_color_list_find(acl, fg, bg, ansi->attrs);
89 if (ac)
90 {
91 ansi->attr_color = ac;
92 return;
93 }
94
95 ac = attr_color_new();
96 ac->attrs = ansi->attrs;
97 ac->fg = ansi->fg;
98 ac->bg = ansi->bg;
99
100 struct CursesColor *cc = curses_color_new(fg, bg);
101 ac->curses_color = cc;
102 ansi->attr_color = ac;
103
104 TAILQ_INSERT_TAIL(acl, ac, entries);
105}
106
118int ansi_color_parse(const char *str, struct AnsiColor *ansi,
119 struct AttrColorList *acl, bool dry_run)
120{
121 int seq_len = 0;
122 int total_len = 0;
123
124 while ((seq_len = ansi_color_parse_single(str + total_len, ansi, dry_run)) != 0)
125 {
126 total_len += seq_len;
127 }
128
129 ansi_color_list_add(acl, ansi);
130
131 return total_len;
132}
int ansi_color_parse(const char *str, struct AnsiColor *ansi, struct AttrColorList *acl, bool dry_run)
Parse a string of ANSI escape sequence.
Definition: ansi.c:118
color_t color_xterm256_to_24bit(const color_t color)
Convert a xterm color to its RGB value.
Definition: attr.c:320
static void ansi_color_list_add(struct AttrColorList *acl, struct AnsiColor *ansi)
Add an Ansi colour to the list.
Definition: ansi.c:49
ANSI Colours.
struct AttrColor * attr_color_list_find(struct AttrColorList *acl, color_t fg, color_t bg, int attrs)
Find an AttrColor in a list.
Definition: attr.c:139
struct AttrColor * attr_color_new(void)
Create a new AttrColor.
Definition: attr.c:90
Colour and attributes.
@ CT_SIMPLE
Simple colour, e.g. "Red".
Definition: attr.h:36
@ CT_PALETTE
Palette colour, e.g. "color207".
Definition: attr.h:37
struct AttrColor * simple_color_get(enum ColorId cid)
Get the colour of an object by its ID.
Definition: simple.c:95
Color and attribute parsing.
#define COLOR_DEFAULT
Definition: color.h:96
@ MT_COLOR_BOLD
Bold text.
Definition: color.h:41
@ MT_COLOR_ITALIC
Italic text.
Definition: color.h:51
@ MT_COLOR_UNDERLINE
Underlined text.
Definition: color.h:76
Curses Colour.
int32_t color_t
Type for 24-bit colour value.
Definition: curses2.h:31
struct CursesColor * curses_color_new(color_t fg, color_t bg)
Create a new CursesColor.
Definition: curses.c:151
Convenience wrapper for the gui headers.
Convenience wrapper for the library headers.
#define A_ITALIC
Definition: mutt_curses.h:49
int ansi_color_parse_single(const char *buf, struct AnsiColor *ansi, bool dry_run)
Parse a single ANSI escape sequence.
Definition: parse_ansi.c:109
Parse ANSI Sequences.
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:827
Simple colour.
An ANSI escape sequence.
Definition: ansi.h:35
int attrs
Text attributes, e.g. A_BOLD.
Definition: ansi.h:38
const struct AttrColor * attr_color
Curses colour of text.
Definition: ansi.h:39
struct ColorElement bg
Background colour.
Definition: ansi.h:37
struct ColorElement fg
Foreground colour.
Definition: ansi.h:36
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
struct CursesColor * curses_color
Underlying Curses colour.
Definition: attr.h:70
enum ColorType type
Type of Colour.
Definition: attr.h:58
color_t color
Colour.
Definition: attr.h:57
Colour in the ncurses palette.
Definition: curses2.h:41
color_t fg
Foreground colour.
Definition: curses2.h:42
color_t bg
Background colour.
Definition: curses2.h:43