Skip to content

Commit

Permalink
支持显示下载进度条
Browse files Browse the repository at this point in the history
  • Loading branch information
bujnlc8 committed Aug 2, 2024
1 parent dac2c66 commit 3e93cf2
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 47 deletions.
70 changes: 65 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "phonerr"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "A CLI to query mobile phone region."
readme = "README.md"
Expand All @@ -18,9 +18,14 @@ clap = { version = "4.5.13", features = ["derive"] }
colored = "2.1.0"
tokio = { version = "1.39.2", features = ["full"] }
reqwest = { version = "0.12", features = ["rustls-tls", "charset", "json"], default-features=false}
indicatif = { version = "0.17.8", optional = true }

[profile.release]
lto = true
opt-level = "z"
panic = "abort"
strip = true

[features]
download-progress = ["indicatif"]
indicatif = ["dep:indicatif"]
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ test:


PLATFORMS:=x86_64-apple-darwin x86_64-unknown-linux-gnu x86_64-unknown-linux-musl aarch64-apple-darwin
VERSION:=0.1.1
VERSION:=0.1.2
CLI:=phoner
HOST_TRIPLE := $(shell rustc -vV | grep 'host:' | awk '{print $$2}')

all: $(PLATFORMS)

x86_64-apple-darwin:
cargo build --release --locked
cargo build --release --locked --features download-progress

x86_64-unknown-linux-musl:
TARGET_CC=x86_64-linux-musl-cc cargo build --release --target x86_64-unknown-linux-musl --locked
TARGET_CC=x86_64-linux-musl-cc cargo build --release --target x86_64-unknown-linux-musl --locked --features download-progress

x86_64-unknown-linux-gnu:
TARGET_CC=x86_64-linux-gnu-cc cargo build --release --target x86_64-unknown-linux-gnu --locked
TARGET_CC=x86_64-linux-gnu-cc cargo build --release --target x86_64-unknown-linux-gnu --locked --features download-progress

aarch64-apple-darwin:
cargo build --release --target aarch64-apple-darwin --locked
cargo build --release --target aarch64-apple-darwin --locked --features download-progress

artifacts:all
@mkdir -p artifacts/$(VERSION)
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

## 下载

- [⬇️ 点击下载 x86_64-apple-darwin](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_x86_64-apple-darwin.tar.gz)
- [⬇️ 点击下载 x86_64-apple-darwin](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_x86_64-apple-darwin.tar.gz)

> [phoner_x86_64-apple-darwin.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_x86_64-apple-darwin.tar.gz.md5)
> [phoner_x86_64-apple-darwin.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_x86_64-apple-darwin.tar.gz.md5)
- [⬇️ 点击下载 aarch64-apple-darwin](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_aarch64-apple-darwin.tar.gz)
- [⬇️ 点击下载 aarch64-apple-darwin](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_aarch64-apple-darwin.tar.gz)

> [phoner_aarch64-apple-darwin.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_aarch64-apple-darwin.tar.gz.md5)
> [phoner_aarch64-apple-darwin.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_aarch64-apple-darwin.tar.gz.md5)
- [⬇️ 点击下载 x86_64-unknown-linux-musl](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_x86_64-unknown-linux-musl.tar.gz)
- [⬇️ 点击下载 x86_64-unknown-linux-musl](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_x86_64-unknown-linux-musl.tar.gz)

> [phoner_x86_64-unknown-linux-musl.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_x86_64-unknown-linux-musl.tar.gz.md5)
> [phoner_x86_64-unknown-linux-musl.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_x86_64-unknown-linux-musl.tar.gz.md5)
- ~~[⬇️ 点击下载 x86_64-unknown-linux-gnu](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_x86_64-unknown-linux-gnu.tar.gz)~~
- ~~[⬇️ 点击下载 x86_64-unknown-linux-gnu](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_x86_64-unknown-linux-gnu.tar.gz)~~

