forked from clap-rs/clap
/
styled_str.rs
118 lines (100 loc) · 3.05 KB
/
styled_str.rs
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
/// Terminal-styling container
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct StyledStr {
pieces: Vec<(Option<Style>, String)>,
}
impl StyledStr {
/// Create an empty buffer
pub const fn new() -> Self {
Self { pieces: Vec::new() }
}
pub(crate) fn good(&mut self, msg: impl Into<String>) {
self.stylize(Some(Style::Good), msg.into());
}
pub(crate) fn warning(&mut self, msg: impl Into<String>) {
self.stylize(Some(Style::Warning), msg.into());
}
pub(crate) fn error(&mut self, msg: impl Into<String>) {
self.stylize(Some(Style::Error), msg.into());
}
#[allow(dead_code)]
pub(crate) fn hint(&mut self, msg: impl Into<String>) {
self.stylize(Some(Style::Hint), msg.into());
}
pub(crate) fn none(&mut self, msg: impl Into<String>) {
self.stylize(None, msg.into());
}
fn stylize(&mut self, style: Option<Style>, msg: String) {
self.pieces.push((style, msg));
}
#[cfg(feature = "color")]
pub(crate) fn write_colored(&self, buffer: &mut termcolor::Buffer) -> std::io::Result<()> {
use std::io::Write;
use termcolor::WriteColor;
for (style, content) in &self.pieces {
let mut color = termcolor::ColorSpec::new();
match style {
Some(Style::Good) => {
color.set_fg(Some(termcolor::Color::Green));
}
Some(Style::Warning) => {
color.set_fg(Some(termcolor::Color::Yellow));
}
Some(Style::Error) => {
color.set_fg(Some(termcolor::Color::Red));
color.set_bold(true);
}
Some(Style::Hint) => {
color.set_dimmed(true);
}
None => {}
}
buffer.set_color(&color)?;
buffer.write_all(content.as_bytes())?;
buffer.reset()?;
}
Ok(())
}
}
impl From<std::string::String> for StyledStr {
fn from(name: std::string::String) -> Self {
let mut styled = StyledStr::new();
styled.none(name);
styled
}
}
impl From<&'_ std::string::String> for StyledStr {
fn from(name: &'_ std::string::String) -> Self {
let mut styled = StyledStr::new();
styled.none(name);
styled
}
}
impl From<&'static str> for StyledStr {
fn from(name: &'static str) -> Self {
let mut styled = StyledStr::new();
styled.none(name);
styled
}
}
impl From<&'_ &'static str> for StyledStr {
fn from(name: &'_ &'static str) -> Self {
StyledStr::from(*name)
}
}
/// Color-unaware printing. Never uses coloring.
impl std::fmt::Display for StyledStr {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
for (_, content) in &self.pieces {
std::fmt::Display::fmt(content, f)?;
}
Ok(())
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Style {
Good,
Warning,
Error,
Hint,
}