Skip to content

Commit

Permalink
Add copy-friendly feature
Browse files Browse the repository at this point in the history
Usefuly when we want to copy output elsewhere.
The unused is then a separate character to
distinguish it from the used part.
  • Loading branch information
mihaigalos committed Feb 10, 2022
1 parent d8be3f9 commit 57e0332
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dusage"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
authors = ["Mihai Galos <[email protected]>"]
description = "💾 A command line disk usage information tool: disk usage (foreground), inodes (background)."
Expand Down
5 changes: 4 additions & 1 deletion src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/colorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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!(
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
22 changes: 12 additions & 10 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stats>, max_width: usize) {
pub fn write_disks(stats: Vec<Stats>, max_width: usize, args: ArgMatches) {
println!(
"{:width$} {:>8} {:>8} {:>8} {:>6} {:>20} {}",
"Filesystem".yellow().bold(),
Expand All @@ -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<Stats>, max_width: usize) {
pub fn write_inodes(stats: Vec<Stats>, max_width: usize, args: ArgMatches) {
println!(
"{:width$} {:>10} {:>10} {:>10} {:>6} {:>20} {}",
"Filesystem".yellow().bold(),
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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));
Expand Down

0 comments on commit 57e0332

Please sign in to comment.