Skip to content

Commit

Permalink
Move legend to popup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealLorenz committed Mar 11, 2024
1 parent 8356d33 commit 626cece
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 88 deletions.
56 changes: 30 additions & 26 deletions rq-cli/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::VecDeque;
use std::fmt::Write;

use anyhow::anyhow;
use ratatui::{
Expand All @@ -16,8 +17,8 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::{
components::{
legend::Legend, menu::Menu, message_dialog::MessageDialog, popup::Popup,
response_panel::ResponsePanel, variables::panel::VarsPanel, BlockComponent, HandleSuccess,
menu::Menu, message_dialog::MessageDialog, popup::Popup, response_panel::ResponsePanel,
variables::panel::VarsPanel, BlockComponent, HandleSuccess,
},
event::{Event, Message},
};
Expand Down Expand Up @@ -130,57 +131,61 @@ impl App {
self.should_exit = true;
}
KeyCode::Char('v') => Event::emit(Event::Focus(FocusState::VarsPanel)),
KeyCode::Char('?') => Event::emit(Event::Message(Message::Custom(
"keymaps".into(),
self.keymaps() + "\nPress any key to close",
))),
_ => (),
};

Ok(())
}

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]]
fn keymaps(&self) -> String {
let keymaps = match self.focus {
FocusState::RequestsList => Self::KEYMAPS.iter().chain(self.request_menu.keymaps()),
FocusState::ResponsePanel => Self::KEYMAPS.iter().chain(self.responses[0].keymaps()),
FocusState::VarsPanel => Self::KEYMAPS.iter().chain(self.vars_panel.keymaps()),
};

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

[x[0], x[1]]
};
keymaps.fold(String::new(), |mut s, (k, v)| {
let _ = writeln!(s, "{k}: {v}");
s
})
}

let (list_border_style, response_border_style, vars_border_style, legend) = match self.focus
{
pub fn draw(&self, f: &mut crate::terminal::Frame<'_>) {
let (list_border_style, response_border_style, vars_border_style) = match self.focus {
FocusState::RequestsList => (
Style::default().fg(Color::Blue),
Style::default(),
Style::default(),
Legend::new(Self::KEYMAPS.iter().chain(self.request_menu.keymaps())),
),
FocusState::ResponsePanel => (
Style::default(),
Style::default().fg(Color::Blue),
Style::default(),
Legend::new(Self::KEYMAPS.iter().chain(self.responses[0].keymaps())),
),
FocusState::VarsPanel => (
Style::default(),
Style::default(),
Style::default().fg(Color::Blue),
Legend::new(Self::KEYMAPS.iter().chain(self.vars_panel.keymaps())),
),
};

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

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

let list_block = Block::default()
.borders(Borders::ALL)
.title(format!(">> {} <<", self.file_path.as_str()))
.title(format!(" {} ", self.file_path.as_str()))
.border_style(list_border_style);

let response_block = Block::default()
Expand Down Expand Up @@ -209,7 +214,6 @@ 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.render(f, legend_chunk, Block::default());

if let Some(popup) = self.popups.front() {
popup.render(f, f.size(), Block::default().borders(Borders::ALL));
Expand Down
47 changes: 0 additions & 47 deletions rq-cli/src/components/legend.rs

This file was deleted.

11 changes: 9 additions & 2 deletions rq-cli/src/components/message_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ impl MessageDialog {
pub fn new(message: Message) -> Self {
Self { content: message }
}

fn format_title(title: &str) -> String {
format!(" {title} ")
}
}

impl BlockComponent for MessageDialog {
Expand All @@ -34,8 +38,11 @@ impl BlockComponent for MessageDialog {
block: ratatui::widgets::Block,
) {
let (content, title, color) = match &self.content {
Message::Info(content) => (content.as_str(), " info ", Color::Green),
Message::Error(content) => (content.as_str(), " error ", Color::Red),
Message::Info(content) => (content.as_str(), Self::format_title("info"), Color::Green),
Message::Error(content) => (content.as_str(), Self::format_title("error"), Color::Red),
Message::Custom(title, content) => {
(content.as_str(), Self::format_title(title), Color::Green)
}
};

let p = Paragraph::new(content)
Expand Down
1 change: 0 additions & 1 deletion rq-cli/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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
14 changes: 2 additions & 12 deletions rq-cli/src/components/popup.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use ratatui::{
prelude::{Constraint, Direction, Layout, Rect},
widgets::{Block, Clear},
widgets::Clear,
};

use super::{legend::Legend, BlockComponent};
use super::BlockComponent;

pub struct Popup<T: BlockComponent> {
component: T,
Expand Down Expand Up @@ -55,16 +55,6 @@ impl<T: BlockComponent> BlockComponent for Popup<T> {

frame.render_widget(Clear, popup_area);

let [popup_area, legend_area] = {
let x = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(1), Constraint::Length(1)])
.split(popup_area);

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

self.component.render(frame, popup_area, block);
Legend::new(self.component.keymaps().iter()).render(frame, legend_area, Block::default());
}
}
1 change: 1 addition & 0 deletions rq-cli/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Event {
pub enum Message {
Info(String),
Error(String),
Custom(String, String),
}

impl Event {
Expand Down

0 comments on commit 626cece

Please sign in to comment.