diff --git a/src/appstate.rs b/src/appstate.rs index 26bf7aa..223f799 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -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>, + progress: Option, http: ureq::Agent, inner: CacheFile, } @@ -35,7 +35,7 @@ fn new_http(timeout: Option) -> 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>> { @@ -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 {