use std::fmt;
use crate::block::Block;
use crate::style::palette::{BERRY, BLUSH, LINE, MUTED, PETAL, RED, ROSE, SHINE, SOFT, TEXT, TRACK};
use crate::style::{Line, RAMP, Rgb, mix, paint, sample};
use crate::term::content_width;
const ARC: [&str; 6] = ["◜", "◠", "◝", "◞", "◡", "◟"];
const ORBIT: [&str; 8] = ["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"];
const STARS: [&str; 4] = ["·", "⋆", "✧", "✦"];
const LEVELS: [&str; 8] = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"];
pub(crate) fn noise(cell: u32, tick: u32) -> f32 {
let mut x = cell.wrapping_mul(0x9E37_79B1) ^ tick.wrapping_mul(0x85EB_CA77);
x ^= x >> 15;
x = x.wrapping_mul(0x2C1B_3C6D);
x ^= x >> 12;
(x & 0xFFFF) as f32 / 65535.0
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Spinner {
#[default]
Sparkle,
Dots,
Comet,
Wave,
Arc,
Orbit,
Pulse,
}
impl Spinner {
pub const ALL: [Spinner; 7] = [
Spinner::Sparkle,
Spinner::Dots,
Spinner::Comet,
Spinner::Wave,
Spinner::Arc,
Spinner::Orbit,
Spinner::Pulse,
];
pub fn name(self) -> &'static str {
match self {
Spinner::Sparkle => "sparkle",
Spinner::Dots => "dots",
Spinner::Comet => "comet",
Spinner::Wave => "wave",
Spinner::Arc => "arc",
Spinner::Orbit => "orbit",
Spinner::Pulse => "pulse",
}
}
pub fn from_name(name: &str) -> Option<Spinner> {
Spinner::ALL.into_iter().find(|s| s.name() == name)
}
pub fn width(self) -> usize {
match self {
Spinner::Sparkle | Spinner::Dots => 3,
Spinner::Comet | Spinner::Wave => 5,
_ => 1,
}
}
pub fn frame(self, t: f32) -> Line {
let rest = mix(TRACK, BERRY, 0.45);
match self {
Spinner::Sparkle => {
const PHASE: [f32; 3] = [0.0, 2.1, 4.2];
const SPEED: [f32; 3] = [4.6, 6.1, 3.9];
(0..3).fold(Line::new(), |line, i| {
let b = ((t * SPEED[i] + PHASE[i]).sin() + 1.0) / 2.0;
let b = b * b;
line.push(STARS[((b * 4.0) as usize).min(3)], sample(&[rest, ROSE, SHINE], b))
})
}
Spinner::Dots => (0..3).fold(Line::new(), |line, i| {
let v = (t * 5.0 - i as f32 * 0.9).sin().max(0.0).powi(2);
line.push("•", sample(&[rest, ROSE, PETAL], v))
}),
Spinner::Wave => (0..5).fold(Line::new(), |line, i| {
let x = i as f32;
let h = 0.5 + 0.3 * (t * 6.0 - x * 0.9).sin() + 0.2 * (t * 9.7 + x * 1.7).sin();
let h = h.clamp(0.0, 1.0);
line.push(
LEVELS[((h * 7.0).round() as usize).min(7)],
sample(&[rest, ROSE, SHINE], h * h),
)
}),
Spinner::Comet => {
let phase = (t * 0.9).rem_euclid(2.0);
let x = if phase > 1.0 { 2.0 - phase } else { phase };
let eased = x * x * (3.0 - 2.0 * x);
let head = eased * 4.0;
let dir = if phase > 1.0 { -1.0 } else { 1.0 };
(0..5).fold(Line::new(), |line, i| {
let d = i as f32 - head;
let v = if d * dir <= 0.0 {
(-d.abs() / 1.1).exp()
} else {
(-d.abs() * 3.0).exp()
};
let glyph = if v > 0.9 {
"✦"
} else if v > 0.6 {
"●"
} else if v > 0.25 {
"•"
} else {
"·"
};
line.push(glyph, sample(&[rest, ROSE, SHINE], v))
})
}
Spinner::Arc => Line::new().push(ARC[(t * 12.0) as usize % ARC.len()], ROSE),
Spinner::Orbit => {
let drift = ((t * 0.8).sin() + 1.0) / 2.0;
Line::new().push(ORBIT[(t * 12.0) as usize % ORBIT.len()], sample(&RAMP, drift))
}
Spinner::Pulse => {
let v = ((t * 3.2).sin() + 1.0) / 2.0;
let glyph = if v > 0.92 {
"✦"
} else if v > 0.66 {
"●"
} else if v > 0.33 {
"•"
} else {
"∙"
};
Line::new().push(glyph, sample(&[BERRY, ROSE, SHINE], v))
}
}
}
}
pub fn shimmer(label: &str, t: f32) -> Line {
let n = label.chars().count() as f32;
let pos = (t * 14.0).rem_euclid(n + 24.0) - 12.0;
let tick = (t * 7.0) as u32;
label.chars().enumerate().fold(Line::new(), |line, (j, c)| {
let k = (-(j as f32 - pos).powi(2) / 12.0).exp();
let glitter = noise(j as u32, tick) < 0.015 + 0.2 * k;
let color = if glitter { SHINE } else { mix(SOFT, PETAL, k) };
line.push(c.encode_utf8(&mut [0; 4]), color)
})
}
pub fn pop(age: f32) -> Option<Line> {
let (glyph, color) = match age {
a if a < 0.1 => ("✧", SHINE),
a if a < 0.22 => ("✦", SHINE),
a if a < 0.34 => ("✦", PETAL),
a if a < 0.46 => ("✧", BLUSH),
_ => return None,
};
Some(Line::new().push(glyph, color))
}
pub fn bar(fraction: f32, width: usize) -> Line {
shiny_bar(fraction, width, None)
}
pub fn live_bar(fraction: f32, width: usize, t: f32) -> Line {
shiny_bar(fraction, width, Some(t))
}
pub(crate) fn fraction(f: f32) -> f32 {
if f.is_nan() { 0.0 } else { f.clamp(0.0, 1.0) }
}
fn shiny_bar(fraction: f32, width: usize, t: Option<f32>) -> Line {
let halves = (self::fraction(fraction) * width as f32 * 2.0).round() as usize;
let filled = halves.div_ceil(2) as f32;
let glint = t.map(|t| (t * 16.0).rem_euclid(filled + 10.0) - 5.0);
(0..width).fold(Line::new(), |line, i| {
let base = sample(&RAMP, i as f32 / width.saturating_sub(1).max(1) as f32);
let c = glint.map_or(base, |g| mix(base, SHINE, (-(i as f32 - g).powi(2) / 3.0).exp() * 0.75));
match halves.saturating_sub(i * 2) {
0 => line.push("━", TRACK),
1 => line.push("╸", c),
_ => line.push("━", c),
}
})
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BarStyle {
#[default]
Line,
Waveform,
}
impl BarStyle {
pub(crate) fn draw(self, fraction: f32, width: usize, t: f32) -> Line {
match self {
BarStyle::Line => live_bar(fraction, width, t),
BarStyle::Waveform => live_waveform(fraction, width, t),
}
}
}
fn wave_height(i: usize) -> f32 {
let x = i as f32;
let h = 0.45 + 0.3 * (x * 0.55).sin() + 0.18 * (x * 1.7 + 1.3).sin() + 0.1 * (x * 0.13).sin();
let h = (h + 0.3 * (noise(i as u32, 7) - 0.5)).clamp(0.0, 1.0);
(h * h * 1.3).clamp(0.08, 1.0)
}
pub fn waveform(fraction: f32, width: usize) -> Line {
wave(fraction, width, None)
}
pub fn live_waveform(fraction: f32, width: usize, t: f32) -> Line {
wave(fraction, width, Some(t))
}
fn wave(fraction: f32, width: usize, t: Option<f32>) -> Line {
let head = fraction.clamp(0.0, 1.0) * width as f32;
(0..width).fold(Line::new(), |line, i| {
let x = i as f32;
let mut h = wave_height(i);
let played = (head - x).clamp(0.0, 1.0);
if let Some(t) = t.filter(|_| played > 0.0 && head < width as f32) {
let near = (-(head - x - 0.5).max(0.0) / 2.5).exp();
h = (h + 0.3 * near * (t * 11.0 + x * 1.9).sin()).clamp(0.12, 1.0);
}
let glyph = LEVELS[((h * 7.0).round() as usize).min(7)];
if played <= 0.0 {
return line.push(glyph, TRACK);
}
let base = sample(&RAMP, x / width.saturating_sub(1).max(1) as f32);
let lit = mix(TRACK, base, played);
let glow = t.map_or(0.0, |_| (-(head - x - 0.5).abs() / 0.8).exp() * 0.7);
line.push(glyph, mix(lit, SHINE, glow))
})
}
#[derive(Clone, Debug, Default)]
pub struct Card {
headline: Option<Line>,
rows: Vec<Line>,
width: Option<usize>,
title: Option<Line>,
footer: Option<Line>,
border: Option<Rgb>,
fit: bool,
}
impl Card {
pub fn new() -> Self {
Self::default()
}
pub fn success(headline: &str) -> Self {
Card::new().headline(Line::new().push("● ", ROSE).push(headline, TEXT))
}
pub fn failure(headline: &str) -> Self {
Card::new().headline(Line::new().push("✕ ", RED).push(headline, TEXT))
}
pub fn headline(mut self, line: Line) -> Self {
self.headline = Some(line);
self
}
pub fn aside(mut self, text: &str) -> Self {
let headline = self.headline.take().unwrap_or_default();
self.headline = Some(headline.push(&format!(" {text}"), MUTED));
self
}
pub fn line(mut self, line: Line) -> Self {
self.rows.push(line);
self
}
pub fn blank(self) -> Self {
self.line(Line::new())
}
pub fn kv(self, key: &str, value: &str) -> Self {
self.kv_with(key, value, TEXT)
}
pub fn kv_with(self, key: &str, value: &str, color: Rgb) -> Self {
self.line(Line::kv(key, value, color))
}
pub fn width(mut self, cols: usize) -> Self {
self.width = Some(cols);
self
}
pub fn title(mut self, title: impl Into<Label>) -> Self {
self.title = Some(title.into().styled(&|t| crate::style::bold(&paint(TEXT, t))));
self
}
pub fn footer(mut self, footer: impl Into<Label>) -> Self {
self.footer = Some(footer.into().styled(&|t| paint(MUTED, t)));
self
}
pub fn border(mut self, color: Rgb) -> Self {
self.border = Some(color);
self
}
pub fn fit(mut self) -> Self {
self.fit = true;
self
}
fn natural_width(&self) -> usize {
let rows = self.headline.iter().chain(&self.rows).map(|l| l.width() + 4);
let labels = self.title.iter().chain(&self.footer).map(|l| l.width() + 6);
rows.chain(labels).max().unwrap_or(0) + 2
}
pub fn lines(&self) -> Block {
let full = content_width().saturating_sub(2);
let width = self
.width
.unwrap_or(if self.fit { self.natural_width().min(full) } else { full });
let mut out = Block::new();
out.push("");
out.extend(self.boxed(width).into_iter().map(|l| format!(" {l}")).collect());
out
}
pub(crate) fn boxed(&self, width: usize) -> Block {
let inner = width.saturating_sub(2);
let border = self.border.unwrap_or(LINE);
let side = paint(border, "│");
let mut rows: Vec<&Line> = Vec::new();
let blank = Line::new();
if let Some(h) = &self.headline {
rows.push(h);
if !self.rows.is_empty() {
rows.push(&blank);
}
}
rows.extend(&self.rows);
let mut out = Block::new();
out.push(edge(border, inner, ('╭', '╮'), self.title.as_ref(), true));
for row in rows.into_iter().flat_map(|row| row.wrap(inner.saturating_sub(2))) {
let pad = inner.saturating_sub(2 + row.width());
out.push(format!("{side} {row}{}{side}", " ".repeat(pad)));
}
out.push(edge(border, inner, ('╰', '╯'), self.footer.as_ref(), false));
out
}
pub(crate) fn fixed_width(&self) -> Option<usize> {
self.width.or(self.fit.then(|| self.natural_width()))
}
pub fn print(&self) {
self.lines().print();
}
}
fn edge(color: Rgb, inner: usize, (left, right): (char, char), label: Option<&Line>, start: bool) -> String {
let rule = |n: usize| paint(color, &"─".repeat(n));
let corner = |c: char| paint(color, &c.to_string());
match label.filter(|l| l.width() + 4 <= inner) {
None => format!("{}{}{}", corner(left), rule(inner), corner(right)),
Some(label) if start => format!(
"{}{} {label} {}{}",
corner(left),
rule(1),
rule(inner - 3 - label.width()),
corner(right)
),
Some(label) => format!(
"{}{} {label} {}{}",
corner(left),
rule(inner - 3 - label.width()),
rule(1),
corner(right)
),
}
}
pub struct Label(LabelText);
enum LabelText {
Plain(String),
Styled(Line),
}
impl Label {
fn styled(self, style: &dyn Fn(&str) -> String) -> Line {
match self.0 {
LabelText::Plain(text) => Line::new().raw(&style(&text)),
LabelText::Styled(line) => line,
}
}
}
impl From<&str> for Label {
fn from(text: &str) -> Label {
Label(LabelText::Plain(text.to_string()))
}
}
impl From<String> for Label {
fn from(text: String) -> Label {
Label(LabelText::Plain(text))
}
}
impl From<Line> for Label {
fn from(line: Line) -> Label {
Label(LabelText::Styled(line))
}
}
impl fmt::Display for Card {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.lines().fmt(f)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_waveform_bar_bounces_behind_the_playhead() {
crate::set_color_mode(crate::ColorMode::None);
let early = BarStyle::Waveform.draw(0.5, 20, 0.0).to_string();
let later = BarStyle::Waveform.draw(0.5, 20, 0.3).to_string();
assert_eq!(crate::text::width(&early), 20);
assert_ne!(early, later);
}
#[test]
fn cards_take_titles_of_any_kind_and_print_like_their_lines() {
crate::set_color_mode(crate::ColorMode::None);
let card = Card::new()
.title(String::from("owned"))
.footer(Line::new().push("styled", ROSE))
.border(crate::palette::SAGE)
.kv("a", "1")
.blank()
.kv("b", "2");
let out = card.to_string();
assert!(out.contains("owned") && out.contains("styled"), "{out}");
assert_eq!(out, card.lines().to_string());
assert!(crate::output::capture(|| card.print()).contains("owned"));
}
#[test]
fn a_fitted_card_is_as_wide_as_it_needs() {
let card = Card::new().kv("key", "value").fit();
assert!(card.fixed_width().is_some_and(|w| w < 30));
}
}