Skip to content

Commit

Permalink
update the byte-unit crate
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Nov 26, 2023
1 parent a48b426 commit 9b28dfe
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 136 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mprober-lib = "0.1"
termcolor = "1"
getch = "0.3"

byte-unit = "4"
byte-unit = "5"
once_cell = "1"

rand = "0.8"
Expand Down
14 changes: 7 additions & 7 deletions src/benchmark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
time::{Duration, SystemTime},
};

use byte_unit::{Byte, ByteUnit};
use byte_unit::{Byte, Unit, UnitType};
use mprober_lib::*;
use rand::{self, Rng};

Expand Down Expand Up @@ -295,10 +295,10 @@ pub fn run_benchmark(config: &BenchmarkConfig) -> Result<BenchmarkResult, Benchm
memory = Some(speed);

if config.print_out.has_stdout() {
let memory_result = Byte::from_unit(speed, ByteUnit::B)
.unwrap()
.get_appropriate_unit(true)
.to_string();
let memory_result = format!(
"{:.2}",
Byte::from_f64(speed).unwrap().get_appropriate_unit(UnitType::Binary)
);

println!("Memory : {memory_result}/s");
}
Expand Down Expand Up @@ -523,8 +523,8 @@ pub fn run_benchmark(config: &BenchmarkConfig) -> Result<BenchmarkResult, Benchm
* BUFFER_SIZE
as f64;

let read_result_string = Byte::from_unit(read_result, ByteUnit::B).unwrap().get_appropriate_unit(true).to_string();
let write_result_string = Byte::from_unit(write_result, ByteUnit::B).unwrap().get_appropriate_unit(true).to_string();
let read_result_string = format!("{:.2}", Byte::from_f64_with_unit(read_result, Unit::B).unwrap().get_appropriate_unit(UnitType::Binary));
let write_result_string = format!("{:.2}", Byte::from_f64_with_unit(write_result, Unit::B).unwrap().get_appropriate_unit(UnitType::Binary));

if config
.print_out
Expand Down
22 changes: 11 additions & 11 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
time::Duration,
};

