use crate::term;
pub struct State {
pub text: String,
pub color: term::Color,
pub symbols: Vec<&'static str>,
pub frames_duration_ms: u64,
}
impl State {
pub fn update(&mut self, update: Update) {
if let Some(text) = update.text {
self.text = text;
}
if let Some(color) = update.color {
self.color = color;
}
if let Some(symbols) = update.symbols {
self.symbols = symbols;
}
if let Some(frames_duration_ms) = update.frames_duration_ms {
self.frames_duration_ms = frames_duration_ms;
}
}
pub fn render(&self, iteration: usize) {
let color = term::color(&self.color);
let frame = self.symbols.clone()[iteration];
let color_reset = term::color(&term::Color::Reset);
let text = &self.text;
term::delete_line();
print!("\r{color}{frame}{color_reset} {text}");
term::flush();
}
}
impl Default for State {
fn default() -> Self {
Self {
text: String::new(),
color: term::Color::default(),
symbols: vec!["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
frames_duration_ms: 65,
}
}
}
#[derive(Debug, Default, Clone)]
pub struct Update {
pub stop: bool,
pub text: Option<String>,
pub color: Option<term::Color>,
pub symbols: Option<Vec<&'static str>>,
pub frames_duration_ms: Option<u64>,
}
impl Update {
pub fn new(text: &str) -> Self {
Self {
text: Some(text.to_owned()),
..Self::default()
}
}
}