Skip to content

Commit f2fc618

Browse files
committed
feat: Add feature for testing colored output easier
1 parent b6d8d79 commit f2fc618

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Diff for: 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 = []

Diff for: 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;

Diff for: src/renderer/mod.rs

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

6363
/// Default terminal styling
64+
///
65+
/// # Note
66+
/// Some colors are OS dependant, meaning that the colors will look
67+
/// different on different operating systems, making testing colored output
68+
/// difficult.
69+
/// If you are testing the colored output, you can use the
70+
/// [`testing-colors` feature](crate#features) to enable colors that are OS
71+
/// independent.
6472
pub const fn styled() -> Self {
65-
const BRIGHT_BLUE: Style = if cfg!(windows) {
73+
const USE_WINDOWS_COLORS: bool = cfg!(windows) && !cfg!(feature = "testing-colors");
74+
const BRIGHT_BLUE: Style = if USE_WINDOWS_COLORS {
6675
AnsiColor::BrightCyan.on_default()
6776
} else {
6877
AnsiColor::BrightBlue.on_default()
6978
};
7079
Self {
7180
stylesheet: Stylesheet {
7281
error: AnsiColor::BrightRed.on_default().effects(Effects::BOLD),
73-
warning: if cfg!(windows) {
82+
warning: if USE_WINDOWS_COLORS {
7483
AnsiColor::BrightYellow.on_default()
7584
} else {
7685
AnsiColor::Yellow.on_default()
@@ -80,7 +89,7 @@ impl Renderer {
8089
note: AnsiColor::BrightGreen.on_default().effects(Effects::BOLD),
8190
help: AnsiColor::BrightCyan.on_default().effects(Effects::BOLD),
8291
line_no: BRIGHT_BLUE.effects(Effects::BOLD),
83-
emphasis: if cfg!(windows) {
92+
emphasis: if USE_WINDOWS_COLORS {
8493
AnsiColor::BrightWhite.on_default()
8594
} else {
8695
Style::new()

0 commit comments

Comments
 (0)