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
134
135
136
137
138
139
140
141
142
143
use std::fmt::Display;
use std::io::stdout;
use std::io::Write;

/// These are all the attributes which **could** be apply to font.
/// There are few things to note
/// - Not all attributes are supported, some of them are only supported on Windows some only on Unix,
/// and some are only very rarely supported.
/// - I got those attributes, descriptions, supportability from here: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
/// - Take note of the fact that when running your program cross-platform that some attributes might not work because of their support.
/// - When an attribute is not supported nothing will happen with the terminal state.
///
/// # Example
/// You can use an attribute in a write statement to apply the attribute to the terminal output.
///
/// ```rust
/// println!(
///     "{} Underlined {} No Underline",
///     Attribute::Underlined,
///     Attribute::NoUnderline
/// );
/// ```
///
/// You can also call attribute functions on a `&'static str`:
/// ```rust
/// use crossterm(_style)::Colorizer;
///
/// println!("{}", style("Bold text").bold());
/// println!("{}", style("Underlined text").underlined());
/// println!("{}", style("Negative text").negative());
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub enum Attribute {
    /// All attributes off
    /// [info]: This will reset all current set attributes.
    /// [Supportability]: Windows, UNIX.
    Reset = 0,
    /// Increased Intensity
    /// [info]: This will increase the font sensitivity also known as bold.
    /// [Supportability]: Windows, UNIX.
    Bold = 1,
    /// Decreased Intensity
    /// [info]: This will decrease the font sensitivity also known as bold.
    /// [Supportability]: Windows, UNIX.
    Dim = 2,
    /// Italic Text
    /// [info]: This will make the font italic.
    /// [Supportability]: Not widely supported, sometimes treated as inverse.
    Italic = 3,
    /// This will draw a line under the font.
    /// [info]: An line under a word, especially in order to show its importance.
    /// [Supportability]: Windows, UNIX
    Underlined = 4,
    /// Slow Blinking Text
    /// [info]: Blink Less than 150 per minute.
    /// [Supportability]: UNIX
    SlowBlink = 5,
    /// Slow Blinking Text
    /// [info]: MS-DOS ANSI.SYS; 150+ per minute;
    /// [Supportability]: Not widely supported
    RapidBlink = 6,
    /// Swap foreground and background colors
    /// [info]: swap foreground and background colors
    /// [Supportability]: Windows, UNIX
    Reverse = 7,
    /// Hide font
    /// [info]:
    /// - This will make the font hidden.
    /// - Also known as 'Conceal'
    /// [Supportability]: Windows, UNIX
    Hidden = 8,
    /// Cross-out font
    /// [info]: Characters legible, but marked for deletion.
    /// [Supportability]: UNIX
    CrossedOut = 9,
    /// The Fraktur is a typeface belonging to the group of Gothic typefaces.
    /// [info]: https://nl.wikipedia.org/wiki/Fraktur
    /// [Supportability]: Rarely supported
    Fraktur = 20,
    /// This will turn off the bold attribute.
    /// [info]:
    /// - Double-underline per ECMA-48.
    /// - WikiPedia: https://en.wikipedia.org/wiki/Talk:ANSI_escape_code#SGR_21%E2%80%94%60Bold_off%60_not_widely_supported
    /// - Opposite of `Bold`(1)
    /// [Supportability]: not widely supported
    NoBold = 21,
    /// This will turn off the underline attribute.
    /// [info]:
    /// - Not singly or doubly underlined will be turned off.
    /// - Opposite of `Underlined.`(4)
    /// [Supportability]: Windows, UNIX
    NoUnderline = 24,
    /// This will turn off the blinking attribute
    /// [info]: Opposite of `Slow and Rapid blink.`(5,6)
    /// [Supportability]: Unknown
    NoBlink = 25,
    /// This will turn off the reverse attribute.
    /// [info]: Opposite of `Reverse`(7)
    /// [Supportability]: Windows, unknown
    NoInverse = 27,
    /// This will make the font visible.
    /// [info]: Opposite of `Hidden`(8)
    /// [Supportability]: Unknown
    NoHidden = 28,
    /// This will turn off the crossed out attribute.
    /// [info]: Opposite of `CrossedOut`(9)
    /// [Supportability]: Not widely supported
    NotCrossedOut = 29,
    /// This will reset the foreground color to default.
    /// [info]: Implementation defined (according to standard)
    /// [Supportability]: Unknown
    DefaultForegroundColor = 48,
    /// This will reset the background color to default.
    /// [info]: Implementation defined (according to standard)
    /// [Supportability]: Unknown
    DefaultBackgroundColor = 49,
    /// Framed font.
    /// [Supportability]: Not widely supported
    Framed = 51,
    /// This will turn on the encircled attribute.
    Encircled = 52,
    /// This will draw a line at the top of the font.
    /// [info]: Implementation defined (according to standard)
    /// [Supportability]: Unknown
    OverLined = 53,
    /// This will turn off the framed or encircled attribute.
    NotFramedOrEncircled = 54,
    /// This will turn off the overLined attribute.
    /// [info]: Opposite of `OverLined`(7)
    /// [Supportability]: Windows, unknown
    NotOverLined = 55,

    #[doc(hidden)]
    __Nonexhaustive,
}

impl Display for Attribute {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
        write!(f, "{}", format!(csi!("{}m"), *self as i16))?;
        stdout().flush();
        Ok(())
    }
}