anes/
macros.rs

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/// Creates a control sequence.
///
/// This macro prepends provided sequence with the control sequence introducer `ESC [` (`\x1B[`).
///
/// # Examples
///
/// ```
/// use anes::csi;
///
/// assert_eq!(csi!("?1049h"), "\x1B[?1049h");
/// ```
#[macro_export]
macro_rules! csi {
    ($($arg:expr),*) => { concat!("\x1B[", $($arg),*) };
}

/// Creates an escape sequence.
///
/// This macro prepends provided sequence with the `ESC` (`\x1B`) character.
///
/// # Examples
///
/// ```
/// use anes::esc;
///
/// assert_eq!(esc!("7"), "\x1B7");
/// ```
#[macro_export]
macro_rules! esc {
    ($($arg:expr),*) => { concat!("\x1B", $($arg),*) };
}

/// Creates a select graphic rendition sequence.
///
/// This macro prepends provided sequence with the `ESC[` (`\x1B[`) character and appends `m` character.
///
/// Also known as Set Graphics Rendition on Linux.
///
/// # Examples
///
/// ```
/// use anes::sgr;
///
/// assert_eq!(sgr!("0"), "\x1B[0m");
/// ```
#[macro_export]
macro_rules! sgr {
    ($($arg:expr),*) => { concat!("\x1B[", $($arg),* , "m") };
}

