Skip to content

Commit

Permalink
feat: hiding body tab when request method has no body
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 27, 2024
1 parent 1c80b48 commit 0b7a9ed
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
11 changes: 4 additions & 7 deletions tui/src/pages/collection_viewer/collection_viewer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::pages::{
collection_viewer::{
req_editor::{ReqEditor, ReqEditorState, ReqEditorTabs},
req_editor::{ReqEditor, ReqEditorState},
req_uri::{ReqUri, ReqUriState},
res_viewer::{ResViewer, ResViewerState, ResViewerTabs},
sidebar::{Sidebar, SidebarState},
Expand Down Expand Up @@ -146,7 +146,6 @@ pub struct CollectionViewer<'ae> {

sync_interval: std::time::Instant,
editor: ReqEditor<'ae>,
editor_tab: ReqEditorTabs,

sender: Option<UnboundedSender<Command>>,

Expand All @@ -162,7 +161,7 @@ impl<'ae> CollectionViewer<'ae> {
) -> Self {
let layout = build_layout(size);

let mut selected_request = collection.requests.as_ref().and_then(|requests| {
let selected_request = collection.requests.as_ref().and_then(|requests| {
requests.first().and_then(|req| {
if let RequestKind::Single(req) = req {
Some(Rc::new(RefCell::new(req.clone())))
Expand All @@ -186,8 +185,7 @@ impl<'ae> CollectionViewer<'ae> {
colors,
config,

editor: ReqEditor::new(colors, selected_request.as_mut(), layout.req_editor, config),
editor_tab: ReqEditorTabs::Request,
editor: ReqEditor::new(colors, selected_request.clone(), layout.req_editor, config),

res_viewer: ResViewer::new(colors, None),

Expand Down Expand Up @@ -232,7 +230,7 @@ impl<'ae> CollectionViewer<'ae> {
self.selected_request = Some(Rc::new(RefCell::new(req.clone())));
self.editor = ReqEditor::new(
self.colors,
self.selected_request.as_mut(),
self.selected_request.clone(),
self.layout.req_editor,
self.config,
);
Expand Down Expand Up @@ -376,7 +374,6 @@ impl<'ae> CollectionViewer<'ae> {
.as_ref()
.map(|sel| sel.eq(&PaneFocus::Editor))
.unwrap_or(false),
&self.editor_tab,
);
self.editor
.get_components(self.layout.req_editor, frame, &mut state);
Expand Down
91 changes: 55 additions & 36 deletions tui/src/pages/collection_viewer/req_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ratatui::{
Frame,
};
use reqtui::{
collection::types::Request,
collection::types::{Request, RequestMethod},
command::Command,
syntax::highlighter::HIGHLIGHTER,
text_object::{cursor::Cursor, TextObject, Write},
Expand All @@ -26,7 +26,7 @@ use tree_sitter::Tree;
#[derive(Debug, Default, Clone)]
pub enum ReqEditorTabs {
#[default]
Request,
Body,
Headers,
Query,
Auth,
Expand All @@ -38,21 +38,10 @@ pub struct ReqEditorLayout {
pub content_pane: Rect,
}

impl From<ReqEditorTabs> for usize {
fn from(value: ReqEditorTabs) -> Self {
match value {
ReqEditorTabs::Request => 0,
ReqEditorTabs::Headers => 1,
ReqEditorTabs::Query => 2,
ReqEditorTabs::Auth => 3,
}
}
}

impl Display for ReqEditorTabs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReqEditorTabs::Request => f.write_str("Request"),
ReqEditorTabs::Body => f.write_str("Request"),
ReqEditorTabs::Headers => f.write_str("Headers"),
ReqEditorTabs::Query => f.write_str("Query"),
ReqEditorTabs::Auth => f.write_str("Auth"),
Expand All @@ -66,17 +55,15 @@ impl AsRef<ReqEditorTabs> for ReqEditorTabs {
}
}

pub struct ReqEditorState<'re> {
pub struct ReqEditorState {
is_focused: bool,
is_selected: bool,
curr_tab: &'re ReqEditorTabs,
}

impl<'re> ReqEditorState<'re> {
pub fn new(is_focused: bool, is_selected: bool, curr_tab: &'re ReqEditorTabs) -> Self {
impl<'re> ReqEditorState {
pub fn new(is_focused: bool, is_selected: bool) -> Self {
ReqEditorState {
is_focused,
curr_tab,
is_selected,
}
}
Expand All @@ -94,6 +81,9 @@ pub struct ReqEditor<'re> {
col_scroll: usize,
layout: ReqEditorLayout,
config: &'re config::Config,
request: Option<Rc<RefCell<Request>>>,

curr_tab: ReqEditorTabs,

/// whenever we press a key that is a subset of any keymap, we buffer the keymap until we can
/// determine which keymap was pressed or cancel if no matches.
Expand All @@ -106,7 +96,8 @@ pub struct ReqEditor<'re> {
impl<'re> ReqEditor<'re> {
pub fn new(
colors: &'re colors::Colors,
request: Option<&mut Rc<RefCell<Request>>>,
request: Option<Rc<RefCell<Request>>>,

size: Rect,
config: &'re config::Config,
) -> Self {
Expand Down Expand Up @@ -135,7 +126,13 @@ impl<'re> ReqEditor<'re> {
row_scroll: 0,
col_scroll: 0,
layout: build_layout(size),

curr_tab: request
.as_ref()
.map(request_has_no_body)
.unwrap()
.then_some(ReqEditorTabs::Headers)
.unwrap_or_default(),
request,
keymap_buffer: None,
}
}
Expand Down Expand Up @@ -233,14 +230,9 @@ impl<'re> ReqEditor<'re> {
Paragraph::new(lines_in_view).render(request_pane, buf);
}

fn draw_current_tab(
&self,
state: &mut ReqEditorState,
buf: &mut Buffer,
size: Rect,
) -> anyhow::Result<()> {
match state.curr_tab {
ReqEditorTabs::Request => self.draw_editor(buf, size),
fn draw_current_tab(&self, buf: &mut Buffer, size: Rect) -> anyhow::Result<()> {
match self.curr_tab {
ReqEditorTabs::Body => self.draw_editor(buf, size),
ReqEditorTabs::Headers => {}
ReqEditorTabs::Query => {}
ReqEditorTabs::Auth => {}
Expand All @@ -249,16 +241,36 @@ impl<'re> ReqEditor<'re> {
Ok(())
}

fn draw_tabs(&self, buf: &mut Buffer, state: &ReqEditorState, size: Rect) {
let tabs = Tabs::new(["Request", "Headers", "Query", "Auth"])
fn draw_tabs(&self, buf: &mut Buffer, size: Rect) {
let (tabs, active) = if self.request.as_ref().map(request_has_no_body).unwrap() {
let tabs = vec!["Headers", "Query", "Auth"];
let active = match self.curr_tab {
ReqEditorTabs::Headers => 0,
ReqEditorTabs::Query => 1,
ReqEditorTabs::Auth => 2,
_ => 0,
};
(tabs, active)
} else {
let tabs = vec!["Body", "Headers", "Query", "Auth"];
let active = match self.curr_tab {
ReqEditorTabs::Body => 0,
ReqEditorTabs::Headers => 1,
ReqEditorTabs::Query => 2,
ReqEditorTabs::Auth => 3,
};
(tabs, active)
};

Tabs::new(tabs)
.style(Style::default().fg(self.colors.bright.black))
.select(state.curr_tab.clone().into())
.select(active)
.highlight_style(
Style::default()
.fg(self.colors.normal.white)
.bg(self.colors.normal.blue),
);
tabs.render(size, buf);
)
.render(size, buf);
}

fn draw_container(&self, size: Rect, buf: &mut Buffer, state: &mut ReqEditorState) {
Expand All @@ -282,8 +294,8 @@ impl<'re> ReqEditor<'re> {

pub fn get_components(&self, size: Rect, frame: &mut Frame, state: &mut ReqEditorState) {
self.draw_container(size, frame.buffer_mut(), state);
self.draw_tabs(frame.buffer_mut(), state, self.layout.tabs_pane);
self.draw_current_tab(state, frame.buffer_mut(), self.layout.content_pane)
self.draw_tabs(frame.buffer_mut(), self.layout.tabs_pane);
self.draw_current_tab(frame.buffer_mut(), self.layout.content_pane)
.ok();
}

Expand Down Expand Up @@ -784,3 +796,10 @@ fn keycode_as_string(key_event: KeyEvent) -> String {
_ => String::default(),
}
}

fn request_has_no_body(request: &Rc<RefCell<Request>>) -> bool {
matches!(
request.borrow().method,
RequestMethod::Get | RequestMethod::Delete
)
}

0 comments on commit 0b7a9ed

Please sign in to comment.