From 60b0d0eadba1bdb485f8b3ed9e17a03e1555cc67 Mon Sep 17 00:00:00 2001 From: MaxVerevkin <34583604+MaxVerevkin@users.noreply.github.com> Date: Wed, 16 Aug 2023 05:39:07 +0000 Subject: [PATCH] =?UTF-8?q?Deploying=20to=20gh-pages=20from=20@=20greshake?= =?UTF-8?q?/i3status-rust@1370e6b9145fcf49fc9de8ec7040d0eb8d11471a=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- i3status_rs/formatting/formatter/index.html | 2 +- .../formatting/prefix/enum.Prefix.html | 2 +- i3status_rs/formatting/prefix/index.html | 2 +- src/i3status_rs/formatting/formatter.rs.html | 100 +++++++- src/i3status_rs/formatting/prefix.rs.html | 220 +++++++++++++++++- 5 files changed, 315 insertions(+), 11 deletions(-) diff --git a/i3status_rs/formatting/formatter/index.html b/i3status_rs/formatting/formatter/index.html index 4a30ddac93..d8e4d94b24 100644 --- a/i3status_rs/formatting/formatter/index.html +++ b/i3status_rs/formatting/formatter/index.html @@ -1 +1 @@ -i3status_rs::formatting::formatter - Rust

Structs

Constants

Statics

Traits

Functions

\ No newline at end of file +i3status_rs::formatting::formatter - Rust

Structs

Constants

Statics

Traits

Functions

\ No newline at end of file diff --git a/i3status_rs/formatting/prefix/enum.Prefix.html b/i3status_rs/formatting/prefix/enum.Prefix.html index df2d883ffd..5415981446 100644 --- a/i3status_rs/formatting/prefix/enum.Prefix.html +++ b/i3status_rs/formatting/prefix/enum.Prefix.html @@ -27,7 +27,7 @@
§

Gibi

Gi

§

Tera

T

§

Tebi

Ti

-

Implementations§

source§

impl Prefix

source

pub fn min_available() -> Self

source

pub fn max_available() -> Self

source

pub fn max(self, other: Self) -> Self

source

pub fn apply(self, value: f64) -> f64

source

pub fn eng(number: f64) -> Self

source

pub fn eng_binary(number: f64) -> Self

source

pub fn is_binary(&self) -> bool

Trait Implementations§

source§

impl Clone for Prefix

source§

fn clone(&self) -> Prefix

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Prefix

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Prefix

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromStr for Prefix

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl Ord for Prefix

source§

fn cmp(&self, other: &Prefix) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere +

Implementations§

source§

impl Prefix

source

pub fn min_available() -> Self

source

pub fn max_available() -> Self

source

pub fn max(self, other: Self) -> Self

source

pub fn apply(self, value: f64) -> f64

source

pub fn eng(number: f64) -> Self

source

pub fn eng_binary(number: f64) -> Self

source

pub fn is_binary(&self) -> bool

Trait Implementations§

source§

impl Clone for Prefix

source§

fn clone(&self) -> Prefix

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Prefix

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Prefix

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl FromStr for Prefix

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
source§

impl Ord for Prefix

source§

fn cmp(&self, other: &Prefix) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Prefix> for Prefix

source§

fn eq(&self, other: &Prefix) -> bool

This method tests for self and other values to be equal, and is used diff --git a/i3status_rs/formatting/prefix/index.html b/i3status_rs/formatting/prefix/index.html index efe6e80443..ac3d34702b 100644 --- a/i3status_rs/formatting/prefix/index.html +++ b/i3status_rs/formatting/prefix/index.html @@ -1 +1 @@ -i3status_rs::formatting::prefix - Rust
\ No newline at end of file +i3status_rs::formatting::prefix - Rust
\ No newline at end of file diff --git a/src/i3status_rs/formatting/formatter.rs.html b/src/i3status_rs/formatting/formatter.rs.html index 1b3bfa7386..c1774c509c 100644 --- a/src/i3status_rs/formatting/formatter.rs.html +++ b/src/i3status_rs/formatting/formatter.rs.html @@ -652,6 +652,51 @@ 652 653 654 +655 +656 +657 +658 +659 +660 +661 +662 +663 +664 +665 +666 +667 +668 +669 +670 +671 +672 +673 +674 +675 +676 +677 +678 +679 +680 +681 +682 +683 +684 +685 +686 +687 +688 +689 +690 +691 +692 +693 +694 +695 +696 +697 +698 +699
use chrono::format::{Item, StrftimeItems};
 use chrono::{Local, Locale};
 use once_cell::sync::Lazy;
