Skip to content

Commit

Permalink
chore: refactor ui
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeya committed Nov 3, 2024
1 parent a35d505 commit 1f0cb83
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 112 deletions.
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::app::RequestMethod;
#[derive(Parser)]
#[command(version, about, long_about=None, author)]
pub struct Cli {
pub endpoint: Option<String>,
pub url: Option<String>,

#[arg(default_value = "get", short = 'X', long, value_parser = clap::value_parser!(RequestMethod))]
pub method: Option<RequestMethod>,
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ async fn main() -> Result<(), Error> {

let mut app = App::default();

app.endpoint.text = cli.endpoint.unwrap_or_default();
if let Some(url) = cli.url {
app.endpoint.text = url;
}

if let Some(method) = cli.method {
app.method = method;
Expand Down
109 changes: 6 additions & 103 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod input;
mod popup;
mod request_tab;
mod response;
mod syntax;

use std::io::Stdout;
Expand All @@ -9,13 +10,13 @@ use popup::render_popup;
use ratatui::{
prelude::{Alignment, Constraint, CrosstermBackend, Direction, Layout},
style::{Color, Style},
text::Span,
widgets::{Block, Borders, Paragraph, Tabs},
widgets::{Block, Borders, Paragraph},
Frame,
};
use request_tab::render_request_tab;
use response::render_response;

use crate::app::{App, AppBlock, InputMode, OrderNavigation, RequestMethod};
use crate::app::{App, AppBlock, InputMode, RequestMethod};

use self::input::create_input;

Expand Down Expand Up @@ -85,114 +86,16 @@ pub fn draw(frame: &mut Frame<CrosstermBackend<Stdout>>, app: &mut App) {
.title("Help"),
);

let request_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(0)])
.split(content_chunks[0]);

let response_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0), Constraint::Length(3)])
.split(content_chunks[1]);

let request_tabs = vec![
Span::styled("Body", Style::default().fg(Color::White)),
Span::styled("Query", Style::default().fg(Color::White)),
Span::styled("Headers", Style::default().fg(Color::White)),
// Span::styled("Auth", Style::default().fg(Color::White)),
// Span::styled("Cookies", Style::default().fg(Color::White)),
];

let tab = Tabs::new(request_tabs)
.block(selectable_block(AppBlock::Request, app))
.divider(Span::raw("|"))
.select(app.request_tab.clone().get_index())
.highlight_style(Style::default().fg(Color::Green));

frame.render_widget(method_p, header_chunks[0]);
frame.render_widget(endpoint_input, header_chunks[1]);

frame.render_widget(tab, request_chunks[0]);
render_request_tab(app, frame, content_chunks[0]);

render_request_tab(app, frame, request_chunks.to_vec());

render_response(app, frame, response_chunks.to_vec());
render_response(app, frame, content_chunks[1]);

frame.render_widget(help_p, main_chunks[2]);

if let Some(_) = app.popup {
render_popup(app, frame);
}
}

fn render_response(
app: &mut App,
frame: &mut Frame<CrosstermBackend<Stdout>>,
response_chunks: Vec<ratatui::prelude::Rect>,
) {
match app.response.as_ref() {
Some(r) => {
let lines_count = u16::try_from(r.text.lines().count()).unwrap_or(1);
let max_x = if lines_count > response_chunks[0].height {
lines_count - (response_chunks[0].height - 2)
} else {
0
};

app.response_scroll.0 = app.response_scroll.0.clamp(0, max_x);

let lines = syntax::highlight_response(r.text.clone(), r.content_type.clone());

let response_p = Paragraph::new(lines)
.block(selectable_block(AppBlock::Response, app).title("Response"))
// .wrap(Wrap { trim: false })
.scroll(app.response_scroll);

let status_code_style = Style::default().fg(match r.status_code {
200..=299 => Color::Green,
300..=399 => Color::Blue,
400..=499 => Color::Yellow,
500..=599 => Color::Red,
_ => Color::White,
});

let status_code_text = if app.is_loading {
"Loading...".to_string()
} else {
r.status_code.to_string()
};

let status_code_p = Paragraph::new(status_code_text)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(status_code_style),
)
.style(status_code_style)
.alignment(Alignment::Center);

frame.render_widget(response_p, response_chunks[0]);
frame.render_widget(status_code_p, response_chunks[1]);
}
None => {
let helper_text = Paragraph::new("Created with love by @fedeya")
.alignment(Alignment::Center)
.block(selectable_block(AppBlock::Response, app).title("Response"));

let status_blank = Paragraph::new(if app.is_loading {
"Loading..."
} else {
"Press <Enter> to send request"
})
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::White)),
)
.alignment(Alignment::Center);

