Skip to content

Commit 6bb1fe7

Browse files
Fix feature-gated colored formatting
1 parent 6776715 commit 6bb1fe7

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ jobs:
2020
checks: write
2121
pull-requests: write
2222
security-events: write
23-
24-
env:
25-
CLICOLOR_FORCE: 1
26-
COLORTERM: truecolor
27-
TERM: xterm-256color
2823

2924
steps:
3025
- name: Checkout Repository

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ edition = "2018"
1111

1212
[workspace.dependencies]
1313
cfg-if = "1.0"
14-
maybe-impl = "0.1.0"
14+
maybe-impl = "0.1"

src/di/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ optional = true
3737

3838
[dependencies]
3939
cfg-if.workspace = true
40-
colored = { version = "2.0", optional = true }
40+
colored = { version = "3.1", optional = true }
4141

4242
[dev-dependencies.more-di]
4343
path = "."

src/di/collection.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,15 @@ impl std::fmt::Debug for ServiceCollection {
438438

439439
impl std::fmt::Display for ServiceCollection {
440440
fn fmt(&self, f: &mut Formatter<'_>) -> FormatResult {
441-
if f.alternate() && cfg!(feature = "fmt") {
442-
fmt::write(self, fmt::terminal::Renderer, f)
443-
} else {
444-
fmt::write(self, fmt::text::Renderer, f)
441+
cfg_if::cfg_if! {
442+
if #[cfg(feature = "fmt")] {
443+
if f.alternate() {
444+
return fmt::write(self, fmt::terminal::Renderer, f);
445+
}
446+
}
445447
}
448+
449+
fmt::write(self, fmt::text::Renderer, f)
446450
}
447451
}
448452

test/di/format.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use crate::traits::*;
44
use di::*;
5+
use std::env::set_var;
56

67
#[injectable]
78
struct A;
@@ -152,6 +153,12 @@ const TEXT_TERMINAL: &str =
152153
└ dyn more_di_tests::traits::Thing → more_di_tests::format::Thing3 [Scoped]\n \
153154
└ more_di_tests::format::A → more_di_tests::format::A [Singleton]\n";
154155

156+
fn force_color_support() {
157+
set_var("CLICOLOR_FORCE", "1");
158+
set_var("COLORTERM", "truecolor");
159+
set_var("TERM", "xterm-256color");
160+
}
161+
155162
#[test]
156163
fn debug_should_format_service_collection() {
157164
// arrange
@@ -181,6 +188,8 @@ fn alt_display_should_format_service_collection() {
181188
// arrange
182189
let services = new_service_collection();
183190

191+
force_color_support();
192+
184193
// act
185194
let output = format!("{services:#}");
186195

0 commit comments

Comments
 (0)