theme_printer.rs

  1use std::fmt::{self, Debug};
  2
  3use gpui::{Hsla, Rgba};
  4use theme::{
  5    Appearance, PlayerColor, PlayerColors, StatusColors, StatusColorsRefinement, SyntaxTheme,
  6    SystemColors, ThemeColorsRefinement, UserTheme, UserThemeFamily, UserThemeStylesRefinement,
  7};
  8
  9struct RawSyntaxPrinter<'a>(&'a str);
 10
 11impl<'a> Debug for RawSyntaxPrinter<'a> {
 12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 13        write!(f, "{}", self.0)
 14    }
 15}
 16
 17struct HslaPrinter(Hsla);
 18
 19impl Debug for HslaPrinter {
 20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 21        write!(f, "{:?}", IntoPrinter(&Rgba::from(self.0)))
 22    }
 23}
 24
 25struct IntoPrinter<'a, D: Debug>(&'a D);
 26
 27impl<'a, D: Debug> Debug for IntoPrinter<'a, D> {
 28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 29        write!(f, "{:?}.into()", self.0)
 30    }
 31}
 32
 33pub struct VecPrinter<'a, T>(&'a Vec<T>);
 34
 35impl<'a, T: Debug> Debug for VecPrinter<'a, T> {
 36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 37        write!(f, "vec!{:?}", &self.0)
 38    }
 39}
 40
 41pub struct UserThemeFamilyPrinter(UserThemeFamily);
 42
 43impl UserThemeFamilyPrinter {
 44    pub fn new(theme_family: UserThemeFamily) -> Self {
 45        Self(theme_family)
 46    }
 47}
 48
 49impl Debug for UserThemeFamilyPrinter {
 50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 51        f.debug_struct("UserThemeFamily")
 52            .field("name", &IntoPrinter(&self.0.name))
 53            .field("author", &IntoPrinter(&self.0.author))
 54            .field(
 55                "themes",
 56                &VecPrinter(
 57                    &self
 58                        .0
 59                        .themes
 60                        .iter()
 61                        .map(|theme| UserThemePrinter(theme))
 62                        .collect(),
 63                ),
 64            )
 65            .finish()
 66    }
 67}
 68
 69pub struct UserThemePrinter<'a>(&'a UserTheme);
 70
 71impl<'a> Debug for UserThemePrinter<'a> {
 72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 73        f.debug_struct("UserTheme")
 74            .field("name", &IntoPrinter(&self.0.name))
 75            .field("appearance", &AppearancePrinter(self.0.appearance))
 76            .field("styles", &UserThemeStylesRefinementPrinter(&self.0.styles))
 77            .finish()
 78    }
 79}
 80
 81pub struct AppearancePrinter(Appearance);
 82
 83impl Debug for AppearancePrinter {
 84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 85        write!(f, "Appearance::{:?}", self.0)
 86    }
 87}
 88
 89pub struct UserThemeStylesRefinementPrinter<'a>(&'a UserThemeStylesRefinement);
 90
 91impl<'a> Debug for UserThemeStylesRefinementPrinter<'a> {
 92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 93        f.debug_struct("UserThemeStylesRefinement")
 94            .field("colors", &ThemeColorsRefinementPrinter(&self.0.colors))
 95            .field("status", &StatusColorsRefinementPrinter(&self.0.status))
 96            .finish()
 97    }
 98}
 99
