From 84589c3ab3efd7dc9b6cd81dc2572dca6377e741 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Tue, 26 Sep 2023 04:44:21 -0400 Subject: [PATCH] WIP --- src/{command.rs => action.rs} | 2 +- src/app.rs | 48 +++++++++++++++++------------------ src/components.rs | 12 ++++----- src/components/task_report.rs | 16 ++++++------ src/config.rs | 8 +++--- src/main.rs | 2 +- 6 files changed, 44 insertions(+), 44 deletions(-) rename src/{command.rs => action.rs} (97%) diff --git a/src/command.rs b/src/action.rs similarity index 97% rename from src/command.rs rename to src/action.rs index a87a1f0e..d4b2b7ca 100644 --- a/src/command.rs +++ b/src/action.rs @@ -1,7 +1,7 @@ use serde_derive::{Deserialize, Serialize}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum Command { +pub enum Action { Tick, Render, Resize(u16, u16), diff --git a/src/app.rs b/src/app.rs index 54f0cc9c..edcd45d1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,7 +4,7 @@ use serde_derive::{Deserialize, Serialize}; use tokio::sync::mpsc; use crate::{ - command::Command, + action::Action, components::{task_report::TaskReport, Component}, config::Config, tui, @@ -48,7 +48,7 @@ impl App { } pub async fn run(&mut self) -> Result<()> { - let (command_tx, mut command_rx) = mpsc::unbounded_channel(); + let (action_tx, mut action_rx) = mpsc::unbounded_channel(); let mut tui = tui::Tui::new()?; tui.tick_rate(self.tick_rate); @@ -56,7 +56,7 @@ impl App { tui.enter()?; for component in self.components.iter_mut() { - component.register_command_handler(command_tx.clone())?; + component.register_action_handler(action_tx.clone())?; } for component in self.components.iter_mut() { @@ -70,44 +70,44 @@ impl App { loop { if let Some(e) = tui.next().await { match e { - tui::Event::Quit => command_tx.send(Command::Quit)?, - tui::Event::Tick => command_tx.send(Command::Tick)?, - tui::Event::Render => command_tx.send(Command::Render)?, - tui::Event::Resize(x, y) => command_tx.send(Command::Resize(x, y))?, + tui::Event::Quit => action_tx.send(Action::Quit)?, + tui::Event::Tick => action_tx.send(Action::Tick)?, + tui::Event::Render => action_tx.send(Action::Render)?, + tui::Event::Resize(x, y) => action_tx.send(Action::Resize(x, y))?, tui::Event::Key(key) => { self.last_tick_key_events.push(key); if let Some(keymap) = self.config.keybindings.get(&self.mode) { - if let Some(command) = keymap.get(&self.last_tick_key_events) { - command_tx.send(command.clone())?; + if let Some(action) = keymap.get(&self.last_tick_key_events) { + action_tx.send(action.clone())?; }; }; }, _ => {}, } for component in self.components.iter_mut() { - if let Some(command) = component.handle_events(Some(e.clone()))? { - command_tx.send(command)?; + if let Some(action) = component.handle_events(Some(e.clone()))? { + action_tx.send(action)?; } } } - while let Ok(command) = command_rx.try_recv() { - if command != Command::Tick && command != Command::Render { - log::debug!("{command:?}"); + while let Ok(action) = action_rx.try_recv() { + if action != Action::Tick && action != Action::Render { + log::debug!("{action:?}"); } - match command { - Command::Tick => { + match action { + Action::Tick => { self.last_tick_key_events.drain(..); }, - Command::Quit => self.should_quit = true, - Command::Suspend => self.should_suspend = true, - Command::Resume => self.should_suspend = false, - Command::Render => { + Action::Quit => self.should_quit = true, + Action::Suspend => self.should_suspend = true, + Action::Resume => self.should_suspend = false, + Action::Render => { tui.draw(|f| { for component in self.components.iter_mut() { let r = component.draw(f, f.size()); if let Err(e) = r { - command_tx.send(Command::Error(format!("Failed to draw: {:?}", e))).unwrap(); + action_tx.send(Action::Error(format!("Failed to draw: {:?}", e))).unwrap(); } } })?; @@ -115,14 +115,14 @@ impl App { _ => {}, } for component in self.components.iter_mut() { - if let Some(command) = component.update(command.clone())? { - command_tx.send(command)? + if let Some(action) = component.update(action.clone())? { + action_tx.send(action)? }; } } if self.should_suspend { tui.suspend()?; - command_tx.send(Command::Resume)?; + action_tx.send(Action::Resume)?; tui = tui::Tui::new()?; tui.tick_rate(self.tick_rate); tui.frame_rate(self.frame_rate); diff --git a/src/components.rs b/src/components.rs index 8eb7459b..a7d15fb6 100644 --- a/src/components.rs +++ b/src/components.rs @@ -4,7 +4,7 @@ use ratatui::layout::Rect; use tokio::sync::mpsc::UnboundedSender; use crate::{ - command::Command, + action::Action, config::Config, tui::{Event, Frame}, }; @@ -13,7 +13,7 @@ pub mod task_report; pub trait Component { #[allow(unused_variables)] - fn register_command_handler(&mut self, tx: UnboundedSender) -> Result<()> { + fn register_action_handler(&mut self, tx: UnboundedSender) -> Result<()> { Ok(()) } #[allow(unused_variables)] @@ -23,7 +23,7 @@ pub trait Component { fn init(&mut self) -> Result<()> { Ok(()) } - fn handle_events(&mut self, event: Option) -> Result> { + fn handle_events(&mut self, event: Option) -> Result> { 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)?, @@ -32,15 +32,15 @@ pub trait Component { Ok(r) } #[allow(unused_variables)] - fn handle_key_events(&mut self, key: KeyEvent) -> Result> { + fn handle_key_events(&mut self, key: KeyEvent) -> Result> { Ok(None) } #[allow(unused_variables)] - fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result> { + fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result> { Ok(None) } #[allow(unused_variables)] - fn update(&mut self, command: Command) -> Result> { + fn update(&mut self, command: Action) -> Result> { Ok(None) } fn draw(&mut self, f: &mut Frame<'_>, rect: Rect) -> Result<()>; diff --git a/src/components/task_report.rs b/src/components/task_report.rs index fda49343..4941e7c6 100644 --- a/src/components/task_report.rs +++ b/src/components/task_report.rs @@ -14,7 +14,7 @@ use uuid::Uuid; use super::{Component, Frame}; use crate::{ - command::Command, + action::Action, config::{Config, KeyBindings}, }; @@ -90,7 +90,7 @@ pub fn vague_format_date_time(from_dt: NaiveDateTime, to_dt: NaiveDateTime, with #[derive(Default)] pub struct TaskReport { pub config: Config, - pub command_tx: Option>, + pub command_tx: Option>, pub last_export: Option, pub report: String, pub filter: String, @@ -121,7 +121,7 @@ impl TaskReport { Ok(()) } - pub fn send_command(&self, command: Command) -> Result<()> { + pub fn send_action(&self, command: Action) -> Result<()> { if let Some(ref tx) = self.command_tx { tx.send(command)?; } @@ -480,12 +480,12 @@ impl TaskReport { if imported.is_ok() { self.tasks = imported?; log::info!("Imported {} tasks", self.tasks.len()); - self.send_command(Command::ShowTaskReport)?; + self.send_action(Action::ShowTaskReport)?; } else { imported?; } } else { - self.send_command(Command::Error(format!("Unable to parse output of `{:?}`:\n`{:?}`", task, data)))?; + self.send_action(Action::Error(format!("Unable to parse output of `{:?}`:\n`{:?}`", task, data)))?; } Ok(()) @@ -493,7 +493,7 @@ impl TaskReport { } impl Component for TaskReport { - fn register_command_handler(&mut self, tx: UnboundedSender) -> Result<()> { + fn register_action_handler(&mut self, tx: UnboundedSender) -> Result<()> { self.command_tx = Some(tx); Ok(()) } @@ -503,9 +503,9 @@ impl Component for TaskReport { Ok(()) } - fn update(&mut self, command: Command) -> Result> { + fn update(&mut self, command: Action) -> Result> { match command { - Command::Tick => { + Action::Tick => { self.task_export()?; self.export_headers()?; self.generate_rows()?; diff --git a/src/config.rs b/src/config.rs index 17d9590f..e2115bf0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,7 +7,7 @@ use ratatui::style::{Color, Modifier, Style}; use serde::de::{self, Deserialize, Deserializer, MapAccess, Visitor}; use serde_derive::Deserialize; -use crate::{app::Mode, command::Command}; +use crate::{action::Action, app::Mode}; const CONFIG: &str = include_str!("../.config/config.json5"); @@ -59,14 +59,14 @@ impl Config { } #[derive(Clone, Debug, Default, Deref, DerefMut)] -pub struct KeyBindings(pub HashMap, Command>>); +pub struct KeyBindings(pub HashMap, Action>>); impl<'de> Deserialize<'de> for KeyBindings { fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { - let parsed_map = HashMap::>::deserialize(deserializer)?; + let parsed_map = HashMap::>::deserialize(deserializer)?; let keybindings = parsed_map .into_iter() @@ -423,7 +423,7 @@ mod tests { let c = Config::new()?; assert_eq!( c.keybindings.get(&Mode::TaskReport).unwrap().get(&parse_key_sequence("").unwrap_or_default()).unwrap(), - &Command::Quit + &Action::Quit ); Ok(()) } diff --git a/src/main.rs b/src/main.rs index b3cac1cc..e91a0cdf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,9 @@ #![allow(unused_imports)] #![allow(unused_variables)] +pub mod action; pub mod app; pub mod cli; -pub mod command; pub mod components; pub mod config; pub mod tui;