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
|
use std;
use term;
#[derive(Debug,Clone)]
pub enum ShellType {
Unknown,
Bash,
Zsh,
}
pub struct Colors {
color_map: std::collections::HashMap<String, term::color::Color>,
unknown_color: term::color::Color,
shell_type: ShellType,
}
impl ShellType {
pub fn from_str(shell: &str) -> Self {
match shell {
"bash" => ShellType::Bash,
"zsh" => ShellType::Zsh,
_ => panic!("unknown shell {}", shell)
}
}
}
impl Colors {
pub fn new(shell_type: ShellType) -> Colors {
let mut color_map = std::collections::HashMap::new();
color_map.insert(String::from("battery_warn"), term::color::YELLOW);
color_map.insert(String::from("battery_crit"), term::color::RED);
color_map.insert(String::from("battery_emerg"), term::color::BRIGHT_RED);
color_map.insert(String::from("battery_full"), term::color::GREEN);
color_map.insert(String::from("battery_charging"), term::color::GREEN);
color_map.insert(String::from("default"), term::color::BRIGHT_BLACK);
color_map.insert(String::from("error"), term::color::RED);
let unknown_color = term::color::YELLOW;
Colors {
color_map: color_map,
unknown_color: unknown_color,
shell_type: shell_type,
}
}
pub fn print(&self, color: &str, text: &str) {
let color = self.color_map.get(color);
self.print_with_color(color, text);
}
pub fn pad(&self, len: usize) {
print!("{}", " ".repeat(len));
}
pub fn newline(&self) {
self.print_wrapped(|| {
print!("{}", "\n");
});
}
pub fn print_host(&self, host: &Option<String>, text: &str) {
let color = host
.clone()
.and_then(|hostname| {
self.color_map.get(&format!("host_{}", hostname))
});
self.print_with_color(color, text);
}
pub fn print_user(&self, user: &Option<String>, text: &str) {
let color = user
.clone()
.and_then(|username| {
self.color_map.get(&format!("user_{}", username))
});
self.print_with_color(color, text);
}
fn print_with_color(&self, color: Option<&term::color::Color>, text: &str) {
let mut t = term::stdout().unwrap();
self.print_color(&mut *t, color);
write!(t, "{}", text).unwrap();
t.reset().unwrap();
}
fn print_color(&self, t: &mut term::StdoutTerminal, color: Option<&term::color::Color>) {
self.print_wrapped(|| {
let real_color = *color.unwrap_or(&self.unknown_color);
t.fg(real_color).unwrap();
match real_color {
term::color::BRIGHT_BLACK
| term::color::BRIGHT_BLUE
| term::color::BRIGHT_CYAN
| term::color::BRIGHT_GREEN
| term::color::BRIGHT_MAGENTA
| term::color::BRIGHT_RED
| term::color::BRIGHT_WHITE
| term::color::BRIGHT_YELLOW => {
t.attr(term::Attr::Bold).unwrap()
},
_ => {},
}
})
}
fn print_wrapped<T>(&self, printer: T)
where T: FnOnce()
{
match self.shell_type {
ShellType::Bash => { print!("{}", "\\["); },
ShellType::Zsh => { print!("{}", "%{"); },
_ => {},
}
printer();
match self.shell_type {
ShellType::Bash => { print!("{}", "\\]"); },
ShellType::Zsh => { print!("{}", "%}"); },
_ => {},
}
}
}
|