1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::error::Error;
use std::io::{self, BufRead};
use rargsxd::*;
const OFF: &'static str = "\x1b[m";
pub trait StyledString {
fn get_str(&self) -> String;
fn do_off(&self) -> &str {
if self.get_str().ends_with(OFF) {""}
else {OFF}
}
fn remove_off(&self) -> String {
let mut cur = self.get_str();
while let Some(new) = cur.strip_suffix(OFF) {
cur = new.to_string();
}
cur
}
fn fgblack(&self) -> String {format!("\x1b[30m{}{}", self.get_str(), self.do_off())}
fn fgred(&self) -> String {format!("\x1b[31m{}{}", self.get_str(), self.do_off())}
fn fggreen(&self) -> String {format!("\x1b[32m{}{}", self.get_str(), self.do_off())}
fn fgyellow(&self) -> String {format!("\x1b[33m{}{}", self.get_str(), self.do_off())}
fn fgblue(&self) -> String {format!("\x1b[34m{}{}", self.get_str(), self.do_off())}
fn fgmagenta(&self) -> String {format!("\x1b[35m{}{}", self.get_str(), self.do_off())}
fn fgcyan(&self) -> String {format!("\x1b[36m{}{}", self.get_str(), self.do_off())}
fn fgwhite(&self) -> String {format!("\x1b[37m{}{}", self.get_str(), self.do_off())}
fn bgblack(&self) -> String {format!("\x1b[40m{}{}", self.get_str(), self.do_off())}
fn bgred(&self) -> String {format!("\x1b[41m{}{}", self.get_str(), self.do_off())}
fn bggreen(&self) -> String {format!("\x1b[42m{}{}", self.get_str(), self.do_off())}
fn bgyellow(&self) -> String {format!("\x1b[43m{}{}", self.get_str(), self.do_off())}
fn bgblue(&self) -> String {format!("\x1b[44m{}{}", self.get_str(), self.do_off())}
fn bgmagenta(&self) -> String {format!("\x1b[45m{}{}", self.get_str(), self.do_off())}
fn bgcyan(&self) -> String {format!("\x1b[46m{}{}", self.get_str(), self.do_off())}
fn bgwhite(&self) -> String {format!("\x1b[47m{}{}", self.get_str(), self.do_off())}
fn rgb(&self, rgb: &str) -> String {format!("\x1b[38;2;{}m{}{}", rgb, self.get_str(), self.do_off())}
fn bold(&self) -> String {format!("\x1b[1m{}{}", self.get_str(), self.do_off())}
fn italics(&self) -> String {format!("\x1b[3m{}{}", self.get_str(), self.do_off())}
fn underline(&self) -> String {format!("\x1b[4m{}{}", self.get_str(), self.do_off())}
fn strike(&self) -> String {format!("\x1b[9m{}{}", self.get_str(), self.do_off())}
}
impl StyledString for String {
fn get_str(&self) -> String {
self.clone()
}
}
impl StyledString for &str {
fn get_str(&self) -> String {
self.to_string()
}
}
pub fn strip_styles(input: String) -> String {
let mut output = String::new();
let mut skip = false;
for ch in input.chars() {
if ch == '\x1b' {
skip = true;
continue;
} else if ch == 'm' && skip {
skip = false;
continue;
} else if skip {
continue;
} else {
output.push(ch);
}
}
output
}
pub fn run(args: ArgParser) -> Result<(), Box<dyn Error>> {
let mut modifier = String::new();
if args.get_bool("bold") {modifier = modifier.bold()}
if args.get_bool("italics") {modifier = modifier.italics()}
if args.get_bool("underline") {modifier = modifier.underline()}
if args.get_bool("strike") {modifier = modifier.strike()}
if args.get_str("fg") != "" {
modifier = match args.get_str("fg").as_str() {
"black" => modifier.fgblack(),
"red" => modifier.fgred(),
"green" => modifier.fggreen(),
"yellow" => modifier.fgyellow(),
"blue" => modifier.fgblue(),
"magenta" => modifier.fgmagenta(),
"cyan" => modifier.fgcyan(),
"white" => modifier.fgwhite(),
_ => "".to_string(),
}
}
if args.get_str("bg") != "" {
modifier = match args.get_str("bg").as_str() {
"black" => modifier.bgblack(),
"red" => modifier.bgred(),
"green" => modifier.bggreen(),
"yellow" => modifier.bgyellow(),
"blue" => modifier.bgblue(),
"magenta" => modifier.bgmagenta(),
"cyan" => modifier.bgcyan(),
"white" => modifier.bgwhite(),
_ => "".to_string(),
}
}
if args.get_str("rgb") != "" {modifier = modifier.rgb(&args.get_str("rgb"))}
modifier = modifier.remove_off();
if args.extra.len() > 0 {
let string = args.extra.join(" ");
println!("{}{}", modifier, string);
} else {
let stdin = io::stdin();
for line in stdin.lock().lines() {
let mut string = line? + "\n";
if !args.get_bool("dontforce") {
string = strip_styles(string);
}
print!("{}{}", modifier, string);
}
}
Ok(())
}