Skip to content

Commit

Permalink
refactor: 👔 added Page and Pane traits, removed Component trait
Browse files Browse the repository at this point in the history
  • Loading branch information
zaghaghi committed Mar 4, 2024
1 parent cf2eb57 commit e117d0b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 148 deletions.
25 changes: 15 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ use ratatui::prelude::Rect;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;

use crate::{action::Action, component::Component, components::home::Home, config::Config, tui};
use crate::{
action::Action,
components::{home::Home, Page},
config::Config,
tui,
};

#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Mode {
Expand All @@ -14,7 +19,7 @@ pub enum Mode {

pub struct App {
pub config: Config,
pub components: Vec<Box<dyn Component>>,
pub pages: Vec<Box<dyn Page>>,
pub should_quit: bool,
pub should_suspend: bool,
pub mode: Mode,
Expand All @@ -27,7 +32,7 @@ impl App {
let config = Config::new()?;
let mode = Mode::Home;
Ok(Self {
components: vec![Box::new(home)],
pages: vec![Box::new(home)],
should_quit: false,
should_suspend: false,
config,
Expand All @@ -42,22 +47,22 @@ impl App {
let mut tui = tui::Tui::new()?;
tui.enter()?;

for component in self.components.iter_mut() {
for component in self.pages.iter_mut() {
component.register_action_handler(action_tx.clone())?;
}

for component in self.components.iter_mut() {
for component in self.pages.iter_mut() {
component.register_config_handler(self.config.clone())?;
}

for component in self.components.iter_mut() {
for component in self.pages.iter_mut() {
component.init()?;
}

loop {
if let Some(e) = tui.next().await {
let mut stop_event_propagation = false;
for component in self.components.iter_mut() {
for component in self.pages.iter_mut() {
if let Some(response) = component.handle_events(Some(e.clone()))? {
match response {
tui::EventResponse::Continue(action) => {
Expand Down Expand Up @@ -114,7 +119,7 @@ impl App {
Action::Resize(w, h) => {
tui.resize(Rect::new(0, 0, w, h))?;
tui.draw(|f| {
for component in self.components.iter_mut() {
for component in self.pages.iter_mut() {
let r = component.draw(f, f.size());
if let Err(e) = r {
action_tx.send(Action::Error(format!("Failed to draw: {:?}", e))).unwrap();
Expand All @@ -124,7 +129,7 @@ impl App {
},
Action::Render => {
tui.draw(|f| {
for component in self.components.iter_mut() {
for component in self.pages.iter_mut() {
let r = component.draw(f, f.size());
if let Err(e) = r {
action_tx.send(Action::Error(format!("Failed to draw: {:?}", e))).unwrap();
Expand All @@ -134,7 +139,7 @@ impl App {
},
_ => {},
}
for component in self.components.iter_mut() {
for component in self.pages.iter_mut() {
if let Some(action) = component.update(action.clone())? {
action_tx.send(action)?
};
Expand Down
121 changes: 0 additions & 121 deletions src/component.rs

This file was deleted.

66 changes: 66 additions & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
use color_eyre::eyre::Result;
use crossterm::event::{KeyEvent, MouseEvent};
use ratatui::layout::Rect;
use tokio::sync::mpsc::UnboundedSender;

use crate::{
action::Action,
config::Config,
tui::{Event, EventResponse, Frame},
};

pub mod home;

pub trait Page {
#[allow(unused_variables)]
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
Ok(())
}

#[allow(unused_variables)]
fn register_config_handler(&mut self, config: Config) -> Result<()> {
Ok(())
}

fn init(&mut self) -> Result<()> {
Ok(())
}

fn handle_events(&mut self, event: Option<Event>) -> Result<Option<EventResponse<Action>>> {
let r = match event {
Some(Event::Key(key_event)) => self.handle_key_events(key_event)?,
Some(Event::Mouse(mouse_event)) => self.handle_mouse_events(mouse_event)?,
_ => None,
};
Ok(r)
}

#[allow(unused_variables)]
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<EventResponse<Action>>> {
Ok(None)
}

#[allow(unused_variables)]
fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result<Option<EventResponse<Action>>> {
Ok(None)
}

#[allow(unused_variables)]
fn update(&mut self, action: Action) -> Result<Option<Action>> {
Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()>;
}

pub trait Pane {
fn init(&mut self) -> Result<()> {
Ok(())
}

#[allow(unused_variables)]
fn update(&mut self, action: Action) -> Result<Option<Action>> {
Ok(None)
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()>;
}
24 changes: 8 additions & 16 deletions src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ratatui::{
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc::UnboundedSender;

use crate::{action::Action, component::Component, config::Config, tui::EventResponse};
use crate::{action::Action, components::Page, config::Config, tui::EventResponse};

#[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum Pane {
Expand Down Expand Up @@ -135,25 +135,17 @@ impl Home {
}

fn method_color(method: &str) -> Color {
// method.0 is private, so matching
if method == "GET" {
return Color::LightCyan;
match method {
"GET" => Color::LightCyan,
"POST" => Color::LightBlue,
"PUT" => Color::LightYellow,
"DELETE" => Color::LightRed,
_ => Color::Gray,
}
if method == "POST" {
return Color::LightBlue;
}
if method == "PUT" {
return Color::LightYellow;
}
if method == "DELETE" {
return Color::LightRed;
}

Color::Gray
}
}

impl Component for Home {
impl Page for Home {
fn init(&mut self) -> Result<()> {
self.openapi_spec = Some(oas3::from_path(self.openapi_path.clone())?);
self.openapi_spec_operations_len = match &self.openapi_spec {
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod action;
pub mod app;
pub mod cli;
pub mod component;
pub mod components;
pub mod config;
pub mod tui;
Expand Down

0 comments on commit e117d0b

Please sign in to comment.