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
use std::io::{stderr, Write};
use std::sync::mpsc::{self, Receiver, Sender};
use std::thread;
use std::time::Duration;
#[derive(Debug)]
pub struct Loading {
sender: Sender<Signal>,
}
impl Default for Loading {
fn default() -> Self {
Self::new(Spinner::default())
}
}
impl Loading {
pub fn new(spinner: Spinner) -> Self {
let (sender, receiver) = mpsc::channel();
Self::update_stdout(receiver);
Self::update_animation(sender.clone(), spinner);
Self { sender }
}
pub fn end(&self) {
let (sender, receiver) = mpsc::channel();
let _ = self.sender.send(Signal::Exit(sender));
let _ = receiver.recv();
}
pub fn text<T: ToString>(&self, text: T) {
let _ = self.sender.send(Signal::Text(text.to_string()));
}
pub fn success<T: ToString>(&self, text: T) {
let _ = self
.sender
.send(Signal::Next(Status::Success, text.to_string()));
}
pub fn fail<T: ToString>(&self, text: T) {
let _ = self
.sender
.send(Signal::Next(Status::Fail, text.to_string()));
}
pub fn warn<T: ToString>(&self, text: T) {
let _ = self
.sender
.send(Signal::Next(Status::Warn, text.to_string()));
}
pub fn info<T: ToString>(&self, text: T) {
let _ = self
.sender
.send(Signal::Next(Status::Info, text.to_string()));
}
pub fn debug<T: ToString>(&self, text: T) {
let text = format!("\x1B[90m{}\x1B[0m", text.to_string());
let _ = self.sender.send(Signal::Next(Status::Debug, text));
}
fn update_animation(sender: Sender<Signal>, mut spinner: Spinner) {
thread::spawn(move || {
while sender.send(Signal::Frame(spinner.next())).is_ok() {
thread::sleep(spinner.interval);
}
});
}
fn update_stdout(receiver: Receiver<Signal>) {
thread::spawn(move || {
let mut output = stderr();
let mut frame = "";
let mut text = String::new();
macro_rules! write_content {
() => {
let _ = output.write(b"\x1B[2K\x1B[0G");
let _ = output.flush();
};
($($arg:tt)*) => {
let _ = output.write(b"\x1B[2K\x1B[0G");
let _ = output.write(format!($($arg)*).as_bytes());
let _ = output.flush();
};
}
let mut show_loader = true;
while let Ok(signal) = receiver.recv() {
match signal {
Signal::Frame(s) => {
frame = s;
if show_loader {
write_content!("[{}] {}", frame, text);
}
}
Signal::Text(s) => {
if show_loader {
write_content!("[{}] {}", frame, s);
}
text = s;
}
Signal::Next(status, s) => {
write_content!("[{}] {}\n", status.as_str(), s);
}
Signal::Exit(sender) => {
write_content!();
show_loader = false;
let _ = sender.send(());
}
}
}
});
}
}
#[derive(Debug)]
enum Signal {
Frame(&'static str),
Text(String),
Next(Status, String),
Exit(Sender<()>),
}
#[derive(Debug, Clone)]
pub struct Spinner {
index: usize,
frames: Vec<&'static str>,
interval: Duration,
}
impl Default for Spinner {
fn default() -> Self {
Self::new(vec!["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"])
}
}
impl Spinner {
pub fn new(frames: Vec<&'static str>) -> Self {
Self {
index: 0,
frames,
interval: Duration::from_millis(80),
}
}
pub fn interval(&mut self, interval: Duration) {
self.interval = interval
}
fn next(&mut self) -> &'static str {
match self.frames.get(self.index) {
Some(s) => {
self.index += 1;
s
}
None => {
self.index = 1;
self.frames[0]
}
}
}
}
#[derive(Debug)]
enum Status {
Success,
Fail,
Warn,
Info,
Debug,
}
impl Status {
fn as_str(&self) -> &'static str {
match self {
Status::Success => "\x1B[92m+\x1B[0m",
Status::Fail => "\x1B[91mFAIL\x1B[0m",
Status::Warn => "\x1B[93m!\x1B[0m",
Status::Info => "\x1B[94m*\x1B[0m",
Status::Debug => "\x1B[90m \x1B[0m",
}
}
}