diff --git a/src/arguments.rs b/src/arguments.rs index f3c632d..0a46cd3 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -17,6 +17,8 @@ pub(crate) struct Arguments { pub(crate) pretty: bool, #[clap(long, help = "Remove commands within markdown documents.")] pub(crate) remove: bool, + #[clap(short, long, help = "Show output of commands being executed.")] + pub(crate) verbose: bool, } impl Arguments { @@ -28,7 +30,7 @@ impl Arguments { .remove(self.remove) .interactive(self.interactive); - file.present()?; + file.present(self.verbose)?; match self.in_place { true => file.save()?, diff --git a/src/command.rs b/src/command.rs index 30cb916..c4b02ae 100644 --- a/src/command.rs +++ b/src/command.rs @@ -19,7 +19,7 @@ impl Command { }) } - pub(crate) fn execute(&self) -> Result { + pub(crate) fn execute(&self, verbose: bool) -> Result { let output = process::Command::new(&self.program) .args(&self.arguments) .output(); @@ -40,6 +40,12 @@ impl Command { }); } - Ok(String::from_utf8(output.stdout)?) + let stdout_str = String::from_utf8(output.stdout)?; + if verbose { + println!("> {} {}", &self.program, self.arguments.join(" ")); + println!("{}", &stdout_str); + } + + Ok(stdout_str) } } diff --git a/src/file.rs b/src/file.rs index 52b968b..107d21c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -71,10 +71,13 @@ impl File { /// /// The [`Diff`]s are returned as results. If the command fails, the item will /// be of the `Err` kind. - pub fn diffs(&self) -> impl Iterator> + '_ { - self.codeblocks.iter().map(|codeblock| { + pub fn diffs( + &self, + verbose: bool, + ) -> impl Iterator> + '_ { + self.codeblocks.iter().map(move |codeblock| { Ok(Diff { - content: codeblock.command.execute()?, + content: codeblock.command.execute(verbose)?, range: match self.remove { // Replace the entire codeblock with `stdout` true => { @@ -93,10 +96,10 @@ impl File { /// /// If [`interactive`](File::interactive) is set to `true`, the user will be /// asked if they want to apply the change for each diff. - pub fn present(&mut self) -> Result { + pub fn present(&mut self, verbose: bool) -> Result { let mut offset: isize = 0; - let diffs = self.diffs().collect::>>()?; + let diffs = self.diffs(verbose).collect::>>()?; for mut diff in diffs { let prev = self.content.len_bytes();