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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//! [![Crates.io](https://img.shields.io/crates/v/throbber)](https://crates.io/crates/throbber)
//! [![docs.rs](https://docs.rs/throbber/badge.svg)](https://docs.rs/throbber)
//! [![GitHub last commit](https://img.shields.io/github/last-commit/Treeniks/throbber)](https://github.com/Treeniks/throbber)
//! [![License](https://img.shields.io/github/license/Treeniks/throbber)](https://github.com/Treeniks/throbber/blob/master/LICENSE)
//!
//! This crate serves as an alternative to [loading](https://crates.io/crates/loading). It is used to display a throbber animation in the terminal while other calculations are done in the main program.
//!
//! ![Throbber Preview](https://user-images.githubusercontent.com/56131826/109326392-68c28b00-7857-11eb-8e8d-dd576c868e7f.gif "Throbber Preview")
//!
//! # Usage
//!
//! Add this to your Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! throbber = "0.1"
//! ```
//!
//! To display a throbber animation, first create a [`Throbber`](Throbber) object:
//!
//! ```rust
//! # use throbber::Throbber;
//! let mut throbber = Throbber::new();
//! ```
//!
//! You can also customize certain settings like the displayed animation and the displayed message:
//!
//! ```rust
//! # use throbber::Throbber;
//! let mut throbber = Throbber::new()
//!     .message("calculating stuff".to_string())
//!     .frames(&throbber::MOVE_EQ_F); // this crate comes with a few predefined animations
//!                                    // see the Constants section
//! ```
//!
//! Then you can simply call [`start`](Throbber::start) wherever you want to start the animation and a *finish function* like [`success`](Throbber::success) where you want to stop it.
//!
//! ```rust
//! # use throbber::Throbber;
//! # let mut throbber = Throbber::new();
//! throbber.start();
//! // do calculations
//! throbber.success("calculations successful!".to_string());
//! ```
//!
//! After which you can call [`start`](Throbber::start) or [`start_with_msg`](Throbber::start_with_msg) again to start the animation again.
//! You can also change everything you could customize during the Throbber object creation, e. g. with [`change_message`](Throbber::change_message) and [`change_frames`](Throbber::change_frames). This also works while an animation is running.
//!
//! If you don't intend to start another animation, you should drop the Throbber object with [`end`](Throbber::end). This action also ends the underlying thread:
//!
//! ```rust
//! # use throbber::Throbber;
//! # let mut throbber = Throbber::new();
//! throbber.end();
//! ```
//!
//! # Examples
//!
//! This is the example from the preview above:
//!
//! ```rust
//! use std::thread;
//! use std::time::Duration;
//! use throbber::Throbber;
//!
//! fn main() {
//!     let mut throbber = Throbber::new().message("calculating stuff".to_string());
//!
//!     throbber.start();
//!     // do stuff
//!     thread::sleep(Duration::from_secs(2));
//!     throbber.success("Success".to_string());
//!
//!     throbber.start_with_msg("calculating more stuff".to_string());
//!     // do other stuff
//!     thread::sleep(Duration::from_secs(2));
//!     throbber.fail("Fail".to_string());
//!
//!     throbber.end();
//! }
//! ```
//!
//! You can also keep track of progress with [`change_message`](Throbber::change_message):
//!
//! ```rust
//! use std::thread;
//! use std::time::Duration;
//! use throbber::Throbber;
//!
//! fn main() {
//!     let mut throbber = Throbber::new()
//!         .message("Downloading file1 0%".to_string())
//!         .frames(&throbber::ROTATE_F)
//!         .interval(Duration::from_millis(100));
//!
//!     throbber.start();
//!     for i in 0..100 {
//!         throbber.change_message(format!("Downloading file1 {}%", i));
//!         thread::sleep(Duration::from_millis(30));
//!     }
//!     throbber.success("Downloaded file1".to_string());
//!
//!     throbber.start_with_msg("Downloading file2 0%".to_string());
//!     for i in 0..69 {
//!         throbber.change_message(format!("Downloading file2 {}%", i));
//!         thread::sleep(Duration::from_millis(30));
//!     }
//!     throbber.fail("Download of file2 failed".to_string());
//!
//!     throbber.end();
//! }
//! ```

use std::io::Write;
use std::sync::mpsc::{self, Receiver, Sender, TryRecvError};
use std::thread::{self, JoinHandle};
use std::time::Duration;

