mirror of
https://github.com/nachoparker/dutree
synced 2024-05-09 23:26:05 +02:00
Avoid dividing by zero (#28)
Signed-off-by: nachoparker <nacho@ownyourbits.com>
This commit is contained in:
parent
203df3473f
commit
c70ecc9e65
|
@ -15,7 +15,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dutree"
|
name = "dutree"
|
||||||
version = "0.2.13"
|
version = "0.2.15"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"getopts 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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)",
|
"regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "dutree"
|
name = "dutree"
|
||||||
version = "0.2.13"
|
version = "0.2.15"
|
||||||
authors = ["nacho <nacho@ownyourbits.com>"]
|
authors = ["nacho <nacho@ownyourbits.com>"]
|
||||||
description = "Command line tool to analyze disk usage"
|
description = "Command line tool to analyze disk usage"
|
||||||
repository = "https://github.com/nachoparker/dutree"
|
repository = "https://github.com/nachoparker/dutree"
|
||||||
|
|
16
src/lib.rs
16
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 _ = bytesi.next();
|
||||||
let mut total = &max_bytes;
|
let mut total = &max_bytes;
|
||||||
let mut part = bytesi.next().unwrap();
|
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 mut pos = width - bars;
|
||||||
|
|
||||||
let block_char = if ascii_flag { vec![ ' ', '#' ] } else { vec![ ' ', '░', '▒', '▓', '█' ] };
|
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 {
|
if x > pos {
|
||||||
total = part;
|
total = part;
|
||||||
part = bytesi.next().unwrap_or(&0);
|
part = bytesi.next().unwrap_or(&0);
|
||||||
bars = ( part * bars ) / total;
|
bars = match total { 0 => 0, _ => (part * bars) / total };
|
||||||
|
|
||||||
pos = width - bars;
|
pos = width - bars;
|
||||||
chr += 1;
|
chr += 1;
|
||||||
if chr == levels || chr >= block_char.len() {
|
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] );
|
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 {
|
fn fmt_size_str( bytes : u64, flag : bool ) -> String {
|
||||||
|
|
Loading…
Reference in New Issue