1
+ //! `style` contains the primitives used to control how your user interface will look.
2
+
1
3
use bitflags:: bitflags;
2
4
3
5
#[ derive( Debug , Clone , Copy , PartialEq ) ]
@@ -25,6 +27,17 @@ pub enum Color {
25
27
}
26
28
27
29
bitflags ! {
30
+ /// Modifier changes the way a piece of text is displayed.
31
+ ///
32
+ /// They are bitflags so they can easily be composed.
33
+ ///
34
+ /// ## Examples
35
+ ///
36
+ /// ```rust
37
+ /// # use tui::style::Modifier;
38
+ ///
39
+ /// let m = Modifier::BOLD | Modifier::ITALIC;
40
+ /// ```
28
41
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
29
42
pub struct Modifier : u16 {
30
43
const BOLD = 0b0000_0000_0001 ;
@@ -39,11 +52,32 @@ bitflags! {
39
52
}
40
53
}
41
54
55
+ /// Style let you control the main characteristics of the displayed elements.
56
+ ///
57
+ /// ## Examples
58
+ ///
59
+ /// ```rust
60
+ /// # use tui::style::{Color, Modifier, Style};
61
+ /// // Using the raw struct initialization:
62
+ /// let s = Style {
63
+ /// fg: Color::Black,
64
+ /// bg: Color::Green,
65
+ /// modifier: Modifier::ITALIC | Modifier::BOLD
66
+ /// };
67
+ /// // Using the provided builder pattern:
68
+ /// let s = Style::default()
69
+ /// .fg(Color::Black)
70
+ /// .bg(Color::Green)
71
+ /// .modifier(Modifier::ITALIC | Modifier::BOLD);
72
+ /// ```
42
73
#[ derive( Debug , Clone , Copy , PartialEq ) ]
43
74
#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
44
75
pub struct Style {
76
+ /// The foreground color.
45
77
pub fg : Color ,
78
+ /// The background color.
46
79
pub bg : Color ,
80
+ /// The emphasis applied to the text graphemes.
47
81
pub modifier : Modifier ,
48
82
}
49
83
@@ -61,20 +95,63 @@ impl Style {
61
95
modifier : Modifier :: empty ( ) ,
62
96
}
63
97
}
98
+
99
+ /// Reinitializes the style properties. Both colors are put back to `Color::Reset` while
100
+ /// all modifiers are cleared.
101
+ ///
102
+ /// ## Examples
103
+ ///
104
+ /// ```rust
105
+ /// # use tui::style::{Color, Modifier, Style};
106
+ /// let mut s = Style::default().fg(Color::Red).bg(Color::Green).modifier(Modifier::BOLD);
107
+ /// s.reset();
108
+ /// assert_eq!(s.fg, Color::Reset);
109
+ /// assert_eq!(s.bg, Color::Reset);
110
+ /// assert_eq!(s.modifier, Modifier::empty());
111
+ /// ```
64
112
pub fn reset ( & mut self ) {
65
113
self . fg = Color :: Reset ;
66
114
self . bg = Color :: Reset ;
67
115
self . modifier = Modifier :: empty ( ) ;
68
116
}
69
117
118
+ /// Changes the foreground color.
119
+ ///
120
+ /// ## Examples
121
+ ///
122
+ /// ```rust
123
+ /// # use tui::style::{Color, Style};
124
+ /// let s = Style::default().fg(Color::Red);
125
+ /// assert_eq!(s.fg, Color::Red);
126
+ /// ```
70
127
pub const fn fg ( mut self , color : Color ) -> Style {
71
128
self . fg = color;
72
129
self
73
130
}
131
+
132
+ /// Changes the background color.
133
+ ///
134
+ /// ## Examples
135
+ ///
136
+ /// ```rust
137
+ /// # use tui::style::{Color, Style};
138
+ /// let s = Style::default().bg(Color::Red);
139
+ /// assert_eq!(s.bg, Color::Red);
140
+ /// ```
74
141
pub const fn bg ( mut self , color : Color ) -> Style {
75
142
self . bg = color;
76
143
self
77
144
}
145
+
146
+ /// Changes the emphasis.
147
+ ///
148
+ /// ## Examples
149
+ ///
150
+ /// ```rust
151
+ /// # use tui::style::{Modifier, Style};
152
+ /// let s = Style::default().modifier(Modifier::BOLD | Modifier::ITALIC);
153
+ /// assert_eq!(s.modifier, Modifier::BOLD | Modifier::ITALIC);
154
+ /// ```
78
155
pub const fn modifier ( mut self , modifier : Modifier ) -> Style {
79
156
self . modifier = modifier;
80
157
self
0 commit comments