aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromagdy <omar.professional8777@gmail.com>2024-12-04 19:26:32 +0200
committeromagdy <omar.professional8777@gmail.com>2024-12-04 19:26:32 +0200
commit50359080f71984d8cae04660a6ab90e5f2272416 (patch)
treeaced1018b85a4569c8b3b47f6c97aadeb34ae790 /src
parent695c004357a3d8a225bf16d2ce8f0ff972006258 (diff)
downloadgof-rs-50359080f71984d8cae04660a6ab90e5f2272416.tar.xz
gof-rs-50359080f71984d8cae04660a6ab90e5f2272416.zip
refactor: Use ratatui instead of deprecated tui library and also fix weird background of the playground it now matches your background of the terminal
Diffstat (limited to 'src')
-rwxr-xr-xsrc/generation.rs23
-rwxr-xr-xsrc/ui.rs46
2 files changed, 35 insertions, 34 deletions
diff --git a/src/generation.rs b/src/generation.rs
index 12a06b3..56a4944 100755
--- a/src/generation.rs
+++ b/src/generation.rs
@@ -5,15 +5,15 @@ use crossterm::{
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use rand::{thread_rng, Rng};
-use std::{error::Error, io};
-use tui::{
- backend::{Backend, CrosstermBackend},
+use ratatui::{
+ backend::CrosstermBackend,
layout::{Alignment, Rect},
style::{Color, Modifier, Style},
- text::{Span, Spans},
+ text::{Line, Span},
widgets::{Block, Borders, Paragraph},
Frame, Terminal,
};
+use std::{error::Error, io};
pub type Gen = Vec<Vec<Cell>>;
@@ -23,11 +23,11 @@ pub enum Cell {
Dead,
}
-pub fn render_gen<B: Backend>(f: &mut Frame<B>, chunk: Rect, spans: &Vec<Spans>) {
+pub fn render_gen(f: &mut Frame, chunk: Rect, spans: &Vec<Line>) {
let create_block = |title| {
Block::default()
.borders(Borders::ALL)
- .style(Style::default().bg(Color::Black).fg(Color::Red))
+ .style(Style::default().bg(Color::Reset).fg(Color::Red))
.title(Span::styled(
title,
Style::default().add_modifier(Modifier::BOLD),
@@ -35,7 +35,7 @@ pub fn render_gen<B: Backend>(f: &mut Frame<B>, chunk: Rect, spans: &Vec<Spans>)
.title_alignment(Alignment::Center)
};
let paragraph = Paragraph::new(spans.clone())
- .style(Style::default().bg(Color::Black).fg(Color::Blue))
+ .style(Style::default().bg(Color::Reset).fg(Color::Blue))
.block(create_block(" Conway's Game-Of-Life "))
.alignment(Alignment::Center);
f.render_widget(paragraph, chunk);
@@ -58,22 +58,17 @@ pub fn new_gen(app: &mut App) -> Gen {
grid
}
-pub fn gen_to_spans(gen: &Gen) -> Vec<Spans> {
+pub fn gen_to_spans(gen: &Gen) -> Vec<Line> {
let mut spans = vec![];
- // let alive_cells = vec!["🟥", "🟦", "🟨", "🟪", "🟧", "🟩", "🟫"];
for i in 0..gen.len() {
let mut txt = String::new();
for j in 0..gen[0].len() {
match gen[i][j] {
- // Cell::Alive => txt.push_str(alive_cells[rand % alive_cells.len()]),
Cell::Alive => txt.push_str("⬜"),
- // Cell::Dead => txt.push_str("⬛️"),
Cell::Dead => txt.push_str(" "),
- // Cell::Alive => print!("😎"),
- // Cell::Alive => txt.push_str("🦀"),
}
}
- spans.push(Spans::from(txt));
+ spans.push(Line::from(txt));
}
spans
}
diff --git a/src/ui.rs b/src/ui.rs
index ac9096f..142eac2 100755
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -1,20 +1,19 @@
use crate::generation::*;
use crossterm::event::{self, Event, KeyCode};
use include_dir::{include_dir, Dir};
-use std::{io, thread::sleep, time::Duration};
-use tui::{
+use ratatui::{
backend::Backend,
layout::{Constraint, Direction, Layout},
style::{Color, Modifier, Style},
- text::Spans,
+ text::Line,
widgets::{Block, Borders, List, ListItem, ListState},
Frame, Terminal,
};
+use std::{io, thread::sleep, time::Duration};
// for including the patterns dir in compile time
static PRESETS_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/presets/patterns/");
-
// struct that holds all the information in the left side bar
pub struct StatefulList<T> {
state: ListState,
@@ -77,7 +76,7 @@ pub struct App {
impl App {
pub fn new() -> App {
- // read the patterns from the files and load it in a vector
+ // read the patterns from the files and load it in a vector
fn read_presets() -> Vec<(String, String)> {
let mut result = Vec::new();
for i in 1..=513 {
@@ -133,19 +132,26 @@ pub fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Resu
}
KeyCode::Char('a') => 'animate: loop {
app.layout = Layout::default()
- .direction(Direction::Horizontal)
- .constraints([Constraint::Percentage(0), Constraint::Percentage(100)].as_ref());
+ .direction(Direction::Horizontal)
+ .constraints(
+ [Constraint::Percentage(0), Constraint::Percentage(100)].as_ref(),
+ );
terminal.draw(|f| ui_game(f, &mut app))?;
sleep(Duration::from_millis(32));
if (crossterm::event::poll(Duration::from_millis(1))).unwrap() {
if let Event::Key(k) = event::read().unwrap() {
match k.code {
- KeyCode::Char('s') =>{
- break 'animate
- app.layout = Layout::default()
- .direction(Direction::Horizontal)
- .constraints([Constraint::Percentage(12), Constraint::Percentage(90)].as_ref());
- }
+ KeyCode::Char('s') => {
+ break 'animate app.layout = Layout::default()
+ .direction(Direction::Horizontal)
+ .constraints(
+ [
+ Constraint::Percentage(12),
+ Constraint::Percentage(90),
+ ]
+ .as_ref(),
+ );
+ }
_ => {}
}
}
@@ -158,16 +164,16 @@ pub fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Resu
}
}
-fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) {
- let chunks = app.layout.split(f.size());
+fn ui_list(f: &mut Frame, app: &mut App) {
+ let chunks = app.layout.split(f.area());
let items: Vec<ListItem> = app
.items
.items
.iter()
.map(|i| {
- let lines = vec![Spans::from(i.0.as_str())];
- ListItem::new(lines).style(Style::default().fg(Color::Red).bg(Color::Black))
+ let lines = vec![Line::from(i.0.as_str())];
+ ListItem::new(lines).style(Style::default().fg(Color::Red).bg(Color::Reset))
})
.collect();
@@ -189,15 +195,15 @@ fn ui_list<B: Backend>(f: &mut Frame<B>, app: &mut App) {
render_gen(f, chunks[1], &spans);
}
-fn ui_game<B: Backend>(f: &mut Frame<B>, app: &mut App) {
- let chunks = app.layout.split(f.size());
+fn ui_game(f: &mut Frame, app: &mut App) {
+ let chunks = app.layout.split(f.area());
let items: Vec<ListItem> = app
.items
.items
.iter()
.map(|i| {
- let lines = vec![Spans::from(i.0.as_str())];
+ let lines = vec![Line::from(i.0.as_str())];
ListItem::new(lines).style(Style::default().fg(Color::Red).bg(Color::Black))
})
.collect();