#const #colored #sgr #generate #output #terminal #sequences

sgr-const

A convenience macro to generate string sequences for colored and stylized terminal output

1 unstable release

0.1.0 Aug 13, 2020

#2850 in Rust patterns

MPL-2.0 license

8KB

sgr_const!

Documentation

A macro to generate SGR control sequences for colored output in terminal applications.

[dependencies]
sgr-const = "0.1"

Example

sgr_const::sgr_const! {
    STYLE_CYAN = Bold | CyanFg;
    STYLE_NONE = Reset;
}

println!("{}{}{}", STYLE_CYAN, "This text will be cyan if your terminal supports it!", STYLE_NONE);

License

This project is licensed under the Mozilla Public License, Version 2.0. See LICENSE for more information.


lib.rs:

A macro to generate SGR (Set Graphic Rendition) control sequences for ECMA-48 compatible terminals.

Examples

The general syntax is:

[visibity] <name> = <attribute> [| attributes...];

You can specify multiple constants at once, as well as add attributes or doc comments to each:

sgr_const::sgr_const! {
    /// Error styling. Should be flashy.
    STYLE_ERROR = Bold | BlackFg | RedBg;
    STYLE_WARN  = Bold | YellowFg;
    STYLE_INFO  = Bold | CyanFg;
    STYLE_DEBUG = MagentaFg;
    #[allow(unused)]
    STYLE_TRACE = GreenFg;
    STYLE_NONE  = Reset;
}

assert_eq!(STYLE_ERROR, "\x1b[1;30;41m");
assert_eq!(STYLE_WARN,  "\x1b[1;33m");
assert_eq!(STYLE_INFO,  "\x1b[1;36m");
assert_eq!(STYLE_DEBUG, "\x1b[35m");
assert_eq!(STYLE_TRACE, "\x1b[32m");
assert_eq!(STYLE_NONE,  "\x1b[0m");

No runtime deps