diff options
author | cyqsimon <28627918+cyqsimon@users.noreply.github.com> | 2024-01-28 04:21:06 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-28 04:21:06 +0800 |
commit | dc0468c8e72d2979a4daa167bfcd726501564ace (patch) | |
tree | a5348d074d7f2fcc3d12629fee398d2c6137edd0 /src | |
parent | 592f733082d2e2b46c52f8bae822ee75c4c9fb62 (diff) |
Generate completion & manpage (#357)
* Generate completion & manpage
* Write changelog
Diffstat (limited to 'src')
-rw-r--r-- | src/cli.rs | 24 | ||||
-rw-r--r-- | src/display/components/display_bandwidth.rs | 33 | ||||
-rw-r--r-- | src/display/ui.rs | 2 |
3 files changed, 36 insertions, 23 deletions
@@ -1,10 +1,9 @@ use std::{net::Ipv4Addr, path::PathBuf}; -use clap::{Args, Parser}; +use clap::{Args, Parser, ValueEnum, ValueHint}; use clap_verbosity_flag::{InfoLevel, Verbosity}; use derivative::Derivative; - -use crate::display::BandwidthUnitFamily; +use strum::EnumIter; #[derive(Clone, Debug, Derivative, Parser)] #[derivative(Default)] @@ -30,7 +29,7 @@ pub struct Opt { /// A dns server ip to use instead of the system default pub dns_server: Option<Ipv4Addr>, - #[arg(long)] + #[arg(long, value_hint = ValueHint::FilePath)] /// Enable debug logging to a file pub log_to: Option<PathBuf>, @@ -58,9 +57,24 @@ pub struct RenderOpts { #[arg(short, long, value_enum, default_value_t)] /// Choose a specific family of units - pub unit_family: BandwidthUnitFamily, + pub unit_family: UnitFamily, #[arg(short, long)] /// Show total (cumulative) usages pub total_utilization: bool, } + +// IMPRV: it would be nice if we can `#[cfg_attr(not(build), derive(strum::EnumIter))]` this +// unfortunately there is no configuration option for build script detection +#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, ValueEnum, EnumIter)] +pub enum UnitFamily { + #[default] + /// bytes, in powers of 2^10 + BinBytes, + /// bits, in powers of 2^10 + BinBits, + /// bytes, in powers of 10^3 + SiBytes, + /// bits, in powers of 10^3 + SiBits, +} diff --git a/src/display/components/display_bandwidth.rs b/src/display/components/display_bandwidth.rs index 5d7a510..bf2b526 100644 --- a/src/display/components/display_bandwidth.rs +++ b/src/display/components/display_bandwidth.rs @@ -1,7 +1,8 @@ use std::fmt; -use clap::ValueEnum; -use strum::EnumIter; +use derivative::Derivative; + +use crate::cli::UnitFamily; #[derive(Copy, Clone, Debug)] pub struct DisplayBandwidth { @@ -16,17 +17,14 @@ impl fmt::Display for DisplayBandwidth { } } -#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, ValueEnum, EnumIter)] -pub enum BandwidthUnitFamily { - #[default] - /// bytes, in powers of 2^10 - BinBytes, - /// bits, in powers of 2^10 - BinBits, - /// bytes, in powers of 10^3 - SiBytes, - /// bits, in powers of 10^3 - SiBits, +/// Type wrapper around [`UnitFamily`] to provide extra functionality. +#[derive(Copy, Clone, Derivative, Default, Eq, PartialEq)] +#[derivative(Debug = "transparent")] +pub struct BandwidthUnitFamily(UnitFamily); +impl From<UnitFamily> for BandwidthUnitFamily { + fn from(value: UnitFamily) -> Self { + Self(value) + } } impl BandwidthUnitFamily { #[inline] @@ -39,9 +37,9 @@ impl BandwidthUnitFamily { /// Binary base: 2^10. const BB: f64 = 1024.0; - use BandwidthUnitFamily as F; + use UnitFamily as F; // probably could macro this stuff, but I'm too lazy - match self { + match self.0 { F::BinBytes => [ (1.0, BB * STEP_UP_FRAC, "B"), (BB, BB.powi(2) * STEP_UP_FRAC, "KiB"), @@ -99,11 +97,12 @@ mod tests { use itertools::Itertools; use strum::IntoEnumIterator; - use crate::display::{BandwidthUnitFamily, DisplayBandwidth}; + use crate::{cli::UnitFamily, display::DisplayBandwidth}; #[test] fn bandwidth_formatting() { - let test_bandwidths_formatted = BandwidthUnitFamily::iter() + let test_bandwidths_formatted = UnitFamily::iter() + .map_into() .cartesian_product( // I feel like this is a decent selection of values (-6..60) diff --git a/src/display/ui.rs b/src/display/ui.rs index 4b6d716..4a2866e 100644 --- a/src/display/ui.rs +++ b/src/display/ui.rs @@ -33,7 +33,7 @@ where let state = { let mut state = UIState::default(); state.interface_name = opts.interface.clone(); - state.unit_family = opts.render_opts.unit_family; + state.unit_family = opts.render_opts.unit_family.into(); state.cumulative_mode = opts.render_opts.total_utilization; state }; |