Skip to content

Commit

Permalink
wip: implementing auth kind selector
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed Aug 11, 2024
1 parent ddfde56 commit 79023ea
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 68 deletions.
10 changes: 4 additions & 6 deletions hac-client/src/components/list_item.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::borrow::Cow;

use ratatui::{
style::{Style, Stylize},
text::{Line, Span},
widgets::{Block, Borders, Paragraph},
};
use ratatui::style::{Style, Stylize};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Paragraph};

use super::component_styles::{color_from_focus, ComponentBorder, ComponentFocus};
use crate::components::component_styles::{color_from_focus, ComponentBorder, ComponentFocus};

pub enum ListItemKind {
_Regular,
Expand Down
8 changes: 8 additions & 0 deletions hac-client/src/pages/collection_viewer/collection_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ impl Eventful for CollectionViewer<'_> {
self.request_tx.clone(),
),
Some(RequestUriEvent::RemoveSelection) => self.update_selection(None),
Some(RequestUriEvent::SelectNext) => {
self.update_selection(None);
self.focus_next();
}
Some(RequestUriEvent::SelectPrev) => {
self.update_selection(None);
self.focus_prev();
}
// when theres no event we do nothing
None => {}
},
Expand Down
1 change: 1 addition & 0 deletions hac-client/src/pages/collection_viewer/request_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ impl Eventful for RequestEditor<'_> {
let mut store = self.collection_store.borrow_mut();
store.push_overlay(CollectionViewerOverlay::ChangeAuthMethod);
}
Some(AuthEditorEvent::Quit) => return Ok(Some(RequestEditorEvent::Quit)),
None => {}
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::auth_kind_prompt::AuthKindPrompt;
use super::auth_kind_prompt::{AuthKindPrompt, AuthKindPromptEvent};
use crate::pages::collection_viewer::collection_store::CollectionStore;
use crate::pages::collection_viewer::collection_viewer::CollectionViewerOverlay;
use crate::pages::{Eventful, Renderable};
Expand All @@ -7,14 +7,16 @@ use std::cell::RefCell;
use std::ops::{Add, Sub};
use std::rc::Rc;

use crossterm::event::{KeyCode, KeyEvent};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use hac_core::collection::types::AuthMethod;
use ratatui::layout::Rect;
use ratatui::style::Stylize;
use ratatui::widgets::{Block, Borders, Paragraph};
use ratatui::Frame;

pub enum AuthEditorEvent {
ChangeAuthMethod,
Quit,
}

#[derive(Debug)]
Expand Down Expand Up @@ -81,7 +83,10 @@ impl Renderable for AuthEditor<'_> {
};

let request = request.read().unwrap();
let has_auth = request.auth_method.is_some();
let has_auth = request
.auth_method
.as_ref()
.is_some_and(|method| !matches!(method, AuthMethod::None));
self.draw_hint(frame, has_auth);