/// `⠋   ⠙   ⠹   ⠸   ⠼   ⠴   ⠦   ⠧   ⠇   ⠏`
///
/// This is the default animation when creating a new Throbber object.
pub const DEFAULT_F: [&str; 10] = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
/// `◐   ◓   ◑   ◒`
pub const CIRCLE_F: [&str; 4] = ["◐", "◓", "◑", "◒"];
/// `|   /   -   \`
pub const ROTATE_F: [&str; 4] = ["|", "/", "-", "\\"];
/// `[=  ]   [ = ]   [  =]   [ = ]`
pub const MOVE_EQ_F: [&str; 4] = ["[=  ]", "[ = ]", "[  =]", "[ = ]"];
/// `[-  ]   [ - ]   [  -]   [ - ]`
pub const MOVE_MIN_F: [&str; 4] = ["[-  ]", "[ - ]", "[  -]", "[ - ]"];
/// `[=    ]   [==   ]   [ ==  ]   [  == ]   [   ==]   [    =]`
pub const MOVE_EQ_LONG_F: [&str; 10] = [
    "[=    ]", "[==   ]", "[ ==  ]", "[  == ]", "[   ==]", "[    =]", "[   ==]", "[  == ]",
    "[ ==  ]", "[==   ]",
];
/// `[-    ]   [--   ]   [ --  ]   [  -- ]   [   --]   [    -]`
pub const MOVE_MIN_LONG_F: [&str; 10] = [
    "[-    ]", "[--   ]", "[ --  ]", "[  -- ]", "[   --]", "[    -]", "[   --]", "[  -- ]",
    "[ --  ]", "[--   ]",
];

/// Representation of a throbber animation. It can start, succeed, fail or end at any point.
///
/// Note that a call to [`end`](Throbber::end) takes ownership of the struct and drops it, as such it should be called to completely remove the throbber animtion object. If you want to start another animation afterwards, you have to create a new [`Throbber`](Throbber) object. This is done because multiple calls to start do not actually create multiple threads, instead a call to a *finish function* (like [`success`](Throbber::success)) simply parks the thread and a following [`start`](Throbber::start) call unparks that thread again. As such, a call to [`end`](Throbber::end) kills the thread entirely. If you want to just stop the animation, but potentially start it again later on, use [`finish`](Throbber::finish) instead.
///
/// # Examples
///
/// ```rust
/// use throbber::Throbber;
/// use std::thread;
/// use std::time::Duration;
///
/// let mut throbber = Throbber::new()
///     .message("calculating stuff".to_string())
///     .interval(Duration::from_millis(50))
///     .frames(&throbber::ROTATE_F);
///
/// throbber.start();
///
/// // do stuff
/// thread::sleep(Duration::from_secs(5));
///
/// throbber.success("calculation successful".to_string());
/// throbber.end();
/// ```

pub struct Throbber {
    anim: Option<ThrobberAnim>,
    message: String,
    interval: Duration,
    frames: &'static [&'static str],
}

struct ThrobberAnim {
    thread: JoinHandle<()>,
    sender: Sender<ThrobberSignal>,
}

enum ThrobberSignal {
    Start,
    Finish,
    Succ(String),
    Fail(String),
    ChMsg(String),
    ChInt(Duration),
    ChFrames(&'static [&'static str]),
    End,
}

impl Throbber {
    /// Creates a new Throbber object.
    ///
    /// # Default Values
    ///
    /// If you do not customize your throbber animation with [`message`](Throbber::message) etc., these are the default values:
    ///
    /// * message: `""`
    /// * interval: `Duration::from_millis(200)`
    /// * frames: `DEFAULT_F (⠋   ⠙   ⠹   ⠸   ⠼   ⠴   ⠦   ⠧   ⠇   ⠏)`
    pub fn new() -> Self {
        Self {
            anim: None,
            message: "".to_string(),
            interval: Duration::from_millis(200),
            frames: &DEFAULT_F,
        }
    }

    /// Sets the message that's supposed to print.
    ///
    /// This does nothing if [`start`](Throbber::start) was called before. To change the message after [`start`](Throbber::start) was called, use [`change_message`](Throbber::change_message) instead.
    pub fn message(mut self, msg: String) -> Self {
        self.message = msg;
        self
    }

    /// Sets the interval in which the animation frames are supposed to print.
    ///
    /// This does nothing if [`start`](Throbber::start) was called before. To change the interval after [`start`](Throbber::start) was called, use [`change_interval`](Throbber::change_interval) instead.
    pub fn interval(mut self, interval: Duration) -> Self {
        self.interval = interval;
        self
    }

