Skip to content

Commit 8b771fd

Browse files
authored
feat: added spinner in snm {install,lts,latest} (#66)
* feat: added spinner in snm {install,lts,latest} * stop snipper if version is not found
1 parent 62d67f5 commit 8b771fd

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

src/cmd/install.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::fetcher::Fetcher;
21
use crate::version::{NodeVersion, Version};
32
use crate::{config::Config, downloader::Downloader};
3+
use crate::{fetcher::Fetcher, progress_bar::Spinner};
44
use clap::Clap;
55
use colored::*;
66

@@ -23,6 +23,8 @@ impl super::Command for Install {
2323
);
2424
}
2525

26+
let spnr = Spinner::fetch();
27+
2628
let (release, is_lts) = match &self.version {
2729
Version::Full(NodeVersion::Lts(lts)) => {
2830
(Fetcher::fetch(&config.dist_mirror)?.lts_name(lts), true)
@@ -37,7 +39,7 @@ impl super::Command for Install {
3739
Some(r) => {
3840
let dwnld = Downloader::new(&r, &config);
3941

40-
let dest = dwnld.download()?;
42+
let dest = dwnld.download(&spnr)?;
4143

4244
if is_lts {
4345
let alias = self.version.to_string();
@@ -51,10 +53,14 @@ impl super::Command for Install {
5153

5254
Ok(())
5355
}
54-
_ => anyhow::bail!(
55-
"No release found with the version {}",
56-
&self.version.to_string().bold()
57-
),
56+
_ => {
57+
spnr.stop();
58+
59+
anyhow::bail!(
60+
"No release found with the version {}",
61+
&self.version.to_string().bold()
62+
);
63+
}
5864
}
5965
}
6066
}

src/cmd/latest.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::fetcher::Fetcher;
21
use crate::{config::Config, downloader::Downloader};
2+
use crate::{fetcher::Fetcher, progress_bar::Spinner};
33
use clap::Clap;
44
use colored::*;
55

@@ -12,11 +12,13 @@ impl super::Command for Latest {
1212
type InitResult = ();
1313

1414
fn init(&self, config: Config) -> anyhow::Result<Self::InitResult> {
15+
let spnr = Spinner::fetch();
16+
1517
let releases = Fetcher::fetch(&config.dist_mirror)?;
1618
let release = releases.latest()?;
1719

1820
let dwnld = Downloader::new(release, &config);
19-
let dest = dwnld.download()?;
21+
let dest = dwnld.download(&spnr)?;
2022

2123
crate::symlink::symlink_to(&dest, &config.alias_dir().join(&ALIAS))?;
2224

src/cmd/lts.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::fetcher::Fetcher;
21
use crate::{config::Config, downloader::Downloader};
2+
use crate::{fetcher::Fetcher, progress_bar::Spinner};
33
use clap::Clap;
44
use colored::*;
55

@@ -12,11 +12,13 @@ impl super::Command for Lts {
1212
type InitResult = ();
1313

1414
fn init(&self, config: Config) -> anyhow::Result<Self::InitResult> {
15+
let spnr = Spinner::fetch();
16+
1517
let releases = Fetcher::fetch(&config.dist_mirror)?;
1618
let release = releases.lts()?;
1719

1820
let dwnld = Downloader::new(release, &config);
19-
let dest = dwnld.download()?;
21+
let dest = dwnld.download(&spnr)?;
2022

2123
crate::symlink::symlink_to(&dest, &config.alias_dir().join(&ALIAS))?;
2224

src/downloader.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::fetcher::Release;
21
use crate::symlink::symlink_to;
32
use crate::{archive::Archive, progress_bar::Bar};
43
use crate::{config::Config, url};
4+
use crate::{fetcher::Release, progress_bar::Spinner};
55
use colored::*;
66
use indicatif::HumanBytes;
77
use std::path::{Path, PathBuf};
@@ -21,7 +21,7 @@ impl<'a> Downloader<'a> {
2121
}
2222
}
2323

24-
pub fn download(&self) -> anyhow::Result<PathBuf> {
24+
pub fn download(&self, spnr: &Spinner) -> anyhow::Result<PathBuf> {
2525
let v = &self.release.version;
2626
let v_str = v.to_string();
2727

@@ -32,7 +32,13 @@ impl<'a> Downloader<'a> {
3232
}
3333

3434
let dist = url::release(&self.config.dist_mirror, &v);
35+
36+
spnr.update_msg("Checking version...".to_string());
37+
3538
let res = ureq::get(&dist.url).call()?;
39+
40+
spnr.stop();
41+
3642
let len = res
3743
.header("Content-Length")
3844
.and_then(|x| x.parse::<u64>().ok());

src/progress_bar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Spinner {
9090
self.spinner.finish_and_clear();
9191
}
9292

93-
// pub fn update_msg(&self, msg: String) {
94-
// self.spinner.set_message(msg);
95-
// }
93+
pub fn update_msg(&self, msg: String) {
94+
self.spinner.set_message(msg);
95+
}
9696
}

0 commit comments

Comments
 (0)