1
1
use std:: io:: Write ;
2
2
use std:: path:: PathBuf ;
3
3
use std:: process;
4
- use std:: process:: Stdio ;
4
+ use std:: process:: { ExitStatus , Stdio } ;
5
5
6
6
use console:: Term ;
7
7
use miette:: IntoDiagnostic ;
@@ -10,20 +10,20 @@ use ockam_core::env::get_env_with_default;
10
10
11
11
use crate :: util:: exitcode;
12
12
13
- pub fn render_help ( help : clap :: Error ) {
13
+ pub fn render_output ( s : & str ) {
14
14
let pager = get_env_with_default ( "PAGER" , "less" . to_string ( ) ) . expect ( "Invalid PAGER value" ) ;
15
15
match which:: which ( pager) {
16
16
Ok ( pager_binary_path) => {
17
- paginate_with ( pager_binary_path, help ) . expect ( "Failed to paginate help " ) ;
17
+ paginate_with ( pager_binary_path, s ) . expect ( "Failed to paginate output " ) ;
18
18
}
19
- // The pager binary was not found, so we just print the help without pagination
19
+ // The pager binary was not found, so we just print the output without pagination
20
20
Err ( _) => {
21
- help . exit ( ) ;
21
+ println ! ( "{}" , s ) ;
22
22
}
23
23
}
24
24
}
25
25
26
- fn paginate_with ( pager_binary_path : PathBuf , help : clap :: Error ) -> miette:: Result < ( ) > {
26
+ fn paginate_with ( pager_binary_path : PathBuf , s : & str ) -> miette:: Result < ExitStatus > {
27
27
let pager = pager_binary_path. file_name ( ) . unwrap ( ) . to_string_lossy ( ) ;
28
28
let mut pager_cmd = process:: Command :: new ( pager. as_ref ( ) ) ;
29
29
if pager. as_ref ( ) == "less" {
@@ -39,18 +39,33 @@ fn paginate_with(pager_binary_path: PathBuf, help: clap::Error) -> miette::Resul
39
39
. stdin
40
40
. take ( )
41
41
. expect ( "Failed to get pager's stdin" ) ;
42
- // Strip ANSI escape sequences if stdout is not a TTY (e.g. when piping to another command)
43
- let rendered_text = if Term :: stdout ( ) . is_term ( ) {
44
- help. render ( ) . ansi ( ) . to_string ( )
45
- } else {
46
- help. render ( ) . to_string ( )
47
- } ;
48
42
// Write the rendered text to the pager's stdin
49
- pager_stdin
50
- . write_all ( rendered_text. as_bytes ( ) )
51
- . into_diagnostic ( ) ?;
43
+ pager_stdin. write_all ( s. as_bytes ( ) ) . into_diagnostic ( ) ?;
52
44
}
53
- let _ = pager_process. wait ( ) ;
45
+ pager_process. wait ( ) . into_diagnostic ( )
46
+ }
47
+
48
+ pub fn render_help ( help : clap:: Error ) {
49
+ let pager = get_env_with_default ( "PAGER" , "less" . to_string ( ) ) . expect ( "Invalid PAGER value" ) ;
50
+ match which:: which ( pager) {
51
+ Ok ( pager_binary_path) => {
52
+ paginate_help_with ( pager_binary_path, help) . expect ( "Failed to paginate help" ) ;
53
+ }
54
+ // The pager binary was not found, so we just print the help without pagination
55
+ Err ( _) => {
56
+ help. exit ( ) ;
57
+ }
58
+ }
59
+ }
60
+
61
+ fn paginate_help_with ( pager_binary_path : PathBuf , help : clap:: Error ) -> miette:: Result < ( ) > {
62
+ // Strip ANSI escape sequences if stdout is not a TTY (e.g. when piping to another command)
63
+ let rendered_text = if Term :: stdout ( ) . is_term ( ) {
64
+ help. render ( ) . ansi ( ) . to_string ( )
65
+ } else {
66
+ help. render ( ) . to_string ( )
67
+ } ;
68
+ paginate_with ( pager_binary_path, & rendered_text) ?;
54
69
let code = if help. use_stderr ( ) {
55
70
exitcode:: USAGE
56
71
} else {
0 commit comments