/// Creates an ANSI sequence.
///
/// You can use this macro to create your own ANSI sequence. All `anes` sequences are
/// created with this macro.
///
/// # Examples
///
/// An unit struct:
///
/// ```
/// use anes::{esc, sequence};
///
/// sequence!(
///   /// Saves the cursor position.    
///   struct SaveCursorPosition => esc!("7")    
/// );
///
/// assert_eq!(&format!("{}", SaveCursorPosition), "\x1B7");
/// ```
///
/// An enum:
///
/// ```
/// use anes::{csi, sequence};
///
/// sequence!(
///     /// Clears part of the buffer.
///     enum ClearBuffer {
///         /// Clears from the cursor position to end of the screen.
///         Below => csi!("J"),
///         /// Clears from the cursor position to beginning of the screen.
///         Above => csi!("1J"),
///         /// Clears the entire buffer.
///         All => csi!("2J"),
///         /// Clears the entire buffer and all saved lines in the scrollback buffer.
///         SavedLines => csi!("3J"),
///     }
/// );
///
/// assert_eq!(&format!("{}", ClearBuffer::Below), "\x1B[J");
/// assert_eq!(&format!("{}", ClearBuffer::Above), "\x1B[1J");
/// assert_eq!(&format!("{}", ClearBuffer::All), "\x1B[2J");
/// assert_eq!(&format!("{}", ClearBuffer::SavedLines), "\x1B[3J");
/// ```
///
/// A struct:
///
/// ```
/// use anes::{csi, sequence};
///
/// sequence!(
///     /// Moves the cursor to the given location (column, row).
///     ///
///     /// # Notes
///     ///
///     /// Top/left cell is represented as `1, 1`.
///     struct MoveCursorTo(u16, u16) =>
///     |this, f| write!(f, csi!("{};{}H"), this.0, this.1)
/// );
///
/// assert_eq!(&format!("{}", MoveCursorTo(10, 5)), "\x1B[10;5H");
/// ```
#[macro_export]
macro_rules! sequence {
    // Static unit struct
    (
        $(#[$meta:meta])*
        struct $name:ident => $value:expr
    ) => {
        $(#[$meta])*
        #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
        pub struct $name;

        impl ::std::fmt::Display for $name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, $value)
            }
        }
    };
    // Static enum
    (
        $(#[$meta:meta])*
        enum $name:ident {
            $(
                $(#[$variant_meta:meta])*
                $variant:ident => $variant_value:expr
            ),*
            $(,)?
        }
    ) => {
        $(#[$meta])*
        #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
        pub enum $name {
            $(
                $(#[$variant_meta])*
                $variant,
            )*
        }

        impl ::std::fmt::Display for $name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{}", match self {
                    $(
                        $name::$variant => $variant_value,
                    )*
                })
            }
        }
    };
    // Dynamic struct
    (
        $(#[$meta:meta])*
        struct $type:ident(
            $($fields:ty),*
            $(,)?
        )
        =>
        $write:expr
    ) => {
        $(#[$meta])*
        #[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
        pub struct $type($(pub $fields),*);

        impl ::std::fmt::Display for $type {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                let write: &dyn Fn(&Self, &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result =
                    &$write;
                write(self, f)
            }
        }
    };
}

/// Queues ANSI escape sequence(s).
///
/// What does queue mean exactly? All sequences are queued with the
/// `write!($dst, "{}", $sequence)` macro without calling the
/// [`flush`](https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.flush) method.
///
/// Check the [`execute!`](macro.execute.html) macro if you'd like execute them
/// immediately (call the `flush` method after all sequences were queued).
///
/// # Examples
///
/// ```no_run
/// use std::io::{Result, Write};
///
/// use anes::queue;
///
/// fn main() -> Result<()> {
///     let mut stdout = std::io::stdout();
///     queue!(
///         &mut stdout,
///         anes::SaveCursorPosition,
///         anes::MoveCursorTo(10, 10)
///     )?;
///
///     queue!(&mut stdout, anes::RestoreCursorPosition,)?;
///
///     // ANSI sequences are not executed until you flush it!
///     stdout.flush()
/// }
/// ```
#[macro_export]
macro_rules! queue {
    ($dst:expr, $($sequence:expr),* $(,)?) => {{
        let mut error = None;

        $(
            if let Err(e) = write!($dst, "{}", $sequence) {
                error = Some(e);
            }
        )*

        if let Some(error) = error {
            Err(error)
        } else {
            Ok(())
        }
    }}
}

/// Executes ANSI escape sequence(s).
///
/// What does execute mean exactly? All sequences are queued with the
/// `write!($dst, "{}", $sequence)` macro and then the
/// [`flush`](https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.flush) method
/// is called.
///
/// Check the [`queue!`](macro.queue.html) macro if you'd like queue sequences
/// and execute them later.
///
/// ```no_run
/// use std::io::{Result, Write};
///
/// use anes::execute;
///
/// fn main() -> Result<()> {
///     let mut stdout = std::io::stdout();
///     execute!(
///         &mut stdout,
///         anes::SaveCursorPosition,
///         anes::MoveCursorTo(10, 10),
///         anes::RestoreCursorPosition
///     )?;
///     Ok(())
/// }
/// ```
#[macro_export]
macro_rules! execute {
    ($dst:expr, $($sequence:expr),* $(,)?) => {{
        if let Err(e) = $crate::queue!($dst, $($sequence),*) {
            Err(e)
        } else {
            $dst.flush()
        }
    }}
}

#[cfg(test)]
macro_rules! test_sequences {
    (
        $(
            $name:ident(
                $($left:expr => $right:expr),*
                $(,)?
            )
        ),*
        $(,)?
    ) => {
        #[cfg(test)]
        mod tests {
            use super::*;

            $(
                #[test]
                fn $name() {
                    $(
                        assert_eq!(&format!("{}", $left), $right);
                    )*
                }
            )*
        }
    }
}

#[cfg(test)]
mod tests {
    use std::io::{Error, ErrorKind, Write};

    #[test]
    fn csi() {
        assert_eq!(csi!("foo"), "\x1B[foo");
    }

    #[test]
    fn esc() {
        assert_eq!(esc!("bar"), "\x1Bbar");
    }

    #[test]
    fn sgr() {
        assert_eq!(sgr!("bar"), "\x1B[barm");
    }

    #[test]
    fn static_struct_sequence() {
        sequence!(
            struct TestSeq => csi!("foo")
        );

        assert_eq!(&format!("{}", TestSeq), "\x1B[foo");
    }

    #[test]
    fn static_enum_sequence() {
        sequence!(
            enum TestSeq {
                Foo => csi!("foo"),
                Bar => esc!("bar"),
            }
        );

        assert_eq!(&format!("{}", TestSeq::Foo), "\x1B[foo");
        assert_eq!(&format!("{}", TestSeq::Bar), "\x1Bbar");
    }

    #[test]
    fn dynamic_struct_sequence() {
        sequence!(
            struct TestSeq(u16) =>
            |this, f| write!(f, csi!("foo{}bar"), this.0)
        );

        assert_eq!(&format!("{}", TestSeq(10)), "\x1B[foo10bar");
    }

    #[test]
    fn queue_allows_trailing_comma() {
        let mut writer = Writer::default();

        assert!(queue!(&mut writer, "foo",).is_ok());
        assert_eq!(&writer.buffer, "foo");
    }

    #[test]
    fn queue_writes_single_sequence() {
        let mut writer = Writer::default();

        assert!(queue!(&mut writer, "foo").is_ok());
        assert_eq!(&writer.buffer, "foo");
    }

    #[test]
    fn queue_writes_multiple_sequences() {
        let mut writer = Writer::default();

        assert!(queue!(&mut writer, "foo", "bar", "baz").is_ok());
        assert_eq!(&writer.buffer, "foobarbaz");
    }

    #[test]
    fn queue_does_not_flush() {
        let mut writer = Writer::default();

        assert!(queue!(&mut writer, "foo").is_ok());
        assert!(!writer.flushed);
        assert!(writer.flushed_buffer.is_empty());
    }

    #[test]
    fn execute_allows_trailing_comma() {
        let mut writer = Writer::default();

        assert!(execute!(&mut writer, "foo",).is_ok());
        assert_eq!(&writer.flushed_buffer, "foo");
    }

    #[test]
    fn execute_writes_single_sequence() {
        let mut writer = Writer::default();

        assert!(execute!(&mut writer, "foo").is_ok());
        assert_eq!(&writer.flushed_buffer, "foo");
    }

    #[test]
    fn execute_writes_multiple_sequences() {
        let mut writer = Writer::default();

        assert!(execute!(&mut writer, "foo", "bar", "baz").is_ok());
        assert_eq!(&writer.flushed_buffer, "foobarbaz");
    }

    #[test]
    fn execute_does_flush() {
        let mut writer = Writer::default();

        assert!(execute!(&mut writer, "foo").is_ok());
        assert!(writer.flushed);
        assert_eq!(&writer.flushed_buffer, "foo");
        assert!(writer.buffer.is_empty());
    }

    #[derive(Default)]
    struct Writer {
        buffer: String,
        flushed_buffer: String,
        flushed: bool,
    }

    impl Write for Writer {
        fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
            let s = std::str::from_utf8(buf).map_err(|_| ErrorKind::InvalidData)?;

            self.buffer.push_str(s);
            Ok(s.len())
        }

        fn flush(&mut self) -> Result<(), Error> {
            self.flushed_buffer = self.buffer.clone();
            self.buffer = String::new();
            self.flushed = true;
            Ok(())
        }
    }
}