-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
shell.rs
310 lines (265 loc) · 8.66 KB
/
shell.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::fmt;
use std::io::prelude::*;
use std::io;
use term::color::{Color, BLACK, RED, GREEN, YELLOW};
use term::{self, Terminal, TerminfoTerminal, color, Attr};
use self::AdequateTerminal::{NoColor, Colored};
use self::Verbosity::{Verbose, Quiet};
use self::ColorConfig::{Auto, Always, Never};
use util::errors::CargoResult;
#[derive(Clone, Copy, PartialEq)]
pub enum Verbosity {
Verbose,
Normal,
Quiet
}
#[derive(Clone, Copy, PartialEq)]
pub enum ColorConfig {
Auto,
Always,
Never
}
impl fmt::Display for ColorConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ColorConfig::Auto => "auto",
ColorConfig::Always => "always",
ColorConfig::Never => "never",
}.fmt(f)
}
}
#[derive(Clone, Copy)]
pub struct ShellConfig {
pub color_config: ColorConfig,
pub tty: bool
}
enum AdequateTerminal {
NoColor(Box<Write + Send>),
Colored(Box<Terminal<Output=Box<Write + Send>> + Send>)
}
pub struct Shell {
terminal: AdequateTerminal,
config: ShellConfig,
}
pub struct MultiShell {
out: Shell,
err: Shell,
verbosity: Verbosity
}
impl MultiShell {
pub fn new(out: Shell, err: Shell, verbosity: Verbosity) -> MultiShell {
MultiShell { out: out, err: err, verbosity: verbosity }
}
// Create a quiet, basic shell from supplied writers.
pub fn from_write(out: Box<Write + Send>, err: Box<Write + Send>) -> MultiShell {
let config = ShellConfig { color_config: ColorConfig::Never, tty: false };
let out = Shell { terminal: NoColor(out), config: config.clone() };
let err = Shell { terminal: NoColor(err), config: config };
MultiShell {
out: out,
err: err,
verbosity: Verbosity::Quiet,
}
}
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)
-> CargoResult<()> {
match self.verbosity {
Quiet => Ok(()),
_ => self.out().say(message, color)
}
}
pub fn status<T, U>(&mut self, status: T, message: U) -> CargoResult<()>
where T: fmt::Display, U: fmt::Display
{
match self.verbosity {
Quiet => Ok(()),
_ => self.err().say_status(status, message, GREEN, true)
}
}
pub fn verbose<F>(&mut self, mut callback: F) -> CargoResult<()>
where F: FnMut(&mut MultiShell) -> CargoResult<()>
{
match self.verbosity {
Verbose => callback(self),
_ => Ok(())
}
}
pub fn concise<F>(&mut self, mut callback: F) -> CargoResult<()>
where F: FnMut(&mut MultiShell) -> CargoResult<()>
{
match self.verbosity {
Verbose => Ok(()),
_ => callback(self)
}
}
pub fn error<T: fmt::Display>(&mut self, message: T) -> CargoResult<()> {
self.err().say_status("error:", message, RED, false)
}
pub fn warn<T: fmt::Display>(&mut self, message: T) -> CargoResult<()> {
match self.verbosity {
Quiet => Ok(()),
_ => self.err().say_status("warning:", message, YELLOW, false),
}
}
pub fn set_verbosity(&mut self, verbosity: Verbosity) {
self.verbosity = verbosity;
}
pub fn set_color_config(&mut self, color: Option<&str>) -> CargoResult<()> {
let cfg = match color {
Some("auto") => Auto,
Some("always") => Always,
Some("never") => Never,
None => Auto,
Some(arg) => bail!("argument for --color must be auto, always, or \
never, but found `{}`", arg),
};
self.out.set_color_config(cfg);
self.err.set_color_config(cfg);
Ok(())
}
pub fn get_verbose(&self) -> Verbosity {
self.verbosity
}
pub fn color_config(&self) -> ColorConfig {
assert!(self.out.config.color_config == self.err.config.color_config);
self.out.config.color_config
}
}
impl Shell {
pub fn create<T: FnMut() -> Box<Write + Send>>(mut out_fn: T, config: ShellConfig) -> Shell {
let term = match Shell::get_term(out_fn()) {
Ok(t) => t,
Err(_) => NoColor(out_fn())
};
Shell {
terminal: term,
config: config,
}
}
#[cfg(any(windows))]
fn get_term(out: Box<Write + Send>) -> CargoResult<AdequateTerminal> {
// Check if the creation of a console will succeed
if ::term::WinConsole::new(vec![0u8; 0]).is_ok() {
let t = ::term::WinConsole::new(out)?;
if !t.supports_color() {
Ok(NoColor(Box::new(t)))
} else {
Ok(Colored(Box::new(t)))
}
} else {
// If we fail to get a windows console, we try to get a `TermInfo` one
Ok(Shell::get_terminfo_term(out))
}
}
#[cfg(any(unix))]
fn get_term(out: Box<Write + Send>) -> CargoResult<AdequateTerminal> {
Ok(Shell::get_terminfo_term(out))
}
fn get_terminfo_term(out: Box<Write + Send>) -> AdequateTerminal {
// Use `TermInfo::from_env()` and `TerminfoTerminal::supports_color()`
// to determine if creation of a TerminfoTerminal is possible regardless
// of the tty status. --color options are parsed after Shell creation so
// always try to create a terminal that supports color output. Fall back
// to a no-color terminal regardless of whether or not a tty is present
// and if color output is not possible.
match ::term::terminfo::TermInfo::from_env() {
Ok(ti) => {
let term = TerminfoTerminal::new_with_terminfo(out, ti);
if !term.supports_color() {
NoColor(term.into_inner())
} else {
// Color output is possible.
Colored(Box::new(term))
}
},
Err(_) => NoColor(out),
}
}
pub fn set_color_config(&mut self, color_config: ColorConfig) {
self.config.color_config = color_config;
}
pub fn say<T: ToString>(&mut self, message: T, color: Color) -> CargoResult<()> {
self.reset()?;
if color != BLACK { self.fg(color)?; }
write!(self, "{}\n", message.to_string())?;
self.reset()?;
self.flush()?;
Ok(())
}
pub fn say_status<T, U>(&mut self,
status: T,
message: U,
color: Color,
justified: bool)
-> CargoResult<()>
where T: fmt::Display, U: fmt::Display
{
self.reset()?;
if color != BLACK { self.fg(color)?; }
if self.supports_attr(Attr::Bold) { self.attr(Attr::Bold)?; }
if justified {
write!(self, "{:>12}", status.to_string())?;
} else {
write!(self, "{}", status)?;
}
self.reset()?;
write!(self, " {}\n", message)?;
self.flush()?;
Ok(())
}
fn fg(&mut self, color: color::Color) -> CargoResult<bool> {
let colored = self.colored();
match self.terminal {
Colored(ref mut c) if colored => c.fg(color)?,
_ => return Ok(false),
}
Ok(true)
}
fn attr(&mut self, attr: Attr) -> CargoResult<bool> {
let colored = self.colored();
match self.terminal {
Colored(ref mut c) if colored => c.attr(attr)?,
_ => return Ok(false)
}
Ok(true)
}
fn supports_attr(&self, attr: Attr) -> bool {
let colored = self.colored();
match self.terminal {
Colored(ref c) if colored => c.supports_attr(attr),
_ => false
}
}
fn reset(&mut self) -> term::Result<()> {
let colored = self.colored();
match self.terminal {
Colored(ref mut c) if colored => c.reset()?,
_ => ()
}
Ok(())
}
fn colored(&self) -> bool {
self.config.tty && Auto == self.config.color_config
|| Always == self.config.color_config
}
}
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()
}
}
}