@@ -37,6 +37,43 @@ impl Mismatch {
37
37
}
38
38
}
39
39
40
+ // This struct handles writing output to stdout and abstracts away the logic
41
+ // of printing in color, if it's possible in the executing environment.
42
+ pub struct OutputWriter {
43
+ terminal : Option < Box < term:: Terminal < Output = io:: Stdout > > > ,
44
+ }
45
+
46
+ impl OutputWriter {
47
+ // Create a new OutputWriter instance based on the caller's preference
48
+ // for colorized output and the capabilities of the terminal.
49
+ pub fn new ( color : Color ) -> Self {
50
+ if let Some ( t) = term:: stdout ( ) {
51
+ if use_colored_tty ( color) && t. supports_color ( ) {
52
+ return OutputWriter { terminal : Some ( t) } ;
53
+ }
54
+ }
55
+ OutputWriter { terminal : None }
56
+ }
57
+
58
+ // Write output in the optionally specified color. The output is written
59
+ // in the specified color if this OutputWriter instance contains a
60
+ // Terminal in its `terminal` field.
61
+ pub fn writeln ( & mut self , msg : & str , color : Option < term:: color:: Color > ) {
62
+ match & mut self . terminal {
63
+ Some ( ref mut t) => {
64
+ if let Some ( color) = color {
65
+ t. fg ( color) . unwrap ( ) ;
66
+ }
67
+ writeln ! ( t, "{}" , msg) . unwrap ( ) ;
68
+ if color. is_some ( ) {
69
+ t. reset ( ) . unwrap ( ) ;
70
+ }
71
+ }
72
+ None => println ! ( "{}" , msg) ,
73
+ }
74
+ }
75
+ }
76
+
40
77
// Produces a diff between the expected output and actual output of rustfmt.
41
78
pub fn make_diff ( expected : & str , actual : & str , context_size : usize ) -> Vec < Mismatch > {
42
79
let mut line_number = 1 ;
@@ -97,80 +134,24 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
97
134
results
98
135
}
99
136
100
- // A representation of how to write output.
101
- pub enum PrintType {
102
- Fancy , // want to output color and the terminal supports it
103
- Basic , // do not want to output color or the terminal does not support color
104
- }
105
-
106
- impl PrintType {
107
- pub fn get ( color : Color ) -> Self {
108
- match term:: stdout ( ) {
109
- Some ( ref t) if use_colored_tty ( color) && t. supports_color ( ) => PrintType :: Fancy ,
110
- _ => PrintType :: Basic ,
111
- }
112
- }
113
- }
114
-
115
137
pub fn print_diff < F > ( diff : Vec < Mismatch > , get_section_title : F , color : Color )
116
138
where
117
139
F : Fn ( u32 ) -> String ,
118
140
{
119
- match PrintType :: get ( color) {
120
- PrintType :: Fancy => print_diff_fancy ( diff, get_section_title, term:: stdout ( ) . unwrap ( ) ) ,
121
- PrintType :: Basic => print_diff_basic ( diff, get_section_title) ,
122
- }
123
- }
124
-
125
- fn print_diff_fancy < F > (
126
- diff : Vec < Mismatch > ,
127
- get_section_title : F ,
128
- mut t : Box < term:: Terminal < Output = io:: Stdout > > ,
129
- ) where
130
- F : Fn ( u32 ) -> String ,
131
- {
132
- for mismatch in diff {
133
- let title = get_section_title ( mismatch. line_number ) ;
134
- writeln ! ( t, "{}" , title) . unwrap ( ) ;
135
-
136
- for line in mismatch. lines {
137
- match line {
138
- DiffLine :: Context ( ref str) => {
139
- t. reset ( ) . unwrap ( ) ;
140
- writeln ! ( t, " {}⏎" , str ) . unwrap ( ) ;
141
- }
142
- DiffLine :: Expected ( ref str) => {
143
- t. fg ( term:: color:: GREEN ) . unwrap ( ) ;
144
- writeln ! ( t, "+{}⏎" , str ) . unwrap ( ) ;
145
- }
146
- DiffLine :: Resulting ( ref str) => {
147
- t. fg ( term:: color:: RED ) . unwrap ( ) ;
148
- writeln ! ( t, "-{}⏎" , str ) . unwrap ( ) ;
149
- }
150
- }
151
- }
152
- t. reset ( ) . unwrap ( ) ;
153
- }
154
- }
141
+ let mut writer = OutputWriter :: new ( color) ;
155
142
156
- pub fn print_diff_basic < F > ( diff : Vec < Mismatch > , get_section_title : F )
157
- where
158
- F : Fn ( u32 ) -> String ,
159
- {
160
143
for mismatch in diff {
161
144
let title = get_section_title ( mismatch. line_number ) ;
162
- println ! ( "{}" , title) ;
145
+ writer . writeln ( & format ! ( "{}" , title) , None ) ;
163
146
164
147
for line in mismatch. lines {
165
148
match line {
166
- DiffLine :: Context ( ref str) => {
167
- println ! ( " {}⏎" , str ) ;
168
- }
149
+ DiffLine :: Context ( ref str) => writer. writeln ( & format ! ( " {}⏎" , str ) , None ) ,
169
150
DiffLine :: Expected ( ref str) => {
170
- println ! ( "+{}⏎" , str ) ;
151
+ writer . writeln ( & format ! ( "+{}⏎" , str ) , Some ( term :: color :: GREEN ) )
171
152
}
172
153
DiffLine :: Resulting ( ref str) => {
173
- println ! ( "-{}⏎" , str ) ;
154
+ writer . writeln ( & format ! ( "-{}⏎" , str ) , Some ( term :: color :: RED ) )
174
155
}
175
156
}
176
157
}
0 commit comments