term: Some updates to the spinner code

This ensures we don't output progress messages on a non-TTY.
This commit is contained in:
cloudhead 2023-08-03 13:14:06 +02:00
parent c30f137bb1
commit 6b143f5f8f
No known key found for this signature in database
2 changed files with 19 additions and 18 deletions

View File

@ -1,4 +1,5 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::io::IsTerminal;
use std::path::Path; use std::path::Path;
use std::str::FromStr; use std::str::FromStr;
use std::time; use std::time;
@ -535,11 +536,12 @@ fn sync(rid: Id, mut node: radicle::Node) -> Result<(), radicle::node::Error> {
eprintln!("Not connected to any seeds."); eprintln!("Not connected to any seeds.");
return Ok(()); return Ok(());
} }
let mut spinner = cli::spinner_to( let message = format!("Syncing with {} node(s)..", connected.len());
format!("Syncing with {} node(s)..", connected.len()), let mut spinner = if io::stderr().is_terminal() {
io::stderr(), cli::spinner_to(message, io::stderr(), io::stderr())
io::stderr(), } else {
); cli::spinner_to(message, io::stderr(), io::sink())
};
let result = node.announce(rid, connected, DEFAULT_SYNC_TIMEOUT, |event| match event { let result = node.announce(rid, connected, DEFAULT_SYNC_TIMEOUT, |event| match event {
node::AnnounceEvent::Announced => {} node::AnnounceEvent::Announced => {}
node::AnnounceEvent::RefsSynced { remote } => { node::AnnounceEvent::RefsSynced { remote } => {

View File

@ -111,7 +111,7 @@ pub fn spinner(message: impl ToString) -> Spinner {
/// Create a new spinner with the given message, and send output to the given writers. /// Create a new spinner with the given message, and send output to the given writers.
pub fn spinner_to( pub fn spinner_to(
message: impl ToString, message: impl ToString,
completion: impl io::Write + Send + 'static, mut completion: impl io::Write + Send + 'static,
animation: impl io::Write + Send + 'static, animation: impl io::Write + Send + 'static,
) -> Spinner { ) -> Spinner {
let message = message.to_string(); let message = message.to_string();
@ -122,13 +122,12 @@ pub fn spinner_to(
let progress = progress.clone(); let progress = progress.clone();
move || { move || {
let mut stdout = completion; let mut animation = termion::cursor::HideCursor::from(animation);
let mut stderr = termion::cursor::HideCursor::from(animation);
loop { loop {
let Ok(mut progress) = progress.lock() else { let Ok(mut progress) = progress.lock() else {
break; break;
}; };
match &mut *progress { match &mut *progress {
Progress { Progress {
state: State::Running { cursor }, state: State::Running { cursor },
@ -137,14 +136,14 @@ pub fn spinner_to(
let spinner = DEFAULT_STYLE[*cursor]; let spinner = DEFAULT_STYLE[*cursor];
write!( write!(
stderr, animation,
"{}{}{spinner} {message}", "{}{}{spinner} {message}",
termion::cursor::Save, termion::cursor::Save,
termion::clear::AfterCursor, termion::clear::AfterCursor,
) )
.ok(); .ok();
write!(stderr, "{}", termion::cursor::Restore).ok(); write!(animation, "{}", termion::cursor::Restore).ok();
*cursor += 1; *cursor += 1;
*cursor %= DEFAULT_STYLE.len(); *cursor %= DEFAULT_STYLE.len();
@ -153,17 +152,17 @@ pub fn spinner_to(
state: State::Done, state: State::Done,
message, message,
} => { } => {
write!(stderr, "{}", termion::clear::AfterCursor).ok(); write!(animation, "{}", termion::clear::AfterCursor).ok();
writeln!(stdout, "{} {message}", Paint::green("")).ok(); writeln!(completion, "{} {message}", Paint::green("")).ok();
break; break;
} }
Progress { Progress {
state: State::Canceled, state: State::Canceled,
message, message,
} => { } => {
write!(stderr, "{}", termion::clear::AfterCursor).ok(); write!(animation, "{}", termion::clear::AfterCursor).ok();
writeln!( writeln!(
stdout, completion,
"{ERROR_PREFIX} {message} {}", "{ERROR_PREFIX} {message} {}",
Paint::red("<canceled>") Paint::red("<canceled>")
) )
@ -174,14 +173,14 @@ pub fn spinner_to(
state: State::Warn, state: State::Warn,
message, message,
} => { } => {
writeln!(stdout, "{WARNING_PREFIX} {message}").ok(); writeln!(completion, "{WARNING_PREFIX} {message}").ok();
break; break;
} }
Progress { Progress {
state: State::Error, state: State::Error,
message, message,
} => { } => {
writeln!(stdout, "{ERROR_PREFIX} {message}").ok(); writeln!(completion, "{ERROR_PREFIX} {message}").ok();
break; break;
} }
} }