@@ -18,6 +18,7 @@ use crate::{
1818} ;
1919use anyhow:: { Result , anyhow} ;
2020use arrow:: datatypes:: SchemaRef ;
21+ use camino:: Utf8Path ;
2122use glob:: glob;
2223use owo_colors:: OwoColorize ;
2324use tabled:: { builder:: Builder , settings:: Style } ;
@@ -31,6 +32,7 @@ pub async fn run(args: TransformCommand) -> Result<()> {
3132 by,
3233 exclude_columns,
3334 list_outputs,
35+ list_outputs_file,
3436 create_dirs,
3537 overwrite,
3638 query,
@@ -249,15 +251,19 @@ pub async fn run(args: TransformCommand) -> Result<()> {
249251 let files = pipeline. execute ( ) . await ?;
250252
251253 if let Some ( format) = list_outputs_format {
252- print_output_files ( & files, format) ?;
254+ print_output_files ( & files, format, list_outputs_file . as_deref ( ) ) ?;
253255 }
254256
255257 Ok ( ( ) )
256258}
257259
258- fn print_output_files ( files : & [ OutputFileInfo ] , format : ListOutputsFormat ) -> Result < ( ) > {
259- match format {
260- ListOutputsFormat :: None => { }
260+ fn print_output_files (
261+ files : & [ OutputFileInfo ] ,
262+ format : ListOutputsFormat ,
263+ output_path : Option < & Utf8Path > ,
264+ ) -> Result < ( ) > {
265+ let output = match format {
266+ ListOutputsFormat :: None => return Ok ( ( ) ) ,
261267 ListOutputsFormat :: Text => {
262268 if files. is_empty ( ) {
263269 return Ok ( ( ) ) ;
@@ -277,8 +283,14 @@ fn print_output_files(files: &[OutputFileInfo], format: ListOutputsFormat) -> Re
277283 header. push ( "Path" . to_string ( ) ) ;
278284 header. push ( "Row Count" . to_string ( ) ) ;
279285
280- let colored_header: Vec < String > = header. iter ( ) . map ( |h| h. bold ( ) . to_string ( ) ) . collect ( ) ;
281- builder. push_record ( colored_header) ;
286+ if output_path. is_none ( ) {
287+ // writing to stdout, so use colors
288+ let colored_header: Vec < String > =
289+ header. iter ( ) . map ( |h| h. bold ( ) . to_string ( ) ) . collect ( ) ;
290+ builder. push_record ( colored_header) ;
291+ } else {
292+ builder. push_record ( header) ;
293+ }
282294
283295 // sort by path for consistent output
284296 let mut sorted_files = files. to_vec ( ) ;
@@ -288,21 +300,38 @@ fn print_output_files(files: &[OutputFileInfo], format: ListOutputsFormat) -> Re
288300 let mut row: Vec < String > = file
289301 . partition_values
290302 . iter ( )
291- . map ( |pv| format_json_value ( & pv. value ) . green ( ) . to_string ( ) )
303+ . map ( |pv| {
304+ let val = format_json_value ( & pv. value ) ;
305+ if output_path. is_none ( ) {
306+ // again, writing to stdout, so use colors
307+ val. green ( ) . to_string ( )
308+ } else {
309+ val
310+ }
311+ } )
292312 . collect ( ) ;
293313 row. push ( file. path . clone ( ) ) ;
294- row. push ( file. row_count . to_string ( ) . cyan ( ) . to_string ( ) ) ;
314+ let row_count = file. row_count . to_string ( ) ;
315+ if output_path. is_none ( ) {
316+ // once again, writing to stdout, so use colors
317+ row. push ( row_count. cyan ( ) . to_string ( ) ) ;
318+ } else {
319+ row. push ( row_count) ;
320+ }
295321 builder. push_record ( row) ;
296322 }
297323
298- let table = builder. build ( ) . with ( Style :: rounded ( ) ) . to_string ( ) ;
299- println ! ( "{}" , table) ;
300- }
301- ListOutputsFormat :: Json => {
302- let json = serde_json:: to_string_pretty ( files) ?;
303- println ! ( "{}" , json) ;
324+ builder. build ( ) . with ( Style :: rounded ( ) ) . to_string ( )
304325 }
326+ ListOutputsFormat :: Json => serde_json:: to_string_pretty ( files) ?,
327+ } ;
328+
329+ if let Some ( path) = output_path {
330+ std:: fs:: write ( path, output) ?;
331+ } else {
332+ println ! ( "{}" , output) ;
305333 }
334+
306335 Ok ( ( ) )
307336}
308337
0 commit comments