anes

Macro sequence

Source
macro_rules! sequence {
    (
        $(#[$meta:meta])*
        struct $name:ident => $value:expr
    ) => { ... };
    (
        $(#[$meta:meta])*
        enum $name:ident {
            $(
                $(#[$variant_meta:meta])*
                $variant:ident => $variant_value:expr
            ),*
            $(,)?
        }
    ) => { ... };
    (
        $(#[$meta:meta])*
        struct $type:ident(
            $($fields:ty),*
            $(,)?
        )
        =>
        $write:expr
    ) => { ... };
}
Expand description

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");