Skip to content

Commit

Permalink
Add loading state in response panel
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealLorenz committed Nov 27, 2023
1 parent 97167f0 commit 5e8dbad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
5 changes: 4 additions & 1 deletion rq-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ impl App {
KeyCode::Enter => match self.focus {
FocusState::RequestsList => self.focus = FocusState::ResponseBuffer,
FocusState::ResponseBuffer => {
let index = self.request_menu.idx();
self.responses[index].set_loading();

self.req_tx
.send((
self.request_menu.selected().clone(),
Expand Down Expand Up @@ -232,7 +235,7 @@ impl App {
pub fn update(&mut self) {
// Poll for request responses
if let Ok((res, i)) = self.res_rx.try_recv() {
self.responses[i] = ResponsePanel::from(res);
self.responses[i].set_response(res);
}

if self.message_popup.is_none() {
Expand Down
54 changes: 36 additions & 18 deletions rq-cli/src/components/response_panel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::anyhow;
use crossterm::event::KeyCode;
use ratatui::{
style::{Color, Style},
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Paragraph, Scrollbar, ScrollbarState, Wrap},
};
Expand Down Expand Up @@ -44,23 +44,30 @@ impl MenuItem for SaveOption {
}
}

#[derive(Clone, Default)]
enum State {
#[default]
Empty,
Loading,
Received(Response),
}

#[derive(Clone, Default)]
pub struct ResponsePanel {
content: Option<Response>,
state: State,
scroll: u16,
input_popup: Option<Popup<Input>>,
save_option: SaveOption,
save_menu: Option<Popup<Menu<SaveOption>>>,
}

impl From<Response> for ResponsePanel {
fn from(value: Response) -> Self {
let default = Self::default();
impl ResponsePanel {
pub fn set_loading(&mut self) {
self.state = State::Loading;
}

Self {
content: Some(value),
..default
}
pub fn set_response(&mut self, value: Response) {
self.state = State::Received(value)
}
}

Expand All @@ -74,15 +81,15 @@ impl ResponsePanel {
}

fn body(&self) -> anyhow::Result<Content> {
match &self.content {
Some(response) => Ok(response.body.clone()),
None => Err(anyhow!("Request not sent")),
match &self.state {
State::Received(response) => Ok(response.body.clone()),
State::Empty | State::Loading => Err(anyhow!("Request not sent")),
}
}

fn to_string(&self) -> anyhow::Result<String> {
match &self.content {
Some(response) => {
match &self.state {
State::Received(response) => {
let headers = response
.headers
.iter()
Expand All @@ -100,7 +107,7 @@ impl ResponsePanel {

Ok(s)
}
None => Err(anyhow!("Request not sent")),
State::Empty | State::Loading => Err(anyhow!("Request not sent")),
}
}
}
Expand Down Expand Up @@ -187,8 +194,8 @@ impl BlockComponent for ResponsePanel {
Err(e) => e.to_string(),
};

let content = match &self.content {
Some(response) => {
let content = match &self.state {
State::Received(response) => {
let mut lines = vec![];

// First line
Expand Down Expand Up @@ -221,7 +228,18 @@ impl BlockComponent for ResponsePanel {

lines
}
None => vec![Line::styled("<Empty>", Style::default().fg(Color::Yellow))],
State::Empty => vec![Line::styled(
"Empty",
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::ITALIC),
)],
State::Loading => vec![Line::styled(
"Loading...",
Style::default()
.fg(Color::Yellow)
.add_modifier(Modifier::ITALIC),
)],
};

let content_length = content.len();
Expand Down

0 comments on commit 5e8dbad

Please sign in to comment.