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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use crate::{progress, render::line::draw, Root, Throughput};
use std::{
io,
ops::RangeInclusive,
sync::atomic::{AtomicBool, Ordering},
time::Duration,
};
#[derive(Clone)]
pub struct Options {
pub output_is_terminal: bool,
pub colored: bool,
pub timestamp: bool,
pub terminal_dimensions: (u16, u16),
pub hide_cursor: bool,
pub throughput: bool,
pub level_filter: Option<RangeInclusive<progress::key::Level>>,
pub initial_delay: Option<Duration>,
pub frames_per_second: f32,
pub keep_running_if_progress_is_empty: bool,
}
impl Default for Options {
fn default() -> Self {
Options {
output_is_terminal: true,
colored: true,
timestamp: false,
terminal_dimensions: (80, 20),
hide_cursor: false,
level_filter: None,
initial_delay: None,
frames_per_second: 6.0,
throughput: false,
keep_running_if_progress_is_empty: true,
}
}
}
pub struct JoinHandle {
inner: Option<std::thread::JoinHandle<io::Result<()>>>,
connection: std::sync::mpsc::SyncSender<Event>,
disconnected: bool,
}
impl JoinHandle {
pub fn detach(mut self) {
self.disconnect();
self.forget();
}
pub fn disconnect(&mut self) {
self.disconnected = true;
}
pub fn forget(&mut self) {
self.inner.take();
}
pub fn wait(mut self) {
self.inner.take().and_then(|h| h.join().ok());
}
pub fn shutdown(&mut self) {
if !self.disconnected {
self.connection.send(Event::Tick).ok();
self.connection.send(Event::Quit).ok();
}
}
pub fn shutdown_and_wait(mut self) {
self.shutdown();
self.wait();
}
}
impl Drop for JoinHandle {
fn drop(&mut self) {
self.shutdown();
self.inner.take().and_then(|h| h.join().ok());
}
}
#[derive(Debug)]
enum Event {
Tick,
Quit,
}
pub fn render(
mut out: impl io::Write + Send + 'static,
progress: impl Root + Send + 'static,
config: Options,
) -> JoinHandle {
let Options {
output_is_terminal,
colored,
timestamp,
level_filter,
terminal_dimensions,
initial_delay,
frames_per_second,
keep_running_if_progress_is_empty,
hide_cursor,
throughput,
} = config;
let config = draw::Options {
level_filter,
terminal_dimensions,
keep_running_if_progress_is_empty,
output_is_terminal,
colored,
timestamp,
hide_cursor,
};
let (event_send, event_recv) = std::sync::mpsc::sync_channel::<Event>(1);
let show_cursor = possibly_hide_cursor(&mut out, event_send.clone(), hide_cursor && output_is_terminal);
static SHOW_PROGRESS: AtomicBool = AtomicBool::new(false);
let handle = std::thread::spawn({
let tick_send = event_send.clone();
move || {
{
let initial_delay = initial_delay.unwrap_or_else(Duration::default);
SHOW_PROGRESS.store(initial_delay == Duration::default(), Ordering::Relaxed);
if !SHOW_PROGRESS.load(Ordering::Relaxed) {
std::thread::spawn(move || {
std::thread::sleep(initial_delay);
SHOW_PROGRESS.store(true, Ordering::Relaxed);
});
}
}
let mut state = draw::State::default();
if throughput {
state.throughput = Some(Throughput::default());
}
let secs = 1.0 / frames_per_second;
let _ticker = std::thread::spawn(move || loop {
if tick_send.send(Event::Tick).is_err() {
break;
}
std::thread::sleep(Duration::from_secs_f32(secs));
});
for event in event_recv {
match event {
Event::Tick => {
draw::all(
&mut out,
&progress,
SHOW_PROGRESS.load(Ordering::Relaxed),
&mut state,
&config,
)?;
}
Event::Quit => break,
}
}
if show_cursor {
crosstermion::execute!(out, crosstermion::cursor::Show).ok();
}
Ok(())
}
});
JoinHandle {
inner: Some(handle),
connection: event_send,
disconnected: false,
}
}
#[allow(unused_mut)]
fn possibly_hide_cursor(
out: &mut impl io::Write,
quit_send: std::sync::mpsc::SyncSender<Event>,
mut hide_cursor: bool,
) -> bool {
#[cfg(not(feature = "ctrlc"))]
drop(quit_send);
#[cfg(feature = "ctrlc")]
if hide_cursor {
hide_cursor = ctrlc::set_handler(move || {
quit_send.send(Event::Quit).ok();
})
.is_ok();
}
if hide_cursor {
crosstermion::execute!(out, crosstermion::cursor::Hide).is_ok()
} else {
false
}
}