Skip to content

Commit

Permalink
Improve event handling on headers tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Reiter committed Jul 13, 2024
1 parent 0bea9a4 commit e315ad1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
52 changes: 40 additions & 12 deletions wireman/src/events/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum HeadersEvents {
PrevCol,
NextColForce,
PrevColForce,
NextAuth,
PrevAuth,
Unselect,
AddHeaders,
DelHeaders,
Expand All @@ -27,16 +29,18 @@ impl fmt::Display for HeadersEvents {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
HeadersEvents::NextTab => "Next Tab",
HeadersEvents::PrevTab => "Previous Tab",
HeadersEvents::PrevTab => "Prev Tab",
HeadersEvents::NextRow => "Next Row",
HeadersEvents::PrevRow => "Previous Row",
HeadersEvents::PrevRow => "Prev Row",
HeadersEvents::NextCol => "Next Column",
HeadersEvents::PrevCol => "Previous Column",
HeadersEvents::PrevCol => "Prev Column",
HeadersEvents::NextAuth => "Next auth mode",
HeadersEvents::PrevAuth => "Prev auth mode",
HeadersEvents::NextColForce => "Next Column (Force)",
HeadersEvents::PrevColForce => "Previous Column (Force)",
HeadersEvents::PrevColForce => "Prev Column (Force)",
HeadersEvents::Unselect => "Unselect",
HeadersEvents::AddHeaders => "Add Headers",
HeadersEvents::DelHeaders => "Delete Headers",
HeadersEvents::DelHeaders => "Del Headers",
HeadersEvents::SaveHistory => "Save Request",
HeadersEvents::LoadHistory1 => "Load History 1",
HeadersEvents::LoadHistory2 => "Load History 2",
Expand Down Expand Up @@ -81,6 +85,12 @@ impl EventHandler for HeadersEventHandler {
HeadersEvents::PrevCol => {
ctx.headers.borrow_mut().prev_col();
}
HeadersEvents::NextAuth => {
ctx.headers.borrow_mut().next_col();
}
HeadersEvents::PrevAuth => {
ctx.headers.borrow_mut().prev_col();
}
HeadersEvents::NextColForce => {
ctx.headers.borrow_mut().next_col();
}
Expand Down Expand Up @@ -125,12 +135,18 @@ impl EventHandler for HeadersEventHandler {
}

fn key_event_mappings(ctx: &Self::Context) -> Vec<(KeyEvent, HeadersEvents)> {
let disabled_root_events = ctx.headers.borrow().disabled_root_events();
let (is_first_col, is_last_col) = match ctx.headers.borrow().selected_editor() {
let headers = ctx.headers.borrow();
let disabled_root_events = headers.disabled_root_events();
let selected_editor = headers.selected_editor();
let (is_first_col, is_last_col) = match selected_editor {
Some(e) => (e.is_first_col(), e.is_last_col()),
None => (true, true),
};
let is_meta_tab = ctx.headers.borrow().tab == HeadersTab::Meta;
let enable_switch_auth_tab = ctx.headers.borrow().tab == HeadersTab::Auth
&& selected_editor.map_or(true, |e| e.is_empty());
let enable_switch_col_force = ctx.headers.borrow().tab == HeadersTab::Meta;
let enable_next_col = enable_switch_col_force && is_last_col;
let enable_prev_col = enable_switch_col_force && is_first_col;
let mut map = Vec::new();
map.extend([(KeyEvent::new(KeyCode::Enter), HeadersEvents::NextTab)]);
if !disabled_root_events {
Expand Down Expand Up @@ -167,19 +183,31 @@ impl EventHandler for HeadersEventHandler {
),
]);
}
if !disabled_root_events && is_last_col {
if !disabled_root_events && enable_switch_auth_tab {
map.extend([
(KeyEvent::new(KeyCode::Right), HeadersEvents::NextAuth),
(KeyEvent::new(KeyCode::Char('l')), HeadersEvents::NextAuth),
]);
}
if !disabled_root_events && enable_switch_auth_tab {
map.extend([
(KeyEvent::new(KeyCode::Left), HeadersEvents::PrevAuth),
(KeyEvent::new(KeyCode::Char('h')), HeadersEvents::PrevAuth),
]);
}
if !disabled_root_events && enable_next_col {
map.extend([
(KeyEvent::new(KeyCode::Right), HeadersEvents::NextCol),
(KeyEvent::new(KeyCode::Char('l')), HeadersEvents::NextCol),
]);
}
if !disabled_root_events && is_first_col {
if !disabled_root_events && enable_prev_col {
map.extend([
(KeyEvent::new(KeyCode::Left), HeadersEvents::PrevCol),
(KeyEvent::new(KeyCode::Char('h')), HeadersEvents::PrevCol),
]);
}
if is_meta_tab {
if enable_switch_col_force {
map.extend([
(
KeyEvent::shift(KeyCode::Char('L')),
Expand All @@ -195,7 +223,7 @@ impl EventHandler for HeadersEventHandler {
KeyEvent::ctrl(KeyCode::Char('a')),
HeadersEvents::AddHeaders,
)]);
if is_meta_tab {
if enable_switch_col_force {
map.extend([(
KeyEvent::ctrl(KeyCode::Char('d')),
HeadersEvents::DelHeaders,
Expand Down
2 changes: 1 addition & 1 deletion wireman/src/events/messages/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl fmt::Display for RequestEvents {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
RequestEvents::NextTab => "Next Tab",
RequestEvents::PrevTab => "Previous Tab",
RequestEvents::PrevTab => "Prev Tab",
RequestEvents::MakeRequest => "Make Request",
RequestEvents::AbortRequest => "Abort Request",
RequestEvents::GoToResponse => "Go to Response",
Expand Down
2 changes: 1 addition & 1 deletion wireman/src/events/messages/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl fmt::Display for ResponseEvents {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
ResponseEvents::NextTab => "Next Tab",
ResponseEvents::PrevTab => "Previous Tab",
ResponseEvents::PrevTab => "Prev Tab",
ResponseEvents::GoToRequest => "Go to Request",
ResponseEvents::CopyAsGrpCurl => "Copy as cURL",
};
Expand Down
4 changes: 2 additions & 2 deletions wireman/src/events/selection/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl fmt::Display for MethodsSelectionEvents {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
MethodsSelectionEvents::Next => "Next",
MethodsSelectionEvents::Prev => "Previous",
MethodsSelectionEvents::Prev => "Prev",
MethodsSelectionEvents::NextTab => "Next Tab",
MethodsSelectionEvents::PrevTab => "Previous Tab",
MethodsSelectionEvents::PrevTab => "Prev Tab",
MethodsSelectionEvents::Select => "Select",
MethodsSelectionEvents::Search => "Search",
MethodsSelectionEvents::Unselect => "Unselect",
Expand Down
2 changes: 1 addition & 1 deletion wireman/src/events/selection/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl fmt::Display for ServicesSelectionEvents {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
ServicesSelectionEvents::Next => "Next",
ServicesSelectionEvents::Prev => "Previous",
ServicesSelectionEvents::Prev => "Prev",
ServicesSelectionEvents::Select => "Select",
ServicesSelectionEvents::Search => "Search",
ServicesSelectionEvents::ClearSearch => "Clear Search",
Expand Down

0 comments on commit e315ad1

Please sign in to comment.