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
//! 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.
//!
//! # Usage
//! First, add this to your Cargo.toml
//! ```toml
//! [dependencies]
//! throbber = "0.1"
//! ```

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

/// `⠋   ⠙   ⠹   ⠸   ⠼   ⠴   ⠦   ⠧   ⠇   ⠏`
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 strart, succeed, fail or end at any point.
///
/// Note that a call to `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` object. This is done because multiple calls to start do not actually create multiple threads, instead a call to a *finish function* (like `succeed()`) simply parks the thread and a following `start()` call unparks that thread again. As such, a call to `end()` kills the thread entirely. If you want to just stop the animation, but potentially start it again later on, use `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.
    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()` was called before. To change the message after `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()` was called before. To change the interval after `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()` was called before. To change the animation frames after `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()` 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()` 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()` 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(), 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*.
    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);
        }
    }

    /// Encds 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();
    }
}