feat: add colors

This commit is contained in:
pythops 2023-05-02 11:26:16 +02:00
parent 378c40435b
commit 4c0c2e33a5
4 changed files with 110 additions and 6 deletions

50
Cargo.lock generated
View File

@ -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"

View File

@ -1,7 +1,7 @@
[package]
name = "bouncinamation"
description = "Bouncing DvD logo animation"
version = "0.1.0"
version = "0.2.0"
authors = ["pythops <contact@pythops.com>"]
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"

View File

@ -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<T> = std::result::Result<T, Box<dyn error::Error>>;
#[derive(Debug)]
enum LogoColor {
White,
Red,
Blue,
Cyan,
Yellow,
Magenta,
Green,
Gray,
}
impl From<LogoColor> 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<LogoColor> for Standard {
fn sample<R: Rng + ?Sized>(&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::<LogoColor>().into();
}
if y == 0 {
self.move_top = false
self.move_top = false;
self.color = rand::random::<LogoColor>().into();
}
// 31 is the logo width
if x >= term_size.width - 31 {
self.move_left = true;
self.color = rand::random::<LogoColor>().into();
}
if y >= term_size.height - 9 {
self.move_top = true
self.move_top = true;
self.color = rand::random::<LogoColor>().into();
}
if self.move_left {

View File

@ -48,6 +48,7 @@ pub fn render<B: Backend>(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()));