@@ -9,7 +9,6 @@ use lscolors::{Indicator, LsColors, Style};
99use crate :: cli:: OutputFormat ;
1010use crate :: config:: Config ;
1111use crate :: dir_entry:: DirEntry ;
12- use crate :: exit_codes:: ExitCode ;
1312use crate :: fmt:: FormatTemplate ;
1413use crate :: hyperlink:: PathUrl ;
1514
@@ -45,42 +44,11 @@ struct FileDetail<'a> {
4544pub struct Printer < ' a , W > {
4645 config : & ' a Config ,
4746 pub stdout : W ,
48- started : bool ,
4947}
5048
5149impl < ' a , W : Write > Printer < ' a , W > {
5250 pub fn new ( config : & ' a Config , stdout : W ) -> Self {
53- Self {
54- config,
55- stdout,
56- started : false ,
57- }
58- }
59-
60- /// Begin JSON array output if in JSON format.
61- /// Returns an error if writing to output fails.
62- pub fn begin ( & mut self ) -> Result < ( ) , ExitCode > {
63- if self . config . output == OutputFormat :: Json
64- && let Err ( e) = writeln ! ( self . stdout, "[" )
65- && e. kind ( ) != :: std:: io:: ErrorKind :: BrokenPipe
66- {
67- crate :: error:: print_error ( format ! ( "Could not write to output: {e}" ) ) ;
68- return Err ( ExitCode :: GeneralError ) ;
69- }
70- Ok ( ( ) )
71- }
72-
73- /// End JSON array output if in JSON format.
74- /// Returns an error if writing to output fails.
75- pub fn end ( & mut self ) -> Result < ( ) , ExitCode > {
76- if self . config . output == OutputFormat :: Json
77- && let Err ( e) = writeln ! ( self . stdout, "\n ]" )
78- && e. kind ( ) != :: std:: io:: ErrorKind :: BrokenPipe
79- {
80- crate :: error:: print_error ( format ! ( "Could not write to output: {e}" ) ) ;
81- return Err ( ExitCode :: GeneralError ) ;
82- }
83- Ok ( ( ) )
51+ Self { config, stdout }
8452 }
8553
8654 // TODO: this function is performance critical and can probably be optimized
@@ -94,37 +62,19 @@ impl<'a, W: Write> Printer<'a, W> {
9462 }
9563 match (
9664 & self . config . format ,
97- & self . config . output ,
9865 & self . config . jsonl ,
9966 & self . config . ls_colors ,
10067 ) {
101- ( Some ( template) , _, _, _) => self . print_entry_format ( entry, template) ?,
102- ( None , _, true , _) => self . print_entry_detail ( OutputFormat :: Jsonl , entry) ?,
103- ( None , OutputFormat :: Json , false , _) => {
104- self . print_entry_detail ( OutputFormat :: Json , entry) ?
105- }
106- ( None , OutputFormat :: Yaml , false , _) => {
107- self . print_entry_detail ( OutputFormat :: Yaml , entry) ?
108- }
109- ( None , OutputFormat :: Jsonl , false , _) => {
110- self . print_entry_detail ( OutputFormat :: Jsonl , entry) ?
111- }
112- ( None , OutputFormat :: Plain , false , Some ( ls_colors) ) => {
113- self . print_entry_colorized ( entry, ls_colors) ?
114- }
115- ( None , OutputFormat :: Plain , false , None ) => self . print_entry_uncolorized ( entry) ?,
68+ ( Some ( template) , _, _) => self . print_entry_format ( entry, template) ?,
69+ ( None , true , _) => self . print_entry_detail ( OutputFormat :: Jsonl , entry) ?,
70+ ( None , false , Some ( ls_colors) ) => self . print_entry_colorized ( entry, ls_colors) ?,
71+ ( None , false , None ) => self . print_entry_uncolorized ( entry) ?,
11672 } ;
11773
11874 if has_hyperlink {
11975 write ! ( self . stdout, "\x1B ]8;;\x1B \\ " ) ?;
12076 }
12177
122- self . started = true ;
123-
124- if matches ! ( self . config. output, OutputFormat :: Json ) {
125- return Ok ( ( ) ) ;
126- }
127-
12878 if self . config . null_separator {
12979 write ! ( self . stdout, "\0 " )
13080 } else {
@@ -234,54 +184,7 @@ impl<'a, W: Write> Printer<'a, W> {
234184 }
235185 }
236186
237- fn print_entry_yaml_obj ( & mut self , detail : & FileDetail ) -> io:: Result < ( ) > {
238- // Manually construct a simple YAML representation
239- // to avoid adding a dependency on serde_yaml (deprecated).
240- //
241- // Write YAML fragments directly to stdout (should be buffered)
242- write ! ( self . stdout, "- " ) ?;
243-
244- match & detail. path {
245- PathEncoding :: Utf8 ( path) => {
246- writeln ! ( self . stdout, "path: \" {}\" " , path) ?;
247- }
248- PathEncoding :: Bytes ( bytes) => {
249- writeln ! (
250- self . stdout,
251- "path_base64: \" {}\" " ,
252- BASE64_STANDARD . encode( bytes)
253- ) ?;
254- }
255- }
256-
257- writeln ! ( self . stdout, " type: {}" , detail. file_type) ?;
258-
259- if let Some ( size) = detail. size {
260- writeln ! ( self . stdout, " size: {}" , size) ?;
261- }
262- if let Some ( mode) = detail. mode {
263- writeln ! ( self . stdout, " mode: 0o{mode:o}" ) ?;
264- }
265- if let Some ( modified) = & detail. modified {
266- writeln ! ( self . stdout, " modified: \" {}\" " , modified) ?;
267- }
268- if let Some ( accessed) = & detail. accessed {
269- writeln ! ( self . stdout, " accessed: \" {}\" " , accessed) ?;
270- }
271- if let Some ( created) = & detail. created {
272- writeln ! ( self . stdout, " created: \" {}\" " , created) ?;
273- }
274- Ok ( ( ) )
275- }
276-
277- fn print_entry_json_obj ( & mut self , detail : & FileDetail , comma : bool ) -> io:: Result < ( ) > {
278- if self . started && comma {
279- writeln ! ( self . stdout, "," ) ?;
280- }
281-
282- if comma {
283- write ! ( self . stdout, " " ) ?;
284- }
187+ fn print_entry_json_obj ( & mut self , detail : & FileDetail ) -> io:: Result < ( ) > {
285188 write ! ( self . stdout, "{{" ) ?;
286189
287190 match & detail. path {
@@ -380,9 +283,7 @@ impl<'a, W: Write> Printer<'a, W> {
380283 }
381284 } ;
382285 match format {
383- OutputFormat :: Json => self . print_entry_json_obj ( & detail, true ) ,
384- OutputFormat :: Jsonl => self . print_entry_json_obj ( & detail, false ) ,
385- OutputFormat :: Yaml => self . print_entry_yaml_obj ( & detail) ,
286+ OutputFormat :: Jsonl => self . print_entry_json_obj ( & detail) ,
386287 OutputFormat :: Plain => unreachable ! ( "Plain format should not call print_entry_detail" ) ,
387288 }
388289 }
0 commit comments