Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
feat: add new menu for saved commands
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Oct 17, 2023
1 parent a37c183 commit a6ac896
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 178 deletions.
19 changes: 9 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/database/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
5 changes: 0 additions & 5 deletions src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
_ => {}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/screens/input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ fn render_input_with_prompt<B: Backend>(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) {
Expand Down
2 changes: 0 additions & 2 deletions src/screens/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub mod input;

Check warning on line 1 in src/screens/input/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

module has the same name as its containing module

warning: module has the same name as its containing module --> src/screens/input/mod.rs:1:1 | 1 | pub mod input; | ^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#module_inception = note: `#[warn(clippy::module_inception)]` on by default
pub mod test;
pub mod url;
86 changes: 0 additions & 86 deletions src/screens/input/test.rs

This file was deleted.

57 changes: 0 additions & 57 deletions src/screens/input/url.rs

This file was deleted.

15 changes: 9 additions & 6 deletions src/screens/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ pub fn render<B: Backend>(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
{
Expand Down Expand Up @@ -87,7 +84,13 @@ pub fn render<B: Backend>(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);
}
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions src/screens/saved_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tui::Frame;

pub fn handle_saved_commands_screen<B: Backend>(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));
}
Expand All @@ -20,20 +21,21 @@ pub fn handle_alert_menu<B: Backend>(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,
});
Expand All @@ -57,8 +59,8 @@ pub fn handle_alert_menu<B: Backend>(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);
Expand All @@ -70,14 +72,18 @@ pub fn handle_alert_menu<B: Backend>(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) {
app.goto_screen(Screen::Error(e.to_string()));
}
app.goto_screen(Screen::Success);
}
// cancel
Some(3) => {
app.goto_screen(Screen::SavedCommands);
}
Expand Down

0 comments on commit a6ac896

Please sign in to comment.