Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed Sep 26, 2023
1 parent e84a172 commit 84589c3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/command.rs → src/action.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
48 changes: 24 additions & 24 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -48,15 +48,15 @@ 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);
tui.frame_rate(self.frame_rate);
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() {
Expand All @@ -70,59 +70,59 @@ 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();
}
}
})?;
},
_ => {},
}
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);
Expand Down
12 changes: 6 additions & 6 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ratatui::layout::Rect;
use tokio::sync::mpsc::UnboundedSender;

use crate::{
command::Command,
action::Action,
config::Config,
tui::{Event, Frame},
};
Expand All @@ -13,7 +13,7 @@ pub mod task_report;

pub trait Component {
#[allow(unused_variables)]
fn register_command_handler(&mut self, tx: UnboundedSender<Command>) -> Result<()> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
Ok(())
}
#[allow(unused_variables)]
Expand All @@ -23,7 +23,7 @@ pub trait Component {
fn init(&mut self) -> Result<()> {
Ok(())
}
fn handle_events(&mut self, event: Option<Event>) -> Result<Option<Command>> {
fn handle_events(&mut self, event: Option<Event>) -> Result<Option<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)?,
Expand All @@ -32,15 +32,15 @@ pub trait Component {
Ok(r)
}
#[allow(unused_variables)]
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Command>> {
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Option<Action>> {
Ok(None)
}
#[allow(unused_variables)]
fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result<Option<Command>> {
fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Result<Option<Action>> {
Ok(None)
}
#[allow(unused_variables)]
fn update(&mut self, command: Command) -> Result<Option<Command>> {
fn update(&mut self, command: Action) -> Result<Option<Action>> {
Ok(None)
}
fn draw(&mut self, f: &mut Frame<'_>, rect: Rect) -> Result<()>;
Expand Down
16 changes: 8 additions & 8 deletions src/components/task_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use uuid::Uuid;

use super::{Component, Frame};
use crate::{
command::Command,
action::Action,
config::{Config, KeyBindings},
};

Expand Down Expand Up @@ -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<UnboundedSender<Command>>,
pub command_tx: Option<UnboundedSender<Action>>,
pub last_export: Option<std::time::SystemTime>,
pub report: String,
pub filter: String,
Expand Down Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -480,20 +480,20 @@ 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(())
}
}

impl Component for TaskReport {
fn register_command_handler(&mut self, tx: UnboundedSender<Command>) -> Result<()> {
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
self.command_tx = Some(tx);
Ok(())
}
Expand All @@ -503,9 +503,9 @@ impl Component for TaskReport {
Ok(())
}

fn update(&mut self, command: Command) -> Result<Option<Command>> {
fn update(&mut self, command: Action) -> Result<Option<Action>> {
match command {
Command::Tick => {
Action::Tick => {
self.task_export()?;
self.export_headers()?;
self.generate_rows()?;
Expand Down
8 changes: 4 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -59,14 +59,14 @@ impl Config {
}

#[derive(Clone, Debug, Default, Deref, DerefMut)]
pub struct KeyBindings(pub HashMap<Mode, HashMap<Vec<KeyEvent>, Command>>);
pub struct KeyBindings(pub HashMap<Mode, HashMap<Vec<KeyEvent>, Action>>);

impl<'de> Deserialize<'de> for KeyBindings {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let parsed_map = HashMap::<Mode, HashMap<String, Command>>::deserialize(deserializer)?;
let parsed_map = HashMap::<Mode, HashMap<String, Action>>::deserialize(deserializer)?;

let keybindings = parsed_map
.into_iter()
Expand Down Expand Up @@ -423,7 +423,7 @@ mod tests {
let c = Config::new()?;
assert_eq!(
c.keybindings.get(&Mode::TaskReport).unwrap().get(&parse_key_sequence("<q>").unwrap_or_default()).unwrap(),
&Command::Quit
&Action::Quit
);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 84589c3

Please sign in to comment.