mirror of
https://github.com/nachoparker/dutree
synced 2024-05-28 02:26:05 +02:00
Avoid dividing by zero
This commit is contained in:
parent
72344f2780
commit
2712a7ffdf
13
src/lib.rs
13
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![ ' ', '░', '▒', '▓', '█' ] };
|
||||||
|
@ -451,7 +454,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