2020-04-12 03:39:13 +02:00
|
|
|
use crate::util::termcolor::{Buffer, BufferWriter, ColorChoice};
|
2020-04-12 10:20:08 +02:00
|
|
|
#[cfg(feature = "color")]
|
2020-04-12 03:39:13 +02:00
|
|
|
use crate::util::termcolor::{Color, ColorSpec, WriteColor};
|
|
|
|
|
|
|
|
use std::fmt::{self, Debug, Formatter};
|
|
|
|
use std::io::{Result, Write};
|
2016-05-30 04:07:44 -04:00
|
|
|
|
|
|
|
#[cfg(feature = "color")]
|
2020-04-12 03:39:13 +02:00
|
|
|
fn is_a_tty(stderr: bool) -> bool {
|
2020-04-22 20:14:47 +02:00
|
|
|
debug!("is_a_tty: stderr={:?}", stderr);
|
2020-04-12 03:39:13 +02:00
|
|
|
|
2017-02-19 11:12:55 -05:00
|
|
|
let stream = if stderr {
|
|
|
|
atty::Stream::Stderr
|
|
|
|
} else {
|
|
|
|
atty::Stream::Stdout
|
|
|
|
};
|
2020-04-12 03:39:13 +02:00
|
|
|
|
2017-02-19 11:12:55 -05:00
|
|
|
atty::is(stream)
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "color"))]
|
2020-04-12 03:39:13 +02:00
|
|
|
fn is_a_tty(_: bool) -> bool {
|
2020-04-22 20:14:47 +02:00
|
|
|
debug!("is_a_tty");
|
2016-05-30 04:07:44 -04:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-03-19 10:17:52 +03:00
|
|
|
pub(crate) struct Colorizer {
|
2020-04-12 03:39:13 +02:00
|
|
|
writer: BufferWriter,
|
|
|
|
buffer: Buffer,
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
impl Debug for Colorizer {
|
|
|
|
#[cfg(feature = "color")]
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", String::from_utf8_lossy(self.buffer.as_slice()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "color"))]
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", String::from_utf8_lossy(&self.buffer))
|
|
|
|
}
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Colorizer {
|
2020-04-12 03:39:13 +02:00
|
|
|
pub(crate) fn new(use_stderr: bool, when: ColorChoice) -> Self {
|
|
|
|
let checked_when = if is_a_tty(use_stderr) {
|
|
|
|
when
|
|
|
|
} else {
|
|
|
|
ColorChoice::Never
|
|
|
|
};
|
|
|
|
|
|
|
|
let writer = if use_stderr {
|
|
|
|
BufferWriter::stderr(checked_when)
|
|
|
|
} else {
|
|
|
|
BufferWriter::stdout(checked_when)
|
|
|
|
};
|
|
|
|
|
|
|
|
let buffer = writer.buffer();
|
|
|
|
|
|
|
|
Self { writer, buffer }
|
2017-05-26 15:25:38 -07:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
pub(crate) fn print(&self) -> Result<()> {
|
|
|
|
self.writer.print(&self.buffer)
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
#[cfg(feature = "color")]
|
|
|
|
pub(crate) fn good(&mut self, msg: &str) -> Result<()> {
|
|
|
|
self.buffer
|
|
|
|
.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?;
|
|
|
|
self.write_all(msg.as_bytes())?;
|
|
|
|
self.buffer.reset()
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
#[cfg(not(feature = "color"))]
|
|
|
|
pub(crate) fn good(&mut self, msg: &str) -> Result<()> {
|
|
|
|
self.none(msg)
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
#[cfg(feature = "color")]
|
|
|
|
pub(crate) fn warning(&mut self, msg: &str) -> Result<()> {
|
|
|
|
self.buffer
|
|
|
|
.set_color(ColorSpec::new().set_fg(Some(Color::Yellow)))?;
|
|
|
|
self.write_all(msg.as_bytes())?;
|
|
|
|
self.buffer.reset()
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
#[cfg(not(feature = "color"))]
|
|
|
|
pub(crate) fn warning(&mut self, msg: &str) -> Result<()> {
|
|
|
|
self.none(msg)
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
2015-05-22 18:17:57 -04:00
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
#[cfg(feature = "color")]
|
|
|
|
pub(crate) fn error(&mut self, msg: &str) -> Result<()> {
|
|
|
|
self.buffer
|
|
|
|
.set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_bold(true))?;
|
|
|
|
self.write_all(msg.as_bytes())?;
|
|
|
|
self.buffer.reset()
|
2015-05-22 18:17:57 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
#[cfg(not(feature = "color"))]
|
|
|
|
pub(crate) fn error(&mut self, msg: &str) -> Result<()> {
|
|
|
|
self.none(msg)
|
2015-05-22 18:17:57 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
pub(crate) fn none(&mut self, msg: &str) -> Result<()> {
|
|
|
|
self.write_all(msg.as_bytes())?;
|
|
|
|
Ok(())
|
2020-01-31 10:13:44 +01:00
|
|
|
}
|
2016-05-30 04:07:44 -04:00
|
|
|
}
|
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
impl Write for Colorizer {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
|
|
|
self.buffer.write(buf)
|
2020-01-31 10:13:44 +01:00
|
|
|
}
|
2015-09-04 11:38:48 -04:00
|
|
|
|
2020-04-12 03:39:13 +02:00
|
|
|
fn flush(&mut self) -> Result<()> {
|
|
|
|
self.buffer.flush()
|
2015-09-04 11:38:48 -04:00
|
|
|
}
|
2015-09-06 21:07:46 -04:00
|
|
|
}
|