@@ -24,6 +24,24 @@ pub trait CommandDisplay: Display {
2424 /// );
2525 /// ```
2626 fn program ( & self ) -> Cow < ' _ , str > ;
27+
28+ /// The command's program name, shell-quoted.
29+ ///
30+ /// ```
31+ /// # use std::process::Command;
32+ /// # use command_error::Utf8ProgramAndArgs;
33+ /// # use command_error::CommandDisplay;
34+ /// let command = Command::new("ooga booga");
35+ /// let displayed: Utf8ProgramAndArgs = (&command).into();
36+ /// assert_eq!(
37+ /// displayed.program_quoted(),
38+ /// "'ooga booga'",
39+ /// );
40+ /// ```
41+ fn program_quoted ( & self ) -> Cow < ' _ , str > {
42+ Cow :: Owned ( shell_words:: quote ( & self . program ( ) ) . into_owned ( ) )
43+ }
44+
2745 /// The command's arguments, decoded as UTF-8.
2846 ///
2947 /// ```
@@ -43,10 +61,8 @@ pub trait CommandDisplay: Display {
4361
4462/// A program name and arguments stored as UTF-8 [`String`]s.
4563///
46- /// By default (with the `shell-words` feature enabled), the program name and arguments are
47- /// shell-quoted when [`Display`]ed, so that spaces are escaped and the displayed command can
48- /// generally be pasted directly into a shell. Otherwise, the program and arguments are formatted
49- /// with [`Debug`].
64+ /// The program name and arguments are shell-quoted when [`Display`]ed, so that spaces are escaped
65+ /// and the displayed command can generally be pasted directly into a shell.
5066///
5167/// ```
5268/// # use std::process::Command;
@@ -66,7 +82,6 @@ pub struct Utf8ProgramAndArgs {
6682 args : Vec < String > ,
6783}
6884
69- #[ cfg( feature = "shell-words" ) ]
7085impl Display for Utf8ProgramAndArgs {
7186 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
7287 write ! ( f, "{}" , shell_words:: quote( & self . program) ) ?;
@@ -77,22 +92,15 @@ impl Display for Utf8ProgramAndArgs {
7792 }
7893}
7994
80- #[ cfg( not( feature = "shell-words" ) ) ]
81- impl Display for Utf8ProgramAndArgs {
82- fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
83- write ! ( f, "{:?}" , & self . program) ?;
84- for arg in & self . args {
85- write ! ( f, " {arg:?}" ) ?;
86- }
87- Ok ( ( ) )
88- }
89- }
90-
9195impl CommandDisplay for Utf8ProgramAndArgs {
9296 fn program ( & self ) -> std:: borrow:: Cow < ' _ , str > {
9397 Cow :: Borrowed ( & self . program )
9498 }
9599
100+ fn program_quoted ( & self ) -> Cow < ' _ , str > {
101+ shell_words:: quote ( & self . program )
102+ }
103+
96104 fn args ( & self ) -> Box < ( dyn Iterator < Item = Cow < ' _ , str > > + ' _ ) > {
97105 Box :: new ( self . args . iter ( ) . map ( |arg| Cow :: Borrowed ( arg. as_str ( ) ) ) )
98106 }
0 commit comments