From 6b143f5f8f3ae21a96929abcff9d9ec644cd4c1c Mon Sep 17 00:00:00 2001 From: cloudhead Date: Thu, 3 Aug 2023 13:14:06 +0200 Subject: [PATCH] term: Some updates to the spinner code This ensures we don't output progress messages on a non-TTY. --- radicle-remote-helper/src/push.rs | 12 +++++++----- radicle-term/src/spinner.rs | 25 ++++++++++++------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/radicle-remote-helper/src/push.rs b/radicle-remote-helper/src/push.rs index fca88855..30875bf4 100644 --- a/radicle-remote-helper/src/push.rs +++ b/radicle-remote-helper/src/push.rs @@ -1,4 +1,5 @@ use std::collections::HashSet; +use std::io::IsTerminal; use std::path::Path; use std::str::FromStr; 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."); return Ok(()); } - let mut spinner = cli::spinner_to( - format!("Syncing with {} node(s)..", connected.len()), - io::stderr(), - io::stderr(), - ); + let message = format!("Syncing with {} node(s)..", connected.len()); + let mut spinner = if io::stderr().is_terminal() { + cli::spinner_to(message, 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 { node::AnnounceEvent::Announced => {} node::AnnounceEvent::RefsSynced { remote } => { diff --git a/radicle-term/src/spinner.rs b/radicle-term/src/spinner.rs index 6fe3b359..225d6f8e 100644 --- a/radicle-term/src/spinner.rs +++ b/radicle-term/src/spinner.rs @@ -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. pub fn spinner_to( message: impl ToString, - completion: impl io::Write + Send + 'static, + mut completion: impl io::Write + Send + 'static, animation: impl io::Write + Send + 'static, ) -> Spinner { let message = message.to_string(); @@ -122,13 +122,12 @@ pub fn spinner_to( let progress = progress.clone(); move || { - let mut stdout = completion; - let mut stderr = termion::cursor::HideCursor::from(animation); + let mut animation = termion::cursor::HideCursor::from(animation); loop { let Ok(mut progress) = progress.lock() else { - break; - }; + break; + }; match &mut *progress { Progress { state: State::Running { cursor }, @@ -137,14 +136,14 @@ pub fn spinner_to( let spinner = DEFAULT_STYLE[*cursor]; write!( - stderr, + animation, "{}{}{spinner} {message}", termion::cursor::Save, termion::clear::AfterCursor, ) .ok(); - write!(stderr, "{}", termion::cursor::Restore).ok(); + write!(animation, "{}", termion::cursor::Restore).ok(); *cursor += 1; *cursor %= DEFAULT_STYLE.len(); @@ -153,17 +152,17 @@ pub fn spinner_to( state: State::Done, message, } => { - write!(stderr, "{}", termion::clear::AfterCursor).ok(); - writeln!(stdout, "{} {message}", Paint::green("✓")).ok(); + write!(animation, "{}", termion::clear::AfterCursor).ok(); + writeln!(completion, "{} {message}", Paint::green("✓")).ok(); break; } Progress { state: State::Canceled, message, } => { - write!(stderr, "{}", termion::clear::AfterCursor).ok(); + write!(animation, "{}", termion::clear::AfterCursor).ok(); writeln!( - stdout, + completion, "{ERROR_PREFIX} {message} {}", Paint::red("") ) @@ -174,14 +173,14 @@ pub fn spinner_to( state: State::Warn, message, } => { - writeln!(stdout, "{WARNING_PREFIX} {message}").ok(); + writeln!(completion, "{WARNING_PREFIX} {message}").ok(); break; } Progress { state: State::Error, message, } => { - writeln!(stdout, "{ERROR_PREFIX} {message}").ok(); + writeln!(completion, "{ERROR_PREFIX} {message}").ok(); break; } }