diff --git a/src/appstate.rs b/src/appstate.rs index 19eb63e..078d04f 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -1,13 +1,13 @@ use std::{fmt, io, time::Duration}; -use console::style; -use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; +use indicatif::{ProgressBar, ProgressDrawTarget}; use log::{debug, warn}; use crate::{ cachefile::CacheFile, cli::Cli, file::{Chunk, FileTrait}, + output::new_progressbar, sharry::{self, Client}, }; @@ -32,20 +32,6 @@ fn new_http(timeout: Option) -> ureq::Agent { .into() } -fn new_progressbar() -> ProgressBar { - ProgressBar::hidden().with_style( - ProgressStyle::with_template(&format!( - concat!( - "{{bar:50.cyan/blue}} {{msg:.magenta}}: ", - "{{binary_bytes:.yellow}}{}{{binary_total_bytes:.yellow}} ", - "({{eta}})", - ), - style("/").magenta(), - )) - .expect("invalid style template"), - ) -} - impl AppState { fn new(http: ureq::Agent, inner: CacheFile) -> Self { Self { @@ -60,38 +46,17 @@ impl AppState { .inspect_err(|e| debug!("could not resume from hash {:?}: {e}", args.get_hash())) .ok()?; + inner.peek_uploading(); + Some(Self::new(new_http(args.get_timeout()), inner)) } pub fn from_args(args: &Cli) -> sharry::Result { - let mut files = args.files.clone(); - - // TODO CLI switch begin - - let bar = new_progressbar(); - bar.set_draw_target(ProgressDrawTarget::stderr()); - // BOOKMARK assumption: total file size < 2 EiB - bar.set_length(files.iter().map(|f| f.get_size()).sum()); - bar.enable_steady_tick(Duration::from_millis(50)); - - for chk in &mut files { - bar.set_message(format!("hashing {:?}", chk.get_name())); - chk.hash(|bytes| bar.inc(bytes))?; - debug!("{chk:?}"); - } - - bar.finish(); - - // TODO CLI switch end - let http = new_http(args.get_timeout()); let share_id = http.share_create(&args.get_uri(), &args.alias, args.get_share_request())?; - Ok(Self::new( - http, - CacheFile::from_args(args, share_id).replace_files(files), - )) + Ok(Self::new(http, CacheFile::from_args(args, share_id)?)) } fn with_progressbar(&mut self, f: impl FnOnce(&ProgressBar), drop_bar: bool) { @@ -179,7 +144,7 @@ impl AppState { self.http .share_create(&args.get_uri(), &args.alias, args.get_share_request())?; - Ok(Self::new(self.http, CacheFile::from_args(args, share_id))) + Ok(Self::new(self.http, CacheFile::from_args(args, share_id)?)) } pub fn save(&self) -> io::Result<()> { diff --git a/src/cachefile.rs b/src/cachefile.rs index 1aed7bb..f595a29 100644 --- a/src/cachefile.rs +++ b/src/cachefile.rs @@ -3,14 +3,17 @@ use std::{ fs, io::{self, Write}, path::PathBuf, + time::Duration, }; -use log::trace; +use indicatif::ProgressDrawTarget; +use log::{debug, trace}; use serde::{Deserialize, Serialize}; use crate::{ cli::Cli, - file::{self, Chunk}, + file::{self, Chunk, FileTrait}, + output::new_progressbar, sharry::{self, Client, Uri}, }; @@ -56,22 +59,35 @@ impl CacheFile { Ok(Self { file_name, ..state }) } - pub fn from_args(args: &Cli, share_id: String) -> Self { - Self { + pub fn from_args(args: &Cli, share_id: String) -> io::Result { + // TODO CLI switch begin + + let mut files = args.files.clone(); + + let bar = new_progressbar(); + bar.set_draw_target(ProgressDrawTarget::stderr()); + // BOOKMARK assumption: total file size < 2 EiB + bar.set_length(files.iter().map(|f| f.get_size()).sum()); + bar.enable_steady_tick(Duration::from_millis(50)); + + for chk in &mut files { + bar.set_message(format!("hashing {:?}", chk.get_name())); + chk.hash(|bytes| bar.inc(bytes))?; + debug!("{chk:?}"); + } + + bar.finish_with_message("finished hashing files"); + + // TODO CLI switch end + + Ok(Self { file_name: Self::cache_file(args), uri: args.get_uri(), alias_id: args.alias.clone(), share_id, uploading: None, - files: args.files.clone().into(), - } - } - - pub fn replace_files(self, files: Vec) -> Self { - Self { files: files.into(), - ..self - } + }) } pub fn queue_empty(&self) -> bool { diff --git a/src/output.rs b/src/output.rs index 0ca1038..73bd80c 100644 --- a/src/output.rs +++ b/src/output.rs @@ -2,6 +2,7 @@ use std::{fmt, process, sync::LazyLock}; use console::{StyledObject, style}; use dialoguer::{Select, theme::ColorfulTheme}; +use indicatif::{ProgressBar, ProgressStyle}; use log::{error, info}; use crate::sharry; @@ -43,6 +44,20 @@ where strs.iter().map(|&s| f(style(s)).to_string()).collect() } +pub fn new_progressbar() -> ProgressBar { + ProgressBar::hidden().with_style( + ProgressStyle::with_template(&format!( + concat!( + "{{bar:50.cyan/blue}} {{msg:.magenta}}: ", + "{{binary_bytes:.yellow}}{}{{binary_total_bytes:.yellow}} ", + "({{eta}})", + ), + style("/").magenta(), + )) + .expect("invalid style template"), + ) +} + pub enum Log {} impl Log {