diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/clap_builder/src/output/fmt.rs | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | third_party/rust/clap_builder/src/output/fmt.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/third_party/rust/clap_builder/src/output/fmt.rs b/third_party/rust/clap_builder/src/output/fmt.rs new file mode 100644 index 0000000000..0c9a24f33a --- /dev/null +++ b/third_party/rust/clap_builder/src/output/fmt.rs @@ -0,0 +1,83 @@ +use crate::builder::StyledStr; +use crate::util::color::ColorChoice; + +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub(crate) enum Stream { + Stdout, + Stderr, +} + +#[derive(Clone, Debug)] +pub(crate) struct Colorizer { + stream: Stream, + #[allow(unused)] + color_when: ColorChoice, + content: StyledStr, +} + +impl Colorizer { + pub(crate) fn new(stream: Stream, color_when: ColorChoice) -> Self { + Colorizer { + stream, + color_when, + content: Default::default(), + } + } + + pub(crate) fn with_content(mut self, content: StyledStr) -> Self { + self.content = content; + self + } +} + +/// Printing methods. +impl Colorizer { + #[cfg(feature = "color")] + pub(crate) fn print(&self) -> std::io::Result<()> { + let color_when = match self.color_when { + ColorChoice::Always => anstream::ColorChoice::Always, + ColorChoice::Auto => anstream::ColorChoice::Auto, + ColorChoice::Never => anstream::ColorChoice::Never, + }; + + let mut stdout; + let mut stderr; + let writer: &mut dyn std::io::Write = match self.stream { + Stream::Stderr => { + stderr = anstream::AutoStream::new(std::io::stderr().lock(), color_when); + &mut stderr + } + Stream::Stdout => { + stdout = anstream::AutoStream::new(std::io::stdout().lock(), color_when); + &mut stdout + } + }; + + self.content.write_to(writer) + } + + #[cfg(not(feature = "color"))] + pub(crate) fn print(&self) -> std::io::Result<()> { + // [e]println can't be used here because it panics + // if something went wrong. We don't want that. + match self.stream { + Stream::Stdout => { + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + self.content.write_to(&mut stdout) + } + Stream::Stderr => { + let stderr = std::io::stderr(); + let mut stderr = stderr.lock(); + self.content.write_to(&mut stderr) + } + } + } +} + +/// Color-unaware printing. Never uses coloring. +impl std::fmt::Display for Colorizer { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + self.content.fmt(f) + } +} |