Skip to content

Commit

Permalink
Move App back to app module
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Aug 27, 2024
1 parent 5e8984d commit ddb5fe3
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 204 deletions.
102 changes: 102 additions & 0 deletions simple-generated/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::time::Duration;

use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
layout::Alignment,
style::{Style, Stylize},
widgets::{Block, BorderType, 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,
}

impl App {
/// Construct a new instance of [`App`].
pub fn new() -> Self {
Self::default()
}

pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
let tick_interval = Duration::from_secs_f64(1.0 / 10.0); // 10 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());
}
}
}
Ok(())
}

/// Handles the tick event of the terminal.
pub fn on_tick(&self) {}

/// Handles the key events and updates the state of [`App`].
pub fn on_key_event(&mut self, key: KeyEvent) -> Result<()> {
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) {
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) {
// 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);
frame.render_widget(
Paragraph::new(text)
.block(block)
.style(Style::new().cyan().on_black())
.centered(),
frame.area(),
)
}
}
105 changes: 3 additions & 102 deletions simple-generated/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,111 +1,12 @@
use std::time::Duration;

use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use event::{Event, EventSource};
use ratatui::{
layout::Alignment,
style::{Style, Stylize},
widgets::{Block, BorderType, Paragraph},
DefaultTerminal, Frame,
};
pub use app::App;

pub mod app;
pub mod event;

fn main() -> Result<()> {
fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let terminal = ratatui::init();
let result = App::new().run(terminal);
ratatui::restore();
result
}

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

impl App {
/// Construct a new instance of [`App`].
pub fn new() -> Self {
Self::default()
}

pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
let tick_interval = Duration::from_secs_f64(1.0 / 10.0); // 10 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());
}
}
}
Ok(())
}

/// Handles the tick event of the terminal.
pub fn on_tick(&self) {}

/// Handles the key events and updates the state of [`App`].
pub fn on_key_event(&mut self, key: KeyEvent) -> Result<()> {
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) {
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.
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);
frame.render_widget(
Paragraph::new(text)
.block(block)
.style(Style::new().cyan().on_black())
.centered(),
frame.area(),
)
}
}
102 changes: 102 additions & 0 deletions simple/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::time::Duration;

use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
layout::Alignment,
style::{Style, Stylize},
widgets::{Block, BorderType, 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,
}

impl App {
/// Construct a new instance of [`App`].
pub fn new() -> Self {
Self::default()
}

pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
let tick_interval = Duration::from_secs_f64(1.0 / 10.0); // 10 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());
}
}
}
Ok(())
}

/// Handles the tick event of the terminal.
pub fn on_tick(&self) {}

/// Handles the key events and updates the state of [`App`].
pub fn on_key_event(&mut self, key: KeyEvent) -> Result<()> {
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) {
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) {
// 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);
frame.render_widget(
Paragraph::new(text)
.block(block)
.style(Style::new().cyan().on_black())
.centered(),
frame.area(),
)
}
}
Loading

0 comments on commit ddb5fe3

Please sign in to comment.