diff options
author | Richard Russon <rich@flatcap.org> | 2023-10-16 00:58:07 +0100 |
---|---|---|
committer | Richard Russon <rich@flatcap.org> | 2023-10-18 14:14:48 +0100 |
commit | c3a93354b75f4e2e09605b820c40c40e92591d77 (patch) | |
tree | 9ef304ed918282d009e5031575544236776b2ae7 | |
parent | 39f151c665222ba50bd6916119404fe4203cb834 (diff) |
color: refactor the attribute handling
Refactor the common code out of two attribute handling functions.
Create a Mapping table, AttributeNames.
- parse_attr_spec()
- parse_color_pair()
-rw-r--r-- | color/command.c | 58 | ||||
-rw-r--r-- | color/parse_color.c | 93 |
2 files changed, 57 insertions, 94 deletions
diff --git a/color/command.c b/color/command.c index 574772921..85765183d 100644 --- a/color/command.c +++ b/color/command.c @@ -28,7 +28,6 @@ #include "config.h" #include <stddef.h> -#include <assert.h> #include <stdbool.h> #include <stdint.h> #include "mutt/lib.h" @@ -36,7 +35,6 @@ #include "gui/lib.h" #include "mutt.h" #include "parse/lib.h" -#include "attr.h" #include "color.h" #include "command2.h" #include "curses2.h" @@ -120,62 +118,6 @@ const struct Mapping ComposeColorFields[] = { }; /** - * parse_attr_spec - Parse an attribute description - Implements ::parser_callback_t - @ingroup parser_callback_api - */ -enum CommandResult parse_attr_spec(struct Buffer *buf, struct Buffer *s, color_t *fg, - color_t *bg, int *attrs, struct Buffer *err) -{ - if (fg) - *fg = COLOR_DEFAULT; - if (bg) - *bg = COLOR_DEFAULT; - - if (!MoreArgs(s)) - { - buf_printf(err, _("%s: too few arguments"), "mono"); - return MUTT_CMD_WARNING; - } - - parse_extract_token(buf, s, TOKEN_NO_FLAGS); - - if (mutt_istr_equal("bold", buf->data)) - { - *attrs |= A_BOLD; - } - else if (mutt_istr_equal("italic", buf->data)) - { - *attrs |= A_ITALIC; - } - else if (mutt_istr_equal("none", buf->data)) - { - *attrs = A_NORMAL; // Use '=' to clear other bits - } - else if (mutt_istr_equal("normal", buf->data)) - { - *attrs = A_NORMAL; // Use '=' to clear other bits - } - else if (mutt_istr_equal("reverse", buf->data)) - { - *attrs |= A_REVERSE; - } - else if (mutt_istr_equal("standout", buf->data)) - { - *attrs |= A_STANDOUT; - } - else if (mutt_istr_equal("underline", buf->data)) - { - *attrs |= A_UNDERLINE; - } - else - { - buf_printf(err, _("%s: no such attribute"), buf->data); - return MUTT_CMD_WARNING; - } - - return MUTT_CMD_SUCCESS; -} - -/** * get_colorid_name - Get the name of a color id * @param cid Colour, e.g. #MT_COLOR_HEADER * @param buf Buffer for result diff --git a/color/parse_color.c b/color/parse_color.c index a60ba0605..2cfa393f9 100644 --- a/color/parse_color.c +++ b/color/parse_color.c @@ -60,6 +60,22 @@ const struct Mapping ColorNames[] = { }; /** + * AttributeNames - Mapping of attribute names to their IDs + */ +static struct Mapping AttributeNames[] = { + // clang-format off + { "bold", A_BOLD }, + { "italic", A_ITALIC }, + { "none", A_NORMAL }, + { "normal", A_NORMAL }, + { "reverse", A_REVERSE }, + { "standout", A_STANDOUT }, + { "underline", A_UNDERLINE }, + { NULL, 0 }, + // clang-format on +}; + +/** * parse_color_prefix - Parse a colour prefix, e.g. "bright" * @param[in] s String to parse * @param[out] prefix parsed prefix, see #ColorPrefix @@ -309,48 +325,19 @@ enum CommandResult parse_color_pair(struct Buffer *buf, struct Buffer *s, color_ parse_extract_token(buf, s, TOKEN_COMMENT); - if (mutt_istr_equal("bold", buf->data)) - { - *attrs |= A_BOLD; - color_debug(LL_DEBUG5, "bold\n"); - } - else if (mutt_istr_equal("italic", buf->data)) - { - *attrs |= A_ITALIC; - color_debug(LL_DEBUG5, "italic\n"); - } - else if (mutt_istr_equal("none", buf->data)) - { - *attrs = A_NORMAL; // Use '=' to clear other bits - color_debug(LL_DEBUG5, "none\n"); - } - else if (mutt_istr_equal("normal", buf->data)) - { - *attrs = A_NORMAL; // Use '=' to clear other bits - color_debug(LL_DEBUG5, "normal\n"); - } - else if (mutt_istr_equal("reverse", buf->data)) - { - *attrs |= A_REVERSE; - color_debug(LL_DEBUG5, "reverse\n"); - } - else if (mutt_istr_equal("standout", buf->data)) - { - *attrs |= A_STANDOUT; - color_debug(LL_DEBUG5, "standout\n"); - } - else if (mutt_istr_equal("underline", buf->data)) - { - *attrs |= A_UNDERLINE; - color_debug(LL_DEBUG5, "underline\n"); - } - else + int attr = mutt_map_get_value(buf->data, AttributeNames); + if (attr == -1) { enum CommandResult rc = parse_color_name(buf->data, fg, attrs, true, err); if (rc != MUTT_CMD_SUCCESS) return rc; break; } + + if (attr == A_NORMAL) + *attrs = attr; // Clear all attributes + else + *attrs |= attr; // Merge with other attributes } if (!MoreArgsF(s, TOKEN_COMMENT)) @@ -363,3 +350,37 @@ enum CommandResult parse_color_pair(struct Buffer *buf, struct Buffer *s, color_ return parse_color_name(buf->data, bg, attrs, false, err); } + +/** + * parse_attr_spec - Parse an attribute description - Implements ::parser_callback_t - @ingroup parser_callback_api + */ +enum CommandResult parse_attr_spec(struct Buffer *buf, struct Buffer *s, color_t *fg, + color_t *bg, int *attrs, struct Buffer *err) +{ + if (fg) + *fg = COLOR_DEFAULT; + if (bg) + *bg = COLOR_DEFAULT; + + if (!MoreArgs(s)) + { + buf_printf(err, _("%s: too few arguments"), "mono"); + return MUTT_CMD_WARNING; + } + + parse_extract_token(buf, s, TOKEN_NO_FLAGS); + + int attr = mutt_map_get_value(buf->data, AttributeNames); + if (attr == -1) + { + buf_printf(err, _("%s: no such attribute"), buf->data); + return MUTT_CMD_WARNING; + } + + if (attr == A_NORMAL) + *attrs = attr; // Clear all attributes + else + *attrs |= attr; // Merge with other attributes + + return MUTT_CMD_SUCCESS; +} |