mod blend;
mod spring;
use super::next_id;
use crate::style::{Color, Style, round_clamped, to_f64, width};
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 fn with_default_blend(self) -> Self {
self.with_colors(&[BLEND_START, BLEND_END])
}
#[must_use]
pub fn with_colors(mut self, colors: &[Color]) -> Self {
match colors {
[] | [_] => {
self.full_color = colors.first().copied().unwrap_or(FULL_COLOR);
self.blend.clear();
self.color_fn = None;
}
many => self.blend = many.to_vec(),
}
self
}
#[must_use]
pub fn with_color_fn(mut self, f: ColorFn) -> Self {
self.color_fn = Some(f);
self.blend.clear();
self
}
#[must_use]
pub const fn with_fill_characters(mut self, full: char, empty: char) -> Self {
self.full = full;
self.empty = empty;
self
}
#[must_use]
pub const fn without_percentage(mut self) -> Self {
self.show_percentage = false;
self
}
#[must_use]
pub const fn with_width(mut self, columns: usize) -> Self {
self.width = columns;
self
}
#[must_use]
pub fn with_spring_options(mut self, frequency: f64, damping: f64) -> Self {
self.set_spring_options(frequency, damping);
self
}
#[must_use]
pub const fn with_scaled(mut self, on: bool) -> Self {
self.scale_blend = on;
self
}
pub fn set_spring_options(&mut self, frequency: f64, damping: f64) {
self.spring = Spring::new(frame_seconds(), frequency, damping);
}
#[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;
}
#[must_use]
pub fn view(&self) -> String {
self.view_as(self.shown)
}
#[must_use]
pub fn view_as(&self, percent: f64) -> String {
let percent = clamp_percent(percent);
let label = self.percentage_view(percent);
self.bar_view(percent, width(&label)) + &label
}
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 bar_view(&self, percent: f64, label: usize) -> String {
let total = self.width.saturating_sub(label);
let filled = round_clamped(to_f64(total) * percent, total);
let half = self.full == DEFAULT_FULL_CHAR_HALF_BLOCK;
let mut out = if let Some(f) = self.color_fn {
colored_cells(self.full, filled, total, half, |t| f(percent, t))
} else if self.blend.is_empty() {
Style::new()
.fg(self.full_color)
.paint(&self.full.to_string().repeat(filled))
} else {
self.blend_cells(filled, total, half)
};
let empty = self.empty.to_string().repeat(total - filled);
out += &Style::new().fg(self.empty_color).paint(&empty);
out
}
fn blend_cells(&self, filled: usize, total: usize, half: bool) -> String {
let per = if half { 2 } else { 1 };
let span = if self.scale_blend { filled } else { total };
let colors = blend::blend_1d(span * per, &self.blend);
let full = self.full.to_string();
(0..filled)
.map(|i| {
let style = Style::new().fg(colors[i * per]);
let style = if half {
style.bg(colors[i * per + 1])
} else {
style
};
style.paint(&full)
})
.collect()
}
fn percentage_view(&self, percent: f64) -> String {
if !self.show_percentage {
return String::new();
}
self.percentage_style
.paint(&(self.percent_format)(percent * 100.0))
}
}
fn colored_cells(
full: char,
filled: usize,
total: usize,
half: bool,
color: impl Fn(f64) -> Color,
) -> String {
let step = 0.5 / to_f64(total);
(0..filled)
.map(|i| {
let current = to_f64(i) / to_f64(total);
let style = Style::new().fg(color(current));
let style = if half {
style.bg(color((current + step).min(1.0)))
} else {
style
};
style.paint(&full.to_string())
})
.collect()
}
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;