Skip to content

Commit 0e73fef

Browse files
committed
override NO_COLOR env
1 parent 566573f commit 0e73fef

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

crates/schema/src/auto_migrate.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ pub enum MigratePlan<'def> {
2828
Auto(AutoMigratePlan<'def>),
2929
}
3030

31+
#[derive(Copy, Clone, PartialEq, Eq)]
32+
pub enum PrettyPrintStyle {
33+
AnsiColor,
34+
NoColor,
35+
}
36+
3137
impl<'def> MigratePlan<'def> {
3238
/// Get the old `ModuleDef` for this migration plan.
3339
pub fn old_def(&self) -> &'def ModuleDef {
@@ -45,25 +51,25 @@ impl<'def> MigratePlan<'def> {
4551
}
4652
}
4753

48-
pub fn pretty_print(&self, no_color: bool) -> anyhow::Result<String> {
54+
pub fn pretty_print(&self, style: PrettyPrintStyle) -> anyhow::Result<String> {
55+
use PrettyPrintStyle::*;
56+
4957
match self {
5058
MigratePlan::Manual(_) => {
5159
anyhow::bail!("Manual migration plans are not yet supported for pretty printing.")
5260
}
53-
MigratePlan::Auto(plan) => {
54-
if no_color {
61+
62+
MigratePlan::Auto(plan) => match style {
63+
NoColor => {
5564
let mut fmt = plain_formatter::PlainFormatter::new(1024);
56-
format_plan(&mut fmt, plan)
57-
.map_err(|e| anyhow::anyhow!("Failed to format migration plan: {e}"))
58-
.map(|_| fmt.to_string())
59-
} else {
60-
// Use the ANSI formatter with colors.
65+
format_plan(&mut fmt, plan).map(|_| fmt.to_string())
66+
}
67+
AnsiColor => {
6168
let mut fmt = AnsiFormatter::new(1024, ColorScheme::default());
62-
format_plan(&mut fmt, plan)
63-
.map_err(|e| anyhow::anyhow!("Failed to format migration plan: {e}"))
64-
.map(|_| fmt.to_string())
69+
format_plan(&mut fmt, plan).map(|_| fmt.to_string())
6570
}
6671
}
72+
.map_err(|e| anyhow::anyhow!("Failed to format migration plan: {e}")),
6773
}
6874
}
6975
}
@@ -1433,7 +1439,8 @@ mod tests {
14331439

14341440
insta::assert_snapshot!(
14351441
"empty_to_populated_migration",
1436-
plan.pretty_print(false).expect("should pretty print")
1442+
plan.pretty_print(PrettyPrintStyle::AnsiColor)
1443+
.expect("should pretty print")
14371444
);
14381445
}
14391446

@@ -1445,7 +1452,8 @@ mod tests {
14451452

14461453
insta::assert_snapshot!(
14471454
"updated pretty print",
1448-
plan.pretty_print(false).expect("should pretty print")
1455+
plan.pretty_print(PrettyPrintStyle::AnsiColor)
1456+
.expect("should pretty print")
14491457
);
14501458
}
14511459

@@ -1457,7 +1465,8 @@ mod tests {
14571465

14581466
insta::assert_snapshot!(
14591467
"updated pretty print no color",
1460-
plan.pretty_print(true).expect("should pretty print")
1468+
plan.pretty_print(PrettyPrintStyle::NoColor)
1469+
.expect("should pretty print")
14611470
);
14621471
}
14631472
}

crates/schema/src/auto_migrate/ansi_formatter.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
use colored::{Color, Colorize as _};
3+
use colored::{control::set_override, Color, Colorize as _};
44
use spacetimedb_lib::{db::raw_def::v9::TableAccess, AlgebraicType};
55
use spacetimedb_sats::algebraic_type::fmt::fmt_algebraic_type;
66

@@ -55,6 +55,8 @@ impl fmt::Display for AnsiFormatter {
5555
impl AnsiFormatter {
5656
/// Create a new formatter with custom colors
5757
pub fn new(cap: usize, colors: ColorScheme) -> Self {
58+
// This overrides `NO_COLOR` as `ANSIFormatter` should always be colored.
59+
set_override(true);
5860
Self {
5961
buffer: String::with_capacity(cap),
6062
colors,

0 commit comments

Comments
 (0)