frame.render_widget(helper_text, response_chunks[0]);
frame.render_widget(status_blank, response_chunks[1]);
}
}
}
32 changes: 25 additions & 7 deletions src/ui/request_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,37 @@ use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
prelude::CrosstermBackend,
style::{Color, Style},
widgets::{Paragraph, Row, Table, TableState},
text::Span,
widgets::{Paragraph, Row, Table, TableState, Tabs},
Frame,
};

use crate::app::{App, AppBlock, BodyContentType, BodyType, RequestTab};
use crate::app::{App, AppBlock, BodyContentType, BodyType, OrderNavigation, RequestTab};

use super::{input::create_textarea, selectable_block};

pub fn render_request_tab(
app: &App,
frame: &mut Frame<'_, CrosstermBackend<Stdout>>,
request_chunks: Vec<Rect>,
) {
pub fn render_request_tab(app: &App, frame: &mut Frame<'_, CrosstermBackend<Stdout>>, area: Rect) {
let request_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(0)])
.split(area);

let request_tabs = vec![
Span::styled("Body", Style::default().fg(Color::White)),
Span::styled("Query", Style::default().fg(Color::White)),
Span::styled("Headers", Style::default().fg(Color::White)),
// Span::styled("Auth", Style::default().fg(Color::White)),
// Span::styled("Cookies", Style::default().fg(Color::White)),
];

let tab = Tabs::new(request_tabs)
.block(selectable_block(AppBlock::Request, app))
.divider(Span::raw("|"))
.select(app.request_tab.clone().get_index())
.highlight_style(Style::default().fg(Color::Green));

frame.render_widget(tab, request_chunks[0]);

match app.request_tab {
RequestTab::Body => {
let body_chunks = Layout::default()
Expand Down
86 changes: 86 additions & 0 deletions src/ui/response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::io::Stdout;

use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
prelude::CrosstermBackend,
style::{Color, Style},
widgets::{Block, Borders, Paragraph},
Frame,
};

use crate::app::{App, AppBlock};

use super::{selectable_block, syntax};

pub fn render_response(app: &mut App, frame: &mut Frame<CrosstermBackend<Stdout>>, area: Rect) {
let response_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0), Constraint::Length(3)])
.split(area);

match app.response.as_ref() {
Some(r) => {
let lines_count = u16::try_from(r.text.lines().count()).unwrap_or(1);
let max_x = if lines_count > response_chunks[0].height {
lines_count - (response_chunks[0].height - 2)
} else {
0
};

app.response_scroll.0 = app.response_scroll.0.clamp(0, max_x);

let lines = syntax::highlight_response(r.text.clone(), r.content_type.clone());

let response_p = Paragraph::new(lines)
.block(selectable_block(AppBlock::Response, app).title("Response"))
// .wrap(Wrap { trim: false })
.scroll(app.response_scroll);

let status_code_style = Style::default().fg(match r.status_code {
200..=299 => Color::Green,
300..=399 => Color::Blue,
400..=499 => Color::Yellow,
500..=599 => Color::Red,
_ => Color::White,
});

let status_code_text = if app.is_loading {
"Loading...".to_string()
} else {
r.status_code.to_string()
};

let status_code_p = Paragraph::new(status_code_text)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(status_code_style),
)
.style(status_code_style)
.alignment(Alignment::Center);

frame.render_widget(response_p, response_chunks[0]);
frame.render_widget(status_code_p, response_chunks[1]);
}
None => {
let helper_text = Paragraph::new("Created with love by @fedeya")
.alignment(Alignment::Center)
.block(selectable_block(AppBlock::Response, app).title("Response"));

let status_blank = Paragraph::new(if app.is_loading {
"Loading..."
} else {
"Press <Enter> to send request"
})
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::White)),
)
.alignment(Alignment::Center);

frame.render_widget(helper_text, response_chunks[0]);
frame.render_widget(status_blank, response_chunks[1]);
}
}
}

0 comments on commit 1f0cb83

Please sign in to comment.