Skip to content

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

Description

@natalie-o-perret

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions