anes/sequences/
attribute.rs

1use std::fmt;
2
3sequence!(
4    /// Resets all attributes.
5    /// 
6    /// This sequence resets all attributes previously set by the:
7    ///
8    /// * [`SetAttribute`](struct.SetAttribute.html)
9    /// * [`SetForegroundColor`](struct.SetBackgroundColor.html)
10    /// * [`SetBackgroundColor`](struct.SetForegroundColor.html)
11    /// 
12    /// # Examples
13    /// 
14    /// ```no_run
15    /// use std::io::{stdout, Write};
16    /// use anes::ResetAttributes;
17    /// 
18    /// let mut stdout = stdout();
19    /// write!(stdout, "{}", ResetAttributes);
20    /// ```
21    struct ResetAttributes => sgr!("0")
22);
23
24/// A display attribute.
25///
26/// This is **NOT** a full ANSI sequence. `Attribute` must be used along with
27/// the [`SetAttribute`](struct.SetAttribute.html).
28///
29/// # Examples
30///
31/// ```no_run
32/// use std::io::{stdout, Write};
33/// use anes::{Attribute, SetAttribute};
34///
35/// let mut stdout = stdout();
36/// write!(stdout, "{}Bold text", SetAttribute(Attribute::Bold));
37/// ```
38#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
39pub enum Attribute {
40    /// Bold (increased) intensity.
41    Bold = 1,
42    /// Faint (decreased) intensity.
43    Faint = 2,
44    /// Normal intensity (turns off `Bold` and/or `Faint`).
45    Normal = 22,
46
47    /// Italic.
48    Italic = 3,
49    /// Turns off `Italic`.
50    ItalicOff = 23,
51
52    /// Underlined text.
53    Underline = 4,
54    /// Turns off `Underline`.
55    UnderlineOff = 24,
56
57    /// Blinking text.
58    Blink = 5,
59    /// Turns off blinking text (`Blink`).
60    BlinkOff = 25,
61
62    /// Reverse foreground & background colors.
63    Reverse = 7,
64    /// Turns off `Reverse`.
65    ReverseOff = 27,
66
67    /// Concealed (hidden).
68    Conceal = 8,
69    /// Turns off `Conceal`.
70    ConcealOff = 28,
71
72    /// Crossed.
73    Crossed = 9,
74    /// Turns off `Crossed`.
75    CrossedOff = 29,
76}
77
78impl fmt::Display for Attribute {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        write!(f, "{}", *self as i32)
81    }
82}
83
84sequence!(
85    /// Sets the display attribute.
86    ///
87    /// See the [`Attribute`](enum.Attribute.html) enum for a list of attributes you can (un)set.
88    ///
89    /// The [`ResetAttributes`](struct.ResetAttributes.html) sequence can be used to turn off all
90    /// attributes.
91    ///
92    /// # Examples
93    ///
94    /// ```no_run
95    /// use std::io::{stdout, Write};
96    /// use anes::{Attribute, SetAttribute};
97    ///
98    /// let mut stdout = stdout();
99    /// write!(stdout, "{}Blinking text", SetAttribute(Attribute::Blink));
100    /// ```
101    struct SetAttribute(Attribute) =>
102    |this, f| write!(f, sgr!("{}"), this.0)
103);
104
105#[cfg(test)]
106test_sequences!(
107    set_attribute(
108        SetAttribute(Attribute::Bold) => "\x1B[1m",
109        SetAttribute(Attribute::Faint) => "\x1B[2m",
110        SetAttribute(Attribute::Normal) => "\x1B[22m",
111
112        SetAttribute(Attribute::Italic) => "\x1B[3m",
113        SetAttribute(Attribute::ItalicOff) => "\x1B[23m",
114
115        SetAttribute(Attribute::Underline) => "\x1B[4m",
116        SetAttribute(Attribute::UnderlineOff) => "\x1B[24m",
117
118        SetAttribute(Attribute::Blink) => "\x1B[5m",
119        SetAttribute(Attribute::BlinkOff) => "\x1B[25m",
120
121        SetAttribute(Attribute::Reverse) => "\x1B[7m",
122        SetAttribute(Attribute::ReverseOff) => "\x1B[27m",
123
124        SetAttribute(Attribute::Conceal) => "\x1B[8m",
125        SetAttribute(Attribute::ConcealOff) => "\x1B[28m",
126
127        SetAttribute(Attribute::Crossed) => "\x1B[9m",
128        SetAttribute(Attribute::CrossedOff) => "\x1B[29m",
129    ),
130    reset_attributes(
131        ResetAttributes => "\x1B[0m",
132    )
133);