Skip to content

Commit

Permalink
Refactor with InputBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealLorenz committed Jan 16, 2024
1 parent 70f0c22 commit 6ff49bb
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 53 deletions.
26 changes: 2 additions & 24 deletions rq-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,30 +267,8 @@ impl App {
self.responses[self.request_menu.idx()].save_body(&file_name)
}
},
Event::NewInput((content, typ)) => {
match typ {
crate::event::InputType::FileName(save_option) => {
self.input_popup = Some(
InputComponent::from(content.as_str())
.with_cursor(0)
.with_confirm_callback(move |value| {
Event::emit(Event::InputConfirm);
Event::emit(Event::Save((value, save_option)));
})
.popup(),
);
}
crate::event::InputType::VarValue(name) => {
self.input_popup = Some(
InputComponent::from(content.as_str())
.with_confirm_callback(move |value| {
Event::emit(Event::InputConfirm);
Event::emit(Event::UpdateVar((name.clone(), value)));
})
.popup(),
);
}
};
Event::NewInput(builder) => {
self.input_popup = Some(builder.build().popup());
Ok(())
}
Event::InputCancel => {
Expand Down
10 changes: 6 additions & 4 deletions rq-cli/src/components/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::event::Event;

use super::BlockComponent;

pub mod builder;

type ConfirmCallback = Box<dyn Fn(String)>;
type CancelCallback = Box<dyn Fn()>;

Expand All @@ -26,21 +28,21 @@ impl Default for InputComponent {
}

impl InputComponent {
pub fn from(value: &str) -> Self {
fn from(value: &str) -> Self {
Self {
input: Input::from(value),
..Self::default()
}
}

pub fn with_cursor(self, cursor: usize) -> Self {
fn with_cursor(self, cursor: usize) -> Self {
Self {
input: self.input.with_cursor(cursor),
..self
}
}

pub fn with_confirm_callback<F>(self, confirm_callback: F) -> Self
fn with_confirm_callback<F>(self, confirm_callback: F) -> Self
where
F: Fn(String) + 'static,
{
Expand All @@ -50,7 +52,7 @@ impl InputComponent {
}
}

pub fn with_cancel_callback<F>(self, cancel_callback: F) -> Self
fn with_cancel_callback<F>(self, cancel_callback: F) -> Self
where
F: Fn() + 'static,
{
Expand Down
59 changes: 59 additions & 0 deletions rq-cli/src/components/input/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::{components::response_panel::SaveOption, event::Event};

use super::InputComponent;

pub struct InputBuilder {
content: String,
cursor: Option<usize>,
typ: InputType,
}

pub enum InputType {
FileName(SaveOption),
VarValue(String),
}

impl InputBuilder {
pub fn new(typ: InputType) -> Self {
Self {
content: String::new(),
cursor: None,
typ,
}
}

pub fn with_content(self, content: String) -> Self {
Self { content, ..self }
}

pub fn with_cursor(self, cursor: usize) -> Self {
Self {
cursor: Some(cursor),
..self
}
}

fn build_component(&self) -> InputComponent {
let input = InputComponent::from(&self.content);

match self.cursor {
Some(i) => input.with_cursor(i),
None => input,
}
}

pub fn build(self) -> InputComponent {
let input = self.build_component();

match self.typ {
InputType::FileName(save_option) => input.with_confirm_callback(move |value| {
Event::emit(Event::InputConfirm);
Event::emit(Event::Save((value, save_option)));
}),
InputType::VarValue(name) => input.with_confirm_callback(move |value| {
Event::emit(Event::InputConfirm);
Event::emit(Event::UpdateVar((name.clone(), value)));
}),
}
}
}
24 changes: 12 additions & 12 deletions rq-cli/src/components/response_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ use ratatui::{
use rq_core::request::{mime::Payload, Response, StatusCode};
use std::{fmt::Write, iter};

use crate::{
app::FocusState,
event::{Event, InputType},
};
use crate::{app::FocusState, event::Event};

use super::{
input::builder::{InputBuilder, InputType},
message_dialog::{Message, MessageDialog},
BlockComponent, HandleResult, HandleSuccess,
};
Expand Down Expand Up @@ -176,17 +174,19 @@ impl BlockComponent for ResponsePanel {
KeyCode::Down | KeyCode::Char('j') => self.scroll_down(),
KeyCode::Up | KeyCode::Char('k') => self.scroll_up(),
KeyCode::Char('s') => {
Event::emit(Event::NewInput((
self.extension().unwrap_or_default(),
InputType::FileName(SaveOption::Body),
)));
Event::emit(Event::NewInput(
InputBuilder::new(InputType::FileName(SaveOption::Body))
.with_content(self.extension().unwrap_or_default())
.with_cursor(0),
));
}

KeyCode::Char('S') => {
Event::emit(Event::NewInput((
self.extension().unwrap_or_default(),
InputType::FileName(SaveOption::All),
)));
Event::emit(Event::NewInput(
InputBuilder::new(InputType::FileName(SaveOption::All))
.with_content(self.extension().unwrap_or_default())
.with_cursor(0),
));
}
KeyCode::Char('t') => {
self.show_raw = !self.show_raw;
Expand Down
16 changes: 10 additions & 6 deletions rq-cli/src/components/variables/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ use crossterm::event::KeyCode;
use rq_core::parser::variables::TemplateString;

use crate::{
components::{menu::Menu, BlockComponent, HandleSuccess},
event::{Event, InputType},
components::{
input::builder::{InputBuilder, InputType},
menu::Menu,
BlockComponent, HandleSuccess,
},
event::Event,
};

pub struct VarsPanel {
Expand All @@ -17,10 +21,10 @@ impl VarsPanel {
pub fn new(vars: HashMap<String, TemplateString>) -> Self {
let menu = Menu::new(vars.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.with_confirm_callback(|(name, value)| {
Event::emit(Event::NewInput((
value.to_string(),
InputType::VarValue(name.clone()),
)));
Event::emit(Event::NewInput(
InputBuilder::new(InputType::VarValue(name.clone()))
.with_content(value.to_string()),
))
});

Self { vars, menu }
Expand Down
18 changes: 11 additions & 7 deletions rq-cli/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ use std::{collections::VecDeque, sync::Mutex};

use once_cell::sync::Lazy;

use crate::{app::FocusState, components::response_panel::SaveOption};
use crate::{
app::FocusState,
components::{input::builder::InputBuilder, response_panel::SaveOption},
};

static EVENT_QUEUE: Lazy<Mutex<VecDeque<Event>>> = Lazy::new(|| Mutex::new(VecDeque::new()));

pub enum Event {
Focus(FocusState),
Save((String, SaveOption)),
NewInput((String, InputType)),
NewInput(InputBuilder),

InputConfirm,
InputCancel,

// Request index in menu
SendRequest(usize),

// Name, value
UpdateVar((String, String)),

Key(crossterm::event::KeyEvent),
Other(crossterm::event::Event),
}

pub enum InputType {
FileName(SaveOption),
VarValue(String),
}

impl Event {
pub fn emit(event: Event) {
EVENT_QUEUE.lock().unwrap().push_front(event);
Expand Down

0 comments on commit 6ff49bb

Please sign in to comment.