1 /* BFU display helpers. */
10 #include "config/options.h"
11 #include "terminal/color.h"
12 #include "terminal/draw.h"
13 #include "terminal/terminal.h"
14 #include "util/color.h"
15 #include "util/hash.h"
18 struct bfu_color_entry
{
19 /* Pointers to the options tree values. */
23 /* The copy of "text" and "background" colors. */
24 struct color_pair colors
;
27 static struct hash
*bfu_colors
= NULL
;
30 get_bfu_color(struct terminal
*term
, unsigned char *stylename
)
32 static enum color_mode last_color_mode
;
33 struct bfu_color_entry
*entry
;
35 struct hash_item
*item
;
36 enum color_mode color_mode
;
38 if (!term
) return NULL
;
40 color_mode
= get_opt_int_tree(term
->spec
, "colors");
43 /* Initialize the style hash. */
44 bfu_colors
= init_hash(8, &strhash
);
45 if (!bfu_colors
) return NULL
;
47 last_color_mode
= color_mode
;
49 } else if (color_mode
!= last_color_mode
) {
52 /* Change mode by emptying the cache so mono/color colors
54 foreach_hash_item (item
, *bfu_colors
, i
) {
55 mem_free_if(item
->value
);
57 del_hash_item(bfu_colors
, item
->next
);
60 last_color_mode
= color_mode
;
63 stylenamelen
= strlen(stylename
);
64 item
= get_hash_item(bfu_colors
, stylename
, stylenamelen
);
65 entry
= item
? item
->value
: NULL
;
70 /* Construct the color entry. */
71 opt
= get_opt_rec_real(config_options
, color_mode
72 ? "ui.colors.color" : "ui.colors.mono");
73 if (!opt
) return NULL
;
75 opt
= get_opt_rec_real(opt
, stylename
);
76 if (!opt
) return NULL
;
78 entry
= mem_calloc(1, sizeof(*entry
));
79 if (!entry
) return NULL
;
81 item
= add_hash_item(bfu_colors
, stylename
, stylenamelen
, entry
);
87 entry
->foreground
= &get_opt_color_tree(opt
, "text");
88 entry
->background
= &get_opt_color_tree(opt
, "background");
91 /* Always update the color pair. */
92 entry
->colors
.background
= *entry
->background
;
93 entry
->colors
.foreground
= *entry
->foreground
;
95 return &entry
->colors
;
101 struct hash_item
*item
;
107 foreach_hash_item (item
, *bfu_colors
, i
) {
108 mem_free_if(item
->value
);
111 free_hash(bfu_colors
);