Skip to content

Commit

Permalink
Merge branch 'features/legend'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealLorenz committed Nov 27, 2023
2 parents 959998d + b0db4d9 commit 6b963ca
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 6 deletions.
37 changes: 31 additions & 6 deletions rq-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};

use crate::components::{
menu::{Menu, MenuItem},
legend::Legend,
menu::{self, Menu, MenuItem},
message_dialog::{Message, MessageDialog},
popup::Popup,
response_panel::ResponsePanel,
response_panel::{self, ResponsePanel},
BlockComponent, HandleSuccess,
};

const KEYMAPS: &[(&str, &str); 1] = &[("Esc/q", "exit")];

#[derive(Default)]
enum FocusState {
#[default]
Expand Down Expand Up @@ -199,19 +202,36 @@ impl App {
}

pub fn draw(&self, f: &mut crate::terminal::Frame<'_>) {
let [main_chunk, legend_chunk] = {
let x = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(1), Constraint::Length(1)])
.split(f.size());

[x[0], x[1]]
};

// Create two chunks with equal screen space
let [list_chunk, response_chunk] = {
let x = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(f.size());
.split(main_chunk);

[x[0], x[1]]
};

let (list_border_style, response_border_style) = match self.focus {
FocusState::RequestsList => (Style::default().fg(Color::Blue), Style::default()),
FocusState::ResponseBuffer => (Style::default(), Style::default().fg(Color::Blue)),
let (list_border_style, response_border_style, focused_keymaps) = match self.focus {
FocusState::RequestsList => (
Style::default().fg(Color::Blue),
Style::default(),
menu::KEYMAPS.iter(),
),
FocusState::ResponseBuffer => (
Style::default(),
Style::default().fg(Color::Blue),
response_panel::KEYMAPS.iter(),
),
};

let list_block = Block::default()
Expand All @@ -226,6 +246,11 @@ impl App {
self.request_menu.render(f, list_chunk, list_block);
let response_panel = &self.responses[self.request_menu.idx()];
response_panel.render(f, response_chunk, response_block);
Legend::new(KEYMAPS.iter().chain(focused_keymaps)).render(
f,
legend_chunk,
Block::default(),
);

if let Some(popup) = self.message_popup.as_ref() {
popup.render(f, f.size(), Block::default().borders(Borders::ALL));
Expand Down
46 changes: 46 additions & 0 deletions rq-cli/src/components/legend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use ratatui::{
style::{Modifier, Style},
text::{Line, Span},
widgets::Paragraph,
};

use super::BlockComponent;

pub struct Legend<'a> {
keymaps: Vec<&'a (&'a str, &'a str)>,
}

impl<'a> Legend<'a> {
pub fn new<I: Iterator<Item = &'a (&'a str, &'a str)>>(keymaps: I) -> Self {
Self {
keymaps: keymaps.collect(),
}
}
}

impl BlockComponent for Legend<'_> {
fn render(
&self,
frame: &mut crate::terminal::Frame,
area: ratatui::prelude::Rect,
block: ratatui::widgets::Block,
) {
let spans = self
.keymaps
.iter()
.flat_map(|(k, v)| {
[
Span::styled(
format!(" {k} "),
Style::default().add_modifier(Modifier::REVERSED),
),
Span::from(format!(" {v} ")),
]
})
.collect::<Vec<_>>();

let p = Paragraph::new(Line::from(spans));

frame.render_widget(p.block(block), area);
}
}
2 changes: 2 additions & 0 deletions rq-cli/src/components/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use ratatui::{

use super::BlockComponent;

pub const KEYMAPS: &[(&str, &str); 2] = &[("↓/↑ j/k", "next/previous"), ("Enter", "select")];

pub trait MenuItem {
fn render(&self) -> Vec<Line<'_>>;
fn render_highlighted(&self) -> Vec<Line<'_>> {
Expand Down
1 change: 1 addition & 0 deletions rq-cli/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ratatui::{prelude::Rect, widgets::Block};
use crate::terminal::Frame;

pub mod input;
pub mod legend;
pub mod menu;
pub mod message_dialog;
pub mod popup;
Expand Down
6 changes: 6 additions & 0 deletions rq-cli/src/components/response_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ use super::{
BlockComponent, HandleResult, HandleSuccess,
};

pub const KEYMAPS: &[(&str, &str); 3] = &[
("↓/↑ j/k", "scroll down/up"),
("Enter", "send request"),
("s", "save"),
];

#[derive(Copy, Clone, Default)]
enum SaveOption {
#[default]
Expand Down

0 comments on commit 6b963ca

Please sign in to comment.