Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: add helpers to format pairs of ByteSize in progress-like scenarios #68

Open
natalie-o-perret opened this issue Oct 6, 2022 · 0 comments

Comments

@natalie-o-perret
Copy link

I think helpers to format pairs of 2 ByteSize in progress-like scenarios might come in handy:

(Sorry I like F#)

open ByteSizeLib


[<RequireQualifiedAccess>]
module Tuple2 = let map f (a, b) = f a, f b

[<RequireQualifiedAccess>]
module ByteSize =
    
    let ofBytes bytes = ByteSize(bytes=bytes)
    let ofBits bits = ByteSize(bits=bits)
    
    let getDecValueOfSymbol decSymbol (value: ByteSize) = 
        match decSymbol with
        | ByteSize.PetaByteSymbol -> value.PetaBytes
        | ByteSize.TeraByteSymbol -> value.TeraBytes
        | ByteSize.GigaByteSymbol -> value.GigaBytes
        | ByteSize.MegaByteSymbol -> value.MegaBytes
        | ByteSize.KiloByteSymbol -> value.KiloBytes
        | ByteSize.ByteSymbol     -> value.Bytes
        | _                       -> failwith $"{decSymbol} is not supported a decimal symbol."
    let getBinValueOfSymbol binSymbol (value: ByteSize) = 
        match binSymbol with
        | ByteSize.PebiByteSymbol -> value.PebiBytes
        | ByteSize.TebiByteSymbol -> value.TebiBytes
        | ByteSize.GibiByteSymbol -> value.GibiBytes
        | ByteSize.MebiByteSymbol -> value.MebiBytes
        | ByteSize.KibiByteSymbol -> value.KibiBytes
        | ByteSize.BitSymbol      -> double value.Bits
        | _                       -> failwith $"{binSymbol} is not supported a binary symbol."
        
    let toDecString (value: ByteSize) = value.ToString(value.LargestWholeNumberDecimalSymbol)
    let toBinString (value: ByteSize) = value.ToString(value.LargestWholeNumberBinarySymbol)
        
    let formatDecPair separator (value, maxValue: ByteSize) =
        (getDecValueOfSymbol maxValue.LargestWholeNumberDecimalSymbol value, separator, toDecString maxValue)
        |||> sprintf "%g%s%s"
        
    let formatBinPair separator (value, maxValue: ByteSize) =
        (getBinValueOfSymbol maxValue.LargestWholeNumberBinarySymbol value, separator, toBinString maxValue)
        |||> sprintf "%g%s%s"


[<EntryPoint>]
let main _ =
        
    [ 230.         , 5000000.
      512          , 4500123.
      63120.       , 666400000430.
      63120.       , 23400000435000.
      99312312120. , 12312323406706000000. ]
    |> List.map (Tuple2.map ByteSize.ofBytes)
    |> List.map (ByteSize.formatBinPair @"/")
    |> List.iter (printfn "%s")
        

[<EntryPoint>]
let main _ =
        
    [ 230.         , 5000000.
      512          , 4500123.
      63120.       , 666400000430.
      63120.       , 23400000435000.
      99312312120. , 12312323406706000000. ]
    |> List.map (Tuple2.map ByteSize.ofBytes)
    |> List.map (ByteSize.formatBinaryProgress @"/")
    |> List.iter (printfn "%s")

Output:

0.000219345/4.77 MiB
0.000488281/4.29 MiB
5.87851e-05/620.63 GiB
5.74073e-08/21.28 TiB
8.82071e-05/10935.54 PiB

Found it quite convenient

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant