cli_prompts/style/
label_style.rs

1use crate::engine::CommandBuffer;
2
3use super::{Color, Formatting};
4
5/// Style for the common part of all prompts: the prompt itself.
6#[derive(Clone)]
7pub struct LabelStyle {
8    prefix: String,
9    prefix_formatting: Formatting,
10    prompt_formatting: Formatting,
11}
12
13impl LabelStyle {
14
15    /// Sets the string that is displayed before the user's input
16    pub fn prefix<S: Into<String>>(mut self, p: S) -> Self {
17        self.prefix = p.into();
18        self
19    }
20
21    /// Sets formatting for the prefix string
22    pub fn prefix_formatting(mut self, f: Formatting) -> Self {
23        self.prefix_formatting = f;
24        self
25    }
26
27    /// Sets formatting for the user input string
28    pub fn prompt_formatting(mut self, f: Formatting) -> Self {
29        self.prompt_formatting = f;
30        self
31    }
32
33    /// Prints the formatted prefix and the input text to the provided command buffer
34    pub fn print(&self, text: impl Into<String>, cmd_buffer: &mut impl CommandBuffer) {
35        cmd_buffer.set_formatting(&self.prefix_formatting);
36        cmd_buffer.print(&self.prefix);
37        cmd_buffer.reset_formatting();
38        cmd_buffer.print(" ");
39        cmd_buffer.set_formatting(&self.prompt_formatting);
40        cmd_buffer.print(&text.into());
41        cmd_buffer.print(":");
42        cmd_buffer.reset_formatting();
43        cmd_buffer.print(" ");
44    }
45}
46
47impl Default for LabelStyle {
48    fn default() -> Self {
49        LabelStyle {
50            prefix: "?".into(),
51            prefix_formatting: Formatting::default().bold().foreground_color(Color::Green),
52            prompt_formatting: Formatting::default().bold(),
53        }
54    }
55}