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

View File

@ -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<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() {
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()? {
match key.code {
KeyCode::Esc => {
// 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<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<O
app.cmdline_position = 0_u16;
}
KeyCode::Enter => {
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);