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
use crate::{Ansi, Toggle};
use crate::introspect::Attr;
use std::fmt;
/// Represents the control sequences, named Select Graphic Rendition (SGR),
/// that are used to enable various effects (e.g. italic) on ANSI terminals.
///
/// `Effect`s can be combined arbitrarily.
///
/// Note: this enum is designed to be *immutable* and *const*
#[derive(PartialEq, Eq, Clone, Copy, fmt::Debug)]
#[non_exhaustive]
pub enum Effect {
/// Effect with SGR parameter `1`
Bold,
/// Effect with SGR parameter `2`
Faint,
/// Effect with SGR parameter `3`
Italic,
/// Effect with SGR parameter `4`
Underline,
/// Effect with SGR parameter `5`
Blink,
/// Effect with SGR parameter `7`
Reverse,
/// Effect with SGR parameter `8`
Hidden,
/// Effect with SGR parameter `9`
Strike,
}
impl Effect {
const VARIANTS: &'static[Effect] = &[
Self::Bold,
Self::Faint,
Self::Italic,
Self::Underline,
Self::Blink,
Self::Reverse,
Self::Hidden,
Self::Strike,
];
/// Get all `Effect`s, which facilitates iterating.
pub const fn all() -> &'static[Effect] { Self::VARIANTS }
/// Creates an [`Attr`] with this `Effect`
#[inline]
pub const fn attr(&self) -> Attr<Effect> { Attr::new_effect(*self, Toggle::Set) }
/// Creates an [`Ansi`] style with the corresponding `reset` code for this `Effect`,
/// as follows:
///
/// | ANSI effect | SGR parameter |
/// |----------------------------------|--------------:|
/// | [`Bold`](Effect::Bold) | `22` |
/// | [`Faint`](Effect::Faint) | `22` |
/// | [`Italic`](Effect::Italic) | `23` |
/// | [`Underline`](Effect::Underline) | `24` |
/// | [`Blink`](Effect::Blink) | `25` |
/// | [`Reverse`](Effect::Reverse) | `27` |
/// | [`Hidden`](Effect::Hidden) | `28` |
/// | [`Strike`](Effect::Strike) | `29` |
///
/// *Note: `Bold.not()` and `Faint.not()` have the same parameter*
#[inline]
pub const fn not(&self) -> Ansi { Ansi::from_effect(*self, Toggle::Reset) }
/// Creates an [`Ansi`] style with this `Effect` set to [`only`](Ansi::only()).
#[inline]
pub const fn only(&self) -> Ansi { self.ansi().only() }
/// Creates an [`Ansi`] style with this `Effect` set to [`important`](Ansi::important()).
#[inline]
pub const fn important(&self) -> Ansi { self.ansi().important() }
/// Used by the `styled_*!` macros to coerce a style argument to an [`Ansi`] instance.
#[inline]
pub const fn ansi(&self) -> Ansi { Ansi::from_effect(*self, Toggle::Set) }
}
impl fmt::Display for Effect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.ansi(), f) }
}