Skip to content

Commit

Permalink
add a progress bar when copying
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Mar 9, 2020
1 parent e37e780 commit 394b709
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
60 changes: 59 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "save-op1"
version = "0.3.0"
version = "0.4.0"
authors = ["chee <chee@snoot.club>"]
edition = "2018"

Expand All @@ -12,3 +12,5 @@ dialoguer = "0.5.0"
ears = "0.8.0"
slug = "0.1.4"
taglib = "1.0.0"
pbr = "1.0.2"
tee_readwrite = "0.1.0"
27 changes: 21 additions & 6 deletions src/disk.rs
@@ -1,8 +1,10 @@
use super::operator::Track;
use super::song::Song;
use std::fs::{copy, create_dir_all, read_dir};
use std::io::{Error, ErrorKind, Result};
use pbr::{ProgressBar, Units};
use std::fs::{create_dir_all, read_dir, File};
use std::io::{copy, Error, ErrorKind, Result};
use std::path;
use tee_readwrite::TeeWriter;

struct SongsPath {
path: String,
Expand Down Expand Up @@ -94,6 +96,18 @@ pub struct Disk {
*/
}

fn copy_file(source: &path::PathBuf, target: &path::PathBuf) -> Result<()> {
let mut source = File::open(source)?;
let bytes = source.metadata()?.len() as u64;
let mut progress_bar = ProgressBar::new(bytes);
progress_bar.set_units(Units::Bytes);
let mut target = File::create(target)?;
let mut tee = TeeWriter::new(&mut target, &mut progress_bar);
copy(&mut source, &mut tee)?;
progress_bar.finish_print("done");
Ok(())
}

impl Disk {
fn make_song_dir(&self, song: &Song) -> Result<()> {
create_dir_all(self.songs.song(&SongArg::Song(song)))
Expand Down Expand Up @@ -140,17 +154,18 @@ impl Disk {

pub fn save_aif(&self, song: &Song, source: &path::PathBuf) -> Result<()> {
self.make_song_dir(song)?;
copy(source, self.songs.aif(&SongArg::Song(song)))?;
copy_file(source, &self.songs.aif(&SongArg::Song(song)))?;
Ok(())
}

pub fn save_tape(&self, song: &Song, tracks: Vec<&Track>) -> Result<()> {
self.make_tape_dir(song)?;

for track in tracks {
copy(
track.path(),
self.songs.tape_track(&SongArg::Song(song), track),
println!("copying {}", track);
copy_file(
&path::PathBuf::from(track.path()),
&self.songs.tape_track(&SongArg::Song(song), track),
)?;
}

Expand Down

0 comments on commit 394b709

Please sign in to comment.