Skip to content

Commit

Permalink
refactor(help): Make bool's meaning clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Apr 21, 2022
1 parent 639f9e8 commit 6a9a5d0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/build/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::build::{arg::ArgProvider, Arg, ArgGroup, ArgPredicate};
use crate::error::ErrorKind;
use crate::error::Result as ClapResult;
use crate::mkeymap::MKeyMap;
use crate::output::fmt::Stream;
use crate::output::{fmt::Colorizer, Help, HelpWriter, Usage};
use crate::parse::{ArgMatcher, ArgMatches, Parser};
use crate::util::ChildGraph;
Expand Down Expand Up @@ -697,7 +698,7 @@ impl<'help> App<'help> {
self._build();
let color = self.get_color();

let mut c = Colorizer::new(false, color);
let mut c = Colorizer::new(Stream::Stdout, color);
let usage = Usage::new(self);
Help::new(HelpWriter::Buffer(&mut c), self, &usage, false).write_help()?;
c.print()
Expand All @@ -722,7 +723,7 @@ impl<'help> App<'help> {
self._build();
let color = self.get_color();

let mut c = Colorizer::new(false, color);
let mut c = Colorizer::new(Stream::Stdout, color);
let usage = Usage::new(self);
Help::new(HelpWriter::Buffer(&mut c), self, &usage, true).write_help()?;
c.print()
Expand Down
19 changes: 12 additions & 7 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
use crate::{
build::Arg,
output::fmt::Colorizer,
output::fmt::Stream,
parse::features::suggestions,
util::{color::ColorChoice, safe_exit, SUCCESS_CODE, USAGE_CODE},
AppSettings, Command,
Expand Down Expand Up @@ -97,10 +98,14 @@ impl Error {
/// Should the message be written to `stdout` or not?
#[inline]
pub fn use_stderr(&self) -> bool {
!matches!(
self.kind(),
ErrorKind::DisplayHelp | ErrorKind::DisplayVersion
)
self.stream() == Stream::Stderr
}

pub(crate) fn stream(&self) -> Stream {
match self.kind() {
ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => Stream::Stdout,
_ => Stream::Stderr,
}
}

/// Prints the error and exits.
Expand Down Expand Up @@ -608,7 +613,7 @@ impl Error {
if let Some(message) = self.inner.message.as_ref() {
message.formatted()
} else {
let mut c = Colorizer::new(self.use_stderr(), self.inner.color_when);
let mut c = Colorizer::new(self.stream(), self.inner.color_when);

start_error(&mut c);

Expand Down Expand Up @@ -1090,7 +1095,7 @@ impl Message {
fn format(&mut self, cmd: &Command, usage: String) {
match self {
Message::Raw(s) => {
let mut c = Colorizer::new(true, cmd.get_color());
let mut c = Colorizer::new(Stream::Stderr, cmd.get_color());

let mut message = String::new();
std::mem::swap(s, &mut message);
Expand All @@ -1107,7 +1112,7 @@ impl Message {
fn formatted(&self) -> Cow<Colorizer> {
match self {
Message::Raw(s) => {
let mut c = Colorizer::new(true, ColorChoice::Never);
let mut c = Colorizer::new(Stream::Stderr, ColorChoice::Never);
start_error(&mut c);
c.none(s);
Cow::Owned(c)
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ macro_rules! debug {
($($arg:tt)*) => ({
let prefix = format!("[{:>w$}] \t", module_path!(), w = 28);
let body = format!($($arg)*);
let mut color = $crate::output::fmt::Colorizer::new(true, $crate::ColorChoice::Auto);
let mut color = $crate::output::fmt::Colorizer::new($crate::output::fmt::Stream::Stderr, $crate::ColorChoice::Auto);
color.hint(prefix);
color.hint(body);
color.none("\n");
Expand Down
49 changes: 28 additions & 21 deletions src/output/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@ use std::{
io::{self, Write},
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum Stream {
Stdout,
Stderr,
}

#[derive(Clone, Debug)]
pub(crate) struct Colorizer {
use_stderr: bool,
stream: Stream,
#[allow(unused)]
color_when: ColorChoice,
pieces: Vec<(String, Style)>,
}

impl Colorizer {
#[inline(never)]
pub(crate) fn new(use_stderr: bool, color_when: ColorChoice) -> Self {
pub(crate) fn new(stream: Stream, color_when: ColorChoice) -> Self {
Colorizer {
use_stderr,
stream,
color_when,
pieces: vec![],
}
Expand Down Expand Up @@ -58,14 +64,13 @@ impl Colorizer {

let color_when = match self.color_when {
ColorChoice::Always => DepColorChoice::Always,
ColorChoice::Auto if is_a_tty(self.use_stderr) => DepColorChoice::Auto,
ColorChoice::Auto if is_a_tty(self.stream) => DepColorChoice::Auto,
_ => DepColorChoice::Never,
};

let writer = if self.use_stderr {
BufferWriter::stderr(color_when)
} else {
BufferWriter::stdout(color_when)
let writer = match self.stream {
Stream::Stdout => BufferWriter::stderr(color_when),
Stream::Stderr => BufferWriter::stdout(color_when),
};

let mut buffer = writer.buffer();
Expand Down Expand Up @@ -101,14 +106,17 @@ impl Colorizer {
pub(crate) fn print(&self) -> io::Result<()> {
// [e]println can't be used here because it panics
// if something went wrong. We don't want that.
if self.use_stderr {
let stderr = std::io::stderr();
let mut stderr = stderr.lock();
write!(stderr, "{}", self)
} else {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
write!(stdout, "{}", self)
match self.stream {
Stream::Stdout => {
let stdout = std::io::stdout();
let mut stdout = stdout.lock();
write!(stdout, "{}", self)
}
Stream::Stderr => {
let stderr = std::io::stderr();
let mut stderr = stderr.lock();
write!(stderr, "{}", self)
}
}
}
}
Expand Down Expand Up @@ -140,11 +148,10 @@ impl Default for Style {
}

#[cfg(feature = "color")]
fn is_a_tty(stderr: bool) -> bool {
let stream = if stderr {
atty::Stream::Stderr
} else {
atty::Stream::Stdout
fn is_a_tty(stream: Stream) -> bool {
let stream = match stream {
Stream::Stdout => atty::Stream::Stdout,
Stream::Stderr => atty::Stream::Stderr,
};

atty::is(stream)
Expand Down
7 changes: 4 additions & 3 deletions src/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::build::{Arg, Command};
use crate::error::Error as ClapError;
use crate::error::Result as ClapResult;
use crate::mkeymap::KeyType;
use crate::output::fmt::Stream;
use crate::output::{fmt::Colorizer, Help, HelpWriter, Usage};
use crate::parse::features::suggestions;
use crate::parse::{ArgMatcher, SubCommand};
Expand Down Expand Up @@ -1509,7 +1510,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {

pub(crate) fn write_help_err(&self) -> ClapResult<Colorizer> {
let usage = Usage::new(self.cmd);
let mut c = Colorizer::new(true, self.color_help());
let mut c = Colorizer::new(Stream::Stderr, self.color_help());
Help::new(HelpWriter::Buffer(&mut c), self.cmd, &usage, false).write_help()?;
Ok(c)
}
Expand All @@ -1522,7 +1523,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {

use_long = use_long && self.cmd.use_long_help();
let usage = Usage::new(self.cmd);
let mut c = Colorizer::new(false, self.color_help());
let mut c = Colorizer::new(Stream::Stdout, self.color_help());

match Help::new(HelpWriter::Buffer(&mut c), self.cmd, &usage, use_long).write_help() {
Err(e) => e.into(),
Expand All @@ -1534,7 +1535,7 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
debug!("Parser::version_err");

let msg = self.cmd._render_version(use_long);
let mut c = Colorizer::new(false, self.color_help());
let mut c = Colorizer::new(Stream::Stdout, self.color_help());
c.none(msg);
ClapError::display_version(self.cmd, c)
}
Expand Down

0 comments on commit 6a9a5d0

Please sign in to comment.