    /// Sets the animation frames that are supposed to print.
    ///
    /// This does nothing if [`start`](Throbber::start) was called before. To change the animation frames after [`start`](Throbber::start) was called, use [`change_frames`](Throbber::change_frames) instead.
    pub fn frames(mut self, frames: &'static [&'static str]) -> Self {
        self.frames = frames;
        self
    }

    /// Changes the message that's supposed to print.toml
    ///
    /// Unlike [`message`](Throbber::message), this will work both before and after [`start`](Throbber::start) was called.
    pub fn change_message(&mut self, msg: String) {
        if let Some(ref anim) = self.anim {
            anim.sender
                .send(ThrobberSignal::ChMsg(msg.clone()))
                .unwrap();
            anim.thread.thread().unpark();
        }
        self.message = msg;
    }

    /// Changes the interval in which the animation frames are supposed to print.
    ///
    /// Unlike [`interval`](Throbber::interval), this will work both before and after [`start`](Throbber::start) was called.
    pub fn change_interval(&mut self, interval: Duration) {
        if let Some(ref anim) = self.anim {
            anim.sender.send(ThrobberSignal::ChInt(interval)).unwrap();
            anim.thread.thread().unpark();
        }
        self.interval = interval;
    }

    /// Changes the animation frames that are supposed to print.
    ///
    /// Unlike [`frames`](Throbber::frames), this will work both before and after [`start`](Throbber::start) was called.
    pub fn change_frames(&mut self, frames: &'static [&'static str]) {
        if let Some(ref anim) = self.anim {
            anim.sender.send(ThrobberSignal::ChFrames(frames)).unwrap();
            anim.thread.thread().unpark();
        }
        self.frames = frames;
    }

    /// Starts the animation.
    ///
    /// If this is the first call to [`start`](Throbber::start), a new thread gets created to play the animation. Otherwise the thread that already exists gets unparked and starts the animation again.
    pub fn start(&mut self) {
        if let Some(ref anim) = self.anim {
            anim.sender.send(ThrobberSignal::Start).unwrap();
            anim.thread.thread().unpark();
            return;
        }

        let (sender, receiver): (Sender<ThrobberSignal>, Receiver<ThrobberSignal>) =
            mpsc::channel();

        let msg = self.message.clone();
        let frames = self.frames;
        let interval = self.interval;
        let thread = thread::spawn(move || animation_thread(receiver, msg, frames, interval));

        self.anim = Some(ThrobberAnim { thread, sender });
    }

    /// Starts the animation with the specified `msg`.
    ///
    /// Equivalent to `throbber.change_message(msg); throbber.start();`.
    pub fn start_with_msg(&mut self, msg: String) {
        self.change_message(msg);
        self.start();
    }

    /// Stops the current animation, leaving a blank line.
    pub fn finish(&mut self) {
        if let Some(ref anim) = self.anim {
            anim.sender.send(ThrobberSignal::Finish).unwrap();
            anim.thread.thread().unpark();
        }
    }

    /// Stops the current animation and prints `msg` as a *success message*.
    pub fn success(&mut self, msg: String) {
        if let Some(ref anim) = self.anim {
            anim.sender.send(ThrobberSignal::Succ(msg)).unwrap();
            anim.thread.thread().unpark();
        } else {
            println!("\x1B[2K\r✔ {}", msg);
        }
    }

    /// Stops the current animation and prints `msg` as a *fail message*.
    ///
    /// This does currently **not** print the fail message onto stderr, but stdout instead. That might change in a future version.
    pub fn fail(&mut self, msg: String) {
        if let Some(ref anim) = self.anim {
            anim.sender.send(ThrobberSignal::Fail(msg)).unwrap();
            anim.thread.thread().unpark();
        } else {
            println!("\x1B[2K\r✖ {}", msg);
        }
    }

    /// Ends the animation thread and drops the throbber object. If you want to stop the animation without dropping the throbber object, use [`finish`](Throbber::finish) instead.
    pub fn end(mut self) {
        if let Some(anim) = self.anim.take() {
            anim.sender.send(ThrobberSignal::End).unwrap();
            anim.thread.thread().unpark();
            anim.thread.join().unwrap();
        }
    }
}

fn animation_thread(
    receiver: Receiver<ThrobberSignal>,
    mut msg: String,
    mut frames: &'static [&'static str],
    mut interval: Duration,
) {
    let mut play_anim = true;
    let mut frame = 0;
    loop {
        match receiver.try_recv() {
            Ok(ThrobberSignal::Start) => {
                play_anim = true;
                continue;
            }
            Ok(ThrobberSignal::Finish) => {
                print!("\x1B[2K\r");
                std::io::stdout().flush().unwrap();
                play_anim = false;
                continue;
            }
            Ok(ThrobberSignal::Succ(succ_msg)) => {
                println!("\x1B[2K\r✔ {}", succ_msg);
                play_anim = false;
                continue;
            }
            Ok(ThrobberSignal::Fail(fail_msg)) => {
                println!("\x1B[2K\r✖ {}", fail_msg);
                play_anim = false;
                continue;
            }
            Ok(ThrobberSignal::ChMsg(new_msg)) => {
                msg = new_msg;
                continue;
            }
            Ok(ThrobberSignal::ChInt(new_dur)) => {
                interval = new_dur;
                continue;
            }
            Ok(ThrobberSignal::ChFrames(new_frames)) => {
                frames = new_frames;
                frame = 0;
                continue;
            }
            Ok(ThrobberSignal::End) => {
                break;
            }
            Err(TryRecvError::Disconnected) => {
                break;
            }
            Err(TryRecvError::Empty) => {
                if play_anim == false {
                    thread::park();
                    continue;
                }
            }
        }
        print!("\x1B[2K\r");
        print!("{} {}", frames[frame], msg);
        std::io::stdout().flush().unwrap();
        thread::sleep(interval);
        frame = (frame + 1) % frames.len();
    }
}