Skip to content

Commit

Permalink
refactor: improve spinner (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Jun 26, 2024
1 parent 2e1dc1f commit 03b4003
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::{
fs::{create_dir_all, read_dir, read_to_string, remove_file, File, OpenOptions},
io::Write,
path::{Path, PathBuf},
process::exit,
process,
sync::Arc,
};
use syntect::highlighting::ThemeSet;
Expand Down Expand Up @@ -1529,7 +1529,7 @@ fn create_config_file(config_path: &Path) -> Result<()> {
.with_default(true)
.prompt()?;
if !ans {
exit(0);
process::exit(0);
}

let client = Select::new("Platform:", list_client_types()).prompt()?;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/abort_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ pub async fn watch_abort_signal(abort_signal: AbortSignal) {
if abort_signal.aborted() {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
tokio::time::sleep(std::time::Duration::from_millis(25)).await;
}
}
20 changes: 13 additions & 7 deletions src/utils/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use crossterm::{cursor, queue, style, terminal};
use is_terminal::IsTerminal;
use std::{
io::{stdout, Stdout, Write},
io::{stdout, Write},
time::Duration,
};
use tokio::{
Expand Down Expand Up @@ -31,14 +31,20 @@ impl Spinner {
self.message = format!(" {message}");
}

pub fn step(&mut self, writer: &mut Stdout) -> Result<()> {
pub fn step(&mut self) -> Result<()> {
if self.stopped {
return Ok(());
}
let mut writer = stdout();
let frame = Self::DATA[self.index % Self::DATA.len()];
let dots = ".".repeat((self.index / 5) % 4);
let line = format!("{frame}{}{:<3}", self.message, dots);
queue!(writer, cursor::MoveToColumn(0), style::Print(line),)?;
queue!(
writer,
cursor::MoveToColumn(0),
terminal::Clear(terminal::ClearType::FromCursorDown),
style::Print(line),
)?;
if self.index == 0 {
queue!(writer, cursor::Hide)?;
}
Expand All @@ -47,10 +53,11 @@ impl Spinner {
Ok(())
}

pub fn stop(&mut self, writer: &mut Stdout) -> Result<()> {
pub fn stop(&mut self) -> Result<()> {
if self.stopped {
return Ok(());
}
let mut writer = stdout();
self.stopped = true;
queue!(
writer,
Expand All @@ -76,7 +83,6 @@ async fn run_spinner_inner(
stop_rx: oneshot::Receiver<()>,
mut message_rx: mpsc::UnboundedReceiver<String>,
) -> Result<()> {
let mut writer = stdout();
let is_stdout_terminal = stdout().is_terminal();
let mut spinner = Spinner::new(&message);
let mut interval = interval(Duration::from_millis(50));
Expand All @@ -86,7 +92,7 @@ async fn run_spinner_inner(
tokio::select! {
_ = interval.tick() => {
if is_stdout_terminal {
let _ = spinner.step(&mut writer);
let _ = spinner.step();
}
}
message = message_rx.recv() => {
Expand All @@ -99,7 +105,7 @@ async fn run_spinner_inner(
} => {}
_ = stop_rx => {
if is_stdout_terminal {
spinner.stop(&mut writer)?;
spinner.stop()?;
}
}
}
Expand Down

0 comments on commit 03b4003

Please sign in to comment.