term_ctrl/
macros.rs

1// Copyright 2019 Lyndon Brown
2//
3// Licensed under the MIT license or the Apache license (version 2.0), at your option. You may not
4// copy, modify, or distribute this file except in compliance with said license. You can find copies
5// of these licenses either in the LICENSE-MIT and LICENSE-APACHE files, or alternatively at
6// <http://opensource.org/licenses/MIT> and <http://www.apache.org/licenses/LICENSE-2.0>
7// respectively.
8
9//! Macros
10
11/// Constructs a control sequence
12///
13/// A sequence can be created with one or more numbers separated by commas.
14///
15/// # Examples:
16///
17/// ```rust
18/// # use term_ctrl::seq;
19/// // Bold
20/// assert_eq!("\u{1B}[1m", seq!(1));
21/// // Red + bold + underline
22/// assert_eq!("\u{1B}[31;1;4m", seq!(31,1,4));
23/// ```
24#[macro_export]
25macro_rules! seq {
26    ($n1:expr, $($n2:expr),*) => { concat!("\u{1B}[", $n1, $(concat!(";", $n2)),*, "m") };
27    ($n1:expr, $($n2:expr,)*) => { concat!("\u{1B}[", $n1, $(concat!(";", $n2)),*, "m") };
28    ($n:expr) => { concat!("\u{1B}[", $n, "m") };
29    () => { "" };
30}
31
32/// Constructs a code set (to be used in a control sequence)
33///
34/// This simply turns a sequence of numbers into a string with a semi-colon separator.
35///
36/// Note that this will actually happily accept strings also, not just numeric literals.
37///
38/// # Examples:
39///
40/// ```rust
41/// # use term_ctrl::{seq, codes};
42/// assert_eq!("1", codes!(1));
43/// assert_eq!("1;2;3", codes!(1, 2, 3));
44/// assert_eq!("1;2;3;4;5", codes!(1, "2;3;4", 5));
45/// // Use in a sequence
46/// assert_eq!("\u{1B}[1;2;3m", seq!(codes!(1,2,3)));
47/// assert_eq!("\u{1B}[1;2;3;4;5;6m", seq!(1,2,codes!(3,4,5),6));
48/// ```
49#[macro_export]
50macro_rules! codes {
51    ($n1:expr, $($n2:expr),*) => { concat!($n1, $(concat!(";", $n2)),*) };
52    ($n1:expr, $($n2:expr,)*) => { concat!($n1, $(concat!(";", $n2)),*) };
53    ($n:expr) => { concat!($n) };
54    () => { "" };
55}
56
57/// Constructs a 256-colour foreground (text) colour code set (to be used in a control sequence)
58///
59/// # Examples:
60///
61/// ```rust
62/// # use term_ctrl::{seq, c256_fg};
63/// // Get the codes
64/// assert_eq!("38;5;238", c256_fg!(238));
65/// // Use in a sequence
66/// assert_eq!("\u{1B}[38;5;238m", seq!(c256_fg!(238)));
67/// ```
68#[macro_export]
69macro_rules! c256_fg {
70    ($col:expr) => { concat!("38;5;", $col) };
71}
72
73/// Constructs a 256-colour background colour code set (to be used in a control sequence)
74///
75/// # Examples:
76///
77/// ```rust
78/// # use term_ctrl::{seq, c256_bg};
79/// // Get the codes
80/// assert_eq!("48;5;238", c256_bg!(238));
81/// // Use in a sequence
82/// assert_eq!("\u{1B}[48;5;238m", seq!(c256_bg!(238)));
83/// ```
84#[macro_export]
85macro_rules! c256_bg {
86    ($col:expr) => { concat!("48;5;", $col) };
87}
88
89/// Constructs an RGB foreground (text) colour code set (to be used in a control sequence)
90///
91/// # Examples:
92///
93/// ```rust
94/// # use term_ctrl::{seq, rgb_fg};
95/// // Get the RGB codes
96/// assert_eq!("38;2;180;15;70", rgb_fg!(180, 15, 70));
97/// // Use in a sequence
98/// assert_eq!("\u{1B}[38;2;180;15;70m", seq!(rgb_fg!(180, 15, 70)));
99/// ```
100#[macro_export]
101macro_rules! rgb_fg {
102    ($red:expr, $green:expr, $blue:expr) => { concat!("38;2;", $red, ";", $green, ";", $blue) };
103}
104
105/// Constructs an RGB background colour code set (to be used in a control sequence)
106///
107/// # Examples:
108///
109/// ```rust
110/// # use term_ctrl::{seq, rgb_bg};
111/// // Get the RGB codes
112/// assert_eq!("48;2;180;15;70", rgb_bg!(180, 15, 70));
113/// // Use in a sequence
114/// assert_eq!("\u{1B}[48;2;180;15;70m", seq!(rgb_bg!(180, 15, 70)));
115/// ```
116#[macro_export]
117macro_rules! rgb_bg {
118    ($red:expr, $green:expr, $blue:expr) => { concat!("48;2;", $red, ";", $green, ";", $blue) };
119}