From c57b75735d323c92f2a58226882085b6d52c1101 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Fri, 16 Dec 2022 21:28:40 -0500 Subject: [PATCH] Do not clone resulting vec each time --- src/command.rs | 25 ++++++++++--------------- src/main.rs | 22 +++++++--------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/command.rs b/src/command.rs index fc42adb..13c98ee 100644 --- a/src/command.rs +++ b/src/command.rs @@ -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, + pub stderr: Vec, } -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, } } diff --git a/src/main.rs b/src/main.rs index c163999..d8b123d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, /// 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(f: &mut Frame, app: &App) { fn ui_output(f: &mut Frame, 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(f: &mut Frame, 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], );