vela_protocol/objects/
cli_style.rs1use colored::{ColoredString, Colorize};
17use std::io::IsTerminal;
18use std::sync::Once;
19
20pub const MOSS: (u8, u8, u8) = (0x3F, 0x6B, 0x4E);
23pub const BRASS: (u8, u8, u8) = (0x8A, 0x6A, 0x1F);
24pub const DUST: (u8, u8, u8) = (0x7A, 0x6F, 0x5C);
25pub const MADDER: (u8, u8, u8) = (0x8A, 0x3A, 0x3A);
26pub const SIGNAL: (u8, u8, u8) = (0x3B, 0x5B, 0xDB);
27
28static INIT: Once = Once::new();
31
32pub fn init() {
37 INIT.call_once(|| {
38 let no_color = std::env::var_os("NO_COLOR").is_some();
39 let is_tty = std::io::stdout().is_terminal();
40 if no_color || !is_tty {
41 colored::control::set_override(false);
42 }
43 });
44}
45
46#[must_use]
49pub fn dim(s: &str) -> ColoredString {
50 s.dimmed()
51}
52
53#[must_use]
55pub fn tick_row(width: usize) -> String {
56 let row = "·".repeat(width);
57 format!("{}", row.dimmed())
58}
59
60#[must_use]
66pub fn moss(s: impl AsRef<str>) -> ColoredString {
67 let (r, g, b) = MOSS;
68 s.as_ref().truecolor(r, g, b)
69}
70
71#[must_use]
72pub fn madder(s: impl AsRef<str>) -> ColoredString {
73 let (r, g, b) = MADDER;
74 s.as_ref().truecolor(r, g, b)
75}
76
77#[must_use]
78pub fn brass(s: impl AsRef<str>) -> ColoredString {
79 let (r, g, b) = BRASS;
80 s.as_ref().truecolor(r, g, b)
81}
82
83#[must_use]
84pub fn signal(s: impl AsRef<str>) -> ColoredString {
85 let (r, g, b) = SIGNAL;
86 s.as_ref().truecolor(r, g, b)
87}
88
89#[must_use]
93pub fn chip(label: &str, rgb: (u8, u8, u8)) -> String {
94 let dot = "·".truecolor(rgb.0, rgb.1, rgb.2);
95 let text = label.truecolor(rgb.0, rgb.1, rgb.2);
96 format!("{dot} {text}")
97}
98
99#[must_use]
100pub fn ok(label: &str) -> String {
101 chip(label, MOSS)
102}
103
104#[must_use]
105pub fn warn(label: &str) -> String {
106 chip(label, BRASS)
107}
108
109#[must_use]
110pub fn stale(label: &str) -> String {
111 chip(label, DUST)
112}
113
114#[must_use]
115pub fn lost(label: &str) -> String {
116 chip(label, MADDER)
117}
118
119#[must_use]
121pub fn live(label: &str) -> String {
122 chip(label, SIGNAL)
123}
124
125pub fn header(eyebrow: &str, title: &str) {
131 println!(" {}", eyebrow.dimmed());
132 println!(" {}", title.bold());
133 println!(" {}", tick_row(60));
134}
135
136#[must_use]
138pub fn err_prefix() -> String {
139 let (r, g, b) = MADDER;
140 format!("{}", "err ·".truecolor(r, g, b))
141}