Skip to content

Commit

Permalink
Remove event module, tick, etc, counter behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Sep 4, 2024
1 parent a808d3b commit 6a76940
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 348 deletions.
78 changes: 26 additions & 52 deletions simple-generated/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
use std::time::Duration;

use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::{
layout::Alignment,
style::{Style, Stylize},
widgets::{Block, BorderType, Paragraph},
style::Stylize,
text::Line,
widgets::{Block, Paragraph},
DefaultTerminal, Frame,
};

use crate::event::{Event, EventSource};

#[derive(Debug, Default)]
pub struct App {
/// Is the application running?
pub running: bool,
/// counter
pub counter: u8,
running: bool,
}

impl App {
Expand All @@ -26,76 +20,56 @@ impl App {
}

pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
const TICKS_PER_SECOND: f64 = 10.0;
let tick_interval = Duration::from_secs_f64(1.0 / TICKS_PER_SECOND);
let events = EventSource::new(tick_interval);
self.running = true;
while self.running {
terminal.draw(|frame| self.draw(frame))?;
match events.next()? {
Event::Tick => self.on_tick(),
Event::Key(key_event) => self.on_key_event(key_event)?,
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
Event::Error(err) => {
return Err(err.into());
}
}
self.handle_crossterm_events()?;
}
Ok(())
}

/// Handles the tick event of the terminal.
pub fn on_tick(&self) {}
fn handle_crossterm_events(&mut self) -> Result<()> {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key),
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
_ => {}
}
Ok(())
}

/// Handles the key events and updates the state of [`App`].
pub fn on_key_event(&mut self, key: KeyEvent) -> Result<()> {
fn on_key_event(&mut self, key: KeyEvent) {
match (key.modifiers, key.code) {
(_, KeyCode::Esc | KeyCode::Char('q'))
| (KeyModifiers::CONTROL, KeyCode::Char('c') | KeyCode::Char('C')) => self.quit(),
(_, KeyCode::Right) => self.increment_counter(),
(_, KeyCode::Left) => self.decrement_counter(),
// Add other key handlers here.
_ => {}
}
Ok(())
}

/// Set running to false to quit the application.
pub fn quit(&mut self) {
fn quit(&mut self) {
self.running = false;
}

pub fn increment_counter(&mut self) {
self.counter = self.counter.saturating_add(1);
}

pub fn decrement_counter(&mut self) {
self.counter = self.counter.saturating_sub(1);
}

/// Renders the user interface widgets.
pub(crate) fn draw(&mut self, frame: &mut Frame) {
fn draw(&mut self, frame: &mut Frame) {
// This is where you add new widgets.
// See the following resources:
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
// - https://github.com/ratatui/ratatui/tree/master/examples

let text = format!(
"This is a ratatui simple template.\n\
Press `Esc`, `Ctrl-C` or `q` to stop running.\n\
Press left and right to increment and decrement the counter respectively.\n\
Counter: {}",
self.counter
);
let block = Block::bordered()
.border_type(BorderType::Rounded)
.title("Ratatui Simple Template")
.title_alignment(Alignment::Center);
let title = Line::from("Ratatui Simple Template")
.bold()
.blue()
.centered();
let text = "Hello, Ratatui!\n\n\
Created using https://github.com/ratatui/templates\n\
Press `Esc`, `Ctrl-C` or `q` to stop running.";
frame.render_widget(
Paragraph::new(text)
.block(block)
.style(Style::new().cyan().on_black())
.block(Block::bordered().title(title))
.centered(),
frame.area(),
)
Expand Down
121 changes: 0 additions & 121 deletions simple-generated/src/event.rs

This file was deleted.

1 change: 0 additions & 1 deletion simple-generated/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub use app::App;

pub mod app;
pub mod event;

fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
Expand Down
78 changes: 26 additions & 52 deletions simple/template/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
use std::time::Duration;

use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::{
layout::Alignment,
style::{Style, Stylize},
widgets::{Block, BorderType, Paragraph},
style::Stylize,
text::Line,
widgets::{Block, Paragraph},
DefaultTerminal, Frame,
};

use crate::event::{Event, EventSource};

#[derive(Debug, Default)]
pub struct App {
/// Is the application running?
pub running: bool,
/// counter
pub counter: u8,
running: bool,
}

impl App {
Expand All @@ -26,76 +20,56 @@ impl App {
}

pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
const TICKS_PER_SECOND: f64 = 10.0;
let tick_interval = Duration::from_secs_f64(1.0 / TICKS_PER_SECOND);
let events = EventSource::new(tick_interval);
self.running = true;
while self.running {
terminal.draw(|frame| self.draw(frame))?;
match events.next()? {
Event::Tick => self.on_tick(),
Event::Key(key_event) => self.on_key_event(key_event)?,
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
Event::Error(err) => {
return Err(err.into());
}
}
self.handle_crossterm_events()?;
}
Ok(())
}

/// Handles the tick event of the terminal.
pub fn on_tick(&self) {}
fn handle_crossterm_events(&mut self) -> Result<()> {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key),
Event::Mouse(_) => {}
Event::Resize(_, _) => {}
_ => {}
}
Ok(())
}

/// Handles the key events and updates the state of [`App`].
pub fn on_key_event(&mut self, key: KeyEvent) -> Result<()> {
fn on_key_event(&mut self, key: KeyEvent) {
match (key.modifiers, key.code) {
(_, KeyCode::Esc | KeyCode::Char('q'))
| (KeyModifiers::CONTROL, KeyCode::Char('c') | KeyCode::Char('C')) => self.quit(),
(_, KeyCode::Right) => self.increment_counter(),
(_, KeyCode::Left) => self.decrement_counter(),
// Add other key handlers here.
_ => {}
}
Ok(())
}

/// Set running to false to quit the application.
pub fn quit(&mut self) {
fn quit(&mut self) {
self.running = false;
}

pub fn increment_counter(&mut self) {
self.counter = self.counter.saturating_add(1);
}

pub fn decrement_counter(&mut self) {
self.counter = self.counter.saturating_sub(1);
}

/// Renders the user interface widgets.
pub(crate) fn draw(&mut self, frame: &mut Frame) {
fn draw(&mut self, frame: &mut Frame) {
// This is where you add new widgets.
// See the following resources:
// - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
// - https://github.com/ratatui/ratatui/tree/master/examples

let text = format!(
"This is a ratatui simple template.\n\
Press `Esc`, `Ctrl-C` or `q` to stop running.\n\
Press left and right to increment and decrement the counter respectively.\n\
Counter: {}",
self.counter
);
let block = Block::bordered()
.border_type(BorderType::Rounded)
.title("Ratatui Simple Template")
.title_alignment(Alignment::Center);
let title = Line::from("Ratatui Simple Template")
.bold()
.blue()
.centered();
let text = "Hello, Ratatui!\n\n\
Created using https://github.com/ratatui/templates\n\
Press `Esc`, `Ctrl-C` or `q` to stop running.";
frame.render_widget(
Paragraph::new(text)
.block(block)
.style(Style::new().cyan().on_black())
.block(Block::bordered().title(title))
.centered(),
frame.area(),
)
Expand Down
Loading

0 comments on commit 6a76940

Please sign in to comment.