Skip to content

Commit

Permalink
feat: adjusting editor and some performance addressing
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 8, 2024
1 parent 9e1b6f6 commit 6c110d2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 43 deletions.
1 change: 1 addition & 0 deletions reqtui/src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ where
T: AsRef<Path>,
{
let items = std::fs::read_dir(&schemas_dir)?;
tracing::debug!("{:?}", schemas_dir.as_ref());

let mut collections = vec![];

Expand Down
28 changes: 19 additions & 9 deletions tui/src/components/api_explorer/api_explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ pub struct ApiExplorer<'a> {

editor: ReqEditor<'a>,
editor_tab: ReqEditorTabs,
editor_body_scroll: usize,

responses_map: HashMap<Request, Rc<RefCell<ReqtuiResponse>>>,
}
Expand Down Expand Up @@ -99,12 +98,10 @@ impl<'a> ApiExplorer<'a> {
schema,
focused_pane: PaneFocus::ReqUri,
selected_pane: None,
layout,
colors,

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

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

Expand All @@ -121,6 +118,7 @@ impl<'a> ApiExplorer<'a> {

response_rx,
request_tx,
layout,
}
}

Expand All @@ -136,8 +134,11 @@ impl<'a> ApiExplorer<'a> {
}
RequestKind::Single(req) => {
self.selected_request = Some(Rc::new(RefCell::new(req.clone())));
self.editor =
ReqEditor::new(self.colors, self.selected_request.clone());
self.editor = ReqEditor::new(
self.colors,
self.selected_request.clone(),
self.layout.req_editor,
);
}
}
}
Expand Down Expand Up @@ -262,9 +263,9 @@ impl<'a> ApiExplorer<'a> {
.map(|sel| sel.eq(&PaneFocus::Editor))
.unwrap_or(false),
&self.editor_tab,
&mut self.editor_body_scroll,
);
frame.render_stateful_widget(self.editor.clone(), self.layout.req_editor, &mut state)
self.editor
.get_components(self.layout.req_editor, frame, &mut state);
}

fn drain_response_rx(&mut self) {
Expand All @@ -283,6 +284,12 @@ impl<'a> ApiExplorer<'a> {
KeyCode::Enter => self.selected_pane = Some(PaneFocus::Preview),
KeyCode::Tab => self.preview_tab = ResViewerTabs::next(&self.preview_tab),
KeyCode::Esc => self.selected_pane = None,
KeyCode::Char('0') if self.preview_tab.eq(&ResViewerTabs::Headers) => {
self.preview_header_scroll_x = 0;
}
KeyCode::Char('$') if self.preview_tab.eq(&ResViewerTabs::Headers) => {
self.preview_header_scroll_x = usize::MAX;
}
KeyCode::Char('h') => {
if let ResViewerTabs::Headers = self.preview_tab {
self.preview_header_scroll_x = self.preview_header_scroll_x.saturating_sub(1)
Expand Down Expand Up @@ -344,6 +351,7 @@ impl Component for ApiExplorer<'_> {
let row_with_offset = editor_position
.y
.add(cursor.row_with_offset() as u16)
.saturating_sub(self.editor.row_scroll() as u16)
.add(3);
let col_with_offset = editor_position
.x
Expand All @@ -356,7 +364,9 @@ impl Component for ApiExplorer<'_> {
}

fn resize(&mut self, new_size: Rect) {
self.layout = build_layout(new_size);
let new_layout = build_layout(new_size);
self.editor.resize(new_layout.req_editor);
self.layout = new_layout;
}
}

Expand Down
69 changes: 38 additions & 31 deletions tui/src/components/api_explorer/req_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ratatui::{
style::{Style, Stylize},
text::{Line, Span},
widgets::{Block, Borders, Paragraph, StatefulWidget, Tabs, Widget},
Frame,
};
use reqtui::{
schema::types::Request,
Expand All @@ -15,7 +16,7 @@ use reqtui::{
use std::{
cell::RefCell,
fmt::Display,
ops::{Add, Deref, Div, Mul, Sub},
ops::{Add, Div, Mul, Sub},
rc::Rc,
};
use tree_sitter::Tree;
Expand Down Expand Up @@ -44,6 +45,7 @@ pub enum ReqEditorTabs {
Auth,
}

#[derive(Debug)]
pub struct ReqEditorLayout {
tabs_pane: Rect,
content_pane: Rect,
Expand Down Expand Up @@ -81,37 +83,37 @@ pub struct ReqEditorState<'a> {
is_focused: bool,
is_selected: bool,
curr_tab: &'a ReqEditorTabs,
body_scroll: &'a mut usize,
}

impl<'a> ReqEditorState<'a> {
pub fn new(
is_focused: bool,
is_selected: bool,
curr_tab: &'a ReqEditorTabs,
body_scroll: &'a mut usize,
) -> Self {
pub fn new(is_focused: bool, is_selected: bool, curr_tab: &'a ReqEditorTabs) -> Self {
ReqEditorState {
is_focused,
curr_tab,
is_selected,
body_scroll,
}
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct ReqEditor<'a> {
colors: &'a colors::Colors,
body: TextObject<Write>,
tree: Option<Tree>,
cursor: Cursor,
styled_display: Vec<Line<'static>>,
editor_mode: EditorMode,
row_scroll: usize,
layout: ReqEditorLayout,
}

impl<'a> ReqEditor<'a> {
pub fn new(colors: &'a colors::Colors, request: Option<Rc<RefCell<Request>>>) -> Self {
pub fn new(
colors: &'a colors::Colors,
request: Option<Rc<RefCell<Request>>>,
size: Rect,
) -> Self {
tracing::debug!("should only run once");
let (body, tree) =
if let Some(body) = request.as_ref().and_then(|req| req.borrow().body.clone()) {
let mut highlighter = HIGHLIGHTER.write().unwrap();
Expand All @@ -132,9 +134,19 @@ impl<'a> ReqEditor<'a> {
styled_display,
cursor: Cursor::default(),
editor_mode: EditorMode::Normal,
row_scroll: 0,
layout: build_layout(size),
}
}

pub fn row_scroll(&self) -> usize {
self.row_scroll
}

pub fn resize(&mut self, new_size: Rect) {
self.layout = build_layout(new_size);
}

pub fn mode(&self) -> &EditorMode {
&self.editor_mode
}
Expand Down Expand Up @@ -189,24 +201,16 @@ impl<'a> ReqEditor<'a> {
Paragraph::new(Line::from(vec![mode, padding, percentage, cursor])).render(size, buf);
}

fn draw_editor(&self, state: &mut ReqEditorState, buf: &mut Buffer, size: Rect) {
fn draw_editor(&self, buf: &mut Buffer, size: Rect) {
let [request_pane, statusline_pane] = build_preview_layout(size);

self.draw_statusline(buf, statusline_pane);

if state
.body_scroll
.deref()
.ge(&self.styled_display.len().saturating_sub(1))
{
*state.body_scroll = self.styled_display.len().saturating_sub(1);
}

let lines_in_view = self
.styled_display
.clone()
.into_iter()
.skip(*state.body_scroll)
.skip(self.row_scroll)
.chain(std::iter::repeat(Line::from(
"~".fg(self.colors.bright.black),
)))
Expand All @@ -223,7 +227,7 @@ impl<'a> ReqEditor<'a> {
size: Rect,
) -> anyhow::Result<()> {
match state.curr_tab {
ReqEditorTabs::Request => self.draw_editor(state, buf, size),
ReqEditorTabs::Request => self.draw_editor(buf, size),
ReqEditorTabs::Headers => {}
ReqEditorTabs::Query => {}
ReqEditorTabs::Auth => {}
Expand Down Expand Up @@ -261,16 +265,12 @@ impl<'a> ReqEditor<'a> {
pub fn cursor(&self) -> &Cursor {
&self.cursor
}
}

impl<'a> StatefulWidget for ReqEditor<'a> {
type State = ReqEditorState<'a>;

fn render(self, size: Rect, buf: &mut Buffer, state: &mut Self::State) {
let layout = build_layout(size);
self.draw_container(size, buf, state);
self.draw_tabs(buf, state, layout.tabs_pane);
self.draw_current_tab(state, buf, layout.content_pane).ok();
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)
.ok();
}
}

Expand Down Expand Up @@ -333,6 +333,13 @@ impl Eventful for ReqEditor<'_> {
if self.cursor.row().lt(&len_lines.saturating_sub(1)) {
self.cursor.move_down(1);
}
if self
.cursor
.row()
.gt(&self.layout.content_pane.height.into())
{
self.row_scroll += 1;
}
let current_line_len = self.body.line_len(self.cursor.row());
self.cursor.maybe_snap_to_col(current_line_len);
}
Expand Down
4 changes: 2 additions & 2 deletions tui/src/components/api_explorer/res_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct ResViewerState<'a> {
headers_scroll_x: &'a mut usize,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum ResViewerTabs {
Preview,
Raw,
Expand Down Expand Up @@ -200,7 +200,7 @@ impl<'a> ResViewer<'a> {
// we add a blank line after every entry, we account for that here
.ge(&lines.len().saturating_sub(2))
{
*state.headers_scroll_y = lines.len().saturating_sub(1);
*state.headers_scroll_y = lines.len().saturating_sub(2);
}

if state
Expand Down
1 change: 0 additions & 1 deletion tui/src/components/api_explorer/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl<'a> StatefulWidget for Sidebar<'a> {
let block_border = if state.is_focused {
Style::default().fg(self.colors.bright.blue)
} else {
// TODO: we need better border colors
Style::default().fg(self.colors.bright.black)
};

Expand Down

0 comments on commit 6c110d2

Please sign in to comment.