1 //! Terminal back-end for emitting diagnostics.
6 use termcolor
::{ColorChoice, WriteColor}
;
8 use crate::diagnostic
::Diagnostic
;
15 pub use self::config
::{Chars, Config, DisplayStyle, Styles}
;
17 /// Emit a diagnostic using the given writer, context, config, and files.
19 writer
: &mut impl WriteColor
,
22 diagnostic
: &Diagnostic
,
24 use self::views
::{RichDiagnostic, ShortDiagnostic}
;
26 match config
.display_style
{
27 DisplayStyle
::Rich
=> RichDiagnostic
::new(files
, diagnostic
).emit(writer
, config
),
28 DisplayStyle
::Short
=> ShortDiagnostic
::new(files
, diagnostic
).emit(writer
, config
),
32 /// A command line argument that configures the coloring of the output.
34 /// This can be used with command line argument parsers like `clap` or `structopt`.
39 /// use structopt::StructOpt;
40 /// use codespan_reporting::term::termcolor::StandardStream;
41 /// use codespan_reporting::term::ColorArg;
43 /// #[derive(Debug, StructOpt)]
44 /// #[structopt(name = "groovey-app")]
46 /// /// Configure coloring of output
49 /// parse(try_from_str),
50 /// default_value = "auto",
51 /// raw(possible_values = "ColorArg::VARIANTS", case_insensitive = "true")
53 /// pub color: ColorArg,
57 /// let opts = Opts::from_args();
58 /// let writer = StandardStream::stderr(opts.color.into());
61 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
62 pub struct ColorArg(pub ColorChoice
);
65 /// Allowed values the argument.
67 /// This is useful for generating documentation via `clap` or `structopt`'s
68 /// `possible_values` configuration.
69 pub const VARIANTS
: &'
static [&'
static str] = &["auto", "always", "ansi", "never"];
72 impl FromStr
for ColorArg
{
73 type Err
= &'
static str;
75 fn from_str(src
: &str) -> Result
<ColorArg
, &'
static str> {
77 _
if src
.eq_ignore_ascii_case("auto") => Ok(ColorArg(ColorChoice
::Auto
)),
78 _
if src
.eq_ignore_ascii_case("always") => Ok(ColorArg(ColorChoice
::Always
)),
79 _
if src
.eq_ignore_ascii_case("ansi") => Ok(ColorArg(ColorChoice
::AlwaysAnsi
)),
80 _
if src
.eq_ignore_ascii_case("never") => Ok(ColorArg(ColorChoice
::Never
)),
81 _
=> Err("valid values: auto, always, ansi, never"),
86 impl Into
<ColorChoice
> for ColorArg
{
87 fn into(self) -> ColorChoice
{