Skip to content

Commit

Permalink
merge: refactor color parsing
Browse files Browse the repository at this point in the history
 * color: introduce ColorElement
 * color: change parse_color() to use an AttrColor
 * color: move business logic out of parsers
 * color: tidy OptNoCurses cases
  • Loading branch information
flatcap committed Oct 28, 2023
2 parents c386288 + 4fd803b commit b6e3e40
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 287 deletions.
42 changes: 19 additions & 23 deletions color/ansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include "config.h"
#include <stdbool.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "ansi.h"
#include "attr.h"
Expand All @@ -54,11 +52,11 @@ static void ansi_color_list_add(struct AttrColorList *acl, struct AnsiColor *ans
if (!acl || !ansi)
return;

if ((ansi->fg == COLOR_DEFAULT) && (ansi->bg == COLOR_DEFAULT))
if ((ansi->fg.color == COLOR_DEFAULT) && (ansi->bg.color == COLOR_DEFAULT))
{
switch (ansi->attrs)
{
case 0:
case A_NORMAL:
return;
case A_BOLD:
ansi->attr_color = simple_color_get(MT_COLOR_BOLD);
Expand All @@ -72,7 +70,21 @@ static void ansi_color_list_add(struct AttrColorList *acl, struct AnsiColor *ans
}
}

struct AttrColor *ac = attr_color_list_find(acl, ansi->fg, ansi->bg, ansi->attrs);
color_t fg = ansi->fg.color;
color_t bg = ansi->bg.color;

#ifdef NEOMUTT_DIRECT_COLORS
if ((ansi->fg.type == CT_SIMPLE) || (ansi->fg.type == CT_PALETTE))
fg = color_xterm256_to_24bit(fg);
else if (fg < 8)
fg = 8;
if ((ansi->bg.type == CT_SIMPLE) || (ansi->bg.type == CT_PALETTE))
bg = color_xterm256_to_24bit(bg);
else if (bg < 8)
bg = 8;
#endif

struct AttrColor *ac = attr_color_list_find(acl, fg, bg, ansi->attrs);
if (ac)
{
ansi->attr_color = ac;
Expand All @@ -81,24 +93,8 @@ static void ansi_color_list_add(struct AttrColorList *acl, struct AnsiColor *ans

ac = attr_color_new();
ac->attrs = ansi->attrs;

color_t fg = ansi->fg;
color_t bg = ansi->bg;

#ifdef NEOMUTT_DIRECT_COLORS
const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
if (c_color_directcolor)
{
/* If we are running in direct color mode, we must convert the xterm
* color numbers 0-255 to an RGB value. */
fg = color_xterm256_to_24bit(fg);
if (fg < 8)
fg = 8;
bg = color_xterm256_to_24bit(bg);
if (bg < 8)
bg = 8;
}
#endif
ac->fg = ansi->fg;
ac->bg = ansi->bg;

struct CursesColor *cc = curses_color_new(fg, bg);
ac->curses_color = cc;
Expand Down
15 changes: 7 additions & 8 deletions color/ansi.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,21 @@
#define MUTT_COLOR_ANSI_H

#include <stdbool.h>
#include "curses2.h"

struct AttrColorList;
#include "attr.h"

/**
* struct AnsiColor - An ANSI escape sequence
*
* @note AnsiColor doesn't own the AttrColor
*/
struct AnsiColor
{
struct ColorElement fg; ///< Foreground colour
struct ColorElement bg; ///< Background colour
int attrs; ///< Text attributes, e.g. A_BOLD
const struct AttrColor *attr_color; ///< Curses colour of text
int attrs; ///< Attributes, e.g. A_BOLD
color_t fg; ///< Foreground colour
color_t bg; ///< Background colour
};

int ansi_color_parse (const char *str, struct AnsiColor *ansi, struct AttrColorList *acl, bool dry_run);
int ansi_color_seq_length(const char *str);
int ansi_color_parse(const char *str, struct AnsiColor *ansi, struct AttrColorList *acl, bool dry_run);

#endif /* MUTT_COLOR_ANSI_H */
66 changes: 64 additions & 2 deletions color/attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@
#include "config.h"
#include <stddef.h>
#include <assert.h>
#include <string.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "attr.h"
#include "color.h"
#include "curses2.h"
#include "debug.h"

Expand All @@ -50,7 +54,13 @@ void attr_color_clear(struct AttrColor *ac)
if (ac->curses_color)
color_debug(LL_DEBUG5, "clear %p\n", (void *) ac);
curses_color_free(&ac->curses_color);
ac->attrs = 0;

memset(&ac->fg, 0, sizeof(ac->fg));
memset(&ac->bg, 0, sizeof(ac->bg));

ac->fg.color = COLOR_DEFAULT;
ac->bg.color = COLOR_DEFAULT;
ac->attrs = A_NORMAL;
}

/**
Expand Down Expand Up @@ -82,6 +92,16 @@ struct AttrColor *attr_color_new(void)
{
struct AttrColor *ac = mutt_mem_calloc(1, sizeof(*ac));

ac->fg.color = COLOR_DEFAULT;
ac->fg.type = CT_SIMPLE;
ac->fg.prefix = COLOR_PREFIX_NONE;

ac->bg.color = COLOR_DEFAULT;
ac->bg.type = CT_SIMPLE;
ac->bg.prefix = COLOR_PREFIX_NONE;

ac->attrs = A_NORMAL;

ac->ref_count = 1;

return ac;
Expand Down Expand Up @@ -163,7 +183,7 @@ bool attr_color_is_set(const struct AttrColor *ac)
if (!ac)
return false;

return ((ac->attrs != 0) || ac->curses_color);
return ((ac->attrs != A_NORMAL) || ac->curses_color);
}

/**
Expand Down Expand Up @@ -312,6 +332,12 @@ color_t color_xterm256_to_24bit(const color_t color)
if (color < 0)
return color;

const bool c_color_directcolor = cs_subset_bool(NeoMutt->sub, "color_directcolor");
if (!c_color_directcolor)
{
return color;
}

if (color < 16)
{
color_debug(LL_DEBUG5, "Converted color 0-15: %d\n", color);
Expand Down Expand Up @@ -362,3 +388,39 @@ color_t color_xterm256_to_24bit(const color_t color)
return rgb;
}
#endif

/**
* attr_color_overwrite - Update an AttrColor in-place
* @param ac_old AttrColor to overwrite
* @param ac_new AttrColor to copy
*/
void attr_color_overwrite(struct AttrColor *ac_old, struct AttrColor *ac_new)
{
if (!ac_old || !ac_new)
return;

color_t fg = ac_new->fg.color;
color_t bg = ac_new->bg.color;
int attrs = ac_new->attrs;

modify_color_by_prefix(ac_new->fg.prefix, true, &fg, &attrs);
modify_color_by_prefix(ac_new->bg.prefix, false, &bg, &attrs);

#ifdef NEOMUTT_DIRECT_COLORS
if ((ac_new->fg.type == CT_SIMPLE) || (ac_new->fg.type == CT_PALETTE))
fg = color_xterm256_to_24bit(fg);
else if (fg < 8)
fg = 8;
if ((ac_new->bg.type == CT_SIMPLE) || (ac_new->bg.type == CT_PALETTE))
bg = color_xterm256_to_24bit(bg);
else if (bg < 8)
bg = 8;
#endif

struct CursesColor *cc = curses_color_new(fg, bg);
curses_color_free(&ac_old->curses_color);
ac_old->fg = ac_new->fg;
ac_old->bg = ac_new->bg;
ac_old->attrs = attrs;
ac_old->curses_color = cc;
}
26 changes: 25 additions & 1 deletion color/attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
#include "mutt/lib.h"
#include "curses2.h"

/**
* enum ColorType - Type of Colour
*/
enum ColorType
{
CT_SIMPLE, ///< Simple colour, e.g. "Red"
CT_PALETTE, ///< Palette colour, e.g. "color207"
CT_RGB, ///< True colour, e.g. "#11AAFF"
};

/**
* ColorPrefix - Constants for colour prefixes of named colours
*/
Expand All @@ -39,13 +49,25 @@ enum ColorPrefix
COLOR_PREFIX_LIGHT, ///< "light" colour prefix
};

/**
* struct ColorElement - One element of a Colour
*/
struct ColorElement
{
color_t color; ///< Colour
enum ColorType type; ///< Type of Colour
enum ColorPrefix prefix; ///< Optional Colour Modifier
};

/**
* struct AttrColor - A curses colour and its attributes
*/
struct AttrColor
{
struct CursesColor *curses_color; ///< Underlying Curses colour
struct ColorElement fg; ///< Foreground colour
struct ColorElement bg; ///< Background colour
int attrs; ///< Text attributes, e.g. A_BOLD
struct CursesColor *curses_color; ///< Underlying Curses colour
short ref_count; ///< Number of users
TAILQ_ENTRY(AttrColor) entries; ///< Linked list
};
Expand All @@ -61,4 +83,6 @@ struct AttrColor *attr_color_new (void);
void attr_color_list_clear(struct AttrColorList *acl);
struct AttrColor *attr_color_list_find (struct AttrColorList *acl, color_t fg, color_t bg, int attrs);

void attr_color_overwrite(struct AttrColor *ac_old, struct AttrColor *ac_new);

#endif /* MUTT_COLOR_ATTR_H */
6 changes: 4 additions & 2 deletions color/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ void mutt_colors_init(void)
{
color_debug(LL_DEBUG5, "init\n");
color_notify_init();
simple_colors_init();
regex_colors_init();

curses_colors_init();
merged_colors_init();
quoted_colors_init();
regex_colors_init();
simple_colors_init();

start_color();
use_default_colors();
Expand Down
Loading

0 comments on commit b6e3e40

Please sign in to comment.