This commit is contained in:
cartercanedy 2024-10-25 16:03:06 +02:00 committed by GitHub
commit 9e6f9a9b7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 125 additions and 132 deletions

View File

@ -3,11 +3,7 @@ use std::borrow::Cow;
use crate::{float::FloatContent, hint::Shortcut}; use crate::{float::FloatContent, hint::Shortcut};
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{ use ratatui::{prelude::*, widgets::List};
layout::Alignment,
prelude::*,
widgets::{Block, Borders, Clear, List},
};
pub enum ConfirmStatus { pub enum ConfirmStatus {
Confirm, Confirm,
@ -57,19 +53,15 @@ impl ConfirmPrompt {
} }
impl FloatContent for ConfirmPrompt { impl FloatContent for ConfirmPrompt {
fn top_title(&self) -> Option<Line<'_>> {
Some(Line::from(" Confirm selections ").style(Style::default().bold()))
}
fn bottom_title(&self) -> Option<Line<'_>> {
Some(Line::from(" [y] to continue, [n] to abort ").italic())
}
fn draw(&mut self, frame: &mut Frame, area: Rect) { fn draw(&mut self, frame: &mut Frame, area: Rect) {
let block = Block::default()
.borders(Borders::ALL)
.title(" Confirm selections ")
.title_bottom(" [y] to continue, [n] to abort ")
.title_alignment(Alignment::Center)
.title_style(Style::default().bold())
.style(Style::default());
frame.render_widget(block.clone(), area);
let inner_area = block.inner(area);
let paths_text = self let paths_text = self
.names .names
.iter() .iter()
@ -80,8 +72,7 @@ impl FloatContent for ConfirmPrompt {
}) })
.collect::<Text>(); .collect::<Text>();
frame.render_widget(Clear, inner_area); frame.render_widget(List::new(paths_text), area);
frame.render_widget(List::new(paths_text), inner_area);
} }
fn handle_key_event(&mut self, key: &KeyEvent) -> bool { fn handle_key_event(&mut self, key: &KeyEvent) -> bool {

View File

@ -1,6 +1,9 @@
use crossterm::event::{KeyCode, KeyEvent}; use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{ use ratatui::{
layout::{Constraint, Direction, Layout, Rect}, layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Style, Stylize},
text::Line,
widgets::{Block, Borders, Clear},
Frame, Frame,
}; };
@ -8,6 +11,8 @@ use crate::hint::Shortcut;
pub trait FloatContent { pub trait FloatContent {
fn draw(&mut self, frame: &mut Frame, area: Rect); fn draw(&mut self, frame: &mut Frame, area: Rect);
fn top_title(&self) -> Option<Line<'_>>;
fn bottom_title(&self) -> Option<Line<'_>>;
fn handle_key_event(&mut self, key: &KeyEvent) -> bool; fn handle_key_event(&mut self, key: &KeyEvent) -> bool;
fn is_finished(&self) -> bool; fn is_finished(&self) -> bool;
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>); fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>);
@ -50,7 +55,22 @@ impl<Content: FloatContent + ?Sized> Float<Content> {
pub fn draw(&mut self, frame: &mut Frame, parent_area: Rect) { pub fn draw(&mut self, frame: &mut Frame, parent_area: Rect) {
let popup_area = self.floating_window(parent_area); let popup_area = self.floating_window(parent_area);
self.content.draw(frame, popup_area); let mut block = Block::new()
.borders(Borders::ALL)
.title_alignment(Alignment::Center)
.style(Style::new().reset());
if let Some(top_title) = self.content.top_title() {
block = block.title_top(top_title);
}
if let Some(bottom_title) = self.content.bottom_title() {
block = block.title_bottom(bottom_title);
}
frame.render_widget(Clear, popup_area);
frame.render_widget(&block, popup_area);
self.content.draw(frame, block.inner(popup_area));
} }
// Returns true if the floating window is finished. // Returns true if the floating window is finished.

View File

@ -14,7 +14,7 @@ use ratatui::{
layout::Rect, layout::Rect,
style::{Style, Stylize}, style::{Style, Stylize},
text::Line, text::Line,
widgets::{Block, Borders, Clear, List}, widgets::{Block, List},
Frame, Frame,
}; };
@ -29,7 +29,7 @@ pub struct FloatingText {
max_line_width: usize, max_line_width: usize,
v_scroll: usize, v_scroll: usize,
h_scroll: usize, h_scroll: usize,
mode_title: String, title: String,
} }
macro_rules! style { macro_rules! style {
@ -124,7 +124,7 @@ fn get_lines_owned(s: &str) -> Vec<String> {
} }
impl FloatingText { impl FloatingText {
pub fn new(text: String, title: &str) -> Self { pub fn new(text: String, title: String) -> Self {
let src = get_lines(&text) let src = get_lines(&text)
.into_iter() .into_iter()
.map(|s| s.to_string()) .map(|s| s.to_string())
@ -133,7 +133,7 @@ impl FloatingText {
let max_line_width = max_width!(src); let max_line_width = max_width!(src);
Self { Self {
src, src,
mode_title: title.to_string(), title,
max_line_width, max_line_width,
v_scroll: 0, v_scroll: 0,
h_scroll: 0, h_scroll: 0,
@ -146,6 +146,7 @@ impl FloatingText {
// just apply highlights directly // just apply highlights directly
(max_width!(get_lines(cmd)), Some(cmd.clone())) (max_width!(get_lines(cmd)), Some(cmd.clone()))
} }
Command::LocalFile { file, .. } => { Command::LocalFile { file, .. } => {
// have to read from tmp dir to get cmd src // have to read from tmp dir to get cmd src
let raw = std::fs::read_to_string(file) let raw = std::fs::read_to_string(file)
@ -163,7 +164,7 @@ impl FloatingText {
Some(Self { Some(Self {
src, src,
mode_title: title, title,
max_line_width, max_line_width,
h_scroll: 0, h_scroll: 0,
v_scroll: 0, v_scroll: 0,
@ -196,21 +197,22 @@ impl FloatingText {
} }
impl FloatContent for FloatingText { impl FloatContent for FloatingText {
fn top_title(&self) -> Option<Line<'_>> {
let title_text = format!(" {} ", self.title);
let title_line = Line::from(title_text)
.centered()
.style(Style::default().reversed());
Some(title_line)
}
fn bottom_title(&self) -> Option<Line<'_>> {
None
}
fn draw(&mut self, frame: &mut Frame, area: Rect) { fn draw(&mut self, frame: &mut Frame, area: Rect) {
// Define the Block with a border and background color let Rect { height, .. } = area;
let block = Block::default()
.borders(Borders::ALL)
.title(self.mode_title.clone())
.title_alignment(ratatui::layout::Alignment::Center)
.title_style(Style::default().reversed())
.style(Style::default());
// Draw the Block first
frame.render_widget(block.clone(), area);
// Calculate the inner area to ensure text is not drawn over the border
let inner_area = block.inner(area);
let Rect { height, .. } = inner_area;
let lines = self let lines = self
.src .src
.iter() .iter()
@ -253,11 +255,8 @@ impl FloatContent for FloatingText {
.block(Block::default()) .block(Block::default())
.highlight_style(Style::default().reversed()); .highlight_style(Style::default().reversed());
// Clear the text underneath the floats rendered area
frame.render_widget(Clear, inner_area);
// Render the list inside the bordered area // Render the list inside the bordered area
frame.render_widget(list, inner_area); frame.render_widget(list, area);
} }
fn handle_key_event(&mut self, key: &KeyEvent) -> bool { fn handle_key_event(&mut self, key: &KeyEvent) -> bool {
@ -278,7 +277,7 @@ impl FloatContent for FloatingText {
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) { fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
( (
&self.mode_title, &self.title,
Box::new([ Box::new([
Shortcut::new("Scroll down", ["j", "Down"]), Shortcut::new("Scroll down", ["j", "Down"]),
Shortcut::new("Scroll up", ["k", "Up"]), Shortcut::new("Scroll up", ["k", "Up"]),

View File

@ -1,32 +1,39 @@
use crate::{float::FloatContent, hint::Shortcut};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use linutil_core::Command;
use oneshot::{channel, Receiver};
use portable_pty::{
ChildKiller, CommandBuilder, ExitStatus, MasterPty, NativePtySystem, PtySize, PtySystem,
};
use ratatui::{
layout::{Rect, Size},
style::{Color, Style, Stylize},
text::{Line, Span},
widgets::{Block, Borders},
Frame,
};
use std::{ use std::{
cell::{Cell, RefCell},
io::Write, io::Write,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
thread::JoinHandle, thread::JoinHandle,
}; };
use crate::{float::FloatContent, hint::Shortcut};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use oneshot::{channel, Receiver};
use portable_pty::{
ChildKiller, CommandBuilder, ExitStatus, MasterPty, NativePtySystem, PtySize, PtySystem,
};
use ratatui::{
layout::Rect,
style::{Style, Stylize},
text::Line,
Frame,
};
use tui_term::{ use tui_term::{
vt100::{self, Screen}, vt100::{self, Screen},
widget::PseudoTerminal, widget::PseudoTerminal,
}; };
use linutil_core::Command;
pub struct RunningCommand { pub struct RunningCommand {
/// A buffer to save all the command output (accumulates, until the command exits) /// A buffer to save all the command output (accumulates, until the command exits)
buffer: Arc<Mutex<Vec<u8>>>, buffer: Arc<Mutex<Vec<u8>>>,
/// A handle for the thread running the command /// A handle for the thread running the command
command_thread: Option<JoinHandle<ExitStatus>>, command_thread: Cell<Option<JoinHandle<ExitStatus>>>,
/// A handle to kill the running process; it's an option because it can only be used once /// A handle to kill the running process; it's an option because it can only be used once
child_killer: Option<Receiver<Box<dyn ChildKiller + Send + Sync>>>, child_killer: Option<Receiver<Box<dyn ChildKiller + Send + Sync>>>,
/// A join handle for the thread that reads command output and sends it to the main thread /// A join handle for the thread that reads command output and sends it to the main thread
@ -36,56 +43,35 @@ pub struct RunningCommand {
/// Used for sending keys to the emulated terminal /// Used for sending keys to the emulated terminal
writer: Box<dyn Write + Send>, writer: Box<dyn Write + Send>,
/// Only set after the process has ended /// Only set after the process has ended
status: Option<ExitStatus>, status: RefCell<Option<ExitStatus>>,
scroll_offset: usize, scroll_offset: usize,
} }
impl FloatContent for RunningCommand { impl FloatContent for RunningCommand {
fn draw(&mut self, frame: &mut Frame, area: Rect) { fn top_title(&self) -> Option<Line<'_>> {
// Calculate the inner size of the terminal area, considering borders let (content, content_style) = if !self.is_finished() {
let inner_size = Size { (" Running command... ", Style::default().reversed())
width: area.width - 2, // Adjust for border width } else if self.wait_command().success() {
height: area.height - 2, (" Success ", Style::default().bold().green().reversed())
};
// Define the block for the terminal display
let block = if !self.is_finished() {
// Display a block indicating the command is running
Block::default()
.borders(Borders::ALL)
.title_top(Line::from("Running the command....").centered())
.title_style(Style::default().reversed())
.title_bottom(Line::from("Press Ctrl-C to KILL the command"))
} else { } else {
// Display a block with the command's exit status (" Failed ", Style::default().bold().red().reversed())
let mut title_line = if self.get_exit_status().success() {
Line::from(
Span::default()
.content("SUCCESS!")
.style(Style::default().fg(Color::Green).reversed()),
)
} else {
Line::from(
Span::default()
.content("FAILED!")
.style(Style::default().fg(Color::Red).reversed()),
)
};
title_line.push_span(
Span::default()
.content(" press <ENTER> to close this window ")
.style(Style::default()),
);
Block::default()
.borders(Borders::ALL)
.title_top(title_line.centered())
}; };
Some(Line::from(content).style(content_style))
}
fn bottom_title(&self) -> Option<Line<'_>> {
Some(Line::from(if !self.is_finished() {
" Press [CTRL-c] to KILL the command "
} else {
" Press [Enter] to close this window "
}))
}
fn draw(&mut self, frame: &mut Frame, area: Rect) {
// 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(area);
let pseudo_term = PseudoTerminal::new(&screen).block(block); let pseudo_term = PseudoTerminal::new(&screen);
// Render the widget on the frame // Render the widget on the frame
frame.render_widget(pseudo_term, area); frame.render_widget(pseudo_term, area);
@ -116,12 +102,7 @@ impl FloatContent for RunningCommand {
} }
fn is_finished(&self) -> bool { fn is_finished(&self) -> bool {
// Check if the command thread has finished self.status.borrow().is_some()
if let Some(command_thread) = &self.command_thread {
command_thread.is_finished()
} else {
true
}
} }
fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) { fn get_shortcut_list(&self) -> (&str, Box<[Shortcut]>) {
@ -229,17 +210,17 @@ impl RunningCommand {
let writer = pair.master.take_writer().unwrap(); let writer = pair.master.take_writer().unwrap();
Self { Self {
buffer: command_buffer, buffer: command_buffer,
command_thread: Some(command_handle), command_thread: Some(command_handle).into(),
child_killer: Some(rx), child_killer: rx.into(),
_reader_thread: reader_handle, _reader_thread: reader_handle,
pty_master: pair.master, pty_master: pair.master,
writer, writer,
status: None, status: None.into(),
scroll_offset: 0, scroll_offset: 0,
} }
} }
fn screen(&mut self, size: Size) -> Screen { fn screen(&mut self, size: Rect) -> Screen {
// Resize the emulated pty // Resize the emulated pty
self.pty_master self.pty_master
.resize(PtySize { .resize(PtySize {
@ -263,14 +244,16 @@ impl RunningCommand {
} }
/// This function will block if the command is not finished /// This function will block if the command is not finished
fn get_exit_status(&mut self) -> ExitStatus { fn wait_command(&self) -> ExitStatus {
if self.command_thread.is_some() { let status = { self.status.borrow().clone() };
let handle = self.command_thread.take().unwrap(); match status {
let exit_status = handle.join().unwrap(); Some(status) => status,
self.status = Some(exit_status.clone()); None => {
exit_status let handle = self.command_thread.take().unwrap();
} else { let exit_status = handle.join().unwrap();
self.status.as_ref().unwrap().clone() self.status.replace(Some(exit_status.clone()));
exit_status
}
} }
} }
@ -279,6 +262,7 @@ impl RunningCommand {
if !self.is_finished() { if !self.is_finished() {
let mut killer = self.child_killer.take().unwrap().recv().unwrap(); let mut killer = self.child_killer.take().unwrap().recv().unwrap();
killer.kill().unwrap(); killer.kill().unwrap();
self.wait_command();
} }
} }

View File

@ -601,11 +601,6 @@ impl AppState {
None None
} }
fn get_selected_description(&self) -> Option<String> {
self.get_selected_node()
.map(|node| node.description.clone())
}
pub fn go_to_selected_dir(&mut self) { pub fn go_to_selected_dir(&mut self) {
let mut selected_index = self.selection.selected().unwrap_or(0); let mut selected_index = self.selection.selected().unwrap_or(0);
@ -657,18 +652,19 @@ impl AppState {
} }
fn enable_preview(&mut self) { fn enable_preview(&mut self) {
if let Some(list_node) = self.get_selected_node() { if let Some(node) = self.get_selected_node() {
let mut preview_title = "[Preview] - ".to_string(); let preview_title = format!("Command Preview - {}", node.name.as_str());
preview_title.push_str(list_node.name.as_str()); if let Some(preview) = FloatingText::from_command(&node.command, preview_title) {
if let Some(preview) = FloatingText::from_command(&list_node.command, preview_title) {
self.spawn_float(preview, 80, 80); self.spawn_float(preview, 80, 80);
} }
} }
} }
fn enable_description(&mut self) { fn enable_description(&mut self) {
if let Some(command_description) = self.get_selected_description() { if let Some(node) = self.get_selected_node() {
let description = FloatingText::new(command_description, "Command Description"); let desc_title = format!("Command Description - {}", &node.name);
let description = FloatingText::new(node.description.clone(), desc_title);
self.spawn_float(description, 80, 80); self.spawn_float(description, 80, 80);
} }
} }
@ -734,7 +730,10 @@ impl AppState {
fn toggle_task_list_guide(&mut self) { fn toggle_task_list_guide(&mut self) {
self.spawn_float( self.spawn_float(
FloatingText::new(ACTIONS_GUIDE.to_string(), "Important Actions Guide"), FloatingText::new(
ACTIONS_GUIDE.to_string(),
"Important Actions Guide".to_string(),
),
80, 80,
80, 80,
); );