theme_printer.rs

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