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

Commit

Permalink
Merge pull request #127 from PThorpe92/input-menu
Browse files Browse the repository at this point in the history
feat: adjust user input for api key menu. remove unneccessary input screen
  • Loading branch information
PThorpe92 authored Apr 17, 2024
2 parents 3abbfe0 + ba4ffb9 commit 03ee7dc
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# TUI HTTP Client with API/Auth Key Management and Request History/Storage

> This project is still in active development and although it is useable, there may still be bugs and significant changes are still needed to both refactor the codebase and add new features.
### Collaboration is welcome and encouraged! There is lots of low hanging fruit 👍 and cool ideas for additional features.
#### Collaboration is welcome and encouraged! There is lots of low hanging fruit 👍 and cool ideas for additional features.
![image](imgs/demo.gif)

Curl TUI Environment (CuTE). HTTP client/libcurl front-end in Rust, using the awesome [ratatui](https://github.com/ratatui-org/ratatui) library designed to simplify the process of sending HTTP requests in the terminal, allowing you to store and manage your previous requests + API keys.
Expand Down Expand Up @@ -70,7 +70,7 @@ This is a tool for when your requests are not complex enough for something like
```
4. **Move Binary**: Move the binary to a location in your PATH of your choosing:
```
sudo cp target/release/cute ~/.local/bin/
cp target/release/cute ~/.local/bin/
```

## Command Line Options
Expand Down
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a> App<'a> {
self.input.reset();
self.items = screen.get_opts(None);
}
Screen::SavedKeys => {
Screen::SavedKeys(_) => {
self.items = self
.get_saved_keys()
.unwrap_or_default()
Expand Down Expand Up @@ -265,7 +265,7 @@ impl<'a> App<'a> {

pub fn get_special_items(&self) -> Option<Vec<String>> {
match self.current_screen {
Screen::SavedKeys => Some(
Screen::SavedKeys(_) => Some(
self.get_saved_keys()
.unwrap_or_default()
.into_iter()
Expand Down Expand Up @@ -408,7 +408,7 @@ impl<'a> App<'a> {

pub fn delete_saved_key(&mut self, index: i32) -> Result<(), rusqlite::Error> {
self.db.as_ref().delete_key(index)?;
self.goto_screen(&Screen::SavedKeys);
self.goto_screen(&Screen::SavedKeys(None));
Ok(())
}

Expand Down
19 changes: 3 additions & 16 deletions src/events/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,12 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.select_item();
}
KeyCode::Char('a') => {
if app.current_screen == Screen::SavedKeys {
app.goto_screen(&Screen::InputMenu(InputOpt::ApiKey));
if let Screen::SavedKeys(_) = app.current_screen {
app.goto_screen(&Screen::SavedKeys(Some(InputOpt::ApiKey)))
}
}
KeyCode::Char('i') => match &app.current_screen {
Screen::InputMenu(_) => {
app.input_mode = InputMode::Editing;
}
Screen::RequestMenu(opt) if opt.is_some() => {
if !opt.as_ref().unwrap().is_error() {
app.input_mode = InputMode::Editing;
}
}
Screen::SavedCollections(opt) if opt.is_some() => {
if !opt.as_ref().unwrap().is_error() {
app.input_mode = InputMode::Editing;
}
}
Screen::RequestBodyInput => {
screen if screen.is_input_screen() => {
app.input_mode = InputMode::Editing;
}
_ => {}
Expand Down
4 changes: 2 additions & 2 deletions src/screens/input/input_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub fn parse_input(message: String, opt: InputOpt, app: &mut App) {
}
InputOpt::ApiKey => {
let _ = app.add_saved_key(message.clone());
app.goto_screen(&Screen::SavedKeys);
app.goto_screen(&Screen::SavedKeys(None));
}
InputOpt::UnixSocket => {
if let Err(e) = is_valid_unix_socket_path(&message) {
Expand Down Expand Up @@ -315,7 +315,7 @@ pub fn parse_input(message: String, opt: InputOpt, app: &mut App) {
}
}
InputOpt::KeyLabel(id) => match app.set_key_label(id, &message) {
Ok(_) => app.goto_screen(&Screen::SavedKeys),
Ok(_) => app.goto_screen(&Screen::SavedKeys(None)),
Err(e) => app.goto_screen(&Screen::Error(e)),
},
InputOpt::Auth(auth) => {
Expand Down
8 changes: 4 additions & 4 deletions src/screens/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn handle_screen_defaults(app: &mut App, frame: &mut Frame<'_>) {
Screen::Success => (&DEFAULT_MENU_PARAGRAPH, &SUCCESS_MENU_TITLE),
Screen::Error(_) => (&DEFAULT_MENU_PARAGRAPH, &ERROR_MENU_TITLE),
Screen::ViewBody => (&DEFAULT_MENU_PARAGRAPH, &VIEW_BODY_TITLE),
Screen::SavedKeys => (&API_KEY_PARAGRAPH, &API_KEY_TITLE),
Screen::SavedKeys(_) => (&API_KEY_PARAGRAPH, &API_KEY_TITLE),
Screen::HeaderAddRemove => (&DEFAULT_MENU_PARAGRAPH, &DEFAULT_MENU_TITLE),
Screen::SavedCollections(_) => (&DEFAULT_MENU_PARAGRAPH, &POSTMAN_COLLECTION_TITLE),
_ => (&DEFAULT_MENU_PARAGRAPH, &DEFAULT_MENU_TITLE),
Expand All @@ -120,7 +120,7 @@ pub fn handle_screen(app: &mut App, frame: &mut Frame<'_>, screen: Screen) {
0 => app.goto_screen(&Screen::Method),
1 => app.goto_screen(&Screen::SavedCommands(None)),
2 => app.goto_screen(&Screen::SavedCollections(None)),
3 => app.goto_screen(&Screen::SavedKeys),
3 => app.goto_screen(&Screen::SavedKeys(None)),
_ => {}
}
}
Expand Down Expand Up @@ -169,8 +169,8 @@ pub fn handle_screen(app: &mut App, frame: &mut Frame<'_>, screen: Screen) {
Screen::MoreFlags => {
more_flags::handle_more_flags_screen(app, frame);
}
Screen::SavedKeys => {
saved_keys::handle_saved_keys_screen(app, frame);
Screen::SavedKeys(opt) => {
saved_keys::handle_saved_keys_screen(app, frame, opt);
}
Screen::CmdMenu(cmd) => {
saved_commands::handle_alert_menu(app, frame, cmd);
Expand Down
14 changes: 9 additions & 5 deletions src/screens/saved_keys.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use super::input::input_screen::handle_default_input_screen;
use super::render::handle_screen_defaults;
use super::{centered_rect, Screen, ScreenArea};
use crate::app::App;
use crate::display::inputopt::InputOpt;
use crate::display::menuopts::KEY_MENU_OPTIONS;
use tui::prelude::{Constraint, Direction, Layout, Margin};
use tui::style::{Color, Modifier, Style};
use tui::widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph};
use tui::Frame;

pub fn handle_saved_keys_screen(app: &mut App, frame: &mut Frame<'_>) {
pub fn handle_saved_keys_screen(app: &mut App, frame: &mut Frame<'_>, opt: Option<InputOpt>) {
handle_screen_defaults(app, frame);
if app.items.is_empty() {
if let Some(opt) = opt {
handle_default_input_screen(app, frame, opt.clone());
} else if app.items.is_empty() {
let paragraph = Paragraph::new("No Keys Found. Press 'a' to add a new key.").block(
Block::default()
.borders(Borders::ALL)
Expand Down Expand Up @@ -80,9 +84,9 @@ pub fn handle_key_menu(app: &mut App, frame: &mut Frame<'_>, cmd: usize) {
match app.selected {
// Add/Edit Label
Some(0) => {
app.goto_screen(&Screen::InputMenu(
app.goto_screen(&Screen::SavedKeys(Some(
crate::display::inputopt::InputOpt::KeyLabel(selected.get_id()),
));
)));
}
// delete item
Some(1) => {
Expand All @@ -99,7 +103,7 @@ pub fn handle_key_menu(app: &mut App, frame: &mut Frame<'_>, cmd: usize) {
}
// cancel
Some(3) => {
app.goto_screen(&Screen::SavedKeys);
app.goto_screen(&Screen::SavedKeys(None));
}
_ => {}
}
Expand Down
20 changes: 16 additions & 4 deletions src/screens/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum Screen {
ViewSavedCollections,
Authentication,
Success,
SavedKeys,
SavedKeys(Option<InputOpt>),
ColMenu(i32),
// takes optional collection id
SavedCommands(Option<i32>),
Expand All @@ -35,6 +35,18 @@ pub enum Screen {
RequestBodyInput,
CookieOptions,
}
impl Screen {
pub fn is_input_screen(&self) -> bool {
match self {
Screen::RequestMenu(opt) => opt.is_some(),
Screen::InputMenu(_) => true,
Screen::SavedKeys(opt) => opt.is_some(),
Screen::RequestBodyInput => true,
Screen::SavedCollections(opt) => opt.is_some(),
_ => false,
}
}
}

impl Display for Screen {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand All @@ -47,7 +59,7 @@ impl Display for Screen {
Screen::Response(_) => "Response",
Screen::Authentication => "Authentication",
Screen::Success => "Success",
Screen::SavedKeys => "Saved Keys",
Screen::SavedKeys(_) => "Saved Keys",
Screen::SavedCommands(_) => "My Saved Commands",
Screen::Error(_) => "Error",
Screen::ViewBody => "ViewBody",
Expand All @@ -65,7 +77,7 @@ impl Display for Screen {
}
}

pub fn determine_line_size(len: usize) -> &'static str {
fn determine_line_size(len: usize) -> &'static str {
match len {
len if len <= 4 => OPTION_PADDING_MAX,
len if len < 8 => OPTION_PADDING_MID,
Expand Down Expand Up @@ -156,7 +168,7 @@ impl<'a> Screen {
.iter()
.map(|i| ListItem::new(*i))
.collect(),
Screen::SavedKeys => {
Screen::SavedKeys(_) => {
let mut len = 0;
if items.is_some() {
len = items.as_ref().unwrap().len();
Expand Down

0 comments on commit 03ee7dc

Please sign in to comment.