1
1
use std:: fmt;
2
- use std:: io;
2
+ use std:: io:: { self , BufWriter , Write } ;
3
3
use std:: vec:: Vec ;
4
4
5
5
use nu_ansi_term:: Color :: { Fixed , Green , Red , Yellow } ;
@@ -67,35 +67,42 @@ const EMPTY_SYNTECT_STYLE: syntect::highlighting::Style = syntect::highlighting:
67
67
font_style : FontStyle :: empty ( ) ,
68
68
} ;
69
69
70
- pub enum OutputHandle < ' a > {
71
- IoWrite ( & ' a mut dyn io :: Write ) ,
70
+ pub enum OutputHandle < ' a , W : io :: Write > {
71
+ IoWrite ( BufWriter < W > ) ,
72
72
FmtWrite ( & ' a mut dyn fmt:: Write ) ,
73
73
}
74
74
75
- impl < ' a > OutputHandle < ' a > {
75
+ impl < ' a , W : io :: Write > OutputHandle < ' a , W > {
76
76
fn write_fmt ( & mut self , args : fmt:: Arguments < ' _ > ) -> Result < ( ) > {
77
77
match self {
78
78
Self :: IoWrite ( handle) => handle. write_fmt ( args) . map_err ( Into :: into) ,
79
79
Self :: FmtWrite ( handle) => handle. write_fmt ( args) . map_err ( Into :: into) ,
80
80
}
81
81
}
82
+
83
+ pub ( crate ) fn flush ( & mut self ) -> Result < ( ) > {
84
+ match self {
85
+ Self :: IoWrite ( handle) => handle. flush ( ) . map_err ( Into :: into) ,
86
+ Self :: FmtWrite ( _handle) => Ok ( ( ) ) ,
87
+ }
88
+ }
82
89
}
83
90
84
- pub ( crate ) trait Printer {
91
+ pub ( crate ) trait Printer < W : io :: Write > {
85
92
fn print_header (
86
93
& mut self ,
87
- handle : & mut OutputHandle ,
94
+ handle : & mut OutputHandle < W > ,
88
95
input : & OpenedInput ,
89
96
add_header_padding : bool ,
90
97
) -> Result < ( ) > ;
91
- fn print_footer ( & mut self , handle : & mut OutputHandle , input : & OpenedInput ) -> Result < ( ) > ;
98
+ fn print_footer ( & mut self , handle : & mut OutputHandle < W > , input : & OpenedInput ) -> Result < ( ) > ;
92
99
93
- fn print_snip ( & mut self , handle : & mut OutputHandle ) -> Result < ( ) > ;
100
+ fn print_snip ( & mut self , handle : & mut OutputHandle < W > ) -> Result < ( ) > ;
94
101
95
102
fn print_line (
96
103
& mut self ,
97
104
out_of_range : bool ,
98
- handle : & mut OutputHandle ,
105
+ handle : & mut OutputHandle < W > ,
99
106
line_number : usize ,
100
107
line_buffer : & [ u8 ] ,
101
108
) -> Result < ( ) > ;
@@ -115,28 +122,28 @@ impl<'a> SimplePrinter<'a> {
115
122
}
116
123
}
117
124
118
- impl < ' a > Printer for SimplePrinter < ' a > {
125
+ impl < ' a , W : io :: Write > Printer < W > for SimplePrinter < ' a > {
119
126
fn print_header (
120
127
& mut self ,
121
- _handle : & mut OutputHandle ,
128
+ _handle : & mut OutputHandle < W > ,
122
129
_input : & OpenedInput ,
123
130
_add_header_padding : bool ,
124
131
) -> Result < ( ) > {
125
132
Ok ( ( ) )
126
133
}
127
134
128
- fn print_footer ( & mut self , _handle : & mut OutputHandle , _input : & OpenedInput ) -> Result < ( ) > {
135
+ fn print_footer ( & mut self , _handle : & mut OutputHandle < W > , _input : & OpenedInput ) -> Result < ( ) > {
129
136
Ok ( ( ) )
130
137
}
131
138
132
- fn print_snip ( & mut self , _handle : & mut OutputHandle ) -> Result < ( ) > {
139
+ fn print_snip ( & mut self , _handle : & mut OutputHandle < W > ) -> Result < ( ) > {
133
140
Ok ( ( ) )
134
141
}
135
142
136
143
fn print_line (
137
144
& mut self ,
138
145
out_of_range : bool ,
139
- handle : & mut OutputHandle ,
146
+ handle : & mut OutputHandle < W > ,
140
147
_line_number : usize ,
141
148
line_buffer : & [ u8 ] ,
142
149
) -> Result < ( ) > {
@@ -321,9 +328,9 @@ impl<'a> InteractivePrinter<'a> {
321
328
} )
322
329
}
323
330
324
- fn print_horizontal_line_term (
331
+ fn print_horizontal_line_term < W : io :: Write > (
325
332
& mut self ,
326
- handle : & mut OutputHandle ,
333
+ handle : & mut OutputHandle < W > ,
327
334
style : Style ,
328
335
) -> Result < ( ) > {
329
336
writeln ! (
@@ -334,7 +341,7 @@ impl<'a> InteractivePrinter<'a> {
334
341
Ok ( ( ) )
335
342
}
336
343
337
- fn print_horizontal_line ( & mut self , handle : & mut OutputHandle , grid_char : char ) -> Result < ( ) > {
344
+ fn print_horizontal_line < W : io :: Write > ( & mut self , handle : & mut OutputHandle < W > , grid_char : char ) -> Result < ( ) > {
338
345
if self . panel_width == 0 {
339
346
self . print_horizontal_line_term ( handle, self . colors . grid ) ?;
340
347
} else {
@@ -372,7 +379,7 @@ impl<'a> InteractivePrinter<'a> {
372
379
}
373
380
}
374
381
375
- fn print_header_component_indent ( & mut self , handle : & mut OutputHandle ) -> Result < ( ) > {
382
+ fn print_header_component_indent < W : io :: Write > ( & mut self , handle : & mut OutputHandle < W > ) -> Result < ( ) > {
376
383
if self . config . style_components . grid ( ) {
377
384
write ! (
378
385
handle,
@@ -387,18 +394,18 @@ impl<'a> InteractivePrinter<'a> {
387
394
}
388
395
}
389
396
390
- fn print_header_component_with_indent (
397
+ fn print_header_component_with_indent < W : io :: Write > (
391
398
& mut self ,
392
- handle : & mut OutputHandle ,
399
+ handle : & mut OutputHandle < W > ,
393
400
content : & str ,
394
401
) -> Result < ( ) > {
395
402
self . print_header_component_indent ( handle) ?;
396
403
writeln ! ( handle, "{content}" )
397
404
}
398
405
399
- fn print_header_multiline_component (
406
+ fn print_header_multiline_component < W : io :: Write > (
400
407
& mut self ,
401
- handle : & mut OutputHandle ,
408
+ handle : & mut OutputHandle < W > ,
402
409
content : & str ,
403
410
) -> Result < ( ) > {
404
411
let mut content = content;
@@ -446,10 +453,10 @@ impl<'a> InteractivePrinter<'a> {
446
453
}
447
454
}
448
455
449
- impl < ' a > Printer for InteractivePrinter < ' a > {
456
+ impl < ' a , W : io :: Write > Printer < W > for InteractivePrinter < ' a > {
450
457
fn print_header (
451
458
& mut self ,
452
- handle : & mut OutputHandle ,
459
+ handle : & mut OutputHandle < W > ,
453
460
input : & OpenedInput ,
454
461
add_header_padding : bool ,
455
462
) -> Result < ( ) > {
@@ -549,7 +556,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
549
556
Ok ( ( ) )
550
557
}
551
558
552
- fn print_footer ( & mut self , handle : & mut OutputHandle , _input : & OpenedInput ) -> Result < ( ) > {
559
+ fn print_footer ( & mut self , handle : & mut OutputHandle < W > , _input : & OpenedInput ) -> Result < ( ) > {
553
560
if self . config . style_components . grid ( )
554
561
&& ( self . content_type . map_or ( false , |c| c. is_text ( ) ) || self . config . show_nonprintable )
555
562
{
@@ -559,7 +566,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
559
566
}
560
567
}
561
568
562
- fn print_snip ( & mut self , handle : & mut OutputHandle ) -> Result < ( ) > {
569
+ fn print_snip ( & mut self , handle : & mut OutputHandle < W > ) -> Result < ( ) > {
563
570
let panel = self . create_fake_panel ( " ..." ) ;
564
571
let panel_count = panel. chars ( ) . count ( ) ;
565
572
@@ -586,7 +593,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
586
593
fn print_line (
587
594
& mut self ,
588
595
out_of_range : bool ,
589
- handle : & mut OutputHandle ,
596
+ handle : & mut OutputHandle < W > ,
590
597
line_number : usize ,
591
598
line_buffer : & [ u8 ] ,
592
599
) -> Result < ( ) > {
0 commit comments