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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use crate::{Arguments, Formatter, Result, Style};

/// A trait for writing or formatting into attributed Unicode-accepting buffers
/// or streams.
///
///
///
/// This trait only accepts UTF-8–encoded data and is not
/// [flushable](stylish::io::Write::flush). If you only want to accept Unicode
/// and you don’t need flushing, you should implement this trait; otherwise you
/// should implement [`stylish::io::Write`].
pub trait Write {
    /// Writes a string slice with a particular [`Style`] into this writer,
    /// returning whether the write succeeded.
    ///
    /// This method can only succeed if the entire string slice was successfully
    /// written, and this method will not return until all data has been
    /// written or an error occurs.
    ///
    /// ```rust
    /// use stylish::{Color, Foreground, Style, Write};
    ///
    /// let blue = Style::default().with(Foreground(Color::Blue));
    ///
    /// let mut s = String::new();
    /// {
    ///     let mut output = stylish::html(&mut s);
    ///     output.write_str("water is ", Style::default())?;
    ///     output.write_str("blue", blue)?;
    ///     output.finish()?;
    /// }
    ///
    /// assert_eq!(s, "water is <span style=color:blue>blue</span>");
    /// # Ok::<(), core::fmt::Error>(())
    /// ```
    fn write_str(&mut self, s: &str, style: Style) -> Result;

    /// Writes a [`char`] with a particular [`Style`] into this writer,
    /// returning whether the write succeeded.
    ///
    /// A single [`char`] may be encoded as more than one byte. This method can
    /// only succeed if the entire byte sequence was successfully written,
    /// and this method will not return until all data has been written or
    /// an error occurs.
    ///
    /// ```rust
    /// use stylish::{Color, Foreground, Style, Write};
    ///
    /// let yellow = Style::default().with(Foreground(Color::Yellow));
    /// let red = Style::default().with(Foreground(Color::Red));
    ///
    /// let mut s = String::new();
    /// {
    ///     let mut output = stylish::html(&mut s);
    ///     output.write_char('⚠', yellow)?;
    ///     output.write_char(' ', Style::default())?;
    ///     output.write_char('⛔', red)?;
    ///     output.finish()?;
    /// }
    ///
    /// assert_eq!(
    ///     s,
    ///     "<span style=color:yellow>⚠</span> <span style=color:red>⛔</span>"
    /// );
    /// # Ok::<(), core::fmt::Error>(())
    /// ```
    fn write_char(&mut self, c: char, style: Style) -> Result {
        self.write_str(c.encode_utf8(&mut [0; 4]), style)
    }

    /// Glue for usage of the [`stylish::write!`] macro with implementors of
    /// this trait.
    ///
    /// This method should generally not be invoked manually, but rather through
    /// the [`stylish::write!`] macro itself.
    ///
    /// ```rust
    /// let mut s = String::new();
    /// {
    ///     let mut output = stylish::html(&mut s);
    ///     output.write_fmt(stylish::format_args!("{:(fg=red)}", '☎'))?;
    ///     output.finish()?;
    /// }
    ///
    /// assert_eq!(s, "<span style=color:red>☎</span>");
    /// # Ok::<(), core::fmt::Error>(())
    /// ```
    fn write_fmt(mut self: &mut Self, args: Arguments<'_>) -> Result {
        Formatter::new(&mut self).write_fmt(args)
    }
}

impl<W: Write + ?Sized> Write for &mut W {
    fn write_str(&mut self, s: &str, style: Style) -> Result {
        (**self).write_str(s, style)
    }

    fn write_char(&mut self, c: char, style: Style) -> Result {
        (**self).write_char(c, style)
    }

    fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
        (**self).write_fmt(args)
    }
}

/// Writes attributed and formatted data into a buffer.
///
/// This macro accepts a 'writer', a format string, and a list of arguments.
/// Arguments will be formatted according to the specified format string and the
/// result will be passed to the writer. The writer may be any value with a
/// `write_fmt` method of the right signature; generally this comes from an
/// implementation of either the [`stylish::Write`] or the
/// [`stylish::io::Write`] trait. The macro returns whatever the `write_fmt`
/// method returns; commonly a [`core::fmt::Result`], or a [`std::io::Result`].
///
/// See [`stylish`] for more information on the format string syntax.
///
/// # Examples
///
/// ```rust
/// let mut w = stylish::html(String::new());
///
/// stylish::write!(&mut w, "test")?;
/// stylish::write!(&mut w, "formatted {:(fg=yellow)}", "arguments")?;
///
/// assert_eq!(
///     w.finish()?,
///     "testformatted <span style=color:yellow>arguments</span>"
/// );
/// # Ok::<(), core::fmt::Error>(())
/// ```
#[macro_export]
macro_rules! write {
    ($dst:expr, $($arg:tt)*) => {
        $dst.write_fmt($crate::format_args!($($arg)*))
    };
}

/// Write attributed and formatted data into a buffer, with a newline appended.
///
/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`)
/// alone (no additional CARRIAGE RETURN (`\r`/`U+000D`).
///
/// For more information, see [`stylish::write!`]. For information on the format
/// string syntax, see [`stylish`].
///
/// # Examples
///
/// ```rust
/// let mut w = stylish::html(String::new());
///
/// stylish::writeln!(&mut w)?;
/// stylish::writeln!(&mut w, "test")?;
/// stylish::writeln!(&mut w, "formatted {:(fg=yellow)}", "arguments")?;
///
/// assert_eq!(w.finish()?, "\ntest\nformatted <span style=color:yellow>arguments</span>\n");
/// # Ok::<(), core::fmt::Error>(())
#[macro_export]
macro_rules! writeln {
    ($dst:expr $(,)?) => {
        $crate::write!($dst, "\n")
    };
    ($dst:expr, $($arg:tt)*) => {
        $dst.write_fmt($crate::𓀄::format_args_nl!(crate=$crate, $($arg)*))
    };
}