use ratatui::style::Color;
use rand::Rng;
use std::time::{Duration, Instant};
pub struct GlitchyTitle {
text: String,
glitch_chars: Vec<char>,
last_update: Instant,
glitch_interval: Duration,
glitch_intensity: f32,
}
impl GlitchyTitle {
pub fn new(text: &str) -> Self {
Self {
text: text.to_string(),
glitch_chars: vec!['Ṫ', 'ḧ', 'ė', 'Ș', 'ỷ', 'ṡ', 'ṫ', 'ē', 'ṁ',
'̶', '̴', '̸', '̵', '̷', '≠', '҉', '$', '#', '%', '&'],
last_update: Instant::now(),
glitch_interval: Duration::from_millis(150),
glitch_intensity: 0.2,
}
}
pub fn update(&mut self) {
if self.last_update.elapsed() >= self.glitch_interval {
self.last_update = Instant::now();
let mut rng = rand::thread_rng();
self.glitch_interval = Duration::from_millis(rng.gen_range(50..200));
}
}
pub fn get_display_text(&self) -> Vec<(String, Option<Color>)> {
let mut rng = rand::thread_rng();
let mut result = Vec::new();
for c in self.text.chars() {
let glitch_chance = rng.gen::<f32>();
if glitch_chance < self.glitch_intensity {
let effect_type = rng.gen_range(0..4);
match effect_type {
0 => {
let glitch_char = self.glitch_chars[rng.gen_range(0..self.glitch_chars.len())];
result.push((glitch_char.to_string(), Some(Color::Red)));
}
1 => {
result.push((c.to_string(), Some(Color::LightRed)));
}
2 => {
result.push((c.to_string(), None));
result.push((c.to_string(), Some(Color::LightRed)));
}
_ => {
result.push((c.to_string(), Some(Color::LightRed)));
}
}
} else {
result.push((c.to_string(), None));
}
}
result
}
pub fn increase_intensity(&mut self, amount: f32) {
self.glitch_intensity += amount;
if self.glitch_intensity > 0.5 {
self.glitch_intensity = 0.5; }
}
}