use crate::animation::{AnimationTimer, BlinkPattern, IndicatorStyle};
use crate::element::Element;
use crate::style::{Color, Style};
pub fn blink(visible: bool, element: Element) -> Element {
if visible {
element
} else {
Element::Empty
}
}
pub fn blink_or(visible: bool, when_visible: Element, when_hidden: Element) -> Element {
if visible {
when_visible
} else {
when_hidden
}
}
pub fn blink_pattern(
pattern: BlinkPattern,
timer: &AnimationTimer,
visible_text: &str,
hidden_text: &str,
) -> Element {
if pattern.is_visible(timer) {
Element::text(visible_text)
} else {
Element::text(hidden_text)
}
}
pub fn animated_indicator(style: IndicatorStyle, timer: &AnimationTimer) -> Element {
Element::text(style.render(timer))
}
pub fn animated_indicator_colored(
style: IndicatorStyle,
timer: &AnimationTimer,
color: Color,
) -> Element {
Element::styled_text(style.render(timer), Style::new().fg(color))
}
pub fn blinking_dot(timer: &AnimationTimer, interval_ms: u64, color: Color) -> Element {
if timer.blink(interval_ms) {
Element::styled_text("●", Style::new().fg(color))
} else {
Element::text(" ")
}
}
pub fn pulsing_dot(timer: &AnimationTimer, interval_ms: u64, color: Color) -> Element {
if timer.blink(interval_ms) {
Element::styled_text("●", Style::new().fg(color))
} else {
Element::styled_text("○", Style::new().fg(color))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_blink_visible() {
let elem = blink(true, Element::text("test"));
assert!(elem.is_text());
}
#[test]
fn test_blink_hidden() {
let elem = blink(false, Element::text("test"));
assert!(elem.is_empty());
}
#[test]
fn test_blink_or_visible() {
let elem = blink_or(true, Element::text("on"), Element::text("off"));
match elem {
Element::Text { content, .. } => assert_eq!(content, "on"),
_ => panic!("Expected Text"),
}
}
#[test]
fn test_blink_or_hidden() {
let elem = blink_or(false, Element::text("on"), Element::text("off"));
match elem {
Element::Text { content, .. } => assert_eq!(content, "off"),
_ => panic!("Expected Text"),
}
}
#[test]
fn test_animated_indicator() {
let timer = AnimationTimer::new();
let elem = animated_indicator(IndicatorStyle::SpinnerDots, &timer);
assert!(elem.is_text());
}
#[test]
fn test_animated_indicator_colored() {
let timer = AnimationTimer::new();
let elem = animated_indicator_colored(IndicatorStyle::SpinnerDots, &timer, Color::Cyan);
assert!(elem.is_text());
}
#[test]
fn test_blinking_dot() {
let timer = AnimationTimer::new();
let elem = blinking_dot(&timer, 500, Color::Green);
assert!(elem.is_text());
}
#[test]
fn test_pulsing_dot() {
let timer = AnimationTimer::new();
let elem = pulsing_dot(&timer, 500, Color::Yellow);
assert!(elem.is_text());
}
#[test]
fn test_blink_pattern() {
let timer = AnimationTimer::new();
let elem = blink_pattern(BlinkPattern::Standard, &timer, "●", " ");
assert!(elem.is_text());
}
}