Do not clone resulting vec each time

This commit is contained in:
Austen Adler 2022-12-16 21:28:40 -05:00
parent fc416c4e03
commit c57b75735d
2 changed files with 17 additions and 30 deletions

View File

@ -3,22 +3,20 @@ use std::{
process::{Command, Stdio},
};
use tui::widgets::Paragraph;
use crate::App;
pub struct CommandResult<'a> {
pub struct CommandResult {
pub status_success: bool,
pub stdout: Paragraph<'a>,
pub stderr: Paragraph<'a>,
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
}
impl Default for CommandResult<'_> {
impl Default for CommandResult {
fn default() -> Self {
Self {
status_success: true,
stdout: Paragraph::new(""),
stderr: Paragraph::new(""),
stdout: vec![],
stderr: vec![],
}
}
}
@ -29,8 +27,8 @@ pub fn run(app: &mut App) {
Err(e) => {
app.command_result = CommandResult {
status_success: false,
stdout: Paragraph::new(""),
stderr: Paragraph::new(format!("Argument error: {e:?}")),
stdout: vec![],
stderr: format!("Argument error: {e:?}").as_bytes().to_vec(),
};
return;
}
@ -57,12 +55,9 @@ pub fn run(app: &mut App) {
// Collect the output
let output = child.wait_with_output().expect("Failed to read stdout");
let stderr_string = ansi4tui::bytes_to_text(output.stderr);
let stdout_string = ansi4tui::bytes_to_text(output.stdout);
app.command_result = CommandResult {
status_success: output.status.success(),
stdout: Paragraph::new(stdout_string),
stderr: Paragraph::new(stderr_string),
stdout: output.stdout,
stderr: output.stderr,
}
}

View File

@ -3,6 +3,7 @@
#![allow(clippy::module_name_repetitions, clippy::cast_possible_truncation)]
mod command;
use ansi4tui::bytes_to_text;
use anyhow::bail;
use anyhow::Result;
use command::CommandResult;
@ -28,7 +29,7 @@ use crossterm::{
};
/// The state of the application
pub struct App<'a> {
pub struct App {
/// The actual command to be called
hidden_command: String,
@ -45,13 +46,13 @@ pub struct App<'a> {
text_orig: Arc<String>,
/// The result of a command execution
command_result: CommandResult<'a>,
command_result: CommandResult,
/// Should every keystroke transform the original text?
autorun: bool,
}
impl App<'_> {
impl App {
#[must_use]
pub fn from_template(input: String, template: &Template) -> Self {
let text_orig = Arc::new(input);
@ -293,10 +294,7 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &App) {
fn ui_output<B: Backend>(f: &mut Frame<B>, output_region: Rect, app: &App) {
if app.command_result.status_success {
f.render_widget(
// TODO: Do we really have to clone here?
app.command_result
.stdout
.clone()
Paragraph::new(bytes_to_text(&app.command_result.stdout))
.block(Block::default().title("New").borders(Borders::ALL)),
output_region,
);
@ -307,18 +305,12 @@ fn ui_output<B: Backend>(f: &mut Frame<B>, output_region: Rect, app: &App) {
.split(output_region);
f.render_widget(
// TODO: Do we really have to clone here?
app.command_result
.stdout
.clone()
Paragraph::new(bytes_to_text(&app.command_result.stdout))
.block(Block::default().title("Output").borders(Borders::ALL)),
chunks[0],
);
f.render_widget(
// TODO: Do we really have to clone here?
app.command_result
.stderr
.clone()
Paragraph::new(bytes_to_text(&app.command_result.stderr))
.block(Block::default().title("Error").borders(Borders::ALL)),
chunks[1],
);