Added a fancy progressbar.

This commit is contained in:
2016-06-01 17:00:47 +02:00
parent 5a78eb202e
commit c31fbc1a09
3 changed files with 64 additions and 1 deletions

46
Cargo.lock generated
View File

@@ -4,6 +4,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"byteorder 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "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)", "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]] [[package]]
@@ -16,3 +17,48 @@ name = "getopts"
version = "0.2.14" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" 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"

View File

@@ -6,3 +6,4 @@ authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies] [dependencies]
getopts = "0.2" getopts = "0.2"
byteorder = "0.5" byteorder = "0.5"
pbr = "0.2.1"

View File

@@ -1,5 +1,6 @@
extern crate getopts; extern crate getopts;
extern crate byteorder; extern crate byteorder;
extern crate pbr;
use std::env; use std::env;
use std::fs; use std::fs;
@@ -10,6 +11,7 @@ use std::io::Cursor;
use getopts::Options; use getopts::Options;
use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian}; use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian};
use pbr::{ProgressBar, Units};
fn main() { fn main() {
@@ -51,9 +53,14 @@ fn compress(input: &String, output: &String) {
let mut output_file = fs::File::create(output).unwrap(); let mut output_file = fs::File::create(output).unwrap();
let input_file = fs::File::open(input).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 input_buf = BufReader::new(input_file);
let mut i : i32 = 0; let mut i : i32 = 0;
let mut pb = ProgressBar::new(n_bytes);
pb.set_units(Units::Bytes);
for b in input_buf.bytes() { for b in input_buf.bytes() {
let b = b.unwrap(); let b = b.unwrap();
@@ -70,14 +77,21 @@ fn compress(input: &String, output: &String) {
index.write_i32::<LittleEndian>(i).unwrap(); index.write_i32::<LittleEndian>(i).unwrap();
output_file.write_all(&index).unwrap(); output_file.write_all(&index).unwrap();
pb.inc();
} }
} }
fn extract(input: &String, output: &String) { 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 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 buf = vec![0; 4];
let mut pb = ProgressBar::new(n_bytes);
pb.set_units(Units::Bytes);
loop { loop {
let size = input_file.read(&mut buf).unwrap(); let size = input_file.read(&mut buf).unwrap();
@@ -90,6 +104,8 @@ fn extract(input: &String, output: &String) {
let index = c.read_i32::<LittleEndian>().unwrap(); let index = c.read_i32::<LittleEndian>().unwrap();
output_file.write_all(&[get_byte(index)]).unwrap(); output_file.write_all(&[get_byte(index)]).unwrap();
pb.add(size as u64);
} }
} }