mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-14 05:53:59 +00:00
Tweak rgb_color_t back down to 4 bytes
This commit is contained in:
parent
6d3f257439
commit
d81bd296fc
2 changed files with 33 additions and 12 deletions
16
color.cpp
16
color.cpp
|
@ -135,7 +135,17 @@ bool rgb_color_t::try_parse_named(const wcstring &str) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
rgb_color_t::rgb_color_t(unsigned char t, unsigned char i) : type(t), data(), flags() {
|
static const wchar_t *name_for_color_idx(unsigned char idx) {
|
||||||
|
size_t max = sizeof named_colors / sizeof *named_colors;
|
||||||
|
for (size_t i=0; i < max; i++) {
|
||||||
|
if (named_colors[i].idx == idx) {
|
||||||
|
return named_colors[i].name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return L"unknown";
|
||||||
|
}
|
||||||
|
|
||||||
|
rgb_color_t::rgb_color_t(unsigned char t, unsigned char i) : type(t), flags(), data() {
|
||||||
data.name_idx = i;
|
data.name_idx = i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,11 +250,13 @@ wcstring rgb_color_t::description() const {
|
||||||
case type_none:
|
case type_none:
|
||||||
return L"none";
|
return L"none";
|
||||||
case type_named:
|
case type_named:
|
||||||
return format_string(L"named(%d)", (int)data.name_idx);
|
return format_string(L"named(%d: %ls)", (int)data.name_idx, name_for_color_idx(data.name_idx));
|
||||||
case type_rgb:
|
case type_rgb:
|
||||||
return format_string(L"rgb(0x%02x%02x%02x)", data.rgb[0], data.rgb[1], data.rgb[2]);
|
return format_string(L"rgb(0x%02x%02x%02x)", data.rgb[0], data.rgb[1], data.rgb[2]);
|
||||||
case type_reset:
|
case type_reset:
|
||||||
return L"reset";
|
return L"reset";
|
||||||
|
case type_normal:
|
||||||
|
return L"normal";
|
||||||
case type_ignore:
|
case type_ignore:
|
||||||
return L"ignore";
|
return L"ignore";
|
||||||
default:
|
default:
|
||||||
|
|
25
color.h
25
color.h
|
@ -9,8 +9,10 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
|
||||||
/* A type that represents a color */
|
/* A type that represents a color. We work hard to keep it at a size of 4 bytes. */
|
||||||
class rgb_color_t {
|
class rgb_color_t {
|
||||||
|
|
||||||
|
/* Types */
|
||||||
enum {
|
enum {
|
||||||
type_none,
|
type_none,
|
||||||
type_named,
|
type_named,
|
||||||
|
@ -19,12 +21,19 @@ class rgb_color_t {
|
||||||
type_reset,
|
type_reset,
|
||||||
type_ignore
|
type_ignore
|
||||||
};
|
};
|
||||||
unsigned char type;
|
unsigned char type:4;
|
||||||
|
|
||||||
|
/* Flags */
|
||||||
|
enum {
|
||||||
|
flag_bold = 1 << 0,
|
||||||
|
flag_underline = 1 << 1
|
||||||
|
};
|
||||||
|
unsigned char flags:4;
|
||||||
|
|
||||||
union {
|
union {
|
||||||
unsigned char name_idx; //0-10
|
unsigned char name_idx; //0-10
|
||||||
unsigned char rgb[3];
|
unsigned char rgb[3];
|
||||||
} data;
|
} data;
|
||||||
unsigned flags;
|
|
||||||
|
|
||||||
/** Try parsing a special color name like "normal" */
|
/** Try parsing a special color name like "normal" */
|
||||||
bool try_parse_special(const wcstring &str);
|
bool try_parse_special(const wcstring &str);
|
||||||
|
@ -41,7 +50,7 @@ class rgb_color_t {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/** Default constructor of type none */
|
/** Default constructor of type none */
|
||||||
explicit rgb_color_t() : type(type_none), data(), flags() {}
|
explicit rgb_color_t() : type(type_none), flags(), data() {}
|
||||||
|
|
||||||
/** Parse a color from a string */
|
/** Parse a color from a string */
|
||||||
explicit rgb_color_t(const wcstring &str);
|
explicit rgb_color_t(const wcstring &str);
|
||||||
|
@ -95,16 +104,16 @@ class rgb_color_t {
|
||||||
unsigned char to_term256_index() const;
|
unsigned char to_term256_index() const;
|
||||||
|
|
||||||
/** Returns whether the color is bold */
|
/** Returns whether the color is bold */
|
||||||
bool is_bold() const { return flags & 1; }
|
bool is_bold() const { return flags & flag_bold; }
|
||||||
|
|
||||||
/** Set whether the color is bold */
|
/** Set whether the color is bold */
|
||||||
void set_bold(bool x) { if (x) flags |= 1; else flags &= ~1; }
|
void set_bold(bool x) { if (x) flags |= flag_bold; else flags &= ~flag_bold; }
|
||||||
|
|
||||||
/** Returns whether the color is underlined */
|
/** Returns whether the color is underlined */
|
||||||
bool is_underline() const { return flags & 2; }
|
bool is_underline() const { return !! (flags & flag_underline); }
|
||||||
|
|
||||||
/** Set whether the color is underlined */
|
/** Set whether the color is underlined */
|
||||||
void set_underline(bool x) { if (x) flags |= 2; else flags &= ~2; }
|
void set_underline(bool x) { if (x) flags |= flag_underline; else flags &= ~flag_underline; }
|
||||||
|
|
||||||
/** Compare two colors for equality */
|
/** Compare two colors for equality */
|
||||||
bool operator==(const rgb_color_t &other) const {
|
bool operator==(const rgb_color_t &other) const {
|
||||||
|
|
Loading…
Reference in a new issue