Skip to main content

irox_tools/
ansi_colors.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2023 IROX Contributors
3//
4
5//!
6//! List of some basic ANSI Console colors
7//!
8
9use crate::cfg_feature_std;
10
11macro_rules! csi {
12    () => {
13        "\u{1B}["
14    };
15}
16macro_rules! dcs {
17    () => {
18        "\u{1B}P"
19    };
20}
21macro_rules! st {
22    () => {
23        "\u{1B}\\"
24    };
25}
26pub const DCS: &str = dcs!();
27pub const CSI: &str = csi!();
28pub const ST: &str = st!();
29pub const OSC: &str = "\u{1B}]";
30
31macro_rules! sgr {
32    ($($lit:expr)*) => {
33        concat!(csi!(), $($lit,)* "m")
34    };
35}
36
37macro_rules! def_item {
38    ($name:ident,$value:expr) => {
39        pub const $name: &str = $value;
40    };
41}
42
43def_item!(FORMAT_RESET, sgr!(0));
44def_item!(FORMAT_BOLD, sgr!(1));
45
46def_item!(FORMAT_COLOR_FG_BLACK, sgr!(30));
47def_item!(FORMAT_COLOR_FG_RED, sgr!(31));
48def_item!(FORMAT_COLOR_FG_GREEN, sgr!(32));
49def_item!(FORMAT_COLOR_FG_YELLOW, sgr!(33));
50def_item!(FORMAT_COLOR_FG_BLUE, sgr!(34));
51def_item!(FORMAT_COLOR_FG_MAGENTA, sgr!(35));
52def_item!(FORMAT_COLOR_FG_CYAN, sgr!(36));
53def_item!(FORMAT_COLOR_FG_WHITE, sgr!(37));
54def_item!(FORMAT_COLOR_FG_DEFAULT, sgr!(39));
55
56///
57/// Creates a ANSI Terminal Foreground Color Code using the specified RGB values
58/// ```
59/// use irox_tools::format_fg_color;
60/// pub const VARBL : &str = format_fg_color!(0x10,0x10,0x10);
61///
62/// assert_eq!("\u{1B}[38;2;16;16;16m", VARBL);
63/// ```
64#[macro_export]
65macro_rules! format_fg_color {
66    ($red:literal,$green:literal,$blue:literal) => {
67        concat!("\u{1B}[38;2;", $red, ";", $green, ";", $blue, "m")
68    };
69}
70
71def_item!(FORMAT_COLOR_BG_BLACK, sgr!(40));
72def_item!(FORMAT_COLOR_BG_RED, sgr!(41));
73def_item!(FORMAT_COLOR_BG_GREEN, sgr!(42));
74def_item!(FORMAT_COLOR_BG_YELLOW, sgr!(43));
75def_item!(FORMAT_COLOR_BG_BLUE, sgr!(44));
76def_item!(FORMAT_COLOR_BG_MAGENTA, sgr!(45));
77def_item!(FORMAT_COLOR_BG_CYAN, sgr!(46));
78def_item!(FORMAT_COLOR_BG_WHITE, sgr!(47));
79def_item!(FORMAT_COLOR_BG_DEFAULT, sgr!(49));
80
81///
82/// Creates a ANSI Terminal Background Color Code using the specified RGB values
83/// ```
84/// use irox_tools::format_bg_color;
85/// pub const VARBL : &str = format_bg_color!(0x10,0x10,0x10);
86///
87/// assert_eq!("\u{1B}[48;2;16;16;16m", VARBL);
88/// ```
89#[macro_export]
90macro_rules! format_bg_color {
91    ($red:literal,$green:literal,$blue:literal) => {
92        concat!("\u{1B}[48;2;", $red, ";", $green, ";", $blue, "m")
93    };
94}
95
96def_item!(GET_TEXTAREA_SIZE_PIXELS, concat!(csi!(), "14t"));
97def_item!(GET_TEXTAREA_SIZE_CHARS, concat!(csi!(), "18t"));
98def_item!(GET_TERMINFO, concat!(dcs!(), "+q"));
99
100cfg_feature_std! {
101    pub fn get_textarea_size_pixels() -> Result<alloc::vec::Vec<u8>, std::io::Error> {
102        use std::io::{BufRead, Write};
103        let mut stdout = std::io::stdout().lock();
104        let mut stdin = std::io::stdin().lock();
105        stdout.write_all(GET_TEXTAREA_SIZE_CHARS.as_bytes())?;
106        let mut out = alloc::vec::Vec::<u8>::new();
107        stdin.read_until(b't', &mut out)?;
108        Ok(out)
109    }
110    pub fn get_termcap(s: &str) -> Result<alloc::vec::Vec<u8>, std::io::Error> {
111        use std::io::{Write};
112        let mut stdout = std::io::stdout().lock();
113        let _stdin = std::io::stdin().lock();
114        stdout.write_all(GET_TERMINFO.as_bytes())?;
115        stdout.write_all(s.as_bytes())?;
116        stdout.write_all(ST.as_bytes())?;
117        let _out = alloc::vec::Vec::<u8>::new();
118        // stdin.read(&mut out)?;
119        Ok(_out)
120    }
121}