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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#![crate_name = "shell"]
extern crate term;
use std::fmt;
use std::io::prelude::*;
use std::io;
use term::Attr;
use term::color::{Color, BLACK, RED, GREEN, YELLOW};
use term::{Terminal, TerminfoTerminal, color};
use self::AdequateTerminal::{NoColor, Colored};
#[derive(Clone, Copy)]
pub struct ShellConfig {
pub color: bool,
pub verbose: bool,
pub tty: bool
}
enum AdequateTerminal {
NoColor(Box<Write + Send>),
Colored(Box<Terminal<UghWhyIsThisNecessary> + Send>)
}
pub struct Shell {
terminal: AdequateTerminal,
config: ShellConfig,
}
pub struct MultiShell {
out: Shell,
err: Shell,
verbose: bool
}
struct UghWhyIsThisNecessary {
inner: Box<Write + Send>,
}
impl MultiShell {
pub fn new(out: Shell, err: Shell, verbose: bool) -> MultiShell {
MultiShell { out: out, err: err, verbose: verbose }
}
pub fn out(&mut self) -> &mut Shell {
&mut self.out
}
pub fn err(&mut self) -> &mut Shell {
&mut self.err
}
pub fn say<T: ToString>(&mut self, message: T, color: Color) -> io::Result<()> {
self.out().say(message, color)
}
pub fn status<T, U>(&mut self, status: T, message: U) -> io::Result<()>
where T: fmt::Display, U: fmt::Display
{
self.out().say_status(status, message, GREEN)
}
pub fn verbose<F>(&mut self, mut callback: F) -> io::Result<()>
where F: FnMut(&mut MultiShell) -> io::Result<()>
{
if self.verbose { return callback(self) }
Ok(())
}
pub fn concise<F>(&mut self, mut callback: F) -> io::Result<()>
where F: FnMut(&mut MultiShell) -> io::Result<()>
{
if !self.verbose { return callback(self) }
Ok(())
}
pub fn error<T: ToString>(&mut self, message: T) -> io::Result<()> {
self.err().say(message, RED)
}
pub fn warn<T: ToString>(&mut self, message: T) -> io::Result<()> {
self.err().say(message, YELLOW)
}
pub fn set_verbose(&mut self, verbose: bool) {
self.verbose = verbose;
}
pub fn get_verbose(&self) -> bool {
self.verbose
}
}
impl Shell {
pub fn create(out: Box<Write + Send>, config: ShellConfig) -> Shell {
let out = UghWhyIsThisNecessary { inner: out };
if config.tty && config.color {
let term = TerminfoTerminal::new(out);
term.map(|t| Shell {
terminal: Colored(Box::new(t)),
config: config
}).unwrap_or_else(|| {
Shell { terminal: NoColor(Box::new(io::stderr())), config: config }
})
} else {
Shell { terminal: NoColor(out.inner), config: config }
}
}
pub fn verbose<F>(&mut self, mut callback: F) -> io::Result<()>
where F: FnMut(&mut Shell) -> io::Result<()>
{
if self.config.verbose { return callback(self) }
Ok(())
}
pub fn concise<F>(&mut self, mut callback: F) -> io::Result<()>
where F: FnMut(&mut Shell) -> io::Result<()>
{
if !self.config.verbose { return callback(self) }
Ok(())
}
pub fn say<T: ToString>(&mut self, message: T, color: Color) -> io::Result<()> {
try!(self.reset());
if color != BLACK { try!(self.fg(color)); }
try!(write!(self, "{}\n", message.to_string()));
try!(self.reset());
try!(self.flush());
Ok(())
}
pub fn say_status<T, U>(&mut self, status: T, message: U, color: Color)
-> io::Result<()>
where T: fmt::Display, U: fmt::Display
{
try!(self.reset());
if color != BLACK { try!(self.fg(color)); }
if self.supports_attr(Attr::Bold) { try!(self.attr(Attr::Bold)); }
try!(write!(self, "{:>12}", status.to_string()));
try!(self.reset());
try!(write!(self, " {}\n", message));
try!(self.flush());
Ok(())
}
fn fg(&mut self, color: color::Color) -> io::Result<bool> {
match self.terminal {
Colored(ref mut c) => c.fg(color),
NoColor(_) => Ok(false)
}
}
fn attr(&mut self, attr: Attr) -> io::Result<bool> {
match self.terminal {
Colored(ref mut c) => c.attr(attr),
NoColor(_) => Ok(false)
}
}
fn supports_attr(&self, attr: Attr) -> bool {
match self.terminal {
Colored(ref c) => c.supports_attr(attr),
NoColor(_) => false
}
}
fn reset(&mut self) -> io::Result<()> {
match self.terminal {
Colored(ref mut c) => c.reset().map(|_| ()),
NoColor(_) => Ok(())
}
}
}
impl Write for Shell {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self.terminal {
Colored(ref mut c) => c.write(buf),
NoColor(ref mut n) => n.write(buf)
}
}
fn flush(&mut self) -> io::Result<()> {
match self.terminal {
Colored(ref mut c) => c.flush(),
NoColor(ref mut n) => n.flush()
}
}
}
impl Write for UghWhyIsThisNecessary {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.inner.write(bytes)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}