Skip to content

Commit 90233e1

Browse files
committed
feat: add progress display during updates
1 parent c59368d commit 90233e1

6 files changed

Lines changed: 38 additions & 21 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ shellexpand = "3.1.2"
2020
thiserror = "2.0.18"
2121
toml = "1.0.7"
2222
ureq = "3.3.0"
23-
zsync-rs = "0.1.0"
23+
zsync-rs = "0.1.1"
2424

2525
[profile.release]
2626
opt-level = "z"

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod config;
33
pub mod error;
44
pub mod update_info;
55
pub mod updater;
6+
pub mod util;
67

78
pub use error::Error;
89
pub use updater::{UpdateStats, Updater};

src/main.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs;
33
use std::path::PathBuf;
44

55
use appimageupdate::config;
6+
use appimageupdate::util::format_size;
67
use appimageupdate::{Error, Updater};
78
use clap::Parser;
89

@@ -51,22 +52,6 @@ fn main() {
5152
}
5253
}
5354

54-
fn format_size(bytes: u64) -> String {
55-
const KB: u64 = 1024;
56-
const MB: u64 = 1024 * KB;
57-
const GB: u64 = 1024 * MB;
58-
59-
if bytes >= GB {
60-
format!("{:.1} GB", bytes as f64 / GB as f64)
61-
} else if bytes >= MB {
62-
format!("{:.1} MB", bytes as f64 / MB as f64)
63-
} else if bytes >= KB {
64-
format!("{:.1} KB", bytes as f64 / KB as f64)
65-
} else {
66-
format!("{} B", bytes)
67-
}
68-
}
69-
7055
fn run(cli: Cli) -> Result<(), Error> {
7156
if !cli.github_api_proxy.is_empty() {
7257
config::set_proxies(cli.github_api_proxy.clone());

src/updater.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use zsync_rs::{ControlFile, ZsyncAssembly};
66
use crate::appimage::AppImage;
77
use crate::error::{Error, Result};
88
use crate::update_info::UpdateInfo;
9+
use crate::util::format_size;
910

1011
struct UpdateContext {
1112
source_size: u64,
@@ -238,10 +239,20 @@ impl Updater {
238239
zsync_url: &str,
239240
ctx: &UpdateContext,
240241
) -> Result<UpdateStats> {
241-
let assembly = ZsyncAssembly::from_url(zsync_url, output_path)
242+
let mut assembly = ZsyncAssembly::from_url(zsync_url, output_path)
242243
.map_err(|e| Error::Zsync(format!("Failed to initialize zsync: {}", e)))?;
243244

244-
let mut assembly = assembly;
245+
assembly.set_progress_callback(|done, total| {
246+
let percent = total.checked_div(100).map(|p| done / p).unwrap_or(0);
247+
print!(
248+
"\rProgress: {:3}% ({}/{})",
249+
percent,
250+
format_size(done),
251+
format_size(total)
252+
);
253+
use std::io::Write;
254+
std::io::stdout().flush().ok();
255+
});
245256

246257
let blocks_reused = assembly
247258
.submit_source_file(source_path)
@@ -256,6 +267,8 @@ impl Updater {
256267
.download_missing_blocks()
257268
.map_err(|e| Error::Zsync(format!("Failed to download blocks: {}", e)))?;
258269

270+
println!();
271+
259272
assembly
260273
.complete()
261274
.map_err(|e| Error::Zsync(format!("Failed to complete assembly: {}", e)))?;

src/util.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub fn format_size(bytes: u64) -> String {
2+
const KB: u64 = 1024;
3+
const MB: u64 = 1024 * KB;
4+
const GB: u64 = 1024 * MB;
5+
const TB: u64 = 1024 * GB;
6+
7+
if bytes >= TB {
8+
format!("{:.1} TB", bytes as f64 / TB as f64)
9+
} else if bytes >= GB {
10+
format!("{:.1} GB", bytes as f64 / GB as f64)
11+
} else if bytes >= MB {
12+
format!("{:.1} MB", bytes as f64 / MB as f64)
13+
} else if bytes >= KB {
14+
format!("{:.1} KB", bytes as f64 / KB as f64)
15+
} else {
16+
format!("{} B", bytes)
17+
}
18+
}

0 commit comments

Comments
 (0)