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