diff --git a/src/app/form.rs b/src/app/form.rs index d1052a1..3bfdcfe 100644 --- a/src/app/form.rs +++ b/src/app/form.rs @@ -20,12 +20,14 @@ impl FormField { } } + /// Set the value of the input pub fn value(mut self, value: &str) -> Self { self.input.text = value.to_owned(); self } + /// Set the input to be hidden pub fn hidden(mut self) -> Self { self.hidden = true; @@ -47,7 +49,7 @@ pub struct Form { pub kind: FormKind, - pub selected_field: u16, + pub selected_field: usize, } impl Form { @@ -60,6 +62,7 @@ impl Form { } } + /// Set the title of the form pub fn title(mut self, title: &str) -> Self { self.title = title.to_owned(); @@ -67,9 +70,21 @@ impl Form { } } +impl Form { + /// Returns all fields that are not hidden + pub fn visible_fields(&self) -> Vec { + self.fields + .iter() + .filter(|field| !field.hidden) + .cloned() + .collect() + } +} + impl Navigation for Form { + /// Go to the next field fn next(&mut self) { - if self.fields.len() - 1 == self.selected_field as usize { + if self.visible_fields().len() - 1 == self.selected_field { self.selected_field = 0; return; } @@ -77,9 +92,10 @@ impl Navigation for Form { self.selected_field += 1; } + /// Go to the previous field fn previous(&mut self) { if self.selected_field == 0 { - self.selected_field = self.fields.len() as u16 - 1; + self.selected_field = self.visible_fields().len() - 1; return; } diff --git a/src/app/mod.rs b/src/app/mod.rs index 833c9aa..0dbf2e0 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -52,7 +52,10 @@ pub enum RequestTab { } pub trait Navigation { + /// Go to the next item fn next(&mut self) {} + + /// Go to the previous item fn previous(&mut self) {} } @@ -227,7 +230,7 @@ pub struct App { fn handle_requests(mut req_rx: Receiver, res_tx: Sender>) { tokio::spawn(async move { while let Some(req) = req_rx.recv().await { - let res = request::handle_request(req).await; + let res = request::send(req).await; res_tx.send(Some(res)).await.unwrap(); } @@ -236,10 +239,10 @@ fn handle_requests(mut req_rx: Receiver, res_tx: Sender Self { - let mut headers = HashMap::new(); - - headers.insert("Content-Type".to_string(), "application/json".to_string()); - headers.insert("Accept".to_string(), "application/json".to_string()); + let headers = HashMap::from([ + ("Content-Type".to_string(), "application/json".to_string()), + ("Accept".to_string(), "application/json".to_string()), + ]); let (res_tx, res_rx) = channel(1); let (req_tx, req_rx) = channel(1); diff --git a/src/main.rs b/src/main.rs index 24a9f0a..0439665 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,14 +28,10 @@ async fn main() -> Result<(), Error> { original_hook(panic); })); - let res = run(&mut terminal, &mut app).await; + run(&mut terminal, &mut app).await?; restore_terminal()?; - if let Err(err) = res { - println!("{}", err); - } - Ok(()) } diff --git a/src/request.rs b/src/request.rs index 28851dc..9000e20 100644 --- a/src/request.rs +++ b/src/request.rs @@ -4,7 +4,7 @@ use reqwest::header::{HeaderMap, HeaderName}; use crate::app::{Request, RequestMethod, Response}; -pub async fn handle_request(req: Request) -> Response { +pub async fn send(req: Request) -> Response { let method = match req.method { RequestMethod::Get => reqwest::Method::GET, RequestMethod::Post => reqwest::Method::POST, diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 31b8930..d9ae38b 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -237,27 +237,23 @@ pub fn draw(frame: &mut Frame>, app: &mut App) { .borders(Borders::ALL) .border_style(Style::default().fg(Color::Blue)); - let real_fields = form - .fields - .iter() - .filter(|field| field.hidden != true) - .collect::>(); + let visible_fields = form.visible_fields(); - let height = real_fields.len() * 3 + 4; + let height = visible_fields.len() * 3 + 4; let area = centered_rect(70, height as u16, frame.size()); - let inputs = real_fields.iter().enumerate().map(|(index, field)| { + let inputs = visible_fields.iter().enumerate().map(|(index, field)| { let input = create_input(&field.input, &app, index == form.selected_field as usize) .block( Block::default() .borders(Borders::ALL) .border_style(Style::default().fg( - if index as u16 == form.selected_field + if index == form.selected_field && app.input_mode == InputMode::Insert { Color::Green - } else if index as u16 == form.selected_field { + } else if index == form.selected_field { Color::Blue } else { Color::White