@@ -27,6 +27,9 @@ fn main() -> Result<(), Error> {
27
27
None => find_repo_root ( ) ?,
28
28
} ;
29
29
30
+ #[ cfg( unix) ]
31
+ setup_output_formatting ( ) ;
32
+
30
33
let libs = vec ! [
31
34
repo_root. clone( ) . join( "library/core" ) ,
32
35
repo_root. clone( ) . join( "library/alloc" ) ,
@@ -52,3 +55,34 @@ fn find_repo_root() -> Result<PathBuf, Error> {
52
55
path. pop ( ) ;
53
56
Ok ( path)
54
57
}
58
+
59
+ #[ cfg( unix) ]
60
+ fn setup_output_formatting ( ) {
61
+ use nix:: unistd:: { isatty, dup2} ;
62
+ use std:: os:: unix:: io:: AsRawFd ;
63
+ use std:: process:: { Command , Stdio } ;
64
+
65
+ if isatty ( 1 ) == Ok ( true ) {
66
+ // Pipe the output through `bat` for nice formatting and paging, if available.
67
+ if let Ok ( mut bat) = Command :: new ( "bat" )
68
+ . arg ( "--language=rust" )
69
+ . arg ( "--plain" ) // Disable line numbers for easy copy-pasting.
70
+ . stdin ( Stdio :: piped ( ) )
71
+ . stdout ( Stdio :: inherit ( ) )
72
+ . spawn ( )
73
+ {
74
+ // Replace our stdout by the pipe into `bat`.
75
+ dup2 ( bat. stdin . take ( ) . unwrap ( ) . as_raw_fd ( ) , 1 ) . unwrap ( ) ;
76
+ }
77
+ }
78
+
79
+ // Pipe the output through `rustfmt`, if available.
80
+ if let Ok ( mut rustfmt) = Command :: new ( "rustfmt" )
81
+ . stdin ( Stdio :: piped ( ) )
82
+ . stdout ( Stdio :: inherit ( ) ) // This pipes into `bat` if it was executed above.
83
+ . spawn ( )
84
+ {
85
+ // Replace our stdout by the pipe into `rustfmt`.
86
+ dup2 ( rustfmt. stdin . take ( ) . unwrap ( ) . as_raw_fd ( ) , 1 ) . unwrap ( ) ;
87
+ }
88
+ }
0 commit comments