irox_tools/
ansi_colors.rs1use 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#[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#[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 Ok(_out)
120 }
121}