unwrap AppState::progress RefCell
This commit is contained in:
parent
72e9a5d40f
commit
14e1bed708
1 changed files with 14 additions and 11 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::{cell::RefCell, fmt, io, time::Duration};
|
||||
use std::{fmt, io, time::Duration};
|
||||
|
||||
use console::style;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
|
|
@ -12,7 +12,7 @@ use crate::{
|
|||
};
|
||||
|
||||
pub struct AppState {
|
||||
progress: RefCell<Option<ProgressBar>>,
|
||||
progress: Option<ProgressBar>,
|
||||
http: ureq::Agent,
|
||||
inner: CacheFile,
|
||||
}
|
||||
|
|
@ -35,7 +35,7 @@ fn new_http(timeout: Option<Duration>) -> ureq::Agent {
|
|||
impl AppState {
|
||||
fn new(http: ureq::Agent, inner: CacheFile) -> Self {
|
||||
Self {
|
||||
progress: RefCell::new(None),
|
||||
progress: None,
|
||||
http,
|
||||
inner,
|
||||
}
|
||||
|
|
@ -57,9 +57,8 @@ impl AppState {
|
|||
Ok(Self::new(http, CacheFile::from_args(args, share_id)))
|
||||
}
|
||||
|
||||
fn with_progressbar(&self, drop_bar: bool, f: impl FnOnce(&ProgressBar)) {
|
||||
let mut slot = self.progress.borrow_mut();
|
||||
let bar = &*slot.get_or_insert_with(|| {
|
||||
fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) {
|
||||
let bar = &*self.progress.get_or_insert_with(|| {
|
||||
ProgressBar::no_length().with_style(
|
||||
ProgressStyle::with_template(&format!(
|
||||
concat!(
|
||||
|
|
@ -89,12 +88,16 @@ impl AppState {
|
|||
f(bar);
|
||||
|
||||
if drop_bar {
|
||||
*slot = None;
|
||||
self.progress = None;
|
||||
}
|
||||
}
|
||||
|
||||
fn touch_progressbar(&self) {
|
||||
self.with_progressbar(false, |_| ());
|
||||
fn touch_progressbar(&mut self) {
|
||||
self.with_progressbar(|_| (), false);
|
||||
}
|
||||
|
||||
fn drop_progressbar(&mut self, f: impl FnOnce(&ProgressBar)) {
|
||||
self.with_progressbar(f, true);
|
||||
}
|
||||
|
||||
fn next_chunk<'t>(&mut self, buffer: &'t mut [u8]) -> sharry::Result<Option<Chunk<'t>>> {
|
||||
|
|
@ -119,7 +122,7 @@ impl AppState {
|
|||
fn is_done(&mut self) -> bool {
|
||||
if let Some(path) = self.inner.check_eof() {
|
||||
debug!("Finished {:?}!", path.display());
|
||||
self.with_progressbar(true, ProgressBar::finish);
|
||||
self.drop_progressbar(ProgressBar::finish);
|
||||
} else if self.inner.peek_uploading().is_some() {
|
||||
self.touch_progressbar();
|
||||
|
||||
|
|
@ -151,7 +154,7 @@ impl AppState {
|
|||
|
||||
pub fn abort_upload(&mut self) {
|
||||
self.inner.abort_upload();
|
||||
self.with_progressbar(true, ProgressBar::abandon);
|
||||
self.drop_progressbar(ProgressBar::abandon);
|
||||
}
|
||||
|
||||
pub fn rebuild_share(self, args: &Cli) -> sharry::Result<Self> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue