Skip to content

Commit

Permalink
feat: scrolling and line wrap of pretty preview
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 2, 2024
1 parent 4070300 commit 2e01da4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
10 changes: 4 additions & 6 deletions reqtui/src/text_object/text_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ pub struct Write;
pub struct TextObject<State = Readonly> {
content: Rope,
state: std::marker::PhantomData<State>,
pub display: Paragraph<'static>,
pub display: Vec<Line<'static>>,
}

impl TextObject<Readonly> {
pub fn from(content: &str) -> TextObject<Readonly> {
TextObject::<Readonly> {
display: Paragraph::new(content.to_string()),
display: vec![Line::from(content.to_string())],
content: Rope::from_str(content),
state: std::marker::PhantomData::<Readonly>,
}
Expand All @@ -39,7 +39,7 @@ impl TextObject<Readonly> {

impl TextObject {
pub fn with_highlight(self, colors: Vec<ColorInfo>) -> Self {
let mut lines: Vec<Line> = vec![];
let mut display: Vec<Line> = vec![];
let mut current_line: Vec<Span> = vec![];
for (idx, c) in self.to_string().chars().enumerate() {
let style = colors
Expand All @@ -51,13 +51,11 @@ impl TextObject {
current_line.push(c.to_string().set_style(style));

if c.eq(&'\n') {
lines.push(current_line.clone().into());
display.push(current_line.clone().into());
current_line.clear();
}
}

let display = Paragraph::new(lines);

Self {
content: self.content,
state: std::marker::PhantomData,
Expand Down
23 changes: 21 additions & 2 deletions tui/src/components/api_explorer/res_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ratatui::{
text::Line,
widgets::{
Block, Borders, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState, StatefulWidget,
Tabs, Widget,
Tabs, Widget, Wrap,
},
};
use std::{
Expand Down Expand Up @@ -171,7 +171,26 @@ impl<'a> ResViewer<'a> {

fn draw_preview_response(&self, state: &mut ResViewerState, buf: &mut Buffer, size: Rect) {
if let Some(response) = state.response {
response.pretty_body.display.clone().render(size, buf);
let lines = response.pretty_body.display.clone();

// allow for scrolling down until theres only one line left into view
if state.raw_scroll.deref().ge(&lines.len().saturating_sub(1)) {
*state.raw_scroll = lines.len().saturating_sub(1);
}

let [request_pane, scrollbar_pane] = build_preview_layout(size);

self.draw_scrollbar(lines.len(), *state.raw_scroll, buf, scrollbar_pane);

let lines_in_view = lines
.into_iter()
.skip(*state.raw_scroll)
.chain(iter::repeat(Line::from("~".fg(self.colors.normal.magenta))))
.take(size.height.into())
.collect::<Vec<_>>();

let pretty_response = Paragraph::new(lines_in_view).wrap(Wrap { trim: false });
pretty_response.render(request_pane, buf);
}
}
}
Expand Down

0 comments on commit 2e01da4

Please sign in to comment.