From 03d1c4fff62fd2312b1c8479b297c41eb6abfca9 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 16 Feb 2025 23:46:06 +0100 Subject: [PATCH] command line warnings --- src/client/components/command_line.rs | 12 ++++++++++-- src/client/terminal.rs | 18 +++++++----------- src/client/ui.rs | 10 ++++++++-- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/client/components/command_line.rs b/src/client/components/command_line.rs index 90aac95..922531b 100644 --- a/src/client/components/command_line.rs +++ b/src/client/components/command_line.rs @@ -7,19 +7,27 @@ use ratatui::{ }; use crate::client::colors::Theme; -pub fn render_command_line(f: &mut Frame, area: Rect, input: &str, active: bool, theme: &Theme) { +pub fn render_command_line(f: &mut Frame, area: Rect, input: &str, active: bool, theme: &Theme, message: &str) { let prompt = if active { ":" } else { "" }; + + // Combine the prompt, input, and message + let display_text = if message.is_empty() { + format!("{}{}", prompt, input) + } else { + format!("{}{} | {}", prompt, input, message) + }; + let style = if active { Style::default().fg(theme.accent) } else { Style::default().fg(theme.fg) }; - let paragraph = Paragraph::new(format!("{}{}", prompt, input)) + let paragraph = Paragraph::new(display_text) .block(Block::default().style(Style::default().bg(theme.bg))) .style(style); diff --git a/src/client/terminal.rs b/src/client/terminal.rs index 996e9de..523bc9b 100644 --- a/src/client/terminal.rs +++ b/src/client/terminal.rs @@ -39,40 +39,36 @@ impl AppTerminal { Ok(()) } - pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result> { + pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result<(bool, String), Box> { match command_input { "w" => { // Save the state *is_saved = true; - println!("State saved."); - Ok(false) // Do not exit + Ok((false, "State saved.".to_string())) // Do not exit } "q" => { // Quit if saved if *is_saved { self.cleanup()?; - Ok(true) // Exit + Ok((true, "Exiting.".to_string())) // Exit } else { - println!("No changes saved. Use :q! to force quit."); - Ok(false) // Do not exit + Ok((false, "No changes saved. Use :q! to force quit.".to_string())) // Do not exit } } "q!" => { // Force quit without saving self.cleanup()?; - Ok(true) // Exit + Ok((true, "Force exiting without saving.".to_string())) // Exit } "wq" => { // Save and quit *is_saved = true; - println!("State saved."); self.cleanup()?; - Ok(true) // Exit + Ok((true, "State saved. Exiting.".to_string())) // Exit } _ => { // Handle other commands here - println!("Command not recognized: {}", command_input); - Ok(false) // Do not exit + Ok((false, format!("Command not recognized: {}", command_input))) // Do not exit } } } diff --git a/src/client/ui.rs b/src/client/ui.rs index bf8220e..fa6117f 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -43,6 +43,9 @@ pub fn run_ui() -> Result<(), Box> { // Track whether the state has been saved let mut is_saved = false; + // Track the current message to display in the command line + let mut command_message = String::new(); + loop { app_terminal.draw(|f| { let root = Layout::default() @@ -87,7 +90,7 @@ pub fn run_ui() -> Result<(), Box> { render_status_line(f, root[1], ¤t_dir, &theme); // Command line - render_command_line(f, root[2], &command_input, command_mode, &theme); + render_command_line(f, root[2], &command_input, command_mode, &theme, &command_message); })?; // Event handling @@ -96,7 +99,8 @@ pub fn run_ui() -> Result<(), Box> { match key.code { KeyCode::Enter => { // Handle the command - let should_exit = app_terminal.handle_command(&command_input, &mut is_saved)?; + let (should_exit, message) = app_terminal.handle_command(&command_input, &mut is_saved)?; + command_message = message; // Set the message to display if should_exit { return Ok(()); } @@ -110,6 +114,7 @@ pub fn run_ui() -> Result<(), Box> { KeyCode::Esc => { command_mode = false; command_input.clear(); + command_message.clear(); // Clear the message when exiting command mode } _ => {} } @@ -118,6 +123,7 @@ pub fn run_ui() -> Result<(), Box> { KeyCode::Char(':') => { command_mode = true; command_input.clear(); + command_message.clear(); // Clear the message when entering command mode } KeyCode::Tab => { if key.modifiers.contains(KeyModifiers::SHIFT) {