Skip to content

Commit 8ab88a6

Browse files
authored
Merge pull request #82 from Muscraft/testing-colors-feature
feat: Add feature for testing colored output easier
2 parents 1293b88 + 8a29fa8 commit 8ab88a6

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ harness = false
4040

4141
[features]
4242
default = []
43+
testing-colors = []

src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040
//! options, such as color, or margins.
4141
//!
4242
//! Finally, `impl Display` into a final `String` output.
43+
//!
44+
//! # features
45+
//! - `testing-colors` - Makes [Renderer::styled] colors OS independent, which
46+
//! allows for easier testing when testing colored output. It should be added as
47+
//! a feature in `[dev-dependencies]`, which can be done with the following command:
48+
//! ```text
49+
//! cargo add annotate-snippets --dev --feature testing-colors
50+
//! ```
51+
//!
4352
4453
pub mod renderer;
4554
mod snippet;

src/renderer/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,20 @@ impl Renderer {
6161
}
6262

6363
/// Default terminal styling
64+
///
65+
/// # Note
66+
/// When testing styled terminal output, see the [`testing-colors` feature](crate#features)
6467
pub const fn styled() -> Self {
65-
const BRIGHT_BLUE: Style = if cfg!(windows) {
68+
const USE_WINDOWS_COLORS: bool = cfg!(windows) && !cfg!(feature = "testing-colors");
69+
const BRIGHT_BLUE: Style = if USE_WINDOWS_COLORS {
6670
AnsiColor::BrightCyan.on_default()
6771
} else {
6872
AnsiColor::BrightBlue.on_default()
6973
};
7074
Self {
7175
stylesheet: Stylesheet {
7276
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
73-
warning: if cfg!(windows) {
77+
warning: if USE_WINDOWS_COLORS {
7478
AnsiColor::BrightYellow.on_default()
7579
} else {
7680
AnsiColor::Yellow.on_default()
@@ -80,7 +84,7 @@ impl Renderer {
8084
note: AnsiColor::BrightGreen.on_default().effects(Effects::BOLD),
8185
help: AnsiColor::BrightCyan.on_default().effects(Effects::BOLD),
8286
line_no: BRIGHT_BLUE.effects(Effects::BOLD),
83-
emphasis: if cfg!(windows) {
87+
emphasis: if USE_WINDOWS_COLORS {
8488
AnsiColor::BrightWhite.on_default()
8589
} else {
8690
Style::new()

0 commit comments

Comments
 (0)