cargo_swift/console/
theme.rs

1use core::fmt;
2
3use console::Style;
4use dialoguer::theme::{ColorfulTheme, Theme};
5
6pub fn prompt_theme() -> impl Theme {
7    PromptTheme {
8        theme: ColorfulTheme::default(),
9        done: ColorfulTheme {
10            prompt_style: Style::new().for_stderr(),
11            ..ColorfulTheme::default()
12        },
13    }
14}
15
16pub struct PromptTheme<T: Theme> {
17    /// The theme that should be used during the prompt
18    theme: T,
19    /// The theme that should be used after the prompt was finished
20    done: T,
21}
22
23impl<T: Theme> Theme for PromptTheme<T> {
24    /// Formats a prompt.
25    #[inline]
26    fn format_prompt(&self, f: &mut dyn fmt::Write, prompt: &str) -> fmt::Result {
27        self.theme.format_prompt(f, prompt)
28    }
29
30    /// Formats out an error.
31    #[inline]
32    fn format_error(&self, f: &mut dyn fmt::Write, err: &str) -> fmt::Result {
33        self.theme.format_error(f, err)
34    }
35
36    /// Formats a confirm prompt.
37    fn format_confirm_prompt(
38        &self,
39        f: &mut dyn fmt::Write,
40        prompt: &str,
41        default: Option<bool>,
42    ) -> fmt::Result {
43        self.theme.format_confirm_prompt(f, prompt, default)
44    }
45
46    /// Formats a confirm prompt after selection.
47    fn format_confirm_prompt_selection(
48        &self,
49        f: &mut dyn fmt::Write,
50        prompt: &str,
51        selection: Option<bool>,
52    ) -> fmt::Result {
53        self.done
54            .format_confirm_prompt_selection(f, prompt, selection)
55    }
56
57    /// Formats an input prompt.
58    fn format_input_prompt(
59        &self,
60        f: &mut dyn fmt::Write,
61        prompt: &str,
62        default: Option<&str>,
63    ) -> fmt::Result {
64        self.theme.format_input_prompt(f, prompt, default)
65    }
66
67    /// Formats an input prompt after selection.
68    #[inline]
69    fn format_input_prompt_selection(
70        &self,
71        f: &mut dyn fmt::Write,
72        prompt: &str,
73        sel: &str,
74    ) -> fmt::Result {
75        self.done.format_input_prompt_selection(f, prompt, sel)
76    }
77
78    /// Formats a select prompt.
79    #[inline]
80    fn format_select_prompt(&self, f: &mut dyn fmt::Write, prompt: &str) -> fmt::Result {
81        self.theme.format_select_prompt(f, prompt)
82    }
83
84    /// Formats a select prompt after selection.
85    #[inline]
86    fn format_select_prompt_selection(
87        &self,
88        f: &mut dyn fmt::Write,
89        prompt: &str,
90        sel: &str,
91    ) -> fmt::Result {
92        self.done.format_select_prompt_selection(f, prompt, sel)
93    }
94
95    /// Formats a multi select prompt.
96    #[inline]
97    fn format_multi_select_prompt(&self, f: &mut dyn fmt::Write, prompt: &str) -> fmt::Result {
98        self.format_prompt(f, prompt)
99    }
100
101    /// Formats a sort prompt.
102    #[inline]
103    fn format_sort_prompt(&self, f: &mut dyn fmt::Write, prompt: &str) -> fmt::Result {
104        self.format_prompt(f, prompt)
105    }
106
107    /// Formats a multi_select prompt after selection.
108    fn format_multi_select_prompt_selection(
109        &self,
110        f: &mut dyn fmt::Write,
111        prompt: &str,
112        selections: &[&str],
113    ) -> fmt::Result {
114        self.done
115            .format_multi_select_prompt_selection(f, prompt, selections)
116    }
117
118    /// Formats a sort prompt after selection.
119    #[inline]
120    fn format_sort_prompt_selection(
121        &self,
122        f: &mut dyn fmt::Write,
123        prompt: &str,
124        selections: &[&str],
125    ) -> fmt::Result {
126        self.done
127            .format_sort_prompt_selection(f, prompt, selections)
128    }
129
130    /// Formats a select prompt item.
131    fn format_select_prompt_item(
132        &self,
133        f: &mut dyn fmt::Write,
134        text: &str,
135        active: bool,
136    ) -> fmt::Result {
137        self.theme.format_select_prompt_item(f, text, active)
138    }
139
140    /// Formats a multi select prompt item.
141    fn format_multi_select_prompt_item(
142        &self,
143        f: &mut dyn fmt::Write,
144        text: &str,
145        checked: bool,
146        active: bool,
147    ) -> fmt::Result {
148        self.theme
149            .format_multi_select_prompt_item(f, text, checked, active)
150    }
151
152    /// Formats a sort prompt item.
153    fn format_sort_prompt_item(
154        &self,
155        f: &mut dyn fmt::Write,
156        text: &str,
157        picked: bool,
158        active: bool,
159    ) -> fmt::Result {
160        self.theme.format_sort_prompt_item(f, text, picked, active)
161    }
162}