diff --git a/src/app.rs b/src/app.rs index 28f07ba..992a7e7 100644 --- a/src/app.rs +++ b/src/app.rs @@ -132,7 +132,7 @@ impl<'a> App<'a> { pub fn go_back_screen(&mut self) { self.screen_stack.pop(); // current screen match self.screen_stack.last() { - Some(Screen::InputMenu(_)) => self.go_back_screen(), + Some(Screen::InputMenu(_)) | Some(Screen::AlertMenu(_)) => self.go_back_screen(), // is that recursion in prod????? o_0 Some(screen) if screen == &self.current_screen => self.go_back_screen(), Some(screen) => { @@ -233,19 +233,18 @@ impl<'a> App<'a> { self.response.as_ref().unwrap().as_str() } - pub fn delete_saved_command(&self, ind: usize) { - let saved_commands = self.get_saved_commands().unwrap(); - let cmd = saved_commands.get(ind).unwrap(); - if self.db.as_ref().delete_command(cmd.get_id()).is_ok() {} + pub fn delete_saved_command(&mut self, ind: i32) { + if let Err(e) = self.db.as_mut().delete_command(ind) { + println!("Error: {}", e); + } + self.goto_screen(Screen::SavedCommands); } - pub fn delete_saved_key(&self, index: usize) { - let saved_keys = self.get_saved_keys().unwrap(); - let key = saved_keys.get(index).unwrap(); - self.db.as_ref().delete_key(key.get_id()).unwrap(); + pub fn delete_saved_key(&self, index: i32) { + self.db.as_ref().delete_key(index).unwrap(); } - pub fn delete_item(&mut self, ind: usize) { + pub fn delete_item(&mut self, ind: i32) { match self.current_screen { Screen::SavedCommands => self.delete_saved_command(ind), Screen::SavedKeys => self.delete_saved_key(ind), diff --git a/src/database/db.rs b/src/database/db.rs index e1379ef..8be5cf2 100644 --- a/src/database/db.rs +++ b/src/database/db.rs @@ -64,7 +64,7 @@ impl DB { } pub fn delete_command(&self, id: i32) -> Result<(), rusqlite::Error> { - let mut stmt = self.conn.prepare("DELETE FROM commands WHERE id = ?1")?; + let mut stmt = self.conn.prepare("DELETE FROM commands WHERE id = ?")?; stmt.execute(params![id])?; Ok(()) } diff --git a/src/events/handler.rs b/src/events/handler.rs index 91e0c0d..c93cdd2 100644 --- a/src/events/handler.rs +++ b/src/events/handler.rs @@ -54,11 +54,6 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { KeyCode::Char('b') => { app.go_back_screen(); } - KeyCode::Char('x') => { - if !app.items.is_empty() { - app.delete_item(app.cursor); - } - } _ => {} } } diff --git a/src/screens/input/input.rs b/src/screens/input/input.rs index cf7fdb6..49fdd39 100644 --- a/src/screens/input/input.rs +++ b/src/screens/input/input.rs @@ -223,9 +223,8 @@ fn render_input_with_prompt(frame: &mut Frame<'_, B>, prompt: Text) .as_ref(), ) .split(frame.size()); - let message = Paragraph::new(prompt); - frame.render_widget(message, chunks[0]); + frame.render_widget(message, chunks[2]); } fn parse_auth(auth: AuthType, app: &mut App, message: &str) { diff --git a/src/screens/input/mod.rs b/src/screens/input/mod.rs index 30f4e01..7839bc5 100644 --- a/src/screens/input/mod.rs +++ b/src/screens/input/mod.rs @@ -1,3 +1 @@ pub mod input; -pub mod test; -pub mod url; diff --git a/src/screens/input/test.rs b/src/screens/input/test.rs deleted file mode 100644 index b108960..0000000 --- a/src/screens/input/test.rs +++ /dev/null @@ -1,86 +0,0 @@ -use std::rc::Rc; - -use tui::backend::Backend; -use tui::layout::{Constraint, Direction, Layout, Rect}; -use tui::style::{Color, Style}; -use tui::text::Text; -use tui::widgets::{Block, Borders, Paragraph}; -use tui::Frame; - -use crate::app::App; - -fn create_layout(frame: &mut Frame<'_, B>) -> (Rc<[Rect]>, Rc<[Rect]>, Rc<[Rect]>) { - let parent_chunks = Layout::default() - .direction(Direction::Vertical) - .constraints( - [ - Constraint::Length(3), // Title Area - Constraint::Length(3), // Input Area - Constraint::Min(1), - ] - .as_ref(), - ) - .split(frame.size()); - - let body_column_chunks = Layout::default() - .direction(Direction::Horizontal) - .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) - .split(parent_chunks[2]); - - let left_col_chunks = Layout::default() - .direction(Direction::Vertical) - .constraints( - [ - Constraint::Length(1), - Constraint::Length(3), - Constraint::Min(1), - ] - .as_ref(), - ) - .split(body_column_chunks[0]); - - let right_col_chunks = Layout::default() - .direction(Direction::Vertical) - .constraints( - [ - Constraint::Length(1), - Constraint::Length(3), - Constraint::Min(1), - ] - .as_ref(), - ) - .split(body_column_chunks[1]); - - (parent_chunks, right_col_chunks, left_col_chunks) -} - -fn create_screen_title<'a>(title_str: &'a str) -> Paragraph<'a> { - let title_block = Block::default().style(Style::default()); - - let title = Paragraph::new(Text::styled(title_str, Style::default().fg(Color::Green))) - .block(title_block); - title -} - -fn create_input_block<'a>(app: &'a mut App) -> Paragraph<'a> { - let input_block = Block::default() - .borders(Borders::ALL) - .style(Style::default()); - - let input = Paragraph::new(Text::styled( - app.input.value(), - Style::default().fg(Color::Green), - )) - .block(input_block); - input -} - -pub fn TextInput(app: &mut App, frame: &mut Frame<'_, B>) { - // Create Render Objects & Layout - let (parent_chunks, right_col_chunks, _left_col_chunks) = create_layout(frame); - let title = create_screen_title("Test Title"); - let input = create_input_block(app); - // Render Widgets - frame.render_widget(title, parent_chunks[0]); // Title Widget - frame.render_widget(input, right_col_chunks[1]); // Input Widget -} diff --git a/src/screens/input/url.rs b/src/screens/input/url.rs deleted file mode 100644 index 5af8d7b..0000000 --- a/src/screens/input/url.rs +++ /dev/null @@ -1,57 +0,0 @@ -use std::rc::Rc; - -use tui::layout::{Constraint, Direction, Layout, Rect}; -use tui::prelude::Backend; -use tui::style::{Color, Style}; -use tui::text::Text; -use tui::widgets::{Block, Borders, Paragraph}; -use tui::Frame; - -use crate::app::App; - -pub fn handle_url_input_screen(app: &mut App, frame: &mut Frame<'_, B>) { - let layout_chunks = create_layout(frame); - - let title_paragraph = create_screen_title("URL Input"); - let input_block = create_input_block(app); - - frame.render_widget(title_paragraph, layout_chunks[0]); - frame.render_widget(input_block, layout_chunks[1]); -} - -fn create_layout(frame: &mut Frame<'_, B>) -> Rc<[Rect]> { - let parent_chunks = Layout::default() - .direction(Direction::Vertical) - .margin(2) - .constraints( - [ - Constraint::Length(3), // Title Area - Constraint::Length(3), // Input Area - Constraint::Min(1), - ] - .as_ref(), - ) - .split(frame.size()); - - parent_chunks -} - -fn create_screen_title<'a>(title_str: &'a str) -> Paragraph<'a> { - let title_block = Block::default().style(Style::default()); - let title = Paragraph::new(Text::styled(title_str, Style::default().fg(Color::Green))) - .block(title_block); - title -} - -fn create_input_block<'a>(app: &'a mut App) -> Paragraph<'a> { - let title_block = Block::default() - .borders(Borders::ALL) - .style(Style::default()); - - let title = Paragraph::new(Text::styled( - app.input.value(), - Style::default().fg(Color::Green), - )) - .block(title_block); - title -} diff --git a/src/screens/render.rs b/src/screens/render.rs index 92be29e..18f5b7c 100644 --- a/src/screens/render.rs +++ b/src/screens/render.rs @@ -48,10 +48,7 @@ pub fn render(app: &mut App, frame: &mut Frame<'_, B>) { .add_modifier(tui::style::Modifier::BOLD), ) .alignment(Alignment::Center); - frame.render_widget( - logo, - small_rect(frame.size()).intersection(default_rect(frame.size())), - ); + frame.render_widget(logo, small_rect(frame.size())); } else if app.current_screen == Screen::SavedCommands || app.current_screen == Screen::SavedKeys { @@ -87,7 +84,13 @@ pub fn render(app: &mut App, frame: &mut Frame<'_, B>) { let area = small_rect(frame.size()); let response = app.response.clone().unwrap(); let paragraph = Paragraph::new(Text::from(response.as_str())) - .style(Style::default().fg(Color::Yellow).bg(Color::Black)) + .block( + Block::default() + .borders(Borders::ALL) + .border_type(BorderType::Double) + .border_style(Style::new().bold()), + ) + .style(Style::default().fg(Color::Green).bg(Color::Black)) .alignment(Alignment::Center); frame.render_widget(paragraph, area); } @@ -215,7 +218,7 @@ pub fn render_header_paragraph(para: &'static str, title: &'static str) -> Parag .title(title) .title_alignment(Alignment::Center) .borders(Borders::ALL) - .border_type(BorderType::Rounded), + .border_type(BorderType::Double), ) .style(Style::default().fg(Color::Cyan).bg(Color::Black)) .alignment(Alignment::Center) diff --git a/src/screens/saved_commands.rs b/src/screens/saved_commands.rs index 5691007..c41268c 100644 --- a/src/screens/saved_commands.rs +++ b/src/screens/saved_commands.rs @@ -10,6 +10,7 @@ use tui::Frame; pub fn handle_saved_commands_screen(app: &mut App, frame: &mut Frame<'_, B>) { handle_screen_defaults(app, frame); + // if we select a command, open options if let Some(cmd) = app.selected { app.goto_screen(Screen::AlertMenu(cmd)); } @@ -20,20 +21,21 @@ pub fn handle_alert_menu(app: &mut App, frame: &mut Frame<'_, B>, cm .direction(Direction::Vertical) .constraints( [ - Constraint::Length(13), // This will be the alert box - Constraint::Percentage(65), // This aligns the main screen perfectly with the bottom - Constraint::Percentage(22), + Constraint::Percentage(25), + Constraint::Percentage(50), + Constraint::Percentage(25), ] .as_ref(), ) + .horizontal_margin(5) .split(frame.size()); // Render the alert box - let alert_box = layout[0]; + let alert_box = layout[1]; let alert_text_chunk = Block::default() .borders(Borders::ALL) - .style(Style::default().bg(Color::Red).fg(Color::White)) + .style(Style::default().bg(Color::Black).fg(Color::LightGreen)) .title("Alert"); - let options_box = layout[0].inner(&Margin { + let options_box = layout[1].inner(&Margin { vertical: 1, horizontal: 1, }); @@ -57,8 +59,8 @@ pub fn handle_alert_menu(app: &mut App, frame: &mut Frame<'_, B>, cm .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) .split(alert_box)[1]; let show_cmds = app.get_saved_commands().unwrap(); - let get = show_cmds.get(cmd).unwrap().clone(); - let paragraph = Paragraph::new(format!("{:?}", get)) + let selected = show_cmds.get(cmd).unwrap().clone(); + let paragraph = Paragraph::new(format!("{:?}", selected.get_command())) .block(Block::default().borders(Borders::ALL).title("Command")) .alignment(tui::layout::Alignment::Center); frame.render_widget(paragraph, cmd_str); @@ -70,7 +72,10 @@ pub fn handle_alert_menu(app: &mut App, frame: &mut Frame<'_, B>, cm app.execute_saved_command(cmd); app.goto_screen(Screen::Response(app.response.clone().unwrap())); } - Some(1) => app.delete_item(cmd), + // delete item + Some(1) => { + app.delete_item(selected.get_id()); + } // copy to clipboard Some(2) => { if let Err(e) = app.copy_cmd_to_clipboard(cmd) { @@ -78,6 +83,7 @@ pub fn handle_alert_menu(app: &mut App, frame: &mut Frame<'_, B>, cm } app.goto_screen(Screen::Success); } + // cancel Some(3) => { app.goto_screen(Screen::SavedCommands); }