move AppState::is_done into AppState::upload_chunk

This commit is contained in:
Jörn-Michael Miehe 2025-06-24 01:53:27 +00:00
parent 14e1bed708
commit 005c5f7cfa

View file

@ -1,7 +1,7 @@
use std::{fmt, io, time::Duration}; use std::{fmt, io, time::Duration};
use console::style; use console::style;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use log::{debug, warn}; use log::{debug, warn};
use crate::{ use crate::{
@ -59,7 +59,7 @@ impl AppState {
fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) { fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) {
let bar = &*self.progress.get_or_insert_with(|| { let bar = &*self.progress.get_or_insert_with(|| {
ProgressBar::no_length().with_style( ProgressBar::hidden().with_style(
ProgressStyle::with_template(&format!( ProgressStyle::with_template(&format!(
concat!( concat!(
"{{bar:50.cyan/blue}} {{msg:.magenta}}: ", "{{bar:50.cyan/blue}} {{msg:.magenta}}: ",
@ -74,6 +74,7 @@ impl AppState {
if let Some(upl) = self.inner.peek_uploading() { if let Some(upl) = self.inner.peek_uploading() {
if bar.length().is_none() { if bar.length().is_none() {
bar.set_draw_target(ProgressDrawTarget::stderr());
bar.set_length(upl.get_size()); bar.set_length(upl.get_size());
bar.set_message(upl.get_name().to_owned()); bar.set_message(upl.get_name().to_owned());
bar.enable_steady_tick(Duration::from_millis(100)); bar.enable_steady_tick(Duration::from_millis(100));
@ -119,19 +120,6 @@ impl AppState {
Ok(Some(chunk)) Ok(Some(chunk))
} }
fn is_done(&mut self) -> bool {
if let Some(path) = self.inner.check_eof() {
debug!("Finished {:?}!", path.display());
self.drop_progressbar(ProgressBar::finish);
} else if self.inner.peek_uploading().is_some() {
self.touch_progressbar();
return false;
}
self.inner.queue_empty()
}
pub fn upload_chunk(&mut self, buffer: &mut [u8]) -> sharry::Result<bool> { pub fn upload_chunk(&mut self, buffer: &mut [u8]) -> sharry::Result<bool> {
let Some(chunk) = self.next_chunk(buffer)? else { let Some(chunk) = self.next_chunk(buffer)? else {
self.inner self.inner
@ -143,7 +131,14 @@ impl AppState {
self.inner.file_patch(&self.http, &chunk)?; self.inner.file_patch(&self.http, &chunk)?;
Ok(self.is_done()) self.touch_progressbar();
if let Some(path) = self.inner.check_eof() {
debug!("Finished {:?}!", path.display());
self.drop_progressbar(ProgressBar::finish);
}
Ok(self.inner.peek_uploading().is_none() && self.inner.queue_empty())
} }
pub fn rewind_chunk(mut self) -> Option<Self> { pub fn rewind_chunk(mut self) -> Option<Self> {