Skip to content

Commit ce0cb9f

Browse files
authored
refactor(oma-fetch)!: move render download message progress to outside (#579)
1 parent b8a1509 commit ce0cb9f

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

oma-fetch/src/download.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub enum BuilderError {
3939
pub(crate) struct SingleDownloader<'a> {
4040
client: &'a Client,
4141
pub entry: &'a DownloadEntry,
42-
progress: (usize, usize),
42+
total: usize,
4343
retry_times: usize,
4444
msg: Option<Cow<'static, str>>,
4545
download_list_index: usize,
@@ -98,7 +98,7 @@ impl<'a> SingleDownloader<'a> {
9898
pub(crate) fn new(
9999
client: &'a Client,
100100
entry: &'a DownloadEntry,
101-
progress: (usize, usize),
101+
total: usize,
102102
retry_times: usize,
103103
msg: Option<Cow<'static, str>>,
104104
download_list_index: usize,
@@ -114,7 +114,7 @@ impl<'a> SingleDownloader<'a> {
114114
Ok(Self {
115115
client,
116116
entry,
117-
progress,
117+
total,
118118
retry_times,
119119
msg,
120120
download_list_index,
@@ -296,10 +296,10 @@ impl<'a> SingleDownloader<'a> {
296296
}
297297
}
298298

299-
let msg = self.progress_msg();
300299
callback(Event::NewProgressSpinner {
301300
index: self.download_list_index,
302-
msg: msg.clone(),
301+
msg: self.download_message(),
302+
total: self.total,
303303
})
304304
.await;
305305

@@ -385,7 +385,8 @@ impl<'a> SingleDownloader<'a> {
385385

386386
callback(Event::NewProgressBar {
387387
index: self.download_list_index,
388-
msg,
388+
msg: self.download_message(),
389+
total: self.total,
389390
size: total_size,
390391
})
391392
.await;
@@ -566,14 +567,6 @@ impl<'a> SingleDownloader<'a> {
566567
req
567568
}
568569

569-
fn progress_msg(&self) -> String {
570-
let (count, len) = &self.progress;
571-
let msg = self.msg.as_deref().unwrap_or(&self.entry.filename);
572-
let msg = format!("({count}/{len}) {msg}");
573-
574-
msg
575-
}
576-
577570
/// Download local source file
578571
async fn download_local(
579572
&self,
@@ -582,7 +575,6 @@ impl<'a> SingleDownloader<'a> {
582575
callback: &impl AsyncFn(Event),
583576
) -> Result<bool, SingleDownloadError> {
584577
debug!("{:?}", self.entry);
585-
let msg = self.progress_msg();
586578

587579
let url = source.url.strip_prefix("file:").unwrap();
588580

@@ -612,7 +604,8 @@ impl<'a> SingleDownloader<'a> {
612604

613605
callback(Event::NewProgressBar {
614606
index: self.download_list_index,
615-
msg,
607+
total: self.total,
608+
msg: self.download_message(),
616609
size: total_size,
617610
})
618611
.await;
@@ -699,6 +692,13 @@ impl<'a> SingleDownloader<'a> {
699692

700693
Ok(())
701694
}
695+
696+
fn download_message(&self) -> String {
697+
self.msg
698+
.as_deref()
699+
.unwrap_or(&self.entry.filename)
700+
.to_string()
701+
}
702702
}
703703

704704
async fn checksum(

oma-fetch/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ pub enum Event {
156156
ProgressDone(usize),
157157
NewProgressSpinner {
158158
index: usize,
159+
total: usize,
159160
msg: String,
160161
},
161162
NewProgressBar {
162163
index: usize,
164+
total: usize,
163165
msg: String,
164166
size: u64,
165167
},
@@ -235,7 +237,7 @@ impl DownloadManager<'_> {
235237
.maybe_msg(msg)
236238
.download_list_index(i)
237239
.entry(c)
238-
.progress((i + 1, self.download_list.len()))
240+
.total(self.download_list.len())
239241
.retry_times(self.retry_times)
240242
.file_type(c.file_type)
241243
.timeout(self.timeout)

oma-refresh/src/sourceslist.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ impl MirrorSource<'_> {
388388

389389
callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner {
390390
index,
391-
msg: format!("({index}/{total}) {msg}"),
391+
total,
392+
msg,
392393
}))
393394
.await;
394395

@@ -497,12 +498,8 @@ impl MirrorSource<'_> {
497498

498499
callback(Event::DownloadEvent(oma_fetch::Event::NewProgressBar {
499500
index,
500-
msg: format!(
501-
"({}/{}) {}",
502-
index,
503-
total,
504-
self.get_human_download_message(Some(file_name)).unwrap(),
505-
),
501+
total,
502+
msg: self.get_human_download_message(Some(file_name)).unwrap(),
506503
size: total_size,
507504
}))
508505
.await;
@@ -556,7 +553,8 @@ impl MirrorSource<'_> {
556553

557554
callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner {
558555
index,
559-
msg: format!("({index}/{total}) {msg}"),
556+
total,
557+
msg,
560558
}))
561559
.await;
562560

src/pb.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,26 @@ impl OmaMultiProgressBar {
259259
pb.finish_and_clear();
260260
}
261261
}
262-
Event::NewProgressSpinner { index, msg } => {
262+
Event::NewProgressSpinner { index, total, msg } => {
263263
let (sty, inv) = spinner_style();
264264
let pb = self
265265
.mb
266266
.insert(index + 1, ProgressBar::new_spinner().with_style(sty));
267-
pb.set_message(msg);
267+
pb.set_message(format!("({}/{total}) {msg}", index + 1));
268268
pb.enable_steady_tick(inv);
269269
self.pb_map.insert(index + 1, pb);
270270
}
271-
Event::NewProgressBar { index, msg, size } => {
271+
Event::NewProgressBar {
272+
index,
273+
total,
274+
msg,
275+
size,
276+
} => {
272277
let sty = progress_bar_style(&WRITER);
273278
let pb = self
274279
.mb
275280
.insert(index + 1, ProgressBar::new(size).with_style(sty));
276-
pb.set_message(msg);
281+
pb.set_message(format!("({}/{total}) {msg}", index + 1));
277282
self.pb_map.insert(index + 1, pb);
278283
}
279284
Event::ProgressInc { index, size } => {

0 commit comments

Comments
 (0)