Debian Codegraph @

helix-term/src/ui/spinner.rs

raw symbols SCIP
1 use std::{collections::HashMap, time::Instant};
2
3 use helix_lsp::LanguageServerId;
4
5 #[derive(Default, Debug)]
6 pub struct ProgressSpinners {
7 inner: HashMap<LanguageServerId, Spinner>,
8 }
9
10 impl ProgressSpinners {
11 pub fn get(&self, id: LanguageServerId) -> Option<&Spinner> {
12 self.inner.get(&id)
13 }
14
15 pub fn get_or_create(&mut self, id: LanguageServerId) -> &mut Spinner {
16 self.inner.entry(id).or_default()
17 }
18 }
19
20 impl Default for Spinner {
21 fn default() -> Self {
22 Self::dots(80)
23 }
24 }
25
26 #[derive(Debug)]
27 pub struct Spinner {
28 frames: Vec<&'static str>,
29 count: usize,
30 start: Option<Instant>,
31 interval: u64,
32 }
33
34 impl Spinner {
35 /// Creates a new spinner with `frames` and `interval`.
36 /// Expects the frames count and interval to be greater than 0.
37 pub fn new(frames: Vec<&'static str>, interval: u64) -> Self {
38 let count = frames.len();
39 assert!(count > 0);
40 assert!(interval > 0);
41
42 Self {
43 frames,
44 count,
45 interval,
46 start: None,
47 }
48 }
49
50 pub fn dots(interval: u64) -> Self {
51 Self::new(vec!["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"], interval)
52 }
53
54 pub fn start(&mut self) {
55 self.start = Some(Instant::now());
56 }
57
58 pub fn frame(&self) -> Option<&str> {
59 let idx = (self
60 .start
61 .map(|time| Instant::now().duration_since(time))?
62 .as_millis()
63 / self.interval as u128) as usize
64 % self.count;
65
66 self.frames.get(idx).copied()
67 }
68
69 pub fn stop(&mut self) {
70 self.start = None;
71 }
72
73 pub fn is_stopped(&self) -> bool {
74 self.start.is_none()
75 }
76 }