@@ -1097,7 +1142,7 @@
 
                 let sign = if is_negative { "-" } else { "" };
                 let mut retval = match self.0.width as i32 - digits {
-                    i32::MIN..=0 => format!("{sign}{}", val.floor()),
+                    i32::MIN..=0 => format!("{sign}{}", val.round()),
                     1 => format!("{}{sign}{}", self.0.pad_with, val.round() as i64),
                     rest => format!("{sign}{val:.*}", rest as usize - 1),
                 };
@@ -1233,9 +1278,17 @@
 mod tests {
     use super::*;
 
+    macro_rules! fmt {
+    ($name:ident, $($key:ident : $value:tt),*) => {
+        new_formatter(stringify!($name), &[
+            $( Arg { key: stringify!($key), val: stringify!($value) } ),*
+        ]).unwrap()
+    };
+}
+
     #[test]
     fn eng_rounding_and_negatives() {
-        let fmt = new_formatter("eng", &[Arg { key: "w", val: "3" }]).unwrap();
+        let fmt = fmt!(eng, w: 3);
         let config = SharedConfig::default();
 
         let result = fmt
@@ -1260,8 +1313,7 @@
             .unwrap();
         assert_eq!(result, " 10");
 
-        // TODO: This should be " 1KB"
-        let result = fmt
+        let result = fmt
             .format(
                 &Value::Number {
                     val: 999.9,
@@ -1270,7 +1322,7 @@
                 &config,
             )
             .unwrap();
-        assert_eq!(result, "999B");
+        assert_eq!(result, "1.0KB");
 
         let result = fmt
             .format(
@@ -1304,6 +1356,44 @@
             )
             .unwrap();
         assert_eq!(result, " 10");
+
+        let fmt = fmt!(eng, w: 5, p: 1);
+        let result = fmt
+            .format(
+                &Value::Number {
+                    val: 321_600_000_000.,
+                    unit: Unit::Bytes,
+                },
+                &config,
+            )
+            .unwrap();
+        assert_eq!(result, "321.6GB");
+    }
+
+    #[test]
+    fn eng_prefixes() {
+        let config = SharedConfig::default();
+        // 14.96 GiB
+        let val = Value::Number {
+            val: 14.96 * 1024. * 1024. * 1024.,
+            unit: Unit::Bytes,
+        };
+
+        let fmt = fmt!(eng, w: 5, p: Mi);
+        let result = fmt.format(&val, &config).unwrap();
+        assert_eq!(result, "14.96GiB");
+
+        let fmt = fmt!(eng, w: 4, p: Mi);
+        let result = fmt.format(&val, &config).unwrap();
+        assert_eq!(result, "15.0GiB");
+
+        let fmt = fmt!(eng, w: 3, p: Mi);
+        let result = fmt.format(&val, &config).unwrap();
+        assert_eq!(result, " 15GiB");
+
+        let fmt = fmt!(eng, w: 2, p: Mi);
+        let result = fmt.format(&val, &config).unwrap();
+        assert_eq!(result, "15GiB");
     }
 }
 
\ No newline at end of file diff --git a/src/i3status_rs/formatting/prefix.rs.html b/src/i3status_rs/formatting/prefix.rs.html index 9e1edd4775..e0fce9abb8 100644 --- a/src/i3status_rs/formatting/prefix.rs.html +++ b/src/i3status_rs/formatting/prefix.rs.html @@ -151,6 +151,113 @@ 151 152 153 +154 +155 +156 +157 +158 +159 +160 +161 +162 +163 +164 +165 +166 +167 +168 +169 +170 +171 +172 +173 +174 +175 +176 +177 +178 +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260
use crate::errors::*;
 use std::fmt;
 use std::str::FromStr;
@@ -224,11 +331,19 @@
         value / MUL[self as usize]
     }
 
