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
This commit is contained in:
Jeevitha Kannan K S 2024-11-11 23:40:41 +05:30
parent 2f1f5fd543
commit 02b06129e1
No known key found for this signature in database
GPG Key ID: 5904C34A2F7CE333
2 changed files with 27 additions and 42 deletions

View File

@ -8,7 +8,7 @@ use ratatui::{
crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}, crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind},
layout::{Rect, Size}, layout::{Rect, Size},
style::{Style, Stylize}, style::{Style, Stylize},
text::{Line, Span}, text::Line,
widgets::{Block, Borders}, widgets::{Block, Borders},
Frame, Frame,
}; };
@ -19,7 +19,7 @@ use std::{
}; };
use time::{macros::format_description, OffsetDateTime}; use time::{macros::format_description, OffsetDateTime};
use tui_term::{ use tui_term::{
vt100::{self, Screen}, vt100::{Parser, Screen},
widget::PseudoTerminal, widget::PseudoTerminal,
}; };
@ -44,12 +44,6 @@ pub struct RunningCommand {
impl FloatContent for RunningCommand { impl FloatContent for RunningCommand {
fn draw(&mut self, frame: &mut Frame, area: Rect, theme: &Theme) { 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 // Define the block for the terminal display
let block = if !self.is_finished() { let block = if !self.is_finished() {
// Display a block indicating the command is running // 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")) .title_bottom(Line::from("Press Ctrl-C to KILL the command"))
} else { } else {
// Display a block with the command's exit status // Display a block with the command's exit status
let mut title_line = if self.get_exit_status().success() { let title_line = if self.get_exit_status().success() {
Line::from( Line::styled(
Span::default() "SUCCESS! Press <ENTER> to close this window",
.content("SUCCESS!") Style::default().fg(theme.success_color()).reversed(),
.style(Style::default().fg(theme.success_color()).reversed()),
) )
} else { } else {
Line::from( Line::styled(
Span::default() "FAILED! Press <ENTER> to close this window",
.content("FAILED!") Style::default().fg(theme.fail_color()).reversed(),
.style(Style::default().fg(theme.fail_color()).reversed()),
) )
}; };
title_line.push_span( let log_path = if let Some(log_path) = &self.log_path {
Span::default() Line::from(format!(" Log saved: {} ", log_path)).centered()
.content(" Press <ENTER> to close this window ") } else {
.style(Style::default()), Line::from(" Press 'l' to save command log ").centered()
); };
let mut block = Block::default() Block::default()
.borders(Borders::ALL) .borders(Borders::ALL)
.border_set(ratatui::symbols::border::ROUNDED) .border_set(ratatui::symbols::border::ROUNDED)
.title_top(title_line.centered()); .title_top(title_line.centered())
.title_bottom(log_path)
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
}; };
// 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 // Process the buffer and create the pseudo-terminal widget
let screen = self.screen(inner_size); let screen = self.screen(inner_size);
let pseudo_term = PseudoTerminal::new(&screen).block(block); let pseudo_term = PseudoTerminal::new(&screen).block(block);
@ -180,7 +165,7 @@ impl FloatContent for RunningCommand {
} }
impl RunningCommand { impl RunningCommand {
pub fn new(commands: Vec<Command>) -> Self { pub fn new(commands: &[&Command]) -> Self {
let pty_system = NativePtySystem::default(); let pty_system = NativePtySystem::default();
// Build the command based on the provided Command enum variant // Build the command based on the provided Command enum variant
@ -200,10 +185,10 @@ impl RunningCommand {
if let Some(parent_directory) = file.parent() { if let Some(parent_directory) = file.parent() {
script.push_str(&format!("cd {}\n", parent_directory.display())); script.push_str(&format!("cd {}\n", parent_directory.display()));
} }
script.push_str(&executable); script.push_str(executable);
for arg in args { for arg in args {
script.push(' '); 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 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 // 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 // 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 // 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 mutex = self.buffer.lock();
let buffer = mutex.as_ref().unwrap(); let buffer = mutex.as_ref().unwrap();
parser.process(buffer); parser.process(buffer);

View File

@ -9,7 +9,7 @@ use crate::{
theme::Theme, theme::Theme,
}; };
use linutil_core::{ego_tree::NodeId, Config, ListNode, TabList}; use linutil_core::{ego_tree::NodeId, Command, Config, ListNode, TabList};
#[cfg(feature = "tips")] #[cfg(feature = "tips")]
use rand::Rng; use rand::Rng;
use ratatui::{ use ratatui::{
@ -877,13 +877,13 @@ impl AppState {
} }
fn handle_confirm_command(&mut self) { fn handle_confirm_command(&mut self) {
let commands = self let commands: Vec<&Command> = self
.selected_commands .selected_commands
.iter() .iter()
.map(|node| node.command.clone()) .map(|node| &node.command)
.collect(); .collect();
let command = RunningCommand::new(commands); let command = RunningCommand::new(&commands);
self.spawn_float(command, 80, 80); self.spawn_float(command, 80, 80);
self.selected_commands.clear(); self.selected_commands.clear();
} }