|
1 | 1 | use crate::interactive::state::{AppState, InputFocus, Mode}; |
| 2 | +use crate::variables; |
2 | 3 | use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; |
3 | 4 | use std::io; |
4 | 5 | use std::time::Duration; |
@@ -84,11 +85,16 @@ pub fn handle_add_mode(state: &mut AppState, key: KeyEvent) { |
84 | 85 | match key.code { |
85 | 86 | KeyCode::Enter => { |
86 | 87 | if !state.input_key.trim().is_empty() { |
87 | | - state.entries.push(( |
88 | | - state.input_key.trim().to_string(), |
89 | | - state.input_value.trim().to_string(), |
90 | | - )); |
91 | | - state.show_message("Variable added", Duration::from_secs(2)); |
| 88 | + match variables::set_variable(state.input_key.trim(), state.input_value.trim(), true) { |
| 89 | + Err(err) => state.show_message(&format!("Failed to add variable: {}", err), Duration::from_secs(2)), |
| 90 | + Ok(_) => { |
| 91 | + state.entries.push(( |
| 92 | + state.input_key.trim().to_string(), |
| 93 | + state.input_value.trim().to_string(), |
| 94 | + )); |
| 95 | + state.show_message("Variable added", Duration::from_secs(2)); |
| 96 | + } |
| 97 | + } |
92 | 98 | state.mode = Mode::List; |
93 | 99 | } else { |
94 | 100 | state.show_message("Key cannot be empty", Duration::from_secs(2)); |
@@ -158,8 +164,13 @@ pub fn handle_edit_mode(state: &mut AppState, key: KeyEvent) { |
158 | 164 | KeyCode::Enter => { |
159 | 165 | if let Mode::Edit(ref key_name) = state.mode { |
160 | 166 | if let Some(entry) = state.entries.iter_mut().find(|(k, _)| k == key_name) { |
161 | | - entry.1 = state.input_value.trim().to_string(); |
162 | | - state.show_message("Variable updated", Duration::from_secs(2)); |
| 167 | + match variables::set_variable(key_name, state.input_value.trim(), true) { |
| 168 | + Err(err) => state.show_message(&format!("Failed to update variable: {}", err), Duration::from_secs(2)), |
| 169 | + Ok(_) => { |
| 170 | + entry.1 = state.input_value.trim().to_string(); |
| 171 | + state.show_message("Variable updated", Duration::from_secs(2)) |
| 172 | + } |
| 173 | + } |
163 | 174 | } |
164 | 175 | state.mode = Mode::List; |
165 | 176 | } |
@@ -193,8 +204,13 @@ pub fn handle_delete_mode(state: &mut AppState, key: KeyEvent) { |
193 | 204 | match key.code { |
194 | 205 | KeyCode::Char('y') => { |
195 | 206 | if let Mode::Delete(ref key_name) = state.mode { |
196 | | - state.entries.retain(|(k, _)| k != key_name); |
197 | | - state.show_message("Variable deleted", Duration::from_secs(2)); |
| 207 | + match variables::delete_variable(key_name.to_owned(), true) { |
| 208 | + Err(err) => state.show_message(&format!("Failed to delete variable: {}", err), Duration::from_secs(2)), |
| 209 | + Ok(_) => { |
| 210 | + state.entries.retain(|(k, _)| k != key_name); |
| 211 | + state.show_message("Variable deleted", Duration::from_secs(2)) |
| 212 | + } |
| 213 | + } |
198 | 214 | } |
199 | 215 | state.mode = Mode::List; |
200 | 216 | } |
|
0 commit comments