-    pub fn eng(number: f64) -> Self {
+    pub fn eng(mut number: f64) -> Self {
         if number == 0.0 {
             Self::One
         } else {
-            match number.abs().log10().div_euclid(3.) as i32 {
+            number = number.abs();
+            if number > 1.0 {
+                number = number.round();
+            } else {
+                let round_up_to = -(number.log10().ceil() as i32);
+                let m = 10f64.powi(round_up_to);
+                number = (number * m).round() / m;
+            }
+            match number.log10().div_euclid(3.) as i32 {
                 i32::MIN..=-3 => Prefix::Nano,
                 -2 => Prefix::Micro,
                 -1 => Prefix::Milli,
@@ -245,7 +360,7 @@
         if number == 0.0 {
             Self::One
         } else {
-            match number.abs().log2().div_euclid(10.) as i32 {
+            match number.abs().round().log2().div_euclid(10.) as i32 {
                 i32::MIN..=0 => Prefix::OneButBinary,
                 1 => Prefix::Kibi,
                 2 => Prefix::Mebi,
@@ -304,4 +419,103 @@
         }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn eng() {
+        assert_eq!(Prefix::eng(0.000_000_000_1), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_000_001), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_000_01), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_000_1), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_001), Prefix::Micro);
+        assert_eq!(Prefix::eng(0.000_01), Prefix::Micro);
+        assert_eq!(Prefix::eng(0.000_1), Prefix::Micro);
+        assert_eq!(Prefix::eng(0.001), Prefix::Milli);
+        assert_eq!(Prefix::eng(0.01), Prefix::Milli);
+        assert_eq!(Prefix::eng(0.1), Prefix::Milli);
+        assert_eq!(Prefix::eng(1.0), Prefix::One);
+        assert_eq!(Prefix::eng(10.0), Prefix::One);
+        assert_eq!(Prefix::eng(100.0), Prefix::One);
+        assert_eq!(Prefix::eng(1_000.0), Prefix::Kilo);
+        assert_eq!(Prefix::eng(10_000.0), Prefix::Kilo);
+        assert_eq!(Prefix::eng(100_000.0), Prefix::Kilo);
+        assert_eq!(Prefix::eng(1_000_000.0), Prefix::Mega);
+        assert_eq!(Prefix::eng(10_000_000.0), Prefix::Mega);
+        assert_eq!(Prefix::eng(100_000_000.0), Prefix::Mega);
+        assert_eq!(Prefix::eng(1_000_000_000.0), Prefix::Giga);
+        assert_eq!(Prefix::eng(10_000_000_000.0), Prefix::Giga);
+        assert_eq!(Prefix::eng(100_000_000_000.0), Prefix::Giga);
+        assert_eq!(Prefix::eng(1_000_000_000_000.0), Prefix::Tera);
+        assert_eq!(Prefix::eng(10_000_000_000_000.0), Prefix::Tera);
+        assert_eq!(Prefix::eng(100_000_000_000_000.0), Prefix::Tera);
+        assert_eq!(Prefix::eng(1_000_000_000_000_000.0), Prefix::Tera);
+    }
+
+    #[test]
+    fn eng_round() {
+        assert_eq!(Prefix::eng(0.000_000_000_09), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_000_000_9), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_000_009), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_000_09), Prefix::Nano);
+        assert_eq!(Prefix::eng(0.000_000_9), Prefix::Micro);
+        assert_eq!(Prefix::eng(0.000_009), Prefix::Micro);
+        assert_eq!(Prefix::eng(0.000_09), Prefix::Micro);
+        assert_eq!(Prefix::eng(0.000_9), Prefix::Milli);
+        assert_eq!(Prefix::eng(0.009), Prefix::Milli);
+        assert_eq!(Prefix::eng(0.09), Prefix::Milli);
+        assert_eq!(Prefix::eng(0.9), Prefix::One);
+        assert_eq!(Prefix::eng(9.9), Prefix::One);
+        assert_eq!(Prefix::eng(99.9), Prefix::One);
+        assert_eq!(Prefix::eng(999.9), Prefix::Kilo);
+        assert_eq!(Prefix::eng(9_999.9), Prefix::Kilo);
+        assert_eq!(Prefix::eng(99_999.9), Prefix::Kilo);
+        assert_eq!(Prefix::eng(999_999.9), Prefix::Mega);
+        assert_eq!(Prefix::eng(9_999_999.9), Prefix::Mega);
+        assert_eq!(Prefix::eng(99_999_999.9), Prefix::Mega);
+        assert_eq!(Prefix::eng(999_999_999.9), Prefix::Giga);
+        assert_eq!(Prefix::eng(9_999_999_999.9), Prefix::Giga);
+        assert_eq!(Prefix::eng(99_999_999_999.9), Prefix::Giga);
+        assert_eq!(Prefix::eng(999_999_999_999.9), Prefix::Tera);
+        assert_eq!(Prefix::eng(9_999_999_999_999.9), Prefix::Tera);
+        assert_eq!(Prefix::eng(99_999_999_999_999.9), Prefix::Tera);
+        assert_eq!(Prefix::eng(999_999_999_999_999.9), Prefix::Tera);
+    }
+
+    #[test]
+    fn eng_binary() {
+        assert_eq!(Prefix::eng_binary(0.1), Prefix::OneButBinary);
+        assert_eq!(Prefix::eng_binary(1.0), Prefix::OneButBinary);
+        assert_eq!(Prefix::eng_binary((1 << 9) as f64), Prefix::OneButBinary);
+        assert_eq!(Prefix::eng_binary((1 << 10) as f64), Prefix::Kibi);
+        assert_eq!(Prefix::eng_binary((1 << 19) as f64), Prefix::Kibi);
+        assert_eq!(Prefix::eng_binary((1 << 29) as f64), Prefix::Mebi);
+        assert_eq!(Prefix::eng_binary((1 << 20) as f64), Prefix::Mebi);
+        assert_eq!(Prefix::eng_binary((1 << 30) as f64), Prefix::Gibi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 39) as f64), Prefix::Gibi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 40) as f64), Prefix::Tebi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 49) as f64), Prefix::Tebi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 50) as f64), Prefix::Tebi);
+    }
+
+    #[test]
+    fn eng_binary_round() {
+        assert_eq!(Prefix::eng_binary(0.9), Prefix::OneButBinary);
+        assert_eq!(
+            Prefix::eng_binary((1 << 9) as f64 - 0.1),
+            Prefix::OneButBinary
+        );
+        assert_eq!(Prefix::eng_binary((1 << 10) as f64 - 0.1), Prefix::Kibi);
+        assert_eq!(Prefix::eng_binary((1 << 19) as f64 - 0.1), Prefix::Kibi);
+        assert_eq!(Prefix::eng_binary((1 << 29) as f64 - 0.1), Prefix::Mebi);
+        assert_eq!(Prefix::eng_binary((1 << 20) as f64 - 0.1), Prefix::Mebi);
+        assert_eq!(Prefix::eng_binary((1 << 30) as f64 - 0.1), Prefix::Gibi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 39) as f64 - 0.1), Prefix::Gibi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 40) as f64 - 0.1), Prefix::Tebi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 49) as f64 - 0.1), Prefix::Tebi);
+        assert_eq!(Prefix::eng_binary((1_u64 << 50) as f64 - 0.1), Prefix::Tebi);
+    }
+}
 
\ No newline at end of file