100pub struct SystemColorsPrinter<'a>(&'a SystemColors);
101
102impl<'a> Debug for SystemColorsPrinter<'a> {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        f.debug_struct("SystemColors")
105            .field("transparent", &HslaPrinter(self.0.transparent))
106            .field(
107                "mac_os_traffic_light_red",
108                &HslaPrinter(self.0.mac_os_traffic_light_red),
109            )
110            .field(
111                "mac_os_traffic_light_yellow",
112                &HslaPrinter(self.0.mac_os_traffic_light_yellow),
113            )
114            .field(
115                "mac_os_traffic_light_green",
116                &HslaPrinter(self.0.mac_os_traffic_light_green),
117            )
118            .finish()
119    }
120}
121
122pub struct ThemeColorsRefinementPrinter<'a>(&'a ThemeColorsRefinement);
123
124impl<'a> Debug for ThemeColorsRefinementPrinter<'a> {
125    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126        let theme_colors = vec![
127            ("border", self.0.border),
128            ("border_variant", self.0.border_variant),
129            ("border_focused", self.0.border_focused),
130            ("border_selected", self.0.border_selected),
131            ("border_transparent", self.0.border_transparent),
132            ("border_disabled", self.0.border_disabled),
133            (
134                "elevated_surface_background",
135                self.0.elevated_surface_background,
136            ),
137            ("surface_background", self.0.surface_background),
138            ("background", self.0.background),
139            ("element_background", self.0.element_background),
140            ("element_hover", self.0.element_hover),
141            ("element_active", self.0.element_active),
142            ("element_selected", self.0.element_selected),
143            ("element_disabled", self.0.element_disabled),
144            ("drop_target_background", self.0.drop_target_background),
145            ("ghost_element_background", self.0.ghost_element_background),
146            ("ghost_element_hover", self.0.ghost_element_hover),
147            ("ghost_element_active", self.0.ghost_element_active),
148            ("ghost_element_selected", self.0.ghost_element_selected),
149            ("ghost_element_disabled", self.0.ghost_element_disabled),
150            ("text", self.0.text),
151            ("text_muted", self.0.text_muted),
152            ("text_placeholder", self.0.text_placeholder),
153            ("text_disabled", self.0.text_disabled),
154            ("text_accent", self.0.text_accent),
155            ("icon", self.0.icon),
156            ("icon_muted", self.0.icon_muted),
157            ("icon_disabled", self.0.icon_disabled),
158            ("icon_placeholder", self.0.icon_placeholder),
159            ("icon_accent", self.0.icon_accent),
160            ("status_bar_background", self.0.status_bar_background),
161            ("title_bar_background", self.0.title_bar_background),
162            ("toolbar_background", self.0.toolbar_background),
163            ("tab_bar_background", self.0.tab_bar_background),
164            ("tab_inactive_background", self.0.tab_inactive_background),
165            ("tab_active_background", self.0.tab_active_background),
166            ("editor_background", self.0.editor_background),
167            ("editor_gutter_background", self.0.editor_gutter_background),
168            (
169                "editor_subheader_background",
170                self.0.editor_subheader_background,
171            ),
172            (
173                "editor_active_line_background",
174                self.0.editor_active_line_background,
175            ),
176            (
177                "editor_highlighted_line_background",
178                self.0.editor_highlighted_line_background,
179            ),
180            ("editor_line_number", self.0.editor_line_number),
181            (
182                "editor_active_line_number",
183                self.0.editor_active_line_number,
184            ),
185            ("editor_invisible", self.0.editor_invisible),
186            ("editor_wrap_guide", self.0.editor_wrap_guide),
187            ("editor_active_wrap_guide", self.0.editor_active_wrap_guide),
188            (
189                "editor_document_highlight_read_background",
190                self.0.editor_document_highlight_read_background,
191            ),
192            (
193                "editor_document_highlight_write_background",
194                self.0.editor_document_highlight_write_background,
195            ),
196            ("terminal_background", self.0.terminal_background),
197            (
198                "terminal_ansi_bright_black",
199                self.0.terminal_ansi_bright_black,
200            ),
201            ("terminal_ansi_bright_red", self.0.terminal_ansi_bright_red),
202            (
203                "terminal_ansi_bright_green",
204                self.0.terminal_ansi_bright_green,
205            ),
206            (
207                "terminal_ansi_bright_yellow",
208                self.0.terminal_ansi_bright_yellow,
209            ),
210            (
211                "terminal_ansi_bright_blue",
212                self.0.terminal_ansi_bright_blue,
213            ),
214            (
215                "terminal_ansi_bright_magenta",
216                self.0.terminal_ansi_bright_magenta,
217            ),
218            (
219                "terminal_ansi_bright_cyan",
220                self.0.terminal_ansi_bright_cyan,
221            ),
222            (
223                "terminal_ansi_bright_white",
224                self.0.terminal_ansi_bright_white,
225            ),
226            ("terminal_ansi_black", self.0.terminal_ansi_black),
227            ("terminal_ansi_red", self.0.terminal_ansi_red),
228            ("terminal_ansi_green", self.0.terminal_ansi_green),
229            ("terminal_ansi_yellow", self.0.terminal_ansi_yellow),
230            ("terminal_ansi_blue", self.0.terminal_ansi_blue),
231            ("terminal_ansi_magenta", self.0.terminal_ansi_magenta),
232            ("terminal_ansi_cyan", self.0.terminal_ansi_cyan),
233            ("terminal_ansi_white", self.0.terminal_ansi_white),
234        ];
235
236        f.write_str("ThemeColorsRefinement {")?;
237
238        for (color_name, color) in theme_colors {
239            if let Some(color) = color {
240                f.write_str(color_name)?;
241                f.write_str(": ")?;
242                f.write_str("Some(")?;
243                HslaPrinter(color).fmt(f)?;
244                f.write_str(")")?;
245                f.write_str(",")?;
246            }
247        }
248
249        f.write_str("..Default::default()")?;
250        f.write_str("}")
251    }
252}
253
254pub struct StatusColorsRefinementPrinter<'a>(&'a StatusColorsRefinement);
255
256impl<'a> Debug for StatusColorsRefinementPrinter<'a> {
257    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258        let status_colors = vec![
259            ("conflict", self.0.conflict),
260            ("created", self.0.created),
261            ("deleted", self.0.deleted),
262            ("error", self.0.error),
263            ("hidden", self.0.hidden),
264            ("ignored", self.0.ignored),
265            ("info", self.0.info),
266            ("modified", self.0.modified),
267            ("renamed", self.0.renamed),
268            ("success", self.0.success),
269            ("warning", self.0.warning),
270        ];
271
272        f.write_str("StatusColorsRefinement {")?;
273
274        for (color_name, color) in status_colors {
275            if let Some(color) = color {
276                f.write_str(color_name)?;
277                f.write_str(": ")?;
278                f.write_str("Some(")?;
279                HslaPrinter(color).fmt(f)?;
280                f.write_str(")")?;
281                f.write_str(",")?;
282            }
283        }
284
285        f.write_str("..Default::default()")?;
286        f.write_str("}")
287    }
288}
289
290pub struct PlayerColorsPrinter<'a>(&'a PlayerColors);
291
292impl<'a> Debug for PlayerColorsPrinter<'a> {
293    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
294        f.debug_tuple("PlayerColors")
295            .field(&VecPrinter(
296                &self
297                    .0
298                     .0
299                    .iter()
300                    .map(|player_color| PlayerColorPrinter(player_color))
301                    .collect(),
302            ))
303            .finish()
304    }
305}
306
307pub struct PlayerColorPrinter<'a>(&'a PlayerColor);
308
309impl<'a> Debug for PlayerColorPrinter<'a> {
310    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311        f.debug_struct("PlayerColor")
312            .field("cursor", &HslaPrinter(self.0.cursor))
313            .field("background", &HslaPrinter(self.0.background))
314            .field("selection", &HslaPrinter(self.0.selection))
315            .finish()
316    }
317}
318
319pub struct SyntaxThemePrinter<'a>(&'a SyntaxTheme);
320
321impl<'a> Debug for SyntaxThemePrinter<'a> {
322    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
323        f.debug_struct("SyntaxTheme")
324            .field(
325                "highlights",
326                &VecPrinter(
327                    &self
328                        .0
329                        .highlights
330                        .iter()
331                        .map(|(token, highlight)| {
332                            (IntoPrinter(token), HslaPrinter(highlight.color.unwrap()))
333                        })
334                        .collect(),
335                ),
336            )
337            .finish()
338    }
339}