From 4c0c2e33a5eb69e8b655e1983b5148e0a3af3c8d Mon Sep 17 00:00:00 2001 From: pythops Date: Tue, 2 May 2023 11:26:16 +0200 Subject: [PATCH] feat: add colors --- Cargo.lock | 50 ++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 ++- src/app.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++---- src/ui.rs | 1 + 4 files changed, 110 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0ca591..450c8d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,9 +16,10 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bouncinamation" -version = "0.1.0" +version = "0.2.0" dependencies = [ "crossterm", + "rand", "ratatui", ] @@ -59,6 +60,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "getrandom" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "libc" version = "0.2.142" @@ -119,6 +131,42 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "ratatui" version = "0.20.1" diff --git a/Cargo.toml b/Cargo.toml index 5d6e4d4..395151a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "bouncinamation" description = "Bouncing DvD logo animation" -version = "0.1.0" +version = "0.2.0" authors = ["pythops "] license = "AGPL-3.0" edition = "2021" @@ -12,3 +12,4 @@ repository = "https://github.com/pythops/bouncinamation" [dependencies] crossterm = "0.26.1" tui = { package = "ratatui", version = "0.20.1" } +rand = "0.8.5" diff --git a/src/app.rs b/src/app.rs index 3e8b980..5e3f889 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,14 +1,63 @@ +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; + use std::error; -use tui::layout::Rect; +use tui::{layout::Rect, style::Color}; pub type AppResult = std::result::Result>; +#[derive(Debug)] +enum LogoColor { + White, + Red, + Blue, + Cyan, + Yellow, + Magenta, + Green, + Gray, +} + +impl From for Color { + fn from(color: LogoColor) -> Self { + match color { + LogoColor::White => Color::White, + LogoColor::Red => Color::Red, + LogoColor::Blue => Color::Blue, + LogoColor::Cyan => Color::Cyan, + LogoColor::Yellow => Color::Yellow, + LogoColor::Magenta => Color::Magenta, + LogoColor::Green => Color::Green, + LogoColor::Gray => Color::Gray, + } + } +} + +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> LogoColor { + match rng.gen_range(0..=8) { + 0 => LogoColor::White, + 1 => LogoColor::Red, + 2 => LogoColor::Blue, + 3 => LogoColor::Cyan, + 4 => LogoColor::Yellow, + 5 => LogoColor::Magenta, + 6 => LogoColor::Green, + 7 => LogoColor::Gray, + _ => LogoColor::White, + } + } +} + #[derive(Debug)] pub struct App { pub running: bool, pub coordinates: (u16, u16), pub move_left: bool, pub move_top: bool, + pub color: Color, } impl Default for App { @@ -18,6 +67,7 @@ impl Default for App { coordinates: (1, 1), move_left: false, move_top: false, + color: Color::White, } } } @@ -32,20 +82,24 @@ impl App { let mut y = self.coordinates.1; if x == 0 { - self.move_left = false + self.move_left = false; + self.color = rand::random::().into(); } if y == 0 { - self.move_top = false + self.move_top = false; + self.color = rand::random::().into(); } // 31 is the logo width if x >= term_size.width - 31 { self.move_left = true; + self.color = rand::random::().into(); } if y >= term_size.height - 9 { - self.move_top = true + self.move_top = true; + self.color = rand::random::().into(); } if self.move_left { diff --git a/src/ui.rs b/src/ui.rs index 101fc00..09ee056 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -48,6 +48,7 @@ pub fn render(app: &mut App, frame: &mut Frame<'_, B>) { "#; let block = Paragraph::new(logo) + .style(Style::default().fg(app.color)) .alignment(tui::layout::Alignment::Center) .block(Block::default().style(Style::default()));