@@ -30,6 +30,7 @@ pub struct GraphicalReportHandler {
30
30
pub ( crate ) context_lines : usize ,
31
31
pub ( crate ) tab_width : usize ,
32
32
pub ( crate ) with_cause_chain : bool ,
33
+ pub ( crate ) wrap_lines : bool ,
33
34
pub ( crate ) break_words : bool ,
34
35
pub ( crate ) word_separator : Option < textwrap:: WordSeparator > ,
35
36
pub ( crate ) word_splitter : Option < textwrap:: WordSplitter > ,
@@ -54,6 +55,7 @@ impl GraphicalReportHandler {
54
55
context_lines : 1 ,
55
56
tab_width : 4 ,
56
57
with_cause_chain : true ,
58
+ wrap_lines : true ,
57
59
break_words : true ,
58
60
word_separator : None ,
59
61
word_splitter : None ,
@@ -69,6 +71,7 @@ impl GraphicalReportHandler {
69
71
footer : None ,
70
72
context_lines : 1 ,
71
73
tab_width : 4 ,
74
+ wrap_lines : true ,
72
75
with_cause_chain : true ,
73
76
break_words : true ,
74
77
word_separator : None ,
@@ -131,6 +134,12 @@ impl GraphicalReportHandler {
131
134
self
132
135
}
133
136
137
+ /// Enables or disables wrapping of lines to fit the width.
138
+ pub fn with_wrap_lines ( mut self , wrap_lines : bool ) -> Self {
139
+ self . wrap_lines = wrap_lines;
140
+ self
141
+ }
142
+
134
143
/// Enables or disables breaking of words during wrapping.
135
144
pub fn with_break_words ( mut self , break_words : bool ) -> Self {
136
145
self . break_words = break_words;
@@ -197,7 +206,7 @@ impl GraphicalReportHandler {
197
206
opts = opts. word_splitter ( word_splitter) ;
198
207
}
199
208
200
- writeln ! ( f, "{}" , textwrap :: fill ( footer, opts) ) ?;
209
+ writeln ! ( f, "{}" , self . wrap ( footer, opts) ) ?;
201
210
}
202
211
Ok ( ( ) )
203
212
}
@@ -258,7 +267,7 @@ impl GraphicalReportHandler {
258
267
opts = opts. word_splitter ( word_splitter) ;
259
268
}
260
269
261
- writeln ! ( f, "{}" , textwrap :: fill ( & diagnostic. to_string( ) , opts) ) ?;
270
+ writeln ! ( f, "{}" , self . wrap ( & diagnostic. to_string( ) , opts) ) ?;
262
271
263
272
if !self . with_cause_chain {
264
273
return Ok ( ( ) ) ;
@@ -314,10 +323,10 @@ impl GraphicalReportHandler {
314
323
inner_renderer. with_cause_chain = false ;
315
324
inner_renderer. render_report ( & mut inner, diag) ?;
316
325
317
- writeln ! ( f, "{}" , textwrap :: fill ( & inner, opts) ) ?;
326
+ writeln ! ( f, "{}" , self . wrap ( & inner, opts) ) ?;
318
327
}
319
328
ErrorKind :: StdError ( err) => {
320
- writeln ! ( f, "{}" , textwrap :: fill ( & err. to_string( ) , opts) ) ?;
329
+ writeln ! ( f, "{}" , self . wrap ( & err. to_string( ) , opts) ) ?;
321
330
}
322
331
}
323
332
}
@@ -341,7 +350,7 @@ impl GraphicalReportHandler {
341
350
opts = opts. word_splitter ( word_splitter) ;
342
351
}
343
352
344
- writeln ! ( f, "{}" , textwrap :: fill ( & help. to_string( ) , opts) ) ?;
353
+ writeln ! ( f, "{}" , self . wrap ( & help. to_string( ) , opts) ) ?;
345
354
}
346
355
Ok ( ( ) )
347
356
}
@@ -810,6 +819,41 @@ impl GraphicalReportHandler {
810
819
Ok ( ( ) )
811
820
}
812
821
822
+ fn wrap ( & self , text : & str , opts : textwrap:: Options < ' _ > ) -> String {
823
+ if self . wrap_lines {
824
+ textwrap:: fill ( text, opts)
825
+ } else {
826
+ // Format without wrapping, but retain the indentation options
827
+ // Implementation based on `textwrap::indent`
828
+ let mut result = String :: with_capacity ( 2 * text. len ( ) ) ;
829
+ let trimmed_indent = opts. subsequent_indent . trim_end ( ) ;
830
+ for ( idx, line) in text. split_terminator ( '\n' ) . enumerate ( ) {
831
+ if idx > 0 {
832
+ result. push ( '\n' ) ;
833
+ }
834
+ if idx == 0 {
835
+ if line. trim ( ) . is_empty ( ) {
836
+ result. push_str ( opts. initial_indent . trim_end ( ) ) ;
837
+ } else {
838
+ result. push_str ( opts. initial_indent ) ;
839
+ }
840
+ } else {
841
+ if line. trim ( ) . is_empty ( ) {
842
+ result. push_str ( trimmed_indent) ;
843
+ } else {
844
+ result. push_str ( opts. subsequent_indent ) ;
845
+ }
846
+ }
847
+ result. push_str ( line) ;
848
+ }
849
+ if text. ends_with ( '\n' ) {
850
+ // split_terminator will have eaten the final '\n'.
851
+ result. push ( '\n' ) ;
852
+ }
853
+ result
854
+ }
855
+ }
856
+
813
857
fn write_linum ( & self , f : & mut impl fmt:: Write , width : usize , linum : usize ) -> fmt:: Result {
814
858
write ! (
815
859
f,
0 commit comments