term: Add configurable output stream for `Spinner`

This commit is contained in:
Alexis Sellier 2023-05-22 11:38:25 +02:00
parent 45a4578724
commit cfc386ed0e
No known key found for this signature in database
2 changed files with 14 additions and 4 deletions

View File

@ -22,7 +22,7 @@ pub use inquire::ui::Styled;
pub use io::*;
pub use is_terminal::is_terminal;
pub use label::{label, Label};
pub use spinner::{spinner, Spinner};
pub use spinner::{spinner, spinner_to, Spinner};
pub use table::Table;
pub use textarea::{textarea, TextArea};
pub use vstack::{VStack, VStackOptions};

View File

@ -102,16 +102,26 @@ impl Spinner {
}
}
/// Create a new spinner with the given message.
/// Create a new spinner with the given message. Sends animation output to `stderr` and success or
/// failure messages to `stdout`.
pub fn spinner(message: impl ToString) -> Spinner {
spinner_to(message, io::stdout(), io::stderr())
}
/// Create a new spinner with the given message, and send output to the given writers.
pub fn spinner_to(
message: impl ToString,
completion: impl io::Write + Send + 'static,
animation: impl io::Write + Send + 'static,
) -> Spinner {
let message = message.to_string();
let progress = Arc::new(Mutex::new(Progress::new(Paint::new(message))));
let handle = thread::spawn({
let progress = progress.clone();
move || {
let mut stdout = io::stdout();
let mut stderr = termion::cursor::HideCursor::from(io::stderr());
let mut stdout = completion;
let mut stderr = termion::cursor::HideCursor::from(animation);
loop {
let Ok(mut progress) = progress.lock() else {