summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIsaac Parker <parrotmac@gmail.com>2019-11-17 19:22:01 -0800
committernachoparker <nacho@ownyourbits.com>2019-11-17 20:25:13 -0700
commitc70ecc9e65180e95ebde09c47ac903f7a8242a81 (patch)
treef1065453bd740009a5a2fd5d9c0bdaf8b3b90ce6
parent203df3473fe01fafea9b3e4ccdb28a286b047dfe (diff)
Avoid dividing by zero (#28)
Signed-off-by: nachoparker <nacho@ownyourbits.com>
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs16
3 files changed, 14 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1f989d6..cb314ea 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -15,7 +15,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "dutree"
-version = "0.2.13"
+version = "0.2.15"
dependencies = [
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 0fdbb85..dfb5258 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "dutree"
-version = "0.2.13"
+version = "0.2.15"
authors = ["nacho <nacho@ownyourbits.com>"]
description = "Command line tool to analyze disk usage"
repository = "https://github.com/nachoparker/dutree"
diff --git a/src/lib.rs b/src/lib.rs
index e515a9a..c284d8a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -429,7 +429,10 @@ fn fmt_bar( bytes : &Vec<u64>, max_bytes : u64, width : usize, ascii_flag : bool
let _ = bytesi.next();
let mut total = &max_bytes;
let mut part = bytesi.next().unwrap();
- let mut bars = ( part * width ) / total;
+ let mut bars = match total {
+ 0 => 0,
+ _ => (part * width) / total,
+ };
let mut pos = width - bars;
let block_char = if ascii_flag { vec![ ' ', '#' ] } else { vec![ ' ', '░', '▒', '▓', '█' ] };
@@ -440,8 +443,7 @@ fn fmt_bar( bytes : &Vec<u64>, max_bytes : u64, width : usize, ascii_flag : bool
if x > pos {
total = part;
part = bytesi.next().unwrap_or(&0);
- bars = ( part * bars ) / total;
-
+ bars = match total { 0 => 0, _ => (part * bars) / total };
pos = width - bars;
chr += 1;
if chr == levels || chr >= block_char.len() {
@@ -451,7 +453,13 @@ fn fmt_bar( bytes : &Vec<u64>, max_bytes : u64, width : usize, ascii_flag : bool
str.push( block_char[chr] );
}
- format!( "{}│ {:3}%", str, ( bytes[bytes.len()-1] * 100 ) / bytes[bytes.len()-2] )
+ let nominator = bytes[bytes.len()-1] * 100;
+ let denominator = bytes[bytes.len()-2];
+ let result = match denominator {
+ 0 => 0,
+ _ => nominator/denominator,
+ };
+ format!( "{}│ {:3}%", str, result )
}
fn fmt_size_str( bytes : u64, flag : bool ) -> String {