1
1
use std:: { env, path:: PathBuf } ;
2
2
3
- use anyhow:: { Context , Error } ;
3
+ use anyhow:: { bail , Context , Error } ;
4
4
use structopt:: StructOpt ;
5
5
6
6
mod util;
@@ -27,20 +27,21 @@ fn main() -> Result<(), Error> {
27
27
None => find_repo_root ( ) ?,
28
28
} ;
29
29
30
- #[ cfg( unix) ]
31
- setup_output_formatting ( ) ;
30
+ let feature = opt. feature ;
32
31
33
32
let libs = vec ! [
34
33
repo_root. clone( ) . join( "library/core" ) ,
35
34
repo_root. clone( ) . join( "library/alloc" ) ,
36
35
repo_root. clone( ) . join( "library/std" ) ,
37
36
] ;
38
37
39
- for crate_root in libs {
40
- visit:: pub_unstable ( crate_root, & opt. feature ) ?;
41
- }
38
+ with_output_formatting_maybe ( move || {
39
+ for crate_root in libs {
40
+ visit:: pub_unstable ( crate_root, & feature) ?;
41
+ }
42
42
43
- Ok ( ( ) )
43
+ Ok ( ( ) )
44
+ } )
44
45
}
45
46
46
47
fn find_repo_root ( ) -> Result < PathBuf , Error > {
@@ -56,12 +57,46 @@ fn find_repo_root() -> Result<PathBuf, Error> {
56
57
Ok ( path)
57
58
}
58
59
60
+ #[ cfg( not( unix) ) ]
61
+ fn with_output_formatting_maybe < F > ( f : F ) -> Result < ( ) , Error >
62
+ where
63
+ F : FnOnce ( ) -> Result < ( ) , Error > ,
64
+ {
65
+ f ( )
66
+ }
67
+
59
68
#[ cfg( unix) ]
60
- fn setup_output_formatting ( ) {
69
+ fn with_output_formatting_maybe < F > ( f : F ) -> Result < ( ) , Error >
70
+ where
71
+ F : FnOnce ( ) -> Result < ( ) , Error > ,
72
+ {
73
+ use nix:: unistd:: close;
74
+ use std:: io:: { stdout, Write } ;
75
+ use std:: os:: unix:: io:: AsRawFd ;
76
+ let child = spawn_output_formatter ( ) ;
77
+
78
+ let result = f ( ) ;
79
+ let mut stdout = stdout ( ) ;
80
+ stdout. flush ( ) . context ( "couldn't flush stdout" ) ?;
81
+ close ( stdout. as_raw_fd ( ) ) . context ( "couldn't close stdout" ) ?;
82
+
83
+ if let Some ( mut child) = child {
84
+ if !child. wait ( ) ?. success ( ) {
85
+ bail ! ( "output formatting failed" ) ;
86
+ }
87
+ }
88
+
89
+ result
90
+ }
91
+
92
+ #[ cfg( unix) ]
93
+ fn spawn_output_formatter ( ) -> Option < std:: process:: Child > {
61
94
use nix:: unistd:: { isatty, dup2} ;
62
95
use std:: os:: unix:: io:: AsRawFd ;
63
96
use std:: process:: { Command , Stdio } ;
64
97
98
+ let mut final_child = None ;
99
+
65
100
if isatty ( 1 ) == Ok ( true ) {
66
101
// Pipe the output through `bat` for nice formatting and paging, if available.
67
102
if let Ok ( mut bat) = Command :: new ( "bat" )
@@ -73,6 +108,7 @@ fn setup_output_formatting() {
73
108
{
74
109
// Replace our stdout by the pipe into `bat`.
75
110
dup2 ( bat. stdin . take ( ) . unwrap ( ) . as_raw_fd ( ) , 1 ) . unwrap ( ) ;
111
+ final_child = Some ( bat) ;
76
112
}
77
113
}
78
114
@@ -84,5 +120,8 @@ fn setup_output_formatting() {
84
120
{
85
121
// Replace our stdout by the pipe into `rustfmt`.
86
122
dup2 ( rustfmt. stdin . take ( ) . unwrap ( ) . as_raw_fd ( ) , 1 ) . unwrap ( ) ;
123
+ final_child. get_or_insert ( rustfmt) ;
87
124
}
125
+
126
+ final_child
88
127
}
0 commit comments