fluent_ansi/
effect.rs

1use core::fmt::{Display, Formatter, Result};
2
3use enum_iterator::Sequence;
4
5use crate::{
6    CodeWriter, Style, impl_macros::additive_styling::impl_additive_styling_type,
7    impl_styling_atribute_for, impl_styling_element_for,
8};
9pub use underline::*;
10
11mod underline;
12
13pub(crate) type AllEffects = enum_iterator::All<Effect>;
14
15/// An enumeration of all supported text styling effects.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Sequence)]
17pub enum Effect {
18    /// Bold styling.
19    Bold,
20    /// Faint styling.
21    Faint,
22    /// Italic styling.
23    Italic,
24    /// Solid underline styling.
25    SolidUnderline,
26    /// Curly underline styling.
27    CurlyUnderline,
28    /// Dotted underline styling.
29    DottedUnderline,
30    /// Dashed underline styling.
31    DashedUnderline,
32    /// Blink styling.
33    Blink,
34    /// Reverse video styling.
35    Reverse,
36    /// Conceal (hidden) styling.
37    Conceal,
38    /// Strikethrough styling.
39    Strikethrough,
40    /// Double underline styling.
41    DoubleUnderline,
42    /// Overline styling.
43    Overline,
44}
45
46impl Effect {
47    /// An alias for [`Effect::SolidUnderline`].
48    pub const UNDERLINE: Effect = Effect::SolidUnderline;
49
50    #[must_use]
51    pub(crate) fn all() -> AllEffects {
52        enum_iterator::all()
53    }
54
55    pub(crate) fn write_codes(self, code_writer: &mut CodeWriter) -> Result {
56        let codes = match self {
57            Effect::Bold => "1",
58            Effect::Faint => "2",
59            Effect::Italic => "3",
60            Effect::SolidUnderline => "4",
61            Effect::CurlyUnderline => "4:3",
62            Effect::DottedUnderline => "4:4",
63            Effect::DashedUnderline => "4:5",
64            Effect::Blink => "5",
65            Effect::Reverse => "7",
66            Effect::Conceal => "8",
67            Effect::Strikethrough => "9",
68            Effect::DoubleUnderline => "21",
69            Effect::Overline => "53",
70        };
71        code_writer.write_code(codes)
72    }
73}
74
75impl_additive_styling_type!(Effect {
76    args: [self];
77    to_style: { Style::new().effect(self) }
78});
79
80impl_styling_element_for! { Effect {
81    args: [self, composed_styling];
82    add_to: {
83        composed_styling.set_effect(self, true)
84    }
85}}
86
87impl_styling_atribute_for! { Effect {
88    type Value = bool;
89    args: [self, composed_styling, value];
90
91    set_in: {
92        composed_styling.set_effect(self, value)
93    }
94
95    get_from: {
96        composed_styling.get_effect(self)
97    }
98}}
99
100impl Display for Effect {
101    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
102        self.to_style().fmt(f)
103    }
104}