Skip to content

Commit c99e2bb

Browse files
committed
plain formatter
1 parent f63f2b6 commit c99e2bb

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/schema/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ hashbrown.workspace = true
3131
enum-as-inner.workspace = true
3232
enum-map.workspace = true
3333
colored.workspace = true
34+
regex.workspace = true
3435

3536
[dev-dependencies]
3637
spacetimedb-lib = { path = "../lib", features = ["test"] }

crates/schema/src/auto_migrate.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use spacetimedb_sats::{
1717
};
1818
mod ansi_formatter;
1919
mod formatter;
20+
mod plain_formatter;
2021

2122
pub type Result<T> = std::result::Result<T, ErrorStream<AutoMigrateError>>;
2223

@@ -44,16 +45,24 @@ impl<'def> MigratePlan<'def> {
4445
}
4546
}
4647

47-
pub fn pretty_print(&self) -> anyhow::Result<String> {
48+
pub fn pretty_print(&self, no_color: bool) -> anyhow::Result<String> {
4849
match self {
4950
MigratePlan::Manual(_) => {
5051
anyhow::bail!("Manual migration plans are not yet supported for pretty printing.")
5152
}
5253
MigratePlan::Auto(plan) => {
53-
let mut fmt = AnsiFormatter::new(1024, ColorScheme::default());
54-
format_plan(&mut fmt, plan)
55-
.map_err(|e| anyhow::anyhow!("Failed to format migration plan: {e}"))
56-
.map(|_| fmt.to_string())
54+
if no_color {
55+
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.
61+
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())
65+
}
5766
}
5867
}
5968
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::ops::Deref;
2+
3+
use super::{
4+
ansi_formatter::{AnsiFormatter, ColorScheme},
5+
formatter::MigrationFormatter,
6+
};
7+
use lazy_static::lazy_static;
8+
use regex::Regex;
9+
pub struct PlainFormatter(AnsiFormatter);
10+
11+
lazy_static! {
12+
// https://superuser.com/questions/380772/removing-ansi-color-codes-from-text-stream
13+
static ref ANSI_ESCAPE_SEQUENCE: Regex = Regex::new(
14+
r#"(?x) # verbose mode
15+
(?:
16+
\x1b \[ [\x30-\x3f]* [\x20-\x2f]* [\x40-\x7e] # CSI sequences (start with "ESC [")
17+
| \x1b [PX^_] .*? \x1b \\ # String Terminator sequences (end with "ESC \")
18+
| \x1b \] [^\x07]* (?: \x07 | \x1b \\ ) # Sequences ending in BEL ("\x07")
19+
| \x1b [\x40-\x5f]
20+
)"#
21+
)
22+
.unwrap();
23+
}
24+
25+
impl PlainFormatter {
26+
pub fn new(cap: usize) -> Self {
27+
Self(AnsiFormatter::new(cap, ColorScheme::default()))
28+
}
29+
}
30+
31+
impl std::fmt::Display for PlainFormatter {
32+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33+
let formatted = self.0.to_string();
34+
let cleaned = ANSI_ESCAPE_SEQUENCE.replace_all(&formatted, "");
35+
write!(f, "{}", cleaned)
36+
}
37+
}
38+
39+
impl MigrationFormatter for PlainFormatter {
40+
fn format_header(&mut self) {
41+
self.0.format_header();
42+
}
43+
44+
fn format_add_table(&mut self, table_info: &super::formatter::TableInfo) {
45+
self.0.format_add_table(table_info);
46+
}
47+
48+
fn format_index(&mut self, index_info: &super::formatter::IndexInfo, action: super::formatter::Action) {
49+
self.0.format_index(index_info, action);
50+
}
51+
52+
fn format_constraint(
53+
&mut self,
54+
constraint_info: &super::formatter::ConstraintInfo,
55+
action: super::formatter::Action,
56+
) {
57+
self.0.format_constraint(constraint_info, action);
58+
}
59+
60+
fn format_sequence(&mut self, sequence_info: &super::formatter::SequenceInfo, action: super::formatter::Action) {
61+
self.0.format_sequence(sequence_info, action);
62+
}
63+
64+
fn format_change_access(&mut self, access_info: &super::formatter::AccessChangeInfo) {
65+
self.0.format_change_access(access_info);
66+
}
67+
68+
fn format_schedule(&mut self, schedule_info: &super::formatter::ScheduleInfo, action: super::formatter::Action) {
69+
self.0.format_schedule(schedule_info, action);
70+
}
71+
72+
fn format_rls(&mut self, rls_info: &super::formatter::RlsInfo, action: super::formatter::Action) {
73+
self.0.format_rls(rls_info, action);
74+
}
75+
76+
fn format_change_columns(&mut self, column_changes: &super::formatter::ColumnChanges) {
77+
self.0.format_change_columns(column_changes);
78+
}
79+
80+
fn format_add_columns(&mut self, new_columns: &super::formatter::NewColumns) {
81+
self.0.format_add_columns(new_columns);
82+
}
83+
84+
fn format_disconnect_warning(&mut self) {
85+
self.0.format_disconnect_warning();
86+
}
87+
}

0 commit comments

Comments
 (0)