1use log::error;
2use serde::{Deserialize, Deserializer};
3
4use crate::config::{failure_default, LOG_TARGET_CONFIG};
5use crate::term::color::Rgb;
6
7#[serde(default)]
8#[derive(Deserialize, Clone, Debug, Default, PartialEq, Eq)]
9pub struct Colors {
10 #[serde(deserialize_with = "failure_default")]
11 pub primary: PrimaryColors,
12 #[serde(deserialize_with = "failure_default")]
13 pub cursor: CursorColors,
14 #[serde(deserialize_with = "failure_default")]
15 pub selection: SelectionColors,
16 #[serde(deserialize_with = "failure_default")]
17 normal: NormalColors,
18 #[serde(deserialize_with = "failure_default")]
19 bright: BrightColors,
20 #[serde(deserialize_with = "failure_default")]
21 pub dim: Option<AnsiColors>,
22 #[serde(deserialize_with = "failure_default")]
23 pub indexed_colors: Vec<IndexedColor>,
24}
25
26impl Colors {
27 pub fn normal(&self) -> &AnsiColors {
28 &self.normal.0
29 }
30
31 pub fn bright(&self) -> &AnsiColors {
32 &self.bright.0
33 }
34}
35
36#[serde(default)]
37#[derive(Deserialize, Clone, Default, Debug, PartialEq, Eq)]
38pub struct IndexedColor {
39 #[serde(deserialize_with = "deserialize_color_index")]
40 pub index: u8,
41 #[serde(deserialize_with = "failure_default")]
42 pub color: Rgb,
43}
44
45fn deserialize_color_index<'a, D>(deserializer: D) -> ::std::result::Result<u8, D::Error>
46where
47 D: Deserializer<'a>,
48{
49 #[cfg(not(target_os = "fuchsia"))]
50 let value = serde_yaml::Value::deserialize(deserializer)?;
51 #[cfg(target_os = "fuchsia")]
52 let value = serde_json::Value::deserialize(deserializer)?;
53 match u8::deserialize(value) {
54 Ok(index) => {
55 if index < 16 {
56 error!(
57 target: LOG_TARGET_CONFIG,
58 "Problem with config: indexed_color's index is {}, but a value bigger than 15 \
59 was expected; ignoring setting",
60 index
61 );
62
63 Ok(0)
65 } else {
66 Ok(index)
67 }
68 },
69 Err(err) => {
70 error!(target: LOG_TARGET_CONFIG, "Problem with config: {}; ignoring setting", err);
71
72 Ok(0)
74 },
75 }
76}
77
78#[serde(default)]
79#[derive(Deserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
80pub struct CursorColors {
81 #[serde(deserialize_with = "failure_default")]
82 pub text: Option<Rgb>,
83 #[serde(deserialize_with = "failure_default")]
84 pub cursor: Option<Rgb>,
85}
86
87#[serde(default)]
88#[derive(Deserialize, Debug, Copy, Clone, Default, PartialEq, Eq)]
89pub struct SelectionColors {
90 #[serde(deserialize_with = "failure_default")]
91 pub text: Option<Rgb>,
92 #[serde(deserialize_with = "failure_default")]
93 pub background: Option<Rgb>,
94}
95
96#[serde(default)]
97#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
98pub struct PrimaryColors {
99 #[serde(default = "default_background", deserialize_with = "failure_default")]
100 pub background: Rgb,
101 #[serde(default = "default_foreground", deserialize_with = "failure_default")]
102 pub foreground: Rgb,
103 #[serde(deserialize_with = "failure_default")]
104 pub bright_foreground: Option<Rgb>,
105 #[serde(deserialize_with = "failure_default")]
106 pub dim_foreground: Option<Rgb>,
107}
108
109impl Default for PrimaryColors {
110 fn default() -> Self {
111 PrimaryColors {
112 background: default_background(),
113 foreground: default_foreground(),
114 bright_foreground: Default::default(),
115 dim_foreground: Default::default(),
116 }
117 }
118}
119
120fn default_background() -> Rgb {
121 Rgb { r: 0, g: 0, b: 0 }
122}
123
124fn default_foreground() -> Rgb {
125 Rgb { r: 0xea, g: 0xea, b: 0xea }
126}
127
128#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
130pub struct AnsiColors {
131 #[serde(deserialize_with = "failure_default")]
132 pub black: Rgb,
133 #[serde(deserialize_with = "failure_default")]
134 pub red: Rgb,
135 #[serde(deserialize_with = "failure_default")]
136 pub green: Rgb,
137 #[serde(deserialize_with = "failure_default")]
138 pub yellow: Rgb,
139 #[serde(deserialize_with = "failure_default")]
140 pub blue: Rgb,
141 #[serde(deserialize_with = "failure_default")]
142 pub magenta: Rgb,
143 #[serde(deserialize_with = "failure_default")]
144 pub cyan: Rgb,
145 #[serde(deserialize_with = "failure_default")]
146 pub white: Rgb,
147}
148
149#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
150struct NormalColors(AnsiColors);
151
152impl Default for NormalColors {
153 fn default() -> Self {
154 NormalColors(AnsiColors {
155 black: Rgb { r: 0x00, g: 0x00, b: 0x00 },
156 red: Rgb { r: 0xd5, g: 0x4e, b: 0x53 },
157 green: Rgb { r: 0xb9, g: 0xca, b: 0x4a },
158 yellow: Rgb { r: 0xe6, g: 0xc5, b: 0x47 },
159 blue: Rgb { r: 0x7a, g: 0xa6, b: 0xda },
160 magenta: Rgb { r: 0xc3, g: 0x97, b: 0xd8 },
161 cyan: Rgb { r: 0x70, g: 0xc0, b: 0xba },
162 white: Rgb { r: 0xea, g: 0xea, b: 0xea },
163 })
164 }
165}
166
167#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
168struct BrightColors(AnsiColors);
169
170impl Default for BrightColors {
171 fn default() -> Self {
172 BrightColors(AnsiColors {
173 black: Rgb { r: 0x66, g: 0x66, b: 0x66 },
174 red: Rgb { r: 0xff, g: 0x33, b: 0x34 },
175 green: Rgb { r: 0x9e, g: 0xc4, b: 0x00 },
176 yellow: Rgb { r: 0xe7, g: 0xc5, b: 0x47 },
177 blue: Rgb { r: 0x7a, g: 0xa6, b: 0xda },
178 magenta: Rgb { r: 0xb7, g: 0x7e, b: 0xe0 },
179 cyan: Rgb { r: 0x54, g: 0xce, b: 0xd6 },
180 white: Rgb { r: 0xff, g: 0xff, b: 0xff },
181 })
182 }
183}