mod blend;
mod build;
mod spring;
mod view;
use super::next_id;
use crate::style::{Color, Style};
use crate::{Cmd, Msg as TeaMsg};
use spring::Spring;
use std::time::Duration;
pub const DEFAULT_FULL_CHAR_HALF_BLOCK: char = '▌';
pub const DEFAULT_FULL_CHAR_FULL_BLOCK: char = '█';
pub const DEFAULT_EMPTY_CHAR_BLOCK: char = '░';
const FPS: u32 = 60;
const DEFAULT_WIDTH: usize = 40;
const DEFAULT_FREQUENCY: f64 = 18.0;
const DEFAULT_DAMPING: f64 = 1.0;
const fn hex(rgb: u32) -> Color {
let [_, r, g, b] = rgb.to_be_bytes();
Color::Rgb(r, g, b)
}
const BLEND_START: Color = hex(0x5A_56E0);
const BLEND_END: Color = hex(0xEE_6FF8);
const FULL_COLOR: Color = hex(0x75_71F9);
const EMPTY_COLOR: Color = hex(0x60_6060);
pub type ColorFn = fn(total: f64, current: f64) -> Color;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FrameMsg {
id: u64,
tag: u64,
}
pub type Msg = FrameMsg;
#[derive(Debug, Clone)]
pub struct Model {
pub full: char,
pub full_color: Color,
pub empty: char,
pub empty_color: Color,
pub show_percentage: bool,
pub percent_format: fn(f64) -> String,
pub percentage_style: Style,
id: u64,
tag: u64,
width: usize,
spring: Spring,
shown: f64,
target: f64,
velocity: f64,
blend: Vec<Color>,
scale_blend: bool,
color_fn: Option<ColorFn>,
}
impl Default for Model {
fn default() -> Self {
Self::new()
}
}
impl Model {
#[must_use]
pub fn new() -> Self {
Self {
full: DEFAULT_FULL_CHAR_HALF_BLOCK,
full_color: FULL_COLOR,
empty: DEFAULT_EMPTY_CHAR_BLOCK,
empty_color: EMPTY_COLOR,
show_percentage: true,
percent_format: |p| format!(" {p:3.0}%"),
percentage_style: Style::new(),
id: next_id(),
tag: 0,
width: DEFAULT_WIDTH,
spring: Spring::new(frame_seconds(), DEFAULT_FREQUENCY, DEFAULT_DAMPING),
shown: 0.0,
target: 0.0,
velocity: 0.0,
blend: Vec::new(),
scale_blend: false,
color_fn: None,
}
}
#[must_use]
pub const fn id(&self) -> u64 {
self.id
}
pub fn update(&mut self, msg: &TeaMsg<Msg>) -> Cmd<Msg> {
match msg {
TeaMsg::App(f) if f.id == self.id && f.tag == self.tag && self.is_animating() => {
(self.shown, self.velocity) =
self.spring.update(self.shown, self.velocity, self.target);
self.next_frame()
}
_ => Cmd::none(),
}
}
#[must_use]
pub const fn percent(&self) -> f64 {
self.target
}
pub fn set_percent(&mut self, p: f64) -> Cmd<Msg> {
self.target = clamp_percent(p);
self.tag += 1;
self.next_frame()
}
pub fn incr_percent(&mut self, v: f64) -> Cmd<Msg> {
self.set_percent(self.target + v)
}
pub fn decr_percent(&mut self, v: f64) -> Cmd<Msg> {
self.set_percent(self.target - v)
}
#[must_use]
pub fn is_animating(&self) -> bool {
!((self.shown - self.target).abs() < 0.001 && self.velocity < 0.01)
}
#[must_use]
pub const fn width(&self) -> usize {
self.width
}
pub const fn set_width(&mut self, columns: usize) {
self.width = columns;
}
fn next_frame(&self) -> Cmd<Msg> {
let msg = FrameMsg {
id: self.id,
tag: self.tag,
};
crate::tick(Duration::from_secs(1) / FPS, move |_| msg)
}
}
fn frame_seconds() -> f64 {
(Duration::from_secs(1) / FPS).as_secs_f64()
}
fn clamp_percent(p: f64) -> f64 {
if p.is_nan() { 0.0 } else { p.clamp(0.0, 1.0) }
}
#[cfg(test)]
mod tests;