Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 696a058

Browse files
committed
Added the ability to toggle ANSI styling
1 parent e526a3c commit 696a058

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ auto-color = ["concolor", "concolor/auto"]
2121
yansi = "1.0"
2222
unicode-width = "0.2.0"
2323
concolor = { version = "0.1", optional = true }
24+
strip-ansi-escapes = "0.2.1"
2425

2526
[dev-dependencies]
2627
insta = "1.31.0"

src/draw.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,37 @@ impl<T: fmt::Display> fmt::Display for Background<T> {
216216
}
217217
}
218218

219+
#[allow(clippy::large_enum_variant)]
220+
pub(crate) enum WrappedWriter<W: Write> {
221+
Strip(strip_ansi_escapes::Writer<W>),
222+
Keep(W),
223+
}
224+
225+
impl<W: Write> WrappedWriter<W> {
226+
pub(crate) fn new(w: W, config: &Config) -> Self {
227+
match config.ansi_mode {
228+
AnsiMode::Off => Self::Strip(strip_ansi_escapes::Writer::new(w)),
229+
AnsiMode::On => Self::Keep(w),
230+
}
231+
}
232+
}
233+
234+
impl<W: Write> Write for WrappedWriter<W> {
235+
fn flush(&mut self) -> io::Result<()> {
236+
match self {
237+
Self::Strip(w) => w.flush(),
238+
Self::Keep(w) => w.flush(),
239+
}
240+
}
241+
242+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
243+
match self {
244+
Self::Strip(w) => w.write(buf),
245+
Self::Keep(w) => w.write(buf),
246+
}
247+
}
248+
}
249+
219250
/// A type that can generate distinct 8-bit colors.
220251
pub struct ColorGenerator {
221252
state: [u16; 3],

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,15 @@ pub enum IndexType {
446446
Char,
447447
}
448448

449+
/// Whether rendering of ANSI styling, such as color and font weight, is enabled.
450+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
451+
pub enum AnsiMode {
452+
/// ANSI styling is disabled, diagnostics will display without styling.
453+
Off,
454+
/// ANSI styling is disabled, diagnostics will have ANSI styling escape codes included.
455+
On,
456+
}
457+
449458
/// A type used to configure a report
450459
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
451460
pub struct Config {
@@ -460,6 +469,7 @@ pub struct Config {
460469
index_type: IndexType,
461470
minimise_crossings: bool,
462471
context_lines: usize,
472+
ansi_mode: AnsiMode,
463473
}
464474

465475
impl Config {
@@ -544,6 +554,13 @@ impl Config {
544554
self.context_lines = context_lines;
545555
self
546556
}
557+
/// Should ANSI escape code styling be included in the diagnostic after writing?
558+
///
559+
/// If unspecified, this defaults to `AnsiMode::On`.
560+
pub const fn with_ansi_mode(mut self, ansi_mode: AnsiMode) -> Self {
561+
self.ansi_mode = ansi_mode;
562+
self
563+
}
547564

548565
fn error_color(&self) -> Option<Color> {
549566
Some(Color::Red).filter(|_| self.color)
@@ -597,6 +614,7 @@ impl Config {
597614
index_type: IndexType::Char,
598615
minimise_crossings: false,
599616
context_lines: 0,
617+
ansi_mode: AnsiMode::On,
600618
}
601619
}
602620
}

src/write.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::Range;
33

44
use crate::{Config, IndexType, LabelDisplay};
55

6-
use super::draw::{self, StreamAwareFmt, StreamType};
6+
use super::draw::{self, StreamAwareFmt, StreamType, WrappedWriter};
77
use super::{Cache, CharSet, LabelAttach, Report, ReportKind, Show, Span, Write};
88

99
// A WARNING, FOR ALL YE WHO VENTURE IN HERE
@@ -185,9 +185,10 @@ impl<S: Span> Report<'_, S> {
185185
fn write_for_stream<C: Cache<S::SourceId>, W: Write>(
186186
&self,
187187
mut cache: C,
188-
mut w: W,
188+
w: W,
189189
s: StreamType,
190190
) -> io::Result<()> {
191+
let mut w = WrappedWriter::new(w, &self.config);
191192
let draw = match self.config.char_set {
192193
CharSet::Unicode => draw::Characters::unicode(),
193194
CharSet::Ascii => draw::Characters::ascii(),
@@ -338,7 +339,7 @@ impl<S: Span> Report<'_, S> {
338339
multi_labels_with_message.sort_by_key(|m| -(Span::len(&m.char_span) as isize));
339340
}
340341

341-
let write_margin = |w: &mut W,
342+
let write_margin = |w: &mut WrappedWriter<W>,
342343
idx: usize,
343344
is_line: bool,
344345
is_ellipsis: bool,

0 commit comments

Comments
 (0)