Skip to content

Commit

Permalink
feat: deleting headers and help overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed Jun 5, 2024
1 parent 4abb707 commit db523e3
Show file tree
Hide file tree
Showing 18 changed files with 452 additions and 121 deletions.
63 changes: 48 additions & 15 deletions hac-client/src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,54 @@ pub static SMALL_ERROR_ARTS: &[&[&str]] = &[
],
];

pub static LOGO_ART: &[&str] = &[
r#" ,--, "#,
r#" ,--.'| ,---, ,----.. "#,
r#" ,--, | : ' .' \ / / \ "#,
r#",---.'| : ' / ; '. | : : "#,
r#"| | : _' |: : \ . | ;. / "#,
r#": : |.' |: | /\ \ . ; /--` "#,
r#"| ' ' ; :| : ' ;. : ; | ; "#,
r#"' | .'. || | ;/ \ \| : | "#,
r#"| | : | '' : | \ \ ,'. | '___ "#,
r#"' : | : ;| | ' '--' ' ; : .'| "#,
r#"| | ' ,/ | : : ' | '/ : "#,
r#"; : ;--' | | ,' | : / "#,
r#"| ,/ `--'' \ \ .' "#,
r#"'---' `---` "#,
pub static LOGO_ASCII: &[&[&str]] = &[
&[
r#" ,--, "#,
r#" ,--.'| ,---, ,----.. "#,
r#" ,--, | : ' .' \ / / \ "#,
r#",---.'| : ' / ; '. | : : "#,
r#"| | : _' |: : \ . | ;. / "#,
r#": : |.' |: | /\ \ . ; /--` "#,
r#"| ' ' ; :| : ' ;. : ; | ; "#,
r#"' | .'. || | ;/ \ \| : | "#,
r#"| | : | '' : | \ \ ,'. | '___ "#,
r#"' : | : ;| | ' '--' ' ; : .'| "#,
r#"| | ' ,/ | : : ' | '/ : "#,
r#"; : ;--' | | ,' | : / "#,
r#"| ,/ `--'' \ \ .' "#,
r#"'---' `---` "#,
],
&[
r#" ____ ____ ____ _____ "#,
r#"| | | | ____|\ \ ___|\ \ "#,
r#"| | | | / /\ \ / /\ \ "#,
r#"| |_| || | | || | | |"#,
r#"| .-. || |__| || | |____|"#,
r#"| | | || .--. || | ____ "#,
r#"| | | || | | || | | |"#,
r#"|____| |____||____| |____||\ ___\/ /|"#,
r#"| | | || | | || | /____/ |"#,
r#"|____| |____||____| |____| \|___| | /"#,
r#" \( )/ \( )/ \( |____|/ "#,
r#" ' ' ' ' ' )/ "#,
r#" ' "#,
],
&[
r#" .uef^" "#,
r#":d88E "#,
r#"`888E u . "#,
r#" 888E .z8k us888u. .udR88N "#,
r#" 888E~?888L .@88 "8888" <888'888k "#,
r#" 888E 888E 9888 9888 9888 'Y" "#,
r#" 888E 888E 9888 9888 9888 "#,
r#" 888E 888E 9888 9888 9888 "#,
r#" 888E 888E 9888 9888 ?8888u../ "#,
r#"m888N= 888> "888*""888" "8888P' "#,
r#" `Y" 888 ^Y" ^Y' "P' "#,
r#" J88" "#,
r#" @% "#,
r#" :" "#,
],
];

pub static UNDER_CONSTRUCTION: &[&str] = &[
Expand Down
35 changes: 35 additions & 0 deletions hac-client/src/pages/collection_viewer/collection_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use hac_core::collection::types::{Request, RequestKind};
use hac_core::collection::Collection;

use crate::pages::collection_viewer::collection_viewer::CollectionViewerOverlay;
use crate::pages::collection_viewer::collection_viewer::PaneFocus;

use std::cell::RefCell;
Expand All @@ -17,6 +18,7 @@ pub struct CollectionState {
selected_pane: Option<PaneFocus>,
focused_pane: PaneFocus,
has_pending_request: bool,
overlay_stack: Vec<CollectionViewerOverlay>,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -61,6 +63,7 @@ impl CollectionStore {
focused_pane: PaneFocus::Sidebar,
selected_pane: None,
has_pending_request: false,
overlay_stack: vec![],
};

self.state = Some(Rc::new(RefCell::new(state)));
Expand Down Expand Up @@ -145,6 +148,38 @@ impl CollectionStore {
.map(|state| state.borrow().dirs_expanded.clone())
}

pub fn push_overlay(&mut self, overlay: CollectionViewerOverlay) {
if let Some(state) = self.state.as_mut() {
state.borrow_mut().overlay_stack.push(overlay)
}
}

pub fn pop_overlay(&mut self) -> Option<CollectionViewerOverlay> {
self.state
.as_mut()
.and_then(|state| state.borrow_mut().overlay_stack.pop())
}

pub fn peek_overlay(&self) -> CollectionViewerOverlay {
self.state
.as_ref()
.and_then(|state| state.borrow().overlay_stack.last().cloned())
.unwrap_or(CollectionViewerOverlay::None)
}

pub fn has_overlay(&self) -> bool {
self.state
.as_ref()
.map(|state| !state.borrow().overlay_stack.is_empty())
.unwrap_or(false)
}

pub fn clear_overlay(&mut self) {
if let Some(state) = self.state.as_mut() {
state.borrow_mut().overlay_stack.clear()
}
}

pub fn get_requests(&self) -> Option<Arc<RwLock<Vec<RequestKind>>>> {
self.state.as_ref().and_then(|state| {
state
Expand Down
43 changes: 20 additions & 23 deletions hac-client/src/pages/collection_viewer/collection_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ pub struct ExplorerLayout {
pub create_req_form: Rect,
}

#[derive(Debug, PartialEq)]
pub enum Overlays {
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum CollectionViewerOverlay {
None,
CreateRequest,
RequestMethod,
HeadersHelp,
HeadersDelete,
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -145,7 +147,6 @@ pub struct CollectionViewer<'cv> {
collection_sync_timer: std::time::Instant,
collection_store: Rc<RefCell<CollectionStore>>,

curr_overlay: Overlays,
create_req_form_state: CreateReqFormState,

responses_map: HashMap<String, Rc<RefCell<Response>>>,
Expand Down Expand Up @@ -188,7 +189,6 @@ impl<'cv> CollectionViewer<'cv> {
global_command_sender: None,
collection_sync_timer: std::time::Instant::now(),

curr_overlay: Overlays::None,
create_req_form_state: CreateReqFormState::default(),

responses_map: HashMap::default(),
Expand Down Expand Up @@ -430,7 +430,7 @@ impl<'cv> CollectionViewer<'cv> {
}
(KeyCode::Enter, _, FormFocus::CancelButton) | (KeyCode::Esc, _, _) => {
self.create_req_form_state = CreateReqFormState::default();
self.curr_overlay = Overlays::None;
self.collection_store.borrow_mut().clear_overlay();
}
_ => {}
}
Expand All @@ -451,7 +451,7 @@ impl<'cv> CollectionViewer<'cv> {
}
(KeyCode::Esc, _) => {
self.create_req_form_state = CreateReqFormState::default();
self.curr_overlay = Overlays::None;
self.collection_store.borrow_mut().clear_overlay();
}
_ => {}
}
Expand Down Expand Up @@ -525,7 +525,9 @@ impl<'cv> CollectionViewer<'cv> {
fn create_or_ask_for_request_method(&mut self) {
let form_state = &self.create_req_form_state;
if form_state.req_kind.eq(&CreateReqKind::Request) {
self.curr_overlay = Overlays::RequestMethod;
self.collection_store
.borrow_mut()
.push_overlay(CollectionViewerOverlay::RequestMethod);
return;
}
self.create_and_sync_request();
Expand Down Expand Up @@ -564,7 +566,7 @@ impl<'cv> CollectionViewer<'cv> {

self.sidebar.rebuild_tree_view();
self.create_req_form_state = CreateReqFormState::default();
self.curr_overlay = Overlays::None;
self.collection_store.borrow_mut().clear_overlay();

// TODO: maybe the collection store should be responsible for syncing to disk
self.sync_collection_changes();
Expand Down Expand Up @@ -671,10 +673,13 @@ impl Renderable for CollectionViewer<'_> {
self.request_editor.draw(frame, self.layout.req_editor)?;
self.request_uri.draw(frame, self.layout.req_uri)?;

match self.curr_overlay {
Overlays::CreateRequest => self.draw_create_request_form(frame),
Overlays::RequestMethod => self.draw_request_method_form(frame),
Overlays::None => {}
let overlay = self.collection_store.borrow().peek_overlay();
match overlay {
CollectionViewerOverlay::CreateRequest => self.draw_create_request_form(frame),
CollectionViewerOverlay::RequestMethod => self.draw_request_method_form(frame),
CollectionViewerOverlay::HeadersHelp => self.request_editor.draw_overlay(frame)?,
CollectionViewerOverlay::HeadersDelete => self.request_editor.draw_overlay(frame)?,
CollectionViewerOverlay::None => {}
}

if self
Expand Down Expand Up @@ -738,16 +743,6 @@ impl Eventful for CollectionViewer<'_> {
type Result = Command;

fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Command>> {
if self.curr_overlay.ne(&Overlays::None) {
match self.curr_overlay {
Overlays::CreateRequest => return self.handle_create_request_key_event(key_event),
Overlays::RequestMethod => return self.handle_request_method_key_event(key_event),
_ => {}
};

return Ok(None);
}

if let (
None,
KeyEvent {
Expand Down Expand Up @@ -802,7 +797,9 @@ impl Eventful for CollectionViewer<'_> {
match curr_pane {
PaneFocus::Sidebar => match self.sidebar.handle_key_event(key_event)? {
Some(SidebarEvent::CreateRequest) => {
self.curr_overlay = Overlays::CreateRequest
self.collection_store
.borrow_mut()
.push_overlay(CollectionViewerOverlay::CreateRequest);
}
Some(SidebarEvent::RemoveSelection) => self.update_selection(None),
Some(SidebarEvent::Quit) => return Ok(Some(Command::Quit)),
Expand Down
20 changes: 20 additions & 0 deletions hac-client/src/pages/collection_viewer/request_editor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod auth_editor;
mod body_editor;
mod headers_editor;
mod headers_editor_delete_prompt;

use auth_editor::AuthEditor;
use body_editor::{BodyEditor, BodyEditorEvent};
Expand Down Expand Up @@ -216,6 +217,15 @@ impl<'re> RequestEditor<'re> {

frame.render_widget(block, size);
}

pub fn draw_overlay(&mut self, frame: &mut Frame) -> anyhow::Result<()> {
match self.curr_tab {
ReqEditorTabs::Body => todo!(),
ReqEditorTabs::Headers => self.headers_editor.draw_overlay(frame),
ReqEditorTabs::Query => todo!(),
ReqEditorTabs::Auth => todo!(),
}
}
}

impl Renderable for RequestEditor<'_> {
Expand All @@ -241,6 +251,11 @@ impl Eventful for RequestEditor<'_> {
);

if let KeyCode::Tab = key_event.code {
let mut store = self.collection_store.borrow_mut();
if store.has_overlay() {
store.pop_overlay();
return Ok(None);
}
if self.curr_tab.eq(&ReqEditorTabs::Body)
&& self.body_editor.mode().eq(&EditorMode::Insert)
{
Expand All @@ -250,6 +265,11 @@ impl Eventful for RequestEditor<'_> {
}

if let KeyCode::BackTab = key_event.code {
let mut store = self.collection_store.borrow_mut();
if store.has_overlay() {
store.pop_overlay();
return Ok(None);
}
if self.curr_tab.eq(&ReqEditorTabs::Body)
&& self.body_editor.mode().eq(&EditorMode::Insert)
{
Expand Down
Loading

0 comments on commit db523e3

Please sign in to comment.