cli_prompts/style/
formatting.rs1use crate::engine::CommandBuffer;
2
3use super::color::Color;
4
5#[derive(Clone, Copy)]
7pub enum FormattingOption {
8 Reset,
10
11 Bold,
13
14 Italic,
16
17 Underline,
19
20 CrossedOut,
22}
23
24#[derive(Clone)]
29pub struct Formatting {
30 pub foreground_color: Option<Color>,
32
33 pub background_color: Option<Color>,
35
36 pub text_formatting: Vec<FormattingOption>,
38}
39
40impl Default for Formatting {
41 fn default() -> Self {
42 Formatting {
43 foreground_color: None,
44 background_color: None,
45 text_formatting: vec![],
46 }
47 }
48}
49
50impl Formatting {
51
52 pub fn foreground_color(mut self, color: Color) -> Self {
54 self.foreground_color = Some(color);
55 self
56 }
57
58 pub fn background_color(mut self, color: Color) -> Self {
60 self.background_color = Some(color);
61 self
62 }
63
64 pub fn bold(mut self) -> Self {
66 self.text_formatting.push(FormattingOption::Bold);
67 self
68 }
69
70 pub fn italic(mut self) -> Self {
72 self.text_formatting.push(FormattingOption::Italic);
73 self
74 }
75
76 pub fn underline(mut self) -> Self {
78 self.text_formatting.push(FormattingOption::Underline);
79 self
80 }
81
82 pub fn crossed_out(mut self) -> Self {
84 self.text_formatting.push(FormattingOption::CrossedOut);
85 self
86 }
87
88 pub fn reset() -> Self {
90 let mut f = Self::default();
91 f.text_formatting.push(FormattingOption::Reset);
92 f
93 }
94
95 pub fn print(&self, text: impl Into<String>, cmd_buffer: &mut impl CommandBuffer) {
97 cmd_buffer.set_formatting(self);
98 cmd_buffer.print(&text.into());
99 cmd_buffer.reset_formatting();
100 }
101}