From c31fbc1a0974d4d43023c47113b1e83b5b8636d7 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Wed, 1 Jun 2016 17:00:47 +0200 Subject: [PATCH] Added a fancy progressbar. --- Cargo.lock | 46 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 18 +++++++++++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8a24bd1..29b05de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,6 +4,7 @@ version = "0.1.0" dependencies = [ "byteorder 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "pbr 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -16,3 +17,48 @@ name = "getopts" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pbr" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "time" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/Cargo.toml b/Cargo.toml index f662909..d1813ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ authors = ["logaritmisk "] [dependencies] getopts = "0.2" byteorder = "0.5" +pbr = "0.2.1" diff --git a/src/main.rs b/src/main.rs index 9f7a5f5..513f672 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ extern crate getopts; extern crate byteorder; +extern crate pbr; use std::env; use std::fs; @@ -10,6 +11,7 @@ use std::io::Cursor; use getopts::Options; use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian}; +use pbr::{ProgressBar, Units}; fn main() { @@ -51,9 +53,14 @@ fn compress(input: &String, output: &String) { let mut output_file = fs::File::create(output).unwrap(); let input_file = fs::File::open(input).unwrap(); + let n_bytes = input_file.metadata().unwrap().len() as u64; + let input_buf = BufReader::new(input_file); let mut i : i32 = 0; + let mut pb = ProgressBar::new(n_bytes); + + pb.set_units(Units::Bytes); for b in input_buf.bytes() { let b = b.unwrap(); @@ -70,14 +77,21 @@ fn compress(input: &String, output: &String) { index.write_i32::(i).unwrap(); output_file.write_all(&index).unwrap(); + + pb.inc(); } } fn extract(input: &String, output: &String) { - let mut output_file = fs::File::create(output).unwrap(); let mut input_file = fs::File::open(input).unwrap(); + let n_bytes = input_file.metadata().unwrap().len() as u64; + + let mut output_file = fs::File::create(output).unwrap(); let mut buf = vec![0; 4]; + let mut pb = ProgressBar::new(n_bytes); + + pb.set_units(Units::Bytes); loop { let size = input_file.read(&mut buf).unwrap(); @@ -90,6 +104,8 @@ fn extract(input: &String, output: &String) { let index = c.read_i32::().unwrap(); output_file.write_all(&[get_byte(index)]).unwrap(); + + pb.add(size as u64); } }