Skip to content

Commit

Permalink
mount: separate and store numeric and formatted values
Browse files Browse the repository at this point in the history
  • Loading branch information
anthraxx committed May 18, 2020
1 parent c134cf7 commit f825165
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
22 changes: 13 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ fn display_mounts(mnts: &[&MountEntry], theme: &Theme, inodes_mode: bool) {

let fsname_width = column_width(&mnts, &|m: &&MountEntry| m.mnt_fsname.len(), label_fsname);
let type_width = column_width(&mnts, &|m: &&MountEntry| m.mnt_type.len(), label_type);
let used_width = column_width(&mnts, &|m: &&MountEntry| m.used.len(), label_used);
let available_width = column_width(&mnts, &|m: &&MountEntry| m.free.len(), label_available);
let capacity_width = column_width(&mnts, &|m: &&MountEntry| m.capacity.len(), label_capacity);
let used_width = column_width(&mnts, &|m: &&MountEntry| m.used_formatted.len(), label_used);
let available_width = column_width(&mnts, &|m: &&MountEntry| m.free_formatted.len(), label_available);
let capacity_width = column_width(&mnts, &|m: &&MountEntry| m.capacity_formatted.len(), label_capacity);

println!(
"{:<fsname_width$} {:<type_width$} {:<bar_width$} {:>6} {:>used_width$} {:>available_width$} {:>capacity_width$} {}",
Expand Down Expand Up @@ -147,9 +147,9 @@ fn display_mounts(mnts: &[&MountEntry], theme: &Theme, inodes_mode: bool) {
mnt.mnt_type,
bar(bar_width, mnt.used_percentage, &theme),
used_percentage,
mnt.used.color(color_usage),
mnt.free.color(color_usage),
mnt.capacity.color(color_usage),
mnt.used_formatted.color(color_usage),
mnt.free_formatted.color(color_usage),
mnt.capacity_formatted.color(color_usage),
mnt.mnt_dir,
fsname_width = fsname_width,
type_width = type_width,
Expand Down Expand Up @@ -225,9 +225,13 @@ fn run(args: Args) -> Result<()> {
None => (0, 0),
};

mnt.capacity = format_count(capacity as f64, delimiter.get_powers_of());
mnt.free = format_count(free as f64, delimiter.get_powers_of());
mnt.used = format_count((capacity - free) as f64, delimiter.get_powers_of());
mnt.capacity = capacity;
mnt.free = free;
mnt.used = capacity - free;

mnt.capacity_formatted = format_count(mnt.capacity as f64, delimiter.get_powers_of());
mnt.free_formatted = format_count(mnt.free as f64, delimiter.get_powers_of());
mnt.used_formatted = format_count(mnt.used as f64, delimiter.get_powers_of());

if capacity > 0 {
mnt.used_percentage = Some(100.0 - free as f32 * 100.0 / capacity as f32);
Expand Down
18 changes: 12 additions & 6 deletions src/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ pub struct MountEntry {
pub mnt_opts: String,
pub mnt_freq: i32,
pub mnt_passno: i32,
pub capacity: String,
pub free: String,
pub capacity: u64,
pub free: u64,
pub used: u64,
pub capacity_formatted: String,
pub free_formatted: String,
pub used_formatted: String,
pub used_percentage: Option<f32>,
pub used: String,
pub statfs: Option<nix::sys::statfs::Statfs>,
}

Expand All @@ -34,9 +37,12 @@ impl MountEntry {
mnt_opts,
mnt_freq,
mnt_passno,
capacity: "".to_string(),
free: "".to_string(),
used: "".to_string(),
capacity: 0,
free: 0,
used: 0,
capacity_formatted: "".to_string(),
free_formatted: "".to_string(),
used_formatted: "".to_string(),
used_percentage: None,
statfs: None,
}
Expand Down

0 comments on commit f825165

Please sign in to comment.