Instant is more suitable than SystemTime for spinners

This commit is contained in:
Blaž Hrastnik 2022-02-08 18:48:19 +09:00
parent f88c077f99
commit 1bcb624ae6

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, time::SystemTime}; use std::{collections::HashMap, time::Instant};
#[derive(Default, Debug)] #[derive(Default, Debug)]
pub struct ProgressSpinners { pub struct ProgressSpinners {
@ -25,7 +25,7 @@ impl Default for Spinner {
pub struct Spinner { pub struct Spinner {
frames: Vec<&'static str>, frames: Vec<&'static str>,
count: usize, count: usize,
start: Option<SystemTime>, start: Option<Instant>,
interval: u64, interval: u64,
} }
@ -50,14 +50,13 @@ impl Spinner {
} }
pub fn start(&mut self) { pub fn start(&mut self) {
self.start = Some(SystemTime::now()); self.start = Some(Instant::now());
} }
pub fn frame(&self) -> Option<&str> { pub fn frame(&self) -> Option<&str> {
let idx = (self let idx = (self
.start .start
.map(|time| SystemTime::now().duration_since(time))? .map(|time| Instant::now().duration_since(time))?
.ok()?
.as_millis() .as_millis()
/ self.interval as u128) as usize / self.interval as u128) as usize
% self.count; % self.count;