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

View File

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