clap/src/output/fmt.rs

123 lines
3 KiB
Rust
Raw Normal View History

2020-04-12 03:39:13 +02:00
use crate::util::termcolor::{Buffer, BufferWriter, ColorChoice};
#[cfg(feature = "color")]
2020-04-12 03:39:13 +02:00
use crate::util::termcolor::{Color, ColorSpec, WriteColor};
2020-04-27 21:47:08 +03:00
use std::{
fmt::{self, Debug, Formatter},
io::{Result, Write},
};
#[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
let stream = if stderr {
atty::Stream::Stderr
} else {
atty::Stream::Stdout
};
2020-04-12 03:39:13 +02:00
atty::is(stream)
}
#[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");
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,
}
2020-04-12 03:39:13 +02:00
impl Debug for Colorizer {
#[cfg(feature = "color")]
2020-07-19 20:11:29 +03:00
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2020-04-12 03:39:13 +02:00
write!(f, "{}", String::from_utf8_lossy(self.buffer.as_slice()))
}
#[cfg(not(feature = "color"))]
2020-07-19 20:11:29 +03:00
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
2020-04-12 03:39:13 +02:00
write!(f, "{}", String::from_utf8_lossy(&self.buffer))
}
}
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 }
}
2020-04-12 03:39:13 +02:00
pub(crate) fn print(&self) -> Result<()> {
self.writer.print(&self.buffer)
}
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()
}
2020-04-12 03:39:13 +02:00
#[cfg(not(feature = "color"))]
pub(crate) fn good(&mut self, msg: &str) -> Result<()> {
self.none(msg)
}
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()
}
2020-04-12 03:39:13 +02:00
#[cfg(not(feature = "color"))]
pub(crate) fn warning(&mut self, msg: &str) -> Result<()> {
self.none(msg)
}
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()
}
2020-04-12 03:39:13 +02:00
#[cfg(not(feature = "color"))]
pub(crate) fn error(&mut self, msg: &str) -> Result<()> {
self.none(msg)
}
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
}
}
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
}