From 02b06129e16dd5b1db8a48d24ccd8bb7ab2d0ef0 Mon Sep 17 00:00:00 2001 From: Jeevitha Kannan K S Date: Mon, 11 Nov 2024 23:40:41 +0530 Subject: [PATCH] Reference instead of borrowing commands, refact mut variables Reference instead of borrowing commands from state, Refactor draw function variables to immutable, calculate innersize from block instead of manual definition --- tui/src/running_command.rs | 61 ++++++++++++++------------------------ tui/src/state.rs | 8 ++--- 2 files changed, 27 insertions(+), 42 deletions(-) diff --git a/tui/src/running_command.rs b/tui/src/running_command.rs index e675c7b4..9a9ea7a9 100644 --- a/tui/src/running_command.rs +++ b/tui/src/running_command.rs @@ -8,7 +8,7 @@ use ratatui::{ crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}, layout::{Rect, Size}, style::{Style, Stylize}, - text::{Line, Span}, + text::Line, widgets::{Block, Borders}, Frame, }; @@ -19,7 +19,7 @@ use std::{ }; use time::{macros::format_description, OffsetDateTime}; use tui_term::{ - vt100::{self, Screen}, + vt100::{Parser, Screen}, widget::PseudoTerminal, }; @@ -44,12 +44,6 @@ pub struct RunningCommand { impl FloatContent for RunningCommand { fn draw(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) { - // Calculate the inner size of the terminal area, considering borders - let inner_size = Size { - width: area.width - 2, // Adjust for border width - height: area.height - 2, - }; - // Define the block for the terminal display let block = if !self.is_finished() { // Display a block indicating the command is running @@ -61,42 +55,33 @@ impl FloatContent for RunningCommand { .title_bottom(Line::from("Press Ctrl-C to KILL the command")) } else { // Display a block with the command's exit status - let mut title_line = if self.get_exit_status().success() { - Line::from( - Span::default() - .content("SUCCESS!") - .style(Style::default().fg(theme.success_color()).reversed()), + let title_line = if self.get_exit_status().success() { + Line::styled( + "SUCCESS! Press to close this window", + Style::default().fg(theme.success_color()).reversed(), ) } else { - Line::from( - Span::default() - .content("FAILED!") - .style(Style::default().fg(theme.fail_color()).reversed()), + Line::styled( + "FAILED! Press to close this window", + Style::default().fg(theme.fail_color()).reversed(), ) }; - title_line.push_span( - Span::default() - .content(" Press to close this window ") - .style(Style::default()), - ); + let log_path = if let Some(log_path) = &self.log_path { + Line::from(format!(" Log saved: {} ", log_path)).centered() + } else { + Line::from(" Press 'l' to save command log ").centered() + }; - let mut block = Block::default() + Block::default() .borders(Borders::ALL) .border_set(ratatui::symbols::border::ROUNDED) - .title_top(title_line.centered()); - - if let Some(log_path) = &self.log_path { - block = - block.title_bottom(Line::from(format!(" Log saved: {} ", log_path)).centered()); - } else { - block = - block.title_bottom(Line::from(" Press 'l' to save command log ").centered()); - } - - block + .title_top(title_line.centered()) + .title_bottom(log_path) }; + // Calculate the inner size of the terminal area, considering borders + let inner_size = block.inner(area).as_size(); // Process the buffer and create the pseudo-terminal widget let screen = self.screen(inner_size); let pseudo_term = PseudoTerminal::new(&screen).block(block); @@ -180,7 +165,7 @@ impl FloatContent for RunningCommand { } impl RunningCommand { - pub fn new(commands: Vec) -> Self { + pub fn new(commands: &[&Command]) -> Self { let pty_system = NativePtySystem::default(); // Build the command based on the provided Command enum variant @@ -200,10 +185,10 @@ impl RunningCommand { if let Some(parent_directory) = file.parent() { script.push_str(&format!("cd {}\n", parent_directory.display())); } - script.push_str(&executable); + script.push_str(executable); for arg in args { script.push(' '); - script.push_str(&arg); + script.push_str(arg); } script.push('\n'); // Ensures that each command is properly separated for execution preventing directory errors } @@ -286,7 +271,7 @@ impl RunningCommand { // Process the buffer with a parser with the current screen size // We don't actually need to create a new parser every time, but it is so much easier this // way, and doesn't cost that much - let mut parser = vt100::Parser::new(size.height, size.width, 1000); + let mut parser = Parser::new(size.height, size.width, 1000); let mutex = self.buffer.lock(); let buffer = mutex.as_ref().unwrap(); parser.process(buffer); diff --git a/tui/src/state.rs b/tui/src/state.rs index d767d81b..485b13fc 100644 --- a/tui/src/state.rs +++ b/tui/src/state.rs @@ -9,7 +9,7 @@ use crate::{ theme::Theme, }; -use linutil_core::{ego_tree::NodeId, Config, ListNode, TabList}; +use linutil_core::{ego_tree::NodeId, Command, Config, ListNode, TabList}; #[cfg(feature = "tips")] use rand::Rng; use ratatui::{ @@ -877,13 +877,13 @@ impl AppState { } fn handle_confirm_command(&mut self) { - let commands = self + let commands: Vec<&Command> = self .selected_commands .iter() - .map(|node| node.command.clone()) + .map(|node| &node.command) .collect(); - let command = RunningCommand::new(commands); + let command = RunningCommand::new(&commands); self.spawn_float(command, 80, 80); self.selected_commands.clear(); }