Debian Codegraph @

debian/rust-vendor/crossterm/src/style/styled_content.rs

raw
1 //! This module contains the logic to style some content.
2
3 use std::fmt::{self, Display, Formatter};
4
5 use super::{ContentStyle, PrintStyledContent};
6
7 /// The style with the content to be styled.
8 ///
9 /// # Examples
10 ///
11 /// ```rust
12 /// use crossterm::style::{style, Color, Attribute, Stylize};
13 ///
14 /// let styled = "Hello there"
15 /// .with(Color::Yellow)
16 /// .on(Color::Blue)
17 /// .attribute(Attribute::Bold);
18 ///
19 /// println!("{}", styled);
20 /// ```
21 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
22 pub struct StyledContent<D: Display> {
23 /// The style (colors, content attributes).
24 style: ContentStyle,
25 /// A content to apply the style on.
26 content: D,
27 }
28
29 impl<D: Display> StyledContent<D> {
30 /// Creates a new `StyledContent`.
31 #[inline]
32 pub fn new(style: ContentStyle, content: D) -> StyledContent<D> {
33 StyledContent { style, content }
34 }
35
36 /// Returns the content.
37 #[inline]
38 pub fn content(&self) -> &D {
39 &self.content
40 }
41
42 /// Returns the style.
43 #[inline]
44 pub fn style(&self) -> &ContentStyle {
45 &self.style
46 }
47
48 /// Returns a mutable reference to the style, so that it can be further
49 /// manipulated
50 #[inline]
51 pub fn style_mut(&mut self) -> &mut ContentStyle {
52 &mut self.style
53 }
54 }
55
56 impl<D: Display> AsRef<ContentStyle> for StyledContent<D> {
57 fn as_ref(&self) -> &ContentStyle {
58 &self.style
59 }
60 }
61 impl<D: Display> AsMut<ContentStyle> for StyledContent<D> {
62 fn as_mut(&mut self) -> &mut ContentStyle {
63 &mut self.style
64 }
65 }
66
67 impl<D: Display> Display for StyledContent<D> {
68 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
69 crate::command::execute_fmt(
70 f,
71 PrintStyledContent(StyledContent {
72 style: self.style,
73 content: &self.content,
74 }),
75 )
76 }
77 }