Skip to content

Commit

Permalink
Merge pull request #3 from JeromeSchmied/keyrelease
Browse files Browse the repository at this point in the history
Keyrelease
  • Loading branch information
JeromeSchmied authored Mar 24, 2024
2 parents 1451871 + 246d02e commit 33db95b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ impl App {
pub fn i(&self) -> usize {
self.i
}
pub fn render_universe(&self) {
println!("{}", self.universe);
}
// pub fn render_universe(&self) {
// println!("{}", self.universe);
// }
pub fn wh(&self) -> u16 {
self.wh
}
Expand Down
34 changes: 26 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ratatui::{style::Color, widgets::canvas::Shape};

use crate::shapes::HandleError;
use std::{fmt, time::Duration};

Expand Down Expand Up @@ -182,15 +184,31 @@ impl Universe {
}
}

impl fmt::Display for Universe {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for line in self.cells.as_slice().chunks(self.width as usize) {
for &cell in line {
let symbol = if cell == Cell::Dead { ' ' } else { '◼' }; // ◻
write!(f, "{symbol} ")?;
impl Shape for Universe {
fn draw(&self, painter: &mut ratatui::widgets::canvas::Painter) {
for y in 0..self.height {
for x in 0..self.width {
match self.cells.get(self.get_index(x, y)).unwrap() {
Cell::Alive => painter.paint(y.into(), x.into(), Color::White),
Cell::Dead => continue,
}
}
writeln!(f)?;
}
Ok(())
}
}

// impl fmt::Display for Universe {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// writeln!(f, "╭{}╮\r", "─".repeat(self.width as usize * 2))?;
// for line in self.cells.as_slice().chunks(self.width as usize) {
// write!(f, "│")?;
// for &cell in line {
// let symbol = if cell == Cell::Dead { ' ' } else { '◼' }; // ◻
// write!(f, "{symbol} ")?;
// }
// writeln!(f, "│\r")?;
// }
// writeln!(f, "╰{}╯\r", "─".repeat(self.width as usize * 2))?;
// Ok(())
// }
// }
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// create app and run it with width and height from terminal size
let wh = size()?;
let mut app = App::new(wh.1 - 4);
let mut app = App::new((wh.1 + 10) * 3);

let res = run_app(&mut terminal, &mut app);

Expand All @@ -52,7 +52,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<

loop {
let wh = size()?;
app.set_wh(wh.1 + 1 - 5);
app.set_wh((wh.1 + 10) * 3);

terminal.draw(|f| ui::ui(f, app))?;

Expand Down Expand Up @@ -92,7 +92,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> io::Result<
} else {
// resize and restart
let wh = size()?;
app.set_wh(wh.1 + 1 - 5);
app.set_wh((wh.1 + 10) * 3);
app.restart();
}
} else {
Expand Down
30 changes: 19 additions & 11 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Style},
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
widgets::{canvas::Canvas, Block, BorderType, Borders, Paragraph},
Frame,
};

Expand All @@ -28,17 +28,25 @@ pub fn ui(f: &mut Frame, app: &App) {
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.title("Conway's Game of Life");
let universe = Paragraph::new(app.universe.to_string()).block(cgol);
// let universe = Paragraph::new(app.universe.to_string()).block(cgol);
// let universe = Canvas::new().block(cgol);
let universe = Canvas::default()
// .x_bounds([0., main_chunks[0].height as f64 * 2. - 4.])
// .y_bounds([0., main_chunks[0].height as f64 * 2. - 4.])
.paint(|ctx| ctx.draw(&app.universe))
.block(cgol);

f.render_widget(
universe,
Rect::new(
0,
0,
main_chunks[0].height * 2 - 4,
main_chunks[0].height - 1,
),
);
f.render_widget(universe, main_chunks[0]);

// f.render_widget(
// universe,
// Rect::new(
// 0,
// 0,
// main_chunks[0].height * 2 - 4,
// main_chunks[0].height - 1,
// ),
// );

let footer = Layout::default()
.direction(Direction::Horizontal)
Expand Down

0 comments on commit 33db95b

Please sign in to comment.