> ~~[phoner_x86_64-unknown-linux-gnu.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.1/phoner_x86_64-unknown-linux-gnu.tar.gz.md5)~~
> ~~[phoner_x86_64-unknown-linux-gnu.tar.gz.md5](https://github.com/bujnlc8/phoner/releases/download/0.1.2/phoner_x86_64-unknown-linux-gnu.tar.gz.md5)~~
请根据你的操作系统下载相应的版本,可对比 md5 hash 值确定是否下载了最新的版本

Expand All @@ -37,7 +37,7 @@ sudo spctl --master-disable
## 编译

```
cargo build --release --locked
cargo build --release --locked [--features download-progress]
```

**如果在使用过程中发现 bug,欢迎反馈 👏**
50 changes: 33 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pub mod util;

use std::{fmt::Debug, os::unix::fs::MetadataExt, path::PathBuf};
use std::{cmp::Ordering, fmt::Debug, os::unix::fs::MetadataExt, path::PathBuf};

use anyhow::anyhow;
use colored::Colorize;
use tokio::{
fs::{self, File},
io::{AsyncReadExt, AsyncSeekExt},
};
use util::{download_file, replace_home, u8_i32};
use util::{download_file, download_file_silent, replace_home, u8_i32};

#[derive(Debug)]
pub struct PhoneData {
Expand Down Expand Up @@ -120,21 +120,33 @@ impl PhoneData {
replace_home("~/.cache/phoner/phone.dat")
}

pub async fn download_file(&self, download_url: Option<String>) -> Result<(), anyhow::Error> {
pub async fn download_file(
&self,
download_url: Option<String>,
silent: bool,
) -> Result<(), anyhow::Error> {
if !self.data_path.parent().unwrap().exists() {
fs::create_dir_all(self.data_path.parent().unwrap()).await?;
}
download_file(
download_url.unwrap_or(PHONE_DATA_URL.to_string()).as_str(),
&PathBuf::from(Self::get_data_path()),
)
.await?;
if silent {
download_file_silent(
download_url.unwrap_or(PHONE_DATA_URL.to_string()).as_str(),
&PathBuf::from(Self::get_data_path()),
)
.await?;
} else {
download_file(
download_url.unwrap_or(PHONE_DATA_URL.to_string()).as_str(),
&PathBuf::from(Self::get_data_path()),
)
.await?;
}
Ok(())
}

async fn init(&mut self) -> Result<(), anyhow::Error> {
if !self.data_path.exists() {
self.download_file(None).await?;
self.download_file(None, true).await?;
}
let mut file = File::open(self.data_path.clone()).await?;
// 读取头部8个字节 版本号, 第一个索引的偏移
Expand Down Expand Up @@ -174,16 +186,20 @@ impl PhoneData {
// 前4位为手机号
let prefix = &self.index[mid * 9..(mid * 9 + 4)];
let prefix = u8_i32(prefix);
if prefix > phone_prefix {
if mid == 0 {
match prefix.cmp(&phone_prefix) {
Ordering::Greater => {
if mid == 0 {
break;
}
end = mid - 1;
}
Ordering::Equal => {
position = Some(mid);
break;
}
end = mid - 1;
} else if prefix == phone_prefix {
position = Some(mid);
break;
} else {
start = mid + 1;
Ordering::Less => {
start = mid + 1;
}
}
if start > end {
break;
Expand Down
20 changes: 12 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@ async fn main() -> Result<(), anyhow::Error> {
}
let mut client = PhoneData::new(None);
if cli.update {
#[cfg(not(feature = "download-progress"))]
let wait = wait_blink("下载中,请稍候⏬...", 3);
client.download_file(cli.update_url).await?;
wait.sender.send(true).unwrap();
wait.handle.await?;
println!(
"{} {}",
"下载完成 ✅".green(),
format!("{}ms elapsed.", start.elapsed().as_millis()).bright_black()
);
client.download_file(cli.update_url, false).await?;
#[cfg(not(feature = "download-progress"))]
{
wait.sender.send(true).unwrap();
wait.handle.await?;
println!(
"{} {}",
"下载完成 ✅".green(),
format!("{}ms elapsed.", start.elapsed().as_millis()).bright_black()
);
}
return Ok(());
}
if cli.info {
Expand Down
Loading

0 comments on commit 3e93cf2

Please sign in to comment.