Output command and results on completion

This commit is contained in:
Austen Adler 2022-12-16 23:22:21 -05:00
parent c57b75735d
commit 416911a337
2 changed files with 29 additions and 15 deletions

View File

@ -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) .args(&app.hidden_options)
// .arg(&app.cmdline)
.args(args) .args(args)
.stdin(Stdio::piped()) .stdin(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())

View File

@ -31,7 +31,7 @@ use crossterm::{
/// The state of the application /// The state of the application
pub struct App { pub struct App {
/// The actual command to be called /// The actual command to be called
hidden_command: String, command: String,
// A list of hidden options passed to the command // A list of hidden options passed to the command
hidden_options: Vec<&'static str>, hidden_options: Vec<&'static str>,
@ -57,7 +57,7 @@ impl App {
pub fn from_template(input: String, template: &Template) -> Self { pub fn from_template(input: String, template: &Template) -> Self {
let text_orig = Arc::new(input); let text_orig = Arc::new(input);
let command_result = CommandResult::default(); let command_result = CommandResult::default();
let hidden_command = template.command(); let command = template.command();
let cmdline_position = 0; let cmdline_position = 0;
match template { match template {
@ -67,7 +67,7 @@ impl App {
text_orig, text_orig,
command_result: CommandResult::default(), command_result: CommandResult::default(),
autorun: true, autorun: true,
hidden_command, command,
hidden_options: vec![], hidden_options: vec![],
}, },
Template::Jq => Self { Template::Jq => Self {
@ -76,7 +76,7 @@ impl App {
text_orig, text_orig,
command_result, command_result,
autorun: true, autorun: true,
hidden_command, command,
hidden_options: vec!["-C"], hidden_options: vec!["-C"],
}, },
Template::Grep | Template::Rg => Self { Template::Grep | Template::Rg => Self {
@ -85,7 +85,7 @@ impl App {
text_orig, text_orig,
command_result: CommandResult::default(), command_result: CommandResult::default(),
autorun: true, autorun: true,
hidden_command, command,
hidden_options: vec!["--color=always"], hidden_options: vec!["--color=always"],
}, },
Template::Sed => Self { Template::Sed => Self {
@ -94,7 +94,7 @@ impl App {
text_orig, text_orig,
command_result: CommandResult::default(), command_result: CommandResult::default(),
autorun: true, autorun: true,
hidden_command, command,
hidden_options: vec![], hidden_options: vec![],
}, },
Template::Awk => Self { Template::Awk => Self {
@ -103,17 +103,17 @@ impl App {
text_orig, text_orig,
command_result: CommandResult::default(), command_result: CommandResult::default(),
autorun: true, autorun: true,
hidden_command, command,
hidden_options: vec![], hidden_options: vec![],
}, },
Template::Perl => Self { Template::Perl => Self {
cmdline: String::from("'s/./_/'"), cmdline: String::from("-p -e 's/./_/'"),
cmdline_position: 3_u16, cmdline_position: 3_u16,
text_orig, text_orig,
command_result: CommandResult::default(), command_result: CommandResult::default(),
autorun: true, autorun: true,
hidden_command, command,
hidden_options: vec!["-p", "-e"], hidden_options: vec![],
}, },
} }
} }
@ -179,7 +179,7 @@ fn main() -> Result<()> {
// Run the actual application // Run the actual application
let app = App::from_template(text_orig, &Template::from(arg)); 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 // Restore terminal
disable_raw_mode()?; disable_raw_mode()?;
@ -190,10 +190,21 @@ fn main() -> Result<()> {
)?; )?;
terminal.show_cursor()?; 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(()) Ok(())
} }
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<Option<String>> { fn run_app<B: Backend>(
terminal: &mut Terminal<B>,
mut app: App,
) -> io::Result<Option<(String, Vec<u8>)>> {
if !app.cmdline.is_empty() { if !app.cmdline.is_empty() {
command::run(&mut app); command::run(&mut app);
} }
@ -208,6 +219,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<O
if let Event::Key(key) = event::read()? { if let Event::Key(key) = event::read()? {
match key.code { match key.code {
KeyCode::Esc => { KeyCode::Esc => {
// TODO: If there is any command line text, ask if the user is sure they want to quit
return Ok(None); return Ok(None);
} }
KeyCode::Char(c) => { KeyCode::Char(c) => {
@ -243,7 +255,10 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<O
app.cmdline_position = 0_u16; app.cmdline_position = 0_u16;
} }
KeyCode::Enter => { KeyCode::Enter => {
return Ok(Some(app.cmdline.clone())); return Ok(Some((
format!("{} {}", app.command, app.cmdline),
app.command_result.stdout,
)));
} }
KeyCode::Left => { KeyCode::Left => {
app.cmdline_position = app.cmdline_position.saturating_sub(1); app.cmdline_position = app.cmdline_position.saturating_sub(1);