Skip to content

Commit 82fda4a

Browse files
committed
doc(style): improve documentation of Style
1 parent 1d12ddb commit 82fda4a

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/style.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! `style` contains the primitives used to control how your user interface will look.
2+
13
use bitflags::bitflags;
24

35
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -25,6 +27,17 @@ pub enum Color {
2527
}
2628

2729
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+
/// ```
2841
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2942
pub struct Modifier: u16 {
3043
const BOLD = 0b0000_0000_0001;
@@ -39,11 +52,32 @@ bitflags! {
3952
}
4053
}
4154

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+
/// ```
4273
#[derive(Debug, Clone, Copy, PartialEq)]
4374
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4475
pub struct Style {
76+
/// The foreground color.
4577
pub fg: Color,
78+
/// The background color.
4679
pub bg: Color,
80+
/// The emphasis applied to the text graphemes.
4781
pub modifier: Modifier,
4882
}
4983

@@ -61,20 +95,63 @@ impl Style {
6195
modifier: Modifier::empty(),
6296
}
6397
}
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+
/// ```
64112
pub fn reset(&mut self) {
65113
self.fg = Color::Reset;
66114
self.bg = Color::Reset;
67115
self.modifier = Modifier::empty();
68116
}
69117

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+
/// ```
70127
pub const fn fg(mut self, color: Color) -> Style {
71128
self.fg = color;
72129
self
73130
}
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+
/// ```
74141
pub const fn bg(mut self, color: Color) -> Style {
75142
self.bg = color;
76143
self
77144
}
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+
/// ```
78155
pub const fn modifier(mut self, modifier: Modifier) -> Style {
79156
self.modifier = modifier;
80157
self

0 commit comments

Comments
 (0)