use byte_unit::{ByteUnit, UnitIncorrectError};
use byte_unit::{Unit, UnitParseError};
use clap::{CommandFactory, FromArgMatches, Parser, Subcommand};
use concat_with::concat_line;
use regex::Regex;
Expand Down Expand Up @@ -170,9 +170,9 @@ pub enum CLICommands {
#[arg(help = "Show memory stats and refresh every N milliseconds")]
monitor: Option<Duration>,
#[arg(short, long)]
#[arg(value_parser = parse_byte_unit)]
#[arg(value_parser = parse_unit)]
#[arg(help = "Forces to use a fixed unit")]
unit: Option<ByteUnit>,
unit: Option<Unit>,
},
#[command(aliases = ["n", "net", "networks", "bandwidth", "traffic"])]
#[command(about = "Show network stats")]
Expand All @@ -190,9 +190,9 @@ pub enum CLICommands {
#[arg(help = "Show network stats and refresh every N milliseconds")]
monitor: Option<Duration>,
#[arg(short, long)]
#[arg(value_parser = parse_byte_unit)]
#[arg(value_parser = parse_unit)]
#[arg(help = "Forces to use a fixed unit")]
unit: Option<ByteUnit>,
unit: Option<Unit>,
},
#[command(aliases = ["v", "storage", "volumes", "d", "disk", "disks", "blk", "block", "blocks", "mount", "mounts", "ssd", "hdd"])]
#[command(about = "Show volume stats")]
Expand All @@ -210,9 +210,9 @@ pub enum CLICommands {
#[arg(help = "Show volume stats and refresh every N milliseconds")]
monitor: Option<Duration>,
#[arg(short, long)]
#[arg(value_parser = parse_byte_unit)]
#[arg(value_parser = parse_unit)]
#[arg(help = "Forces to use a fixed unit")]
unit: Option<ByteUnit>,
unit: Option<Unit>,
#[arg(short = 'i', long)]
#[arg(help = "Show only information about volumes without I/O rates")]
only_information: bool,
Expand All @@ -236,9 +236,9 @@ pub enum CLICommands {
#[arg(help = "Show process stats and refresh every N milliseconds")]
monitor: Option<Duration>,
#[arg(short, long)]
#[arg(value_parser = parse_byte_unit)]
#[arg(value_parser = parse_unit)]
#[arg(help = "Forces to use a fixed unit")]
unit: Option<ByteUnit>,
unit: Option<Unit>,
#[arg(short = 'i', long)]
#[arg(help = "Show only information about processes without CPU usage")]
only_information: bool,
Expand Down Expand Up @@ -353,8 +353,8 @@ fn parse_duration_sec(arg: &str) -> Result<Duration, ParseIntError> {
}

#[inline]
fn parse_byte_unit(arg: &str) -> Result<ByteUnit, UnitIncorrectError> {
ByteUnit::from_str(arg)
fn parse_unit(arg: &str) -> Result<Unit, UnitParseError> {
Unit::from_str(arg)
}

#[inline]
Expand Down
12 changes: 7 additions & 5 deletions src/commands/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use byte_unit::{Byte, ByteUnit};
use byte_unit::{Byte, Unit, UnitType};
use mprober_lib::{cpu, load_average};

use crate::{terminal::*, CLIArgs, CLICommands};
Expand Down Expand Up @@ -237,8 +237,9 @@ fn draw_cpu_info(monitor: Option<Duration>, separate: bool, only_information: bo
let mut hz_string: Vec<String> = Vec::with_capacity(cpu.siblings);

for cpu_mhz in cpu.cpus_mhz.iter().copied() {
let cpu_hz =
Byte::from_unit(cpu_mhz, ByteUnit::MB).unwrap().get_appropriate_unit(false);
let cpu_hz = Byte::from_f64_with_unit(cpu_mhz, Unit::MB)
.unwrap()
.get_appropriate_unit(UnitType::Decimal);

hz_string.push(format!(
"{:.2} {}Hz",
Expand Down Expand Up @@ -377,8 +378,9 @@ fn draw_cpu_info(monitor: Option<Duration>, separate: bool, only_information: bo

let cpu_mhz: f64 = cpu.cpus_mhz.iter().sum::<f64>() / cpu.cpus_mhz.len() as f64;

let cpu_hz =
Byte::from_unit(cpu_mhz, ByteUnit::MB).unwrap().get_appropriate_unit(false);
let cpu_hz = Byte::from_f64_with_unit(cpu_mhz, Unit::MB)
.unwrap()
.get_appropriate_unit(UnitType::Decimal);

write!(&mut stdout, "{:.2}{}Hz", cpu_hz.get_value(), &cpu_hz.get_unit().as_str()[..1])
.unwrap();
Expand Down
28 changes: 14 additions & 14 deletions src/commands/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use byte_unit::{Byte, ByteUnit};
use byte_unit::{Byte, Unit, UnitType};
use mprober_lib::memory;

use crate::{terminal::*, CLIArgs, CLICommands};
Expand All @@ -20,32 +20,32 @@ pub fn handle_memory(args: CLIArgs) {
}
}

fn draw_memory(unit: Option<ByteUnit>) {
fn draw_memory(unit: Option<Unit>) {
let free = memory::free().unwrap();

let output = get_stdout_output();
let mut stdout = output.buffer();

let (mem_used, mem_total, swap_used, swap_total) = {
let (mem_used, mem_total, swap_used, swap_total) = (
Byte::from_bytes(free.mem.used as u128),
Byte::from_bytes(free.mem.total as u128),
Byte::from_bytes(free.swap.used as u128),
Byte::from_bytes(free.swap.total as u128),
Byte::from(free.mem.used),
Byte::from(free.mem.total),
Byte::from(free.swap.used),
Byte::from(free.swap.total),
);

match unit {
Some(unit) => (
mem_used.get_adjusted_unit(unit).to_string(),
mem_total.get_adjusted_unit(unit).to_string(),
swap_used.get_adjusted_unit(unit).to_string(),
swap_total.get_adjusted_unit(unit).to_string(),
format!("{:.2}", mem_used.get_adjusted_unit(unit)),
format!("{:.2}", mem_total.get_adjusted_unit(unit)),
format!("{:.2}", swap_used.get_adjusted_unit(unit)),
format!("{:.2}", swap_total.get_adjusted_unit(unit)),
),
None => (
mem_used.get_appropriate_unit(true).to_string(),
mem_total.get_appropriate_unit(true).to_string(),
swap_used.get_appropriate_unit(true).to_string(),
swap_total.get_appropriate_unit(true).to_string(),
format!("{:.2}", mem_used.get_appropriate_unit(UnitType::Binary)),
format!("{:.2}", mem_total.get_appropriate_unit(UnitType::Binary)),
format!("{:.2}", swap_used.get_appropriate_unit(UnitType::Binary)),
format!("{:.2}", swap_total.get_appropriate_unit(UnitType::Binary)),
),
}
};
Expand Down
28 changes: 14 additions & 14 deletions src/commands/network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use byte_unit::{Byte, ByteUnit};
use byte_unit::{Byte, Unit, UnitType};
use mprober_lib::network;

use crate::{terminal::*, CLIArgs, CLICommands};
Expand All @@ -20,7 +20,7 @@ pub fn handle_network(args: CLIArgs) {
}
}

fn draw_network(monitor: Option<Duration>, unit: Option<ByteUnit>) {
fn draw_network(monitor: Option<Duration>, unit: Option<Unit>) {
let networks_with_speed = network::get_networks_with_speed(match monitor {
Some(monitor) => monitor,
None => DEFAULT_INTERVAL,
Expand All @@ -41,24 +41,24 @@ fn draw_network(monitor: Option<Duration>, unit: Option<ByteUnit>) {
let mut downloads_total: Vec<String> = Vec::with_capacity(networks_with_speed_len);

for (network, network_speed) in networks_with_speed.iter() {
let upload = Byte::from_unit(network_speed.transmit, ByteUnit::B).unwrap();
let upload_total = Byte::from_bytes(u128::from(network.stat.transmit_bytes));
let upload = Byte::from_f64_with_unit(network_speed.transmit, Unit::B).unwrap();
let upload_total = Byte::from_u64(network.stat.transmit_bytes);

let download = Byte::from_unit(network_speed.receive, ByteUnit::B).unwrap();
let download_total = Byte::from_bytes(u128::from(network.stat.receive_bytes));
let download = Byte::from_f64_with_unit(network_speed.receive, Unit::B).unwrap();
let download_total = Byte::from_u64(network.stat.receive_bytes);

let (mut upload, upload_total, mut download, download_total) = match unit {
Some(unit) => (
upload.get_adjusted_unit(unit).to_string(),
upload_total.get_adjusted_unit(unit).to_string(),
download.get_adjusted_unit(unit).to_string(),
download_total.get_adjusted_unit(unit).to_string(),
format!("{:.2}", upload.get_adjusted_unit(unit)),
format!("{:.2}", upload_total.get_adjusted_unit(unit)),
format!("{:.2}", download.get_adjusted_unit(unit)),
format!("{:.2}", download_total.get_adjusted_unit(unit)),
),
None => (
upload.get_appropriate_unit(false).to_string(),
upload_total.get_appropriate_unit(false).to_string(),
download.get_appropriate_unit(false).to_string(),
download_total.get_appropriate_unit(false).to_string(),
format!("{:.2}", upload.get_appropriate_unit(UnitType::Decimal)),
format!("{:.2}", upload_total.get_appropriate_unit(UnitType::Decimal)),
format!("{:.2}", download.get_appropriate_unit(UnitType::Decimal)),
format!("{:.2}", download_total.get_appropriate_unit(UnitType::Decimal)),
),
};

Expand Down
23 changes: 10 additions & 13 deletions src/commands/process.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{cmp::Ordering, collections::BTreeMap, sync::Arc};

use anyhow::anyhow;
use byte_unit::{Byte, ByteUnit};
use byte_unit::{Byte, Unit, UnitType};
use chrono::SecondsFormat;
use mprober_lib::process;
use regex::Regex;
Expand Down Expand Up @@ -79,7 +79,7 @@ fn draw_process(
monitor: Option<Duration>,
mut top: Option<usize>,
truncate: usize,
unit: Option<ByteUnit>,
unit: Option<Unit>,
only_information: bool,
start_time: bool,
user_filter: Option<&str>,
Expand Down Expand Up @@ -227,22 +227,19 @@ fn draw_process(
pid.push(process.pid.to_string());
ppid.push(process.ppid.to_string());

let (p_vsz, p_rss, p_anon) = (
Byte::from_bytes(process.vsz as u128),
Byte::from_bytes(process.rss as u128),
Byte::from_bytes(process.rss_anon as u128),
);
let (p_vsz, p_rss, p_anon) =
(Byte::from(process.vsz), Byte::from(process.rss), Byte::from(process.rss_anon));

match unit {
Some(byte_unit) => {
vsz.push(p_vsz.get_adjusted_unit(byte_unit).format(1));
rss.push(p_rss.get_adjusted_unit(byte_unit).format(1));
anon.push(p_anon.get_adjusted_unit(byte_unit).format(1));
vsz.push(format!("{:.1}", p_vsz.get_adjusted_unit(byte_unit)));
rss.push(format!("{:.1}", p_rss.get_adjusted_unit(byte_unit)));
anon.push(format!("{:.1}", p_anon.get_adjusted_unit(byte_unit)));
},
None => {
vsz.push(p_vsz.get_appropriate_unit(true).format(1));
rss.push(p_rss.get_appropriate_unit(true).format(1));
anon.push(p_anon.get_appropriate_unit(true).format(1));
vsz.push(format!("{:.1}", p_vsz.get_appropriate_unit(UnitType::Binary)));
rss.push(format!("{:.1}", p_rss.get_appropriate_unit(UnitType::Binary)));
anon.push(format!("{:.1}", p_anon.get_appropriate_unit(UnitType::Binary)));
},
}

Expand Down
Loading

0 comments on commit 9b28dfe

Please sign in to comment.