if !has_auth {
Expand All @@ -107,8 +112,29 @@ impl Eventful for AuthEditor<'_> {
fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
let overlay = self.collection_store.borrow().peek_overlay();

if let (KeyCode::Char('c'), KeyModifiers::CONTROL) = (key_event.code, key_event.modifiers) {
return Ok(Some(AuthEditorEvent::Quit));
}

let mut store = self.collection_store.borrow_mut();
let Some(request) = store.get_selected_request() else {
return Ok(None);
};

let mut request = request.write().unwrap();

if let CollectionViewerOverlay::ChangeAuthMethod = overlay {
self.collection_store.borrow_mut().pop_overlay();
match self.auth_kind_prompt.handle_key_event(key_event)? {
Some(AuthKindPromptEvent::Cancel) => {
store.pop_overlay();
}
Some(AuthKindPromptEvent::Confirm(auth_kind)) => {
request.auth_method = Some(auth_kind);
store.pop_overlay();
}
None => (),
};

return Ok(None);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use crate::ascii::LOGO_ASCII;
use crate::components::component_styles::{ComponentBorder, ComponentFocus};
use crate::components::component_styles::ComponentBorder;
use crate::components::list_item::{list_item, ListItemKind};
use crate::pages::collection_viewer::collection_store::CollectionStore;
use crate::pages::{overlay::make_overlay, Eventful, Renderable};

use std::cell::RefCell;
use std::ops::{Add, Div, Sub};
use std::rc::Rc;

use crossterm::event::{KeyCode, KeyEvent};
use hac_core::AuthKind;
use hac_core::collection::types::AuthMethod;
use rand::Rng;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::layout::{Constraint, Direction, Flex, Layout, Rect};
use ratatui::style::Stylize;
use ratatui::text::Line;
use ratatui::widgets::Paragraph;
use ratatui::Frame;

pub enum AuthKindPromptEvent {
Placeholder,
Confirm(AuthMethod),
Cancel,
}

Expand Down Expand Up @@ -46,10 +50,29 @@ impl Renderable for AuthKindPrompt<'_> {
fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> {
make_overlay(self.colors, self.colors.normal.black, 0.1, frame);

//let mut logo = LOGO_ASCII[self.logo_idx];
//let mut logo_size = logo.len() as u16;
let logo = LOGO_ASCII[self.logo_idx];
let logo_size = logo.len() as u16;
// adding size of the form + spacing + hint

let [_, center, _] = Layout::default()
.constraints([
Constraint::Fill(1),
Constraint::Min(80),
Constraint::Fill(1),
])
.direction(Direction::Horizontal)
.areas(size);

let [logo_size, _, options_size] = Layout::default()
.constraints([
Constraint::Length(logo_size),
Constraint::Length(2),
Constraint::Fill(1),
])
.direction(Direction::Vertical)
.areas(center);

let auth_kinds = AuthKind::iter()
let auth_kinds = AuthMethod::iter()
.enumerate()
.map(|(idx, v)| {
list_item(
Expand All @@ -64,20 +87,26 @@ impl Renderable for AuthKindPrompt<'_> {

let constraints = auth_kinds
.iter()
.flat_map(|_| vec![Constraint::Length(3), Constraint::Length(1)])
.flat_map(|_| vec![Constraint::Length(3)])
.collect::<Vec<_>>();

let layout = Layout::default()
.constraints(constraints)
.constraints(constraints.clone())
.flex(Flex::Center)
.spacing(1)
.direction(Direction::Vertical)
.split(size);
.split(options_size);

let mut idx = 0;
for item in auth_kinds {
for (idx, item) in auth_kinds.iter().enumerate() {
frame.render_widget(item, layout[idx]);
idx += 2;
}

let logo = logo
.iter()
.map(|line| Line::from(line.fg(self.colors.normal.red)).centered())
.collect::<Vec<_>>();
frame.render_widget(Paragraph::new(logo), logo_size);

Ok(())
}
}
Expand All @@ -88,12 +117,15 @@ impl Eventful for AuthKindPrompt<'_> {
fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
match key_event.code {
KeyCode::Esc => return Ok(Some(AuthKindPromptEvent::Cancel)),
KeyCode::Enter => {}
KeyCode::Enter => {
let selected_auth_kind = AuthMethod::from(self.selected_idx);
return Ok(Some(AuthKindPromptEvent::Confirm(selected_auth_kind)));
}
KeyCode::Char('j') | KeyCode::Down => {
self.selected_idx = self.selected_idx.min(self.selected_idx + 1)
self.selected_idx = AuthMethod::len().min(self.selected_idx + 1);
}
KeyCode::Char('k') | KeyCode::Up => {
self.selected_idx = self.selected_idx.saturating_sub(1)
self.selected_idx = self.selected_idx.saturating_sub(1);
}
_ => {}
}
Expand Down
6 changes: 6 additions & 0 deletions hac-client/src/pages/collection_viewer/request_uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ pub enum RequestUriEvent {
/// user pressed `Esc` while request uri was selected, so we bubble
/// the event up for the parent to handle
RemoveSelection,
/// requests the parent to select the next pane
SelectNext,
/// requests the parent to select the previous pane
SelectPrev,
/// user pressed `C-c` hotkey so we bubble up the event for the parent to handle
Quit,
}
Expand Down Expand Up @@ -115,6 +119,8 @@ impl Eventful for RequestUri<'_> {

match key_event.code {
KeyCode::Esc => return Ok(Some(RequestUriEvent::RemoveSelection)),
KeyCode::Tab => return Ok(Some(RequestUriEvent::SelectNext)),
KeyCode::BackTab => return Ok(Some(RequestUriEvent::SelectPrev)),
KeyCode::Char(c) => {
if let Some(req) = self
.collection_store
Expand Down
1 change: 0 additions & 1 deletion hac-client/src/pages/collection_viewer/response_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pub struct ResponseViewer<'a> {
preview_layout: PreviewLayout,
layout: ResViewerLayout,
collection_store: Rc<RefCell<CollectionStore>>,

active_tab: ResViewerTabs,
raw_scroll: usize,
headers_scroll_y: usize,
Expand Down
70 changes: 69 additions & 1 deletion hac-core/src/collection/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,75 @@ pub struct Request {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum AuthMethod {
Bearer,
Basic,
None,
A,
B,
C,
D,
E,
}

#[derive(Default)]
pub struct AuthKindIter {
inner: u8,
}

impl std::fmt::Display for AuthMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AuthMethod::None => write!(f, "None"),
AuthMethod::Bearer => write!(f, "Bearer"),
AuthMethod::A => write!(f, "Bearer"),
AuthMethod::B => write!(f, "Bearer"),
AuthMethod::C => write!(f, "Bearer"),
AuthMethod::D => write!(f, "Bearer"),
AuthMethod::E => write!(f, "Bearer"),
}
}
}

impl From<usize> for AuthMethod {
fn from(value: usize) -> Self {
match value {
0 => AuthMethod::None,
1 => AuthMethod::Bearer,
2 => AuthMethod::A,
3 => AuthMethod::B,
4 => AuthMethod::C,
5 => AuthMethod::D,
6 => AuthMethod::E,
_ => AuthMethod::None,
}
}
}

impl AuthMethod {
pub fn iter() -> AuthKindIter {
AuthKindIter::default()
}

pub fn len() -> usize {
AuthKindIter::default().count()
}
}

impl Iterator for AuthKindIter {
type Item = AuthMethod;

fn next(&mut self) -> Option<Self::Item> {
let variant = match self.inner {
0 => Some(AuthMethod::None),
1 => Some(AuthMethod::Bearer),
2 => Some(AuthMethod::A),
3 => Some(AuthMethod::B),
4 => Some(AuthMethod::C),
5 => Some(AuthMethod::D),
6 => Some(AuthMethod::E),
_ => None,
};
self.inner += 1;
variant
}
}

/// a collection of all available body types we support.
Expand Down
40 changes: 0 additions & 40 deletions hac-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,3 @@ pub mod fs;
pub mod net;
pub mod syntax;
pub mod text_object;

#[derive(Debug, Copy, Clone)]
pub enum AuthKind {
Bearer,
None,
}

#[derive(Default)]
pub struct AuthKindIter {
inner: u8,
}

impl std::fmt::Display for AuthKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AuthKind::None => write!(f, "None"),
AuthKind::Bearer => write!(f, "Bearer"),
}
}
}

impl AuthKind {
pub fn iter() -> AuthKindIter {
AuthKindIter::default()
}
}

impl Iterator for AuthKindIter {
type Item = AuthKind;

fn next(&mut self) -> Option<Self::Item> {
let variant = match self.inner {
0 => Some(AuthKind::Bearer),
1 => Some(AuthKind::None),
_ => None,
};
self.inner += 1;
variant
}
}

0 comments on commit 79023ea

Please sign in to comment.