aboutsummaryrefslogtreecommitdiff
path: root/src/sequences/attribute.rs
blob: b823dc3286dd6fb5505d017f7b3aae5868e2dafe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::fmt;

sequence!(
    /// Resets all attributes.
    ///
    /// This sequence resets all attributes previously set by the:
    ///
    /// * [`SetAttribute`](struct.SetAttribute.html)
    /// * [`SetForegroundColor`](struct.SetBackgroundColor.html)
    /// * [`SetBackgroundColor`](struct.SetForegroundColor.html)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::ResetAttributes;
    ///
    /// let mut stdout = stdout();
    /// write!(stdout, "{}", ResetAttributes);
    /// ```
    struct ResetAttributes => sgr!("0")
);

/// A display attribute.
///
/// This is **NOT** a full ANSI sequence. `Attribute` must be used along with
/// the [`SetAttribute`](struct.SetAttribute.html).
///
/// # Examples
///
/// ```no_run
/// use std::io::{stdout, Write};
/// use anes::{Attribute, SetAttribute};
///
/// let mut stdout = stdout();
/// write!(stdout, "{}Bold text", SetAttribute(Attribute::Bold));
/// ```
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum Attribute {
    /// Bold (increased) intensity.
    Bold = 1,
    /// Faint (decreased) intensity.
    Faint = 2,
    /// Normal intensity (turns off `Bold` and/or `Faint`).
    Normal = 22,

    /// Italic.
    Italic = 3,
    /// Turns off `Italic`.
    ItalicOff = 23,

    /// Underlined text.
    Underline = 4,
    /// Turns off `Underline`.
    UnderlineOff = 24,

    /// Blinking text.
    Blink = 5,
    /// Turns off blinking text (`Blink`).
    BlinkOff = 25,

    /// Reverse foreground & background colors.
    Reverse = 7,
    /// Turns off `Reverse`.
    ReverseOff = 27,

    /// Concealed (hidden).
    Conceal = 8,
    /// Turns off `Conceal`.
    ConcealOff = 28,

    /// Crossed.
    Crossed = 9,
    /// Turns off `Crossed`.
    CrossedOff = 29,
}

impl fmt::Display for Attribute {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", *self as i32)
    }
}

sequence!(
    /// Sets the display attribute.
    ///
    /// See the [`Attribute`](enum.Attribute.html) enum for a list of attributes you can (un)set.
    ///
    /// The [`ResetAttributes`](struct.ResetAttributes.html) sequence can be used to turn off all
    /// attributes.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::io::{stdout, Write};
    /// use anes::{Attribute, SetAttribute};
    ///
    /// let mut stdout = stdout();
    /// write!(stdout, "{}Blinking text", SetAttribute(Attribute::Blink));
    /// ```
    struct SetAttribute(Attribute) =>
    |this, f| write!(f, sgr!("{}"), this.0)
);

#[cfg(test)]
test_sequences!(
    set_attribute(
        SetAttribute(Attribute::Bold) => "\x1B[1m",
        SetAttribute(Attribute::Faint) => "\x1B[2m",
        SetAttribute(Attribute::Normal) => "\x1B[22m",

        SetAttribute(Attribute::Italic) => "\x1B[3m",
        SetAttribute(Attribute::ItalicOff) => "\x1B[23m",

        SetAttribute(Attribute::Underline) => "\x1B[4m",
        SetAttribute(Attribute::UnderlineOff) => "\x1B[24m",

        SetAttribute(Attribute::Blink) => "\x1B[5m",
        SetAttribute(Attribute::BlinkOff) => "\x1B[25m",

        SetAttribute(Attribute::Reverse) => "\x1B[7m",
        SetAttribute(Attribute::ReverseOff) => "\x1B[27m",

        SetAttribute(Attribute::Conceal) => "\x1B[8m",
        SetAttribute(Attribute::ConcealOff) => "\x1B[28m",

        SetAttribute(Attribute::Crossed) => "\x1B[9m",
        SetAttribute(Attribute::CrossedOff) => "\x1B[29m",
    ),
    reset_attributes(
        ResetAttributes => "\x1B[0m",
    )
);