deno.land / x / deno@v1.28.2 / cli / progress_bar.rs

progress_bar.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::colors;use deno_core::parking_lot::Mutex;use indexmap::IndexSet;use std::sync::Arc;use std::time::Duration;
#[derive(Clone, Debug, Default)]pub struct ProgressBar(Arc<Mutex<ProgressBarInner>>);
#[derive(Debug)]struct ProgressBarInner { pb: Option<indicatif::ProgressBar>, is_tty: bool, in_flight: IndexSet<String>,}
impl Default for ProgressBarInner { fn default() -> Self { Self { pb: None, is_tty: colors::is_tty(), in_flight: IndexSet::default(), } }}
impl ProgressBarInner { fn get_or_create_pb(&mut self) -> indicatif::ProgressBar { if let Some(pb) = self.pb.as_ref() { return pb.clone(); }
let pb = indicatif::ProgressBar::new_spinner(); pb.enable_steady_tick(Duration::from_millis(120)); pb.set_prefix("Download"); pb.set_style( indicatif::ProgressStyle::with_template( "{prefix:.green} {spinner:.green} {msg}", ) .unwrap() .tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]), ); self.pb = Some(pb); self.pb.as_ref().unwrap().clone() }
fn add_in_flight(&mut self, msg: &str) { if self.in_flight.contains(msg) { return; }
self.in_flight.insert(msg.to_string()); }
/// Returns if removed "in-flight" was last entry and progress /// bar needs to be updated. fn remove_in_flight(&mut self, msg: &str) -> bool { if !self.in_flight.contains(msg) { return false; }
let mut is_last = false; if let Some(last) = self.in_flight.last() { is_last = last == msg; } self.in_flight.remove(msg); is_last }
fn update_progress_bar(&mut self) { let pb = self.get_or_create_pb(); if let Some(msg) = self.in_flight.last() { pb.set_message(msg.clone()); } }}
pub struct UpdateGuard { pb: ProgressBar, msg: String, noop: bool,}
impl Drop for UpdateGuard { fn drop(&mut self) { if self.noop { return; }
let mut inner = self.pb.0.lock(); if inner.remove_in_flight(&self.msg) { inner.update_progress_bar(); } }}
impl ProgressBar { pub fn update(&self, msg: &str) -> UpdateGuard { let mut guard = UpdateGuard { pb: self.clone(), msg: msg.to_string(), noop: false, }; let mut inner = self.0.lock();
// If we're not running in TTY we're just gonna fallback // to using logger crate. if !inner.is_tty { log::log!(log::Level::Info, "{} {}", colors::green("Download"), msg); guard.noop = true; return guard; }
inner.add_in_flight(msg); inner.update_progress_bar(); guard }
pub fn clear(&self) { let mut inner = self.0.lock();
if let Some(pb) = inner.pb.as_ref() { pb.finish_and_clear(); inner.pb = None; } }
pub fn clear_guard(&self) -> ClearGuard { ClearGuard { pb: self.clone() } }}
pub struct ClearGuard { pb: ProgressBar,}
impl Drop for ClearGuard { fn drop(&mut self) { self.pb.clear(); }}
deno

Version Info

Tagged at
a year ago