From 416911a3375ac3eb1a10b9aee7a383d8fdacd4e6 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Fri, 16 Dec 2022 23:22:21 -0500 Subject: [PATCH] Output command and results on completion --- src/command.rs | 3 +-- src/main.rs | 41 ++++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/command.rs b/src/command.rs index 13c98ee..4c5db45 100644 --- a/src/command.rs +++ b/src/command.rs @@ -34,9 +34,8 @@ pub fn run(app: &mut App) { } }; - let mut child = Command::new(&app.hidden_command) + let mut child = Command::new(&app.command) .args(&app.hidden_options) - // .arg(&app.cmdline) .args(args) .stdin(Stdio::piped()) .stdout(Stdio::piped()) diff --git a/src/main.rs b/src/main.rs index d8b123d..bc7f9d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,7 +31,7 @@ use crossterm::{ /// The state of the application pub struct App { /// The actual command to be called - hidden_command: String, + command: String, // A list of hidden options passed to the command hidden_options: Vec<&'static str>, @@ -57,7 +57,7 @@ impl App { pub fn from_template(input: String, template: &Template) -> Self { let text_orig = Arc::new(input); let command_result = CommandResult::default(); - let hidden_command = template.command(); + let command = template.command(); let cmdline_position = 0; match template { @@ -67,7 +67,7 @@ impl App { text_orig, command_result: CommandResult::default(), autorun: true, - hidden_command, + command, hidden_options: vec![], }, Template::Jq => Self { @@ -76,7 +76,7 @@ impl App { text_orig, command_result, autorun: true, - hidden_command, + command, hidden_options: vec!["-C"], }, Template::Grep | Template::Rg => Self { @@ -85,7 +85,7 @@ impl App { text_orig, command_result: CommandResult::default(), autorun: true, - hidden_command, + command, hidden_options: vec!["--color=always"], }, Template::Sed => Self { @@ -94,7 +94,7 @@ impl App { text_orig, command_result: CommandResult::default(), autorun: true, - hidden_command, + command, hidden_options: vec![], }, Template::Awk => Self { @@ -103,17 +103,17 @@ impl App { text_orig, command_result: CommandResult::default(), autorun: true, - hidden_command, + command, hidden_options: vec![], }, Template::Perl => Self { - cmdline: String::from("'s/./_/'"), + cmdline: String::from("-p -e 's/./_/'"), cmdline_position: 3_u16, text_orig, command_result: CommandResult::default(), autorun: true, - hidden_command, - hidden_options: vec!["-p", "-e"], + command, + hidden_options: vec![], }, } } @@ -179,7 +179,7 @@ fn main() -> Result<()> { // Run the actual application let app = App::from_template(text_orig, &Template::from(arg)); - let _res = run_app(&mut terminal, app); + let res = run_app(&mut terminal, app); // Restore terminal disable_raw_mode()?; @@ -190,10 +190,21 @@ fn main() -> Result<()> { )?; terminal.show_cursor()?; + let res = res?; + + if let Some(res) = res { + std::io::stderr().write_all(res.0.as_bytes())?; + std::io::stderr().write_all(b"\n")?; + std::io::stdout().write_all(&res.1)?; + } + Ok(()) } -fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result> { +fn run_app( + terminal: &mut Terminal, + mut app: App, +) -> io::Result)>> { if !app.cmdline.is_empty() { command::run(&mut app); } @@ -208,6 +219,7 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result { + // TODO: If there is any command line text, ask if the user is sure they want to quit return Ok(None); } KeyCode::Char(c) => { @@ -243,7 +255,10 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result { - return Ok(Some(app.cmdline.clone())); + return Ok(Some(( + format!("{} {}", app.command, app.cmdline), + app.command_result.stdout, + ))); } KeyCode::Left => { app.cmdline_position = app.cmdline_position.saturating_sub(1);