diff --git a/Cargo.lock b/Cargo.lock index 6c16d79..3a316c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,7 +83,7 @@ dependencies = [ [[package]] name = "dusage" -version = "0.2.2" +version = "0.2.3" dependencies = [ "autoclap", "byte-unit", diff --git a/Cargo.toml b/Cargo.toml index f00e365..8ad2cdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dusage" -version = "0.2.2" +version = "0.2.3" edition = "2021" authors = ["Mihai Galos "] description = "💾 A command line disk usage information tool: disk usage (foreground), inodes (background)." diff --git a/src/bar.rs b/src/bar.rs index a9ac674..9ed044c 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -3,14 +3,17 @@ use crate::colorizer::Colorizer; pub struct Bar; impl Bar { - pub fn new_disk(percent_disk: f64, percent_inodes: f64) -> String { + pub fn new_disk(percent_disk: f64, percent_inodes: f64, is_copy_friendly: bool) -> String { let bar_length = 20; let bar_unit = "■"; + let bar_unit_empty = "□"; let count_inode_units = Bar::compute_bar_units(percent_inodes, bar_length); let count_disk_units = Bar::compute_bar_units(percent_disk, bar_length); Colorizer::colorize_bar( bar_length, bar_unit, + bar_unit_empty, + is_copy_friendly, count_disk_units, count_inode_units, percent_disk, diff --git a/src/colorizer.rs b/src/colorizer.rs index 3cad391..09079ee 100644 --- a/src/colorizer.rs +++ b/src/colorizer.rs @@ -48,12 +48,18 @@ impl Colorizer { pub fn colorize_bar( bar_length: usize, bar_unit: &str, + bar_unit_empty: &str, + is_copy_friendly: bool, count_disk_units: usize, count_inode_units: usize, percent_disk: f64, percent_inodes: f64, ) -> String { let mut result = "".to_string(); + let background = match is_copy_friendly { + true => bar_unit_empty.white(), + false => bar_unit.white().dimmed(), + }; for i in 0..bar_length { if i < count_disk_units { result = format!( @@ -62,7 +68,7 @@ impl Colorizer { Colorizer::colorize_disk_used(bar_unit.to_string(), percent_disk) ); } else { - result = format!("{}{}", result, bar_unit.white().dimmed()); + result = format!("{}{}", result, background); } if i < count_inode_units { result = format!( diff --git a/src/main.rs b/src/main.rs index 2a22cfd..ad6fc6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,12 @@ fn main() { .short('i') .help("Display inode information."), ) + .arg( + Arg::new("copy_friendly") + .long("copy_friendly") + .short('c') + .help("Monocrome-friendly background for easy copy-pasting elsewhere."), + ) .try_get_matches() .unwrap_or_else(|e| e.exit()); diff --git a/src/writer.rs b/src/writer.rs index 446745f..2f52588 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -23,12 +23,12 @@ impl Writer { max_width = min_width; } if args.is_present("inodes") { - Writer::write_inodes(stats, max_width); + Writer::write_inodes(stats, max_width, args); } else { - Writer::write_disks(stats, max_width); + Writer::write_disks(stats, max_width, args); } } - pub fn write_disks(stats: Vec, max_width: usize) { + pub fn write_disks(stats: Vec, max_width: usize, args: ArgMatches) { println!( "{:width$} {:>8} {:>8} {:>8} {:>6} {:>20} {}", "Filesystem".yellow().bold(), @@ -40,13 +40,14 @@ impl Writer { "Mounted on".yellow().bold(), width = max_width ); + let is_copy_friendly = args.is_present("copy_friendly"); for stat in stats { if Writer::is_relevant(&stat) { - Writer::write_disk_stat(stat, max_width); + Writer::write_disk_stat(stat, max_width, is_copy_friendly); } } } - pub fn write_inodes(stats: Vec, max_width: usize) { + pub fn write_inodes(stats: Vec, max_width: usize, args: ArgMatches) { println!( "{:width$} {:>10} {:>10} {:>10} {:>6} {:>20} {}", "Filesystem".yellow().bold(), @@ -58,14 +59,15 @@ impl Writer { "Mounted on".yellow().bold(), width = max_width ); + let is_copy_friendly = args.is_present("copy_friendly"); for stat in stats { if Writer::is_relevant(&stat) { - Writer::write_inodes_stat(stat, max_width); + Writer::write_inodes_stat(stat, max_width, is_copy_friendly); } } } - fn write_disk_stat(stat: Stats, max_width: usize) { + fn write_disk_stat(stat: Stats, max_width: usize, is_copy_friendly: bool) { let percent_disk = if stat.percent_disk.is_nan() { " -".to_string() } else { @@ -78,12 +80,12 @@ impl Writer { Writer::iec_representation(stat.used_disk), Writer::iec_representation(stat.available_disk), percent_disk, - Bar::new_disk(stat.percent_disk, stat.percent_inodes), + Bar::new_disk(stat.percent_disk, stat.percent_inodes, is_copy_friendly), width = max_width ); println!("{}", Colorizer::colorize_mountpoint(stat.mount)); } - fn write_inodes_stat(stat: Stats, max_width: usize) { + fn write_inodes_stat(stat: Stats, max_width: usize, is_copy_friendly: bool) { let percent_inodes = if stat.percent_inodes.is_nan() { " -".to_string() } else { @@ -96,7 +98,7 @@ impl Writer { stat.used_inodes, stat.available_inodes, percent_inodes, - Bar::new_disk(stat.percent_disk, stat.percent_inodes), + Bar::new_disk(stat.percent_disk, stat.percent_inodes, is_copy_friendly), width = max_width ); println!("{}", Colorizer::colorize_mountpoint(stat.mount));