Skip to content

Commit

Permalink
Merge branch 'main' into features/event-system
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealLorenz committed Jan 13, 2024
2 parents d84c9d9 + 0a983ae commit 954d294
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 95 deletions.
97 changes: 2 additions & 95 deletions rq-cli/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use anyhow::anyhow;
use ratatui::{
prelude::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::{Line, Span},
style::{Color, Style},
widgets::{Block, Borders},
};
use rq_core::{
Expand All @@ -17,7 +16,7 @@ use crate::{
components::{
input::InputComponent,
legend::Legend,
menu::{Menu, MenuItem},
menu::Menu,
message_dialog::{Message, MessageDialog},
popup::Popup,
response_panel::ResponsePanel,
Expand All @@ -33,98 +32,6 @@ pub enum FocusState {
ResponsePanel,
}

impl MenuItem for HttpRequest {
fn render(&self) -> Vec<ratatui::text::Line<'_>> {
let mut lines = Vec::new();

let mut first_line_spans = vec![
Span::styled(self.method.to_string(), Style::default().fg(Color::Green)),
Span::raw(" "),
Span::raw(self.url.as_str()),
];
let version_span = Span::raw(format!(" {:?}", self.version));

let mut query = self
.query
.iter()
.enumerate()
.map(|(i, (k, v))| {
Line::from(vec![
Span::raw(" ".repeat(self.method.to_string().len() + 1)),
Span::styled(
if i == 0 { "?" } else { "&" },
Style::default().fg(Color::Blue),
),
Span::raw(k),
Span::raw("="),
Span::raw(v),
])
})
.collect::<Vec<_>>();

if query.is_empty() {
first_line_spans.push(version_span);
lines.push(Line::from(first_line_spans));
} else {
lines.push(Line::from(first_line_spans));
query.last_mut().unwrap().spans.push(version_span);
lines.extend(query);
}

let headers: Vec<Line> = self
.headers()
.iter()
.map(|(k, v)| {
Line::from(vec![
Span::styled(k.to_string(), Style::default().fg(Color::Blue)),
Span::raw(": "),
Span::raw(v.to_str().unwrap().to_string()),
])
})
.collect();
lines.extend(headers);

if !self.body.is_empty() {
lines.push(Line::styled(
"Focus to show body",
Style::default()
.fg(Color::Rgb(246, 133, 116))
.add_modifier(Modifier::ITALIC),
));
}

lines.push(Line::from(""));
lines
}

fn render_highlighted(&self) -> Vec<Line<'_>> {
let mut lines = self.render();

// Underline first line
lines[0].patch_style(
Style::default()
.add_modifier(Modifier::UNDERLINED)
.add_modifier(Modifier::BOLD),
);

// Replace body with expanded version
if !self.body.is_empty() {
lines.pop();
lines.pop();

for line in self.body.lines() {
lines.push(Line::styled(
line,
Style::default().fg(Color::Rgb(246, 133, 116)),
));
}
lines.push(Line::from(""));
}

lines
}
}

pub struct App {
res_rx: Receiver<(Response, usize)>,
req_tx: Sender<(HttpRequest, usize)>,
Expand Down
99 changes: 99 additions & 0 deletions rq-cli/src/components/http_request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use ratatui::{
style::{Color, Modifier, Style},
text::{Line, Span},
};
use rq_core::parser::HttpRequest;

use super::menu::MenuItem;

impl MenuItem for HttpRequest {
fn render(&self) -> Vec<ratatui::text::Line<'_>> {
let mut lines = Vec::new();

let mut first_line_spans = vec![
Span::styled(self.method.to_string(), Style::default().fg(Color::Green)),
Span::raw(" "),
Span::raw(self.url.as_str()),
];
let version_span = Span::raw(format!(" {:?}", self.version));

let mut query = self
.query
.iter()
.enumerate()
.map(|(i, (k, v))| {
Line::from(vec![
Span::raw(" ".repeat(self.method.to_string().len() + 1)),
Span::styled(
if i == 0 { "?" } else { "&" },
Style::default().fg(Color::Blue),
),
Span::raw(k),
Span::raw("="),
Span::raw(v),
])
})
.collect::<Vec<_>>();

if query.is_empty() {
first_line_spans.push(version_span);
lines.push(Line::from(first_line_spans));
} else {
lines.push(Line::from(first_line_spans));
query.last_mut().unwrap().spans.push(version_span);
lines.extend(query);
}

let headers: Vec<Line> = self
.headers()
.iter()
.map(|(k, v)| {
Line::from(vec![
Span::styled(k.to_string(), Style::default().fg(Color::Blue)),
Span::raw(": "),
Span::raw(v.to_str().unwrap().to_string()),
])
})
.collect();
lines.extend(headers);

if !self.body.is_empty() {
lines.push(Line::styled(
"Focus to show body",
Style::default()
.fg(Color::Rgb(246, 133, 116))
.add_modifier(Modifier::ITALIC),
));
}

lines.push(Line::from(""));
lines
}

fn render_highlighted(&self) -> Vec<Line<'_>> {
let mut lines = self.render();

// Underline first line
lines[0].patch_style(
Style::default()
.add_modifier(Modifier::UNDERLINED)
.add_modifier(Modifier::BOLD),
);

// Replace body with expanded version
if !self.body.is_empty() {
lines.pop();
lines.pop();

for line in self.body.lines() {
lines.push(Line::styled(
line,
Style::default().fg(Color::Rgb(246, 133, 116)),
));
}
lines.push(Line::from(""));
}

lines
}
}
1 change: 1 addition & 0 deletions rq-cli/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::terminal::Frame;

use self::popup::Popup;

pub mod http_request;
pub mod input;
pub mod legend;
pub mod menu;
Expand Down

0 comments on commit 954d294

Please sign in to comment.