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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (C) 2017-2023 Γlisabeth HENRY.
//
// This file is part of Crowbook.
//
// Crowbook is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Crowbook is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Crowbook. If not, see <http://www.gnu.org/licenses/>.
// Progress bars implementation. Moved into a different file so it is possible
// to make some dependencies (incidacitf) optional.
use crate::book::{Book, Crowbar, CrowbarState};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use rust_i18n::t;
use std::sync::Arc;
use std::time::Duration;
/// Store the progress bars needed for the book
pub struct Bars {
/// Whether or not to use emoji
pub emoji: bool,
/// Container for the progress bars
pub multibar: Option<Arc<MultiProgress>>,
/// Main progress bar (actually a spinner)
pub mainbar: Option<ProgressBar>,
/// Secondary bar
pub secondbar: Option<ProgressBar>,
// /// Guard for thread
// pub guard: Option<thread::JoinHandle<()>>,
/// Spinners for each renderier
pub spinners: Vec<ProgressBar>,
}
impl Bars {
/// Create a new bars storage
pub fn new() -> Bars {
Bars {
emoji: false,
multibar: None,
mainbar: None,
secondbar: None,
// guard: None,
spinners: vec![],
}
}
}
impl Default for Bars {
fn default() -> Self {
Self::new()
}
}
/// Return the style of a bar
impl Book<'_> {
/// Adds a progress bar where where info should be written.
///
/// See [indicatif doc](https://docs.rs/indicatif) for more information.
pub fn private_add_progress_bar(&mut self, emoji: bool) {
self.bars.emoji = emoji;
let multibar = Arc::new(MultiProgress::new());
self.bars.multibar = Some(multibar);
let b = self
.bars
.multibar
.as_ref()
.unwrap()
.add(ProgressBar::new_spinner());
b.enable_steady_tick(Duration::from_millis(200));
self.bars.mainbar = Some(b);
self.bar_set_style(Crowbar::Main, CrowbarState::Running);
}
/// Sets a finished message to the progress bar, if it is set
pub fn bar_finish(&self, bar: Crowbar, state: CrowbarState, msg: &str) {
self.bar_set_style(bar, state);
let pb = match bar {
Crowbar::Main => {
if let Some(ref bar) = self.bars.mainbar {
bar
} else {
return;
}
}
Crowbar::Second => {
if let Some(ref bar) = self.bars.secondbar {
bar
} else {
return;
}
}
Crowbar::Spinner(i) => {
if i < self.bars.spinners.len() {
&self.bars.spinners[i]
} else {
return;
}
}
};
match bar {
Crowbar::Second => pb.finish_and_clear(),
_ => pb.finish_with_message(msg.to_owned()),
};
}
/// Adds a secondary progress bar to display progress of book parsing
pub fn add_second_bar(&mut self, msg: &str, len: u64) {
if let Some(ref multibar) = self.bars.multibar {
let bar = multibar.add(ProgressBar::new(len));
self.bar_set_style(Crowbar::Second, CrowbarState::Running);
bar.set_message(msg.to_owned());
self.bars.secondbar = Some(bar);
}
}
/// Increment second bar
pub fn inc_second_bar(&self) {
if let Some(ref bar) = self.bars.secondbar {
bar.inc(1);
}
}
/// Adds a spinner labeled key to the multibar, and set mainbar to "rendering"
pub fn add_spinner_to_multibar(&mut self, key: &str) -> usize {
if let Some(ref multibar) = self.bars.multibar {
if let Some(ref mainbar) = self.bars.mainbar {
mainbar.set_message(t!("ui.rendering"));
}
let bar = multibar.add(ProgressBar::new_spinner());
bar.enable_steady_tick(Duration::from_millis(200));
bar.set_message(t!("ui.waiting"));
bar.set_prefix(format!("{key}:"));
let i = self.bars.spinners.len();
self.bars.spinners.push(bar);
self.bar_set_style(Crowbar::Spinner(i), CrowbarState::Running);
i
} else {
0
}
}
pub fn bar_set_message(&self, bar: Crowbar, msg: &str) {
let bar = match bar {
Crowbar::Main => {
if let Some(ref bar) = self.bars.mainbar {
bar
} else {
return;
}
}
Crowbar::Second => {
if let Some(ref bar) = self.bars.secondbar {
bar
} else {
return;
}
}
Crowbar::Spinner(i) => {
if i < self.bars.spinners.len() {
&self.bars.spinners[i]
} else {
return;
}
}
};
bar.set_message(msg.to_owned());
}
/// Sets the style of a bar
fn bar_set_style(&self, bar: Crowbar, state: CrowbarState) {
let pb = match bar {
Crowbar::Main => {
if let Some(ref bar) = self.bars.mainbar {
bar
} else {
return;
}
}
Crowbar::Second => {
if let Some(ref bar) = self.bars.secondbar {
bar
} else {
return;
}
}
Crowbar::Spinner(i) => {
if i < self.bars.spinners.len() {
&self.bars.spinners[i]
} else {
return;
}
}
};
let emoji = self.bars.emoji;
let mut style = match bar {
Crowbar::Second => ProgressStyle::default_bar(),
_ => ProgressStyle::default_spinner(),
};
let color = match state {
CrowbarState::Running => "yellow",
CrowbarState::Success => "cyan",
CrowbarState::Error => "red",
};
let tick_chars = match (bar, emoji) {
(Crowbar::Main, false) | (Crowbar::Spinner(_), false) => "-\\|/",
(Crowbar::Main, true) => "ππππππππππππππ",
(Crowbar::Spinner(_), true) => "ββββ",
(_, _) => "",
};
let end_tick = match (state, emoji) {
(CrowbarState::Running, _) => "V",
(CrowbarState::Success, true) => "β",
(CrowbarState::Error, true) => "β",
(CrowbarState::Success, false) => "V",
(CrowbarState::Error, false) => "X",
};
match bar {
Crowbar::Second => {
style = style
.template("{bar:40.cyan/blue} {percent:>7} {wide_msg}")
.expect("Error in second progress bar style")
.progress_chars("##-");
}
bar => {
style = style.tick_chars(&format!("{tick_chars}{end_tick}"));
match bar {
Crowbar::Spinner(_) => {
style = style
.template(&format!(
"{{spinner:.bold.{color}}} {{prefix}} {{wide_msg}}"
))
.expect("Error in spinner progress bar style");
}
_ => {
style = style
.template(&format!("{{spinner:.bold.{color}}} {{prefix}}{{wide_msg}}"))
.expect("Error in progress bar style");
}
};
}
}
pb.set_style(style);
}
}
impl Drop for Book<'_> {
fn drop(&mut self) {
if let Some(ref bar) = self.bars.secondbar {
bar.finish_and_clear();
}
if let Some(ref bar) = self.bars.mainbar {
bar.finish();
// let guard = mem::replace(&mut self.bars.guard, None);
// guard.unwrap().join().unwrap();
}
}
}