cli_prompts/style/
prompts.rs

1pub mod input {
2    use crate::style::{Color, Formatting, LabelStyle};
3
4    /// Style for the `Input` prompt
5    pub struct InputStyle {
6        /// Style of the prompt itself
7        pub label_style: LabelStyle,
8
9        /// Formatting for the default value text
10        pub default_value_formatting: Formatting,
11
12        /// Formatting for the error message
13        pub error_formatting: Formatting,
14
15        /// Formatting for the user's input
16        pub input_formatting: Formatting,
17
18        /// Formatting for the user's input when the prompt is completed
19        pub submitted_formatting: Formatting,
20
21        /// Formatting for the help message
22        pub help_message_formatting: Formatting,
23    }
24
25    impl Default for InputStyle {
26        fn default() -> Self {
27            InputStyle {
28                label_style: LabelStyle::default(),
29                default_value_formatting: Formatting::default().foreground_color(Color::Grey),
30                error_formatting: Formatting::default().foreground_color(Color::Red),
31                input_formatting: Formatting::default(),
32                submitted_formatting: Formatting::default().foreground_color(Color::Green),
33                help_message_formatting: Formatting::default().foreground_color(Color::DarkGreen),
34            }
35        }
36    }
37
38    impl InputStyle {
39        pub fn label_style(mut self, l: LabelStyle) -> Self {
40            self.label_style = l;
41            self
42        }
43
44        pub fn default_value_formatting(mut self, f: Formatting) -> Self {
45            self.default_value_formatting = f;
46            self
47        }
48
49        pub fn error_formatting(mut self, f: Formatting) -> Self {
50            self.error_formatting = f;
51            self
52        }
53
54        pub fn input_formatting(mut self, f: Formatting) -> Self {
55            self.input_formatting = f;
56            self
57        }
58
59        pub fn submitted_formatting(mut self, f: Formatting) -> Self {
60            self.submitted_formatting = f;
61            self
62        }
63
64        pub fn help_message_formatting(mut self, f: Formatting) -> Self {
65            self.help_message_formatting = f;
66            self
67        }
68    }
69}
70
71pub mod confirmation {
72    use crate::style::{Color, Formatting, LabelStyle};
73
74    /// Style for the `Confirmation` prompt
75    pub struct ConfirmationStyle {
76        /// Style for the prompt itself
77        pub label_style: LabelStyle,
78        
79        /// Style for the user's input
80        pub input_formatting: Formatting,
81
82        /// Formatting for the user's input when the prompt is completed
83        pub submitted_formatting: Formatting,
84    }
85
86    impl Default for ConfirmationStyle {
87        fn default() -> Self {
88            ConfirmationStyle {
89                label_style: LabelStyle::default(),
90                input_formatting: Formatting::default(),
91                submitted_formatting: Formatting::default().foreground_color(Color::Green),
92            }
93        }
94    }
95
96    impl ConfirmationStyle {
97        pub fn label_style(mut self, l: LabelStyle) -> Self {
98            self.label_style = l;
99            self
100        }
101
102        pub fn input_formatting(mut self, f: Formatting) -> Self {
103            self.input_formatting = f;
104            self
105        }
106
107        pub fn submitted_formatting(mut self, f: Formatting) -> Self {
108            self.submitted_formatting = f;
109            self
110        }
111    }
112}
113
114pub mod selection {
115    use crate::{
116        engine::CommandBuffer,
117        style::{Color, Formatting, LabelStyle},
118    };
119
120    /// Marker that is displayed before the option that is currently highlighted
121    pub struct Marker {
122        /// Marker string
123        pub marker: String,
124
125        /// Formatting of the marker
126        pub formatting: Formatting,
127    }
128
129    /// Style for the `Selection` prompt
130    pub struct SelectionStyle {
131        /// Style for the prompt itself
132        pub label_style: LabelStyle,
133
134        /// Formatting for the user's input when the prompt is completed
135        pub submitted_formatting: Formatting,
136
137        /// Formatting for the options
138        pub option_formatting: Formatting,
139
140        /// Formatting for the option that is currently highlighted
141        pub selected_option_formatting: Formatting,
142
143        /// Formatting for the filter string
144        pub filter_formatting: Formatting,
145
146        /// Marker for the option which is not highlighted
147        pub not_selected_marker: Marker,
148
149        /// Marker for the option which is currently highlighted
150        pub selected_marker: Marker,
151    }
152
153    impl Default for SelectionStyle {
154        fn default() -> Self {
155            SelectionStyle {
156                label_style: LabelStyle::default(),
157                submitted_formatting: Formatting::default().foreground_color(Color::Green),
158                option_formatting: Formatting::default(),
159                selected_option_formatting: Formatting::default().bold(),
160                filter_formatting: Formatting::default(),
161                not_selected_marker: Marker {
162                    marker: "  ".into(),
163                    formatting: Formatting::default(),
164                },
165                selected_marker: Marker {
166                    marker: "> ".into(),
167                    formatting: Formatting::default().bold(),
168                },
169            }
170        }
171    }
172
173    impl SelectionStyle {
174        pub fn label_style(mut self, l: LabelStyle) -> Self {
175            self.label_style = l;
176            self
177        }
178
179        pub fn submitted_formatting(mut self, f: Formatting) -> Self {
180            self.submitted_formatting = f;
181            self
182        }
183
184        pub fn option_formatting(mut self, f: Formatting) -> Self {
185            self.option_formatting = f;
186            self
187        }
188
189        pub fn not_selected_marker(mut self, m: Marker) -> Self {
190            self.not_selected_marker = m;
191            self
192        }
193
194        pub fn selected_marker(mut self, m: Marker) -> Self {
195            self.selected_marker = m;
196            self
197        }
198    }
199
200    impl Marker {
201        pub fn print(&self, cmd_buffer: &mut impl CommandBuffer) {
202            cmd_buffer.set_formatting(&self.formatting);
203            cmd_buffer.print(&self.marker);
204            cmd_buffer.reset_formatting();
205        }
206    }
207}
208
209pub mod multiselection {
210    use crate::{
211        engine::CommandBuffer,
212        style::{Color, Formatting, LabelStyle},
213    };
214
215    /// Style for the `Multiselection` prompt
216    pub struct MultiselectionStyle {
217        /// Style for the prompt itself
218        pub label_style: LabelStyle,
219
220        /// Formatting for the user's input when the prompt is completed
221        pub submitted_formatting: Formatting,
222
223        /// Formatting for the filter string
224        pub filter_formatting: Formatting,
225
226
227        /// Formatting for the help message
228        pub help_message_formatting: Formatting,
229
230        /// Marker to use
231        pub marker: Marker,
232
233        /// Formatting for the option that is currently highlighted
234        pub highlighted_option_formatting: Formatting,
235
236        /// Formatting for the option which is not currently highlighted
237        pub normal_option_formatting: Formatting,
238    }
239
240    /// Marker for the options. It consists of the opening and closing symbols and the symbol that
241    /// is put in the middle when the option is selected. Example: 
242    /// Not selected: [ ]
243    /// Selected:     [X]
244    pub struct Marker {
245        /// Opening symbol
246        pub opening_sign: String,
247
248        /// Closing symbol
249        pub closing_sign: String,
250
251        /// The symbol that is put between the above two when the option is selected
252        pub selection_sign: String,
253    }
254
255    impl Default for MultiselectionStyle {
256        fn default() -> Self {
257            MultiselectionStyle {
258                label_style: LabelStyle::default(),
259                submitted_formatting: Formatting::default().foreground_color(Color::Green),
260                filter_formatting: Formatting::default(),
261                help_message_formatting: Formatting::default().foreground_color(Color::DarkGreen),
262                marker: Marker {
263                    opening_sign: "[".into(),
264                    selection_sign: "x".into(),
265                    closing_sign: "]".into(),
266                },
267                highlighted_option_formatting: Formatting::default()
268                    .foreground_color(Color::DarkGreen),
269                normal_option_formatting: Formatting::default(),
270            }
271        }
272    }
273
274    impl Marker {
275        /// Prints the marker to the provided command buffer
276        pub fn print(&self, is_selected: bool, commands: &mut impl CommandBuffer) {
277            let sign = if is_selected {
278                &self.selection_sign
279            } else {
280                " "
281            };
282            commands.print(&format!(
283                "{}{}{}",
284                self.opening_sign, sign, self.closing_sign
285            ));
286        }
287    }
288
289    impl MultiselectionStyle {
290
291        /// Prints the option with the given text along with the marker to the provided command
292        /// buffer
293        pub fn print_option(
294            &self,
295            option_text: &str,
296            is_selected: bool,
297            is_highlighted: bool,
298            commands: &mut impl CommandBuffer,
299        ) {
300            let formatting = if is_highlighted {
301                &self.highlighted_option_formatting
302            } else {
303                &self.normal_option_formatting
304            };
305
306            commands.set_formatting(formatting);
307            self.marker.print(is_selected, commands);
308            commands.print(" ");
309
310            commands.print(option_text);
311            commands.reset_formatting();
312        }
313    }
314}