|
| 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