1
0
Fork 0
mirror of https://github.com/nachoparker/dutree synced 2024-05-29 02:36:07 +02:00

Render the bars relative to the largest size shown

This commit is contained in:
Brian Campbell 2018-12-20 22:18:52 -05:00 committed by nachoparker
parent 9bbdcfeef8
commit b947b9cd54
4 changed files with 27 additions and 21 deletions

2
Cargo.lock generated
View File

@ -10,7 +10,7 @@ dependencies = [
[[package]] [[package]]
name = "dutree" name = "dutree"
version = "0.2.10" version = "0.2.11"
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)",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "dutree" name = "dutree"
version = "0.2.10" version = "0.2.11"
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"

View File

@ -323,7 +323,8 @@ impl<'a> Entry<'a> {
} }
fn print_entries( &self, open_parents : Vec<bool>, parent_vals : Vec<u64>, fn print_entries( &self, open_parents : Vec<bool>, parent_vals : Vec<u64>,
bytes_flag : bool, ascii_flag : bool, bar_width : usize, tree_name_width : usize ) { bytes_flag : bool, ascii_flag : bool,
max_bytes : u64, bar_width : usize, tree_name_width : usize ) {
if let Some(ref entries) = self.entries { if let Some(ref entries) = self.entries {
for entry in entries { for entry in entries {
let mut op = open_parents.clone(); let mut op = open_parents.clone();
@ -375,11 +376,11 @@ impl<'a> Entry<'a> {
// print it // print it
println!( "{} {} {:>13}", println!( "{} {} {:>13}",
name, name,
fmt_bar( &bytes, bar_width, ascii_flag ), fmt_bar( &bytes, max_bytes, bar_width, ascii_flag ),
fmt_size_str( entry.bytes, bytes_flag ) ); fmt_size_str( entry.bytes, bytes_flag ) );
if let Some(_) = entry.entries { if let Some(_) = entry.entries {
entry.print_entries( op, bytes, bytes_flag, ascii_flag, entry.print_entries( op, bytes, bytes_flag, ascii_flag,
bar_width, tree_name_width ); max_bytes, bar_width, tree_name_width );
} }
} }
} }
@ -397,30 +398,35 @@ impl<'a> Entry<'a> {
eprintln!("Unable to get terminal size"); eprintln!("Unable to get terminal size");
} }
let size_width = 15; let size_width = 15;
let var_width = twidth - size_width; let var_width = (twidth - size_width) as usize;
let bar_width = var_width as usize * 75 / 100; let tree_name_width = 25.max(var_width * 25 / 100);
let tree_name_width = var_width as usize * 25 / 100; let bar_width = var_width - tree_name_width;
// initalize // initalize
let open_parents : Vec<bool> = Vec::new(); let open_parents : Vec<bool> = Vec::new();
let mut parent_vals : Vec<u64> = Vec::new(); let mut parent_vals : Vec<u64> = Vec::new();
let max_bytes = match self.entries {
Some(ref entries) => entries.iter().map(|e| e.bytes).max().unwrap_or(self.bytes),
None => self.bytes,
};
parent_vals.push( self.bytes ); parent_vals.push( self.bytes );
// print // print
println!( "[ {} {} ]", self.name, fmt_size_str( self.bytes, bytes_flag ) ); println!( "[ {} {} ]", self.name, fmt_size_str( self.bytes, bytes_flag ) );
self.print_entries( open_parents, parent_vals, bytes_flag, ascii_flag, self.print_entries( open_parents, parent_vals, bytes_flag, ascii_flag,
bar_width, tree_name_width ); max_bytes, bar_width, tree_name_width );
} }
} }
fn fmt_bar( bytes : &Vec<u64>, width : usize, ascii_flag : bool ) -> String { fn fmt_bar( bytes : &Vec<u64>, max_bytes : u64, width : usize, ascii_flag : bool ) -> String {
let width = width as u64 - 2 - 5; // not including bars and percentage let width = width as u64 - 2 - 5; // not including bars and percentage
let mut str = String::with_capacity( width as usize ); let mut str = String::with_capacity( width as usize );
str.push( '│' ); str.push( '│' );
let mut bytesi = bytes.iter(); let mut bytesi = bytes.iter();
let mut total = bytesi.next().unwrap(); let _ = bytesi.next();
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 = ( part * width ) / total;
let mut pos = width - bars; let mut pos = width - bars;