11use std:: {
2- io:: { IsTerminal , Read } ,
2+ io:: { self , IsTerminal , Read } ,
33 sync:: {
44 atomic:: { AtomicBool , Ordering } ,
55 Arc , Mutex ,
@@ -25,9 +25,45 @@ use crate::{
2525
2626use super :: utils;
2727
28- pub fn dump_ast ( _shell : & mut Shell , args : DebugDumpAstArgs ) -> anyhow:: Result < ( ) > {
28+ fn format_input_highlighted ( input : & Input , command : Option < & Command > ) -> String {
29+ if !syntax_highlight:: supports_color ( ) {
30+ return format ! ( "{:#}" , input) ;
31+ }
32+
33+ match input {
34+ Input :: Json ( j) => {
35+ let json = serde_json:: to_string_pretty ( j) . unwrap ( ) ;
36+ syntax_highlight:: highlight_to_terminal ( & json, "json" )
37+ }
38+ Input :: String ( s) => {
39+ let syntax = command
40+ . and_then ( |cmd| cmd. kind . as_deref ( ) )
41+ . filter ( |k| * k == "cg3" ) ;
42+
43+ if let Some ( "cg3" ) = syntax {
44+ syntax_highlight:: highlight_to_terminal ( s, "cg3" )
45+ } else {
46+ s. clone ( )
47+ }
48+ }
49+ _ => format ! ( "{:#}" , input) ,
50+ }
51+ }
52+
53+ fn print_input_highlighted (
54+ shell : & mut Shell ,
55+ input : & Input ,
56+ command : Option < & Command > ,
57+ ) -> anyhow:: Result < ( ) > {
58+ let formatted = format_input_highlighted ( input, command) ;
59+ io:: Write :: write_all ( shell. out ( ) , formatted. as_bytes ( ) ) ?;
60+ Ok ( ( ) )
61+ }
62+
63+ pub fn dump_ast ( shell : & mut Shell , args : DebugDumpAstArgs ) -> anyhow:: Result < ( ) > {
2964 let value = crate :: deno_rt:: dump_ast ( & std:: fs:: read_to_string ( args. path ) ?) ?;
30- println ! ( "{}" , serde_json:: to_string_pretty( & value) . unwrap( ) ) ;
65+ let json = serde_json:: to_string_pretty ( & value) . unwrap ( ) ;
66+ shell. print_highlighted_stdout ( & json, "json" ) ?;
3167 Ok ( ( ) )
3268}
3369
@@ -117,7 +153,12 @@ async fn run_repl(
117153 let tap_stepping = tap_stepping. clone ( ) ;
118154
119155 println ! ( "\x1b [41;31m[{}]\x1b [0m {}" , key, cmd) ;
120- println ! ( "\x1b [33m{:#}\x1b [0m" , event) ;
156+ match event {
157+ InputEvent :: Input ( input) => {
158+ println ! ( "{}" , format_input_highlighted( input, Some ( cmd) ) ) ;
159+ }
160+ _ => println ! ( "{:#}" , event) ,
161+ }
121162
122163 // Store the event for the current run
123164 if let Ok ( mut events) = current_events_clone. lock ( ) {
@@ -211,10 +252,9 @@ async fn run_repl(
211252 println ! ( ) ;
212253 }
213254 ":ast" => {
214- println ! (
215- "{}\n " ,
216- serde_json:: to_string_pretty( & * * bundle. definition( ) ) . unwrap( )
217- ) ;
255+ let json = serde_json:: to_string_pretty ( & * * bundle. definition ( ) ) . unwrap ( ) ;
256+ shell. print_highlighted_stdout ( & json, "json" ) ?;
257+ println ! ( ) ;
218258 }
219259 ":step" => {
220260 let cur = is_stepping
@@ -228,7 +268,9 @@ async fn run_repl(
228268 }
229269 }
230270 ":config" => {
231- println ! ( "{}\n " , serde_json:: to_string_pretty( & config) . unwrap( ) ) ;
271+ let json = serde_json:: to_string_pretty ( & config) . unwrap ( ) ;
272+ shell. print_highlighted_stdout ( & json, "json" ) ?;
273+ println ! ( ) ;
232274 }
233275 ":save" => {
234276 let filename = chunks. next ( ) . unwrap_or ( "pipeline_debug.md" ) ;
@@ -309,10 +351,13 @@ async fn run_repl(
309351 // };
310352 let mut stream = pipe. forward ( Input :: String ( line. to_string ( ) ) ) . await ;
311353
354+ let output_cmd = bundle. definition ( ) . output . resolve ( bundle. definition ( ) ) ;
355+
312356 while let Some ( input) = stream. next ( ) . await {
313357 match input {
314358 Ok ( input) => {
315- shell. print ( & "<-" , Some ( & format ! ( "{:#}" , input) ) , Color :: Green , false ) ?;
359+ shell. print ( & "<-" , None , Color :: Green , false ) ?;
360+ print_input_highlighted ( shell, & input, output_cmd) ?;
316361
317362 if let Some ( path) = args. output_path . as_deref ( ) {
318363 match input {
@@ -519,8 +564,10 @@ pub async fn run(shell: &mut Shell, mut args: RunArgs) -> Result<(), Arc<anyhow:
519564 if let Some ( input) = args. input {
520565 let mut stream = pipe. forward ( Input :: String ( input) ) . await ;
521566
567+ let output_cmd = bundle. definition ( ) . output . resolve ( bundle. definition ( ) ) ;
568+
522569 while let Some ( Ok ( input) ) = stream. next ( ) . await {
523- println ! ( "{:#}" , input) ;
570+ print_input_highlighted ( shell , & input, output_cmd ) ? ;
524571 }
525572
526573 // if let Some(path) = args.output_path.as_deref() {
0 commit comments