command line warnings

This commit is contained in:
filipriec
2025-02-16 23:46:06 +01:00
parent b09e868c10
commit 03d1c4fff6
3 changed files with 25 additions and 15 deletions

View File

@@ -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);

View File

@@ -39,40 +39,36 @@ impl AppTerminal {
Ok(())
}
pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result<bool, Box<dyn std::error::Error>> {
pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result<(bool, String), Box<dyn std::error::Error>> {
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
}
}
}

View File

@@ -43,6 +43,9 @@ pub fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
// 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<dyn std::error::Error>> {
render_status_line(f, root[1], &current_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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
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) {