From b09e868c107627029d35567f42c193700d4d1d60 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 16 Feb 2025 23:42:44 +0100 Subject: [PATCH] better working --- src/client/terminal.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/client/ui.rs | 35 ++++------------------------------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/client/terminal.rs b/src/client/terminal.rs index 85eee8d..996e9de 100644 --- a/src/client/terminal.rs +++ b/src/client/terminal.rs @@ -38,4 +38,42 @@ impl AppTerminal { execute!(self.terminal.backend_mut(), LeaveAlternateScreen)?; Ok(()) } + + pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result> { + match command_input { + "w" => { + // Save the state + *is_saved = true; + println!("State saved."); + Ok(false) // Do not exit + } + "q" => { + // Quit if saved + if *is_saved { + self.cleanup()?; + Ok(true) // Exit + } else { + println!("No changes saved. Use :q! to force quit."); + Ok(false) // Do not exit + } + } + "q!" => { + // Force quit without saving + self.cleanup()?; + Ok(true) // Exit + } + "wq" => { + // Save and quit + *is_saved = true; + println!("State saved."); + self.cleanup()?; + Ok(true) // Exit + } + _ => { + // Handle other commands here + println!("Command not recognized: {}", command_input); + Ok(false) // Do not exit + } + } + } } diff --git a/src/client/ui.rs b/src/client/ui.rs index fca8146..bf8220e 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -95,37 +95,10 @@ pub fn run_ui() -> Result<(), Box> { if command_mode { match key.code { KeyCode::Enter => { - match command_input.as_str() { - "w" => { - // Save the state - is_saved = true; - println!("State saved."); - } - "q" => { - // Quit if saved - if is_saved { - app_terminal.cleanup()?; - return Ok(()); - } else { - println!("No changes saved. Use :q! to force quit."); - } - } - "q!" => { - // Force quit without saving - app_terminal.cleanup()?; - return Ok(()); - } - "wq" => { - // Save and quit - is_saved = true; - println!("State saved."); - app_terminal.cleanup()?; - return Ok(()); - } - _ => { - // Handle other commands here - println!("Command not recognized: {}", command_input); - } + // Handle the command + let should_exit = app_terminal.handle_command(&command_input, &mut is_saved)?; + if should_exit { + return Ok(()); } command_mode = false; command_input.clear();