robinpath_modules/modules/
color_mod.rs1use robinpath::{RobinPath, Value};
2
3pub fn register(rp: &mut RobinPath) {
4 rp.register_builtin("color.red", |args, _| {
6 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
7 Ok(Value::String(format!("\x1b[31m{}\x1b[0m", s)))
8 });
9
10 rp.register_builtin("color.green", |args, _| {
11 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
12 Ok(Value::String(format!("\x1b[32m{}\x1b[0m", s)))
13 });
14
15 rp.register_builtin("color.blue", |args, _| {
16 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
17 Ok(Value::String(format!("\x1b[34m{}\x1b[0m", s)))
18 });
19
20 rp.register_builtin("color.yellow", |args, _| {
21 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
22 Ok(Value::String(format!("\x1b[33m{}\x1b[0m", s)))
23 });
24
25 rp.register_builtin("color.cyan", |args, _| {
26 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
27 Ok(Value::String(format!("\x1b[36m{}\x1b[0m", s)))
28 });
29
30 rp.register_builtin("color.magenta", |args, _| {
31 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
32 Ok(Value::String(format!("\x1b[35m{}\x1b[0m", s)))
33 });
34
35 rp.register_builtin("color.white", |args, _| {
36 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
37 Ok(Value::String(format!("\x1b[37m{}\x1b[0m", s)))
38 });
39
40 rp.register_builtin("color.gray", |args, _| {
41 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
42 Ok(Value::String(format!("\x1b[90m{}\x1b[0m", s)))
43 });
44
45 rp.register_builtin("color.bold", |args, _| {
47 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
48 Ok(Value::String(format!("\x1b[1m{}\x1b[0m", s)))
49 });
50
51 rp.register_builtin("color.dim", |args, _| {
52 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
53 Ok(Value::String(format!("\x1b[2m{}\x1b[0m", s)))
54 });
55
56 rp.register_builtin("color.italic", |args, _| {
57 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
58 Ok(Value::String(format!("\x1b[3m{}\x1b[0m", s)))
59 });
60
61 rp.register_builtin("color.underline", |args, _| {
62 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
63 Ok(Value::String(format!("\x1b[4m{}\x1b[0m", s)))
64 });
65
66 rp.register_builtin("color.strikethrough", |args, _| {
67 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
68 Ok(Value::String(format!("\x1b[9m{}\x1b[0m", s)))
69 });
70
71 rp.register_builtin("color.bgRed", |args, _| {
73 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
74 Ok(Value::String(format!("\x1b[41m{}\x1b[0m", s)))
75 });
76
77 rp.register_builtin("color.bgGreen", |args, _| {
78 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
79 Ok(Value::String(format!("\x1b[42m{}\x1b[0m", s)))
80 });
81
82 rp.register_builtin("color.bgBlue", |args, _| {
83 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
84 Ok(Value::String(format!("\x1b[44m{}\x1b[0m", s)))
85 });
86
87 rp.register_builtin("color.strip", |args, _| {
89 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
90 let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
92 Ok(Value::String(re.replace_all(&s, "").to_string()))
93 });
94
95 rp.register_builtin("color.rgb", |args, _| {
97 let s = args.first().map(|v| v.to_display_string()).unwrap_or_default();
98 let r = args.get(1).map(|v| v.to_number() as u8).unwrap_or(255);
99 let g = args.get(2).map(|v| v.to_number() as u8).unwrap_or(255);
100 let b = args.get(3).map(|v| v.to_number() as u8).unwrap_or(255);
101 Ok(Value::String(format!(
102 "\x1b[38;2;{};{};{}m{}\x1b[0m",
103 r, g, b, s
104 )))
105 });
106}