{- |
Module                  : DrCabal.Profile.Format
Copyright               : (c) 2022 Dmitrii Kovanikov
SPDX-License-Identifier : MPL-2.0
Maintainer              : Dmitrii Kovanikov <kovanikov@gmail.com>
Stability               : Experimental
Portability             : Portable

Formatting functions for the @profile@ command.
-}

module DrCabal.Profile.Format
    ( fmt
    , fmtNanos
    , fmtDecimalPlaces
    ) where

import Colourista.Pure (formatWith)
import Data.Text (pack)
import Numeric (showFFloat)


fmt :: [Text] -> Text -> Text
fmt :: [Text] -> Text -> Text
fmt = forall str. (IsString str, Semigroup str) => [str] -> str -> str
formatWith

fmtNanos :: Word64 -> Text
fmtNanos :: Word64 -> Text
fmtNanos Word64
time
    | Word64
time forall a. Ord a => a -> a -> Bool
< Word64
ns  = Text
"0ns"
    | Word64
time forall a. Ord a => a -> a -> Bool
< Word64
mcs = forall b a. (Show a, IsString b) => a -> b
show Word64
nanos   forall a. Semigroup a => a -> a -> a
<> Text
"ns"
    | Word64
time forall a. Ord a => a -> a -> Bool
< Word64
ms  = forall b a. (Show a, IsString b) => a -> b
show Word64
micros  forall a. Semigroup a => a -> a -> a
<> Text
"mcs"
    | Word64
time forall a. Ord a => a -> a -> Bool
< Word64
s   = forall b a. (Show a, IsString b) => a -> b
show Word64
millis  forall a. Semigroup a => a -> a -> a
<> Text
"ms"
    | Word64
time forall a. Ord a => a -> a -> Bool
< Word64
m   = forall b a. (Show a, IsString b) => a -> b
show Word64
seconds forall a. Semigroup a => a -> a -> a
<> Text
"s" forall a. Semigroup a => a -> a -> a
<> Word64 -> Text -> Text
emptyIfZero Word64
millis Text
"ms"
    | Bool
otherwise  = forall b a. (Show a, IsString b) => a -> b
show Word64
minutes forall a. Semigroup a => a -> a -> a
<> Text
"m" forall a. Semigroup a => a -> a -> a
<> Word64 -> Text -> Text
emptyIfZero Word64
seconds Text
"s"
  where
    ns, mcs, ms, s, m :: Word64
    ns :: Word64
ns  = Word64
1
    mcs :: Word64
mcs = Word64
1000 forall a. Num a => a -> a -> a
* Word64
ns
    ms :: Word64
ms  = Word64
1000 forall a. Num a => a -> a -> a
* Word64
mcs
    s :: Word64
s   = Word64
1000 forall a. Num a => a -> a -> a
* Word64
ms
    m :: Word64
m   = Word64
60 forall a. Num a => a -> a -> a
* Word64
s

    nanos :: Word64
    nanos :: Word64
nanos   = Word64
time forall a. Integral a => a -> a -> a
`mod` Word64
mcs
    micros :: Word64
micros  = (Word64
time forall a. Integral a => a -> a -> a
`div` Word64
mcs) forall a. Integral a => a -> a -> a
`mod` Word64
1000
    millis :: Word64
millis  = (Word64
time forall a. Integral a => a -> a -> a
`div` Word64
ms)  forall a. Integral a => a -> a -> a
`mod` Word64
1000
    seconds :: Word64
seconds = (Word64
time forall a. Integral a => a -> a -> a
`div` Word64
s)   forall a. Integral a => a -> a -> a
`mod` Word64
60
    minutes :: Word64
minutes = Word64
time forall a. Integral a => a -> a -> a
`div` Word64
m

    emptyIfZero :: Word64 -> Text -> Text
    emptyIfZero :: Word64 -> Text -> Text
emptyIfZero Word64
0 Text
_    = Text
""
    emptyIfZero Word64
t Text
unit = forall b a. (Show a, IsString b) => a -> b
show Word64
t forall a. Semigroup a => a -> a -> a
<> Text
unit

fmtDecimalPlaces :: Int -> Float -> Text
fmtDecimalPlaces :: Int -> Float -> Text
fmtDecimalPlaces Int
dp Float
f = String -> Text
pack forall a b. (a -> b) -> a -> b
$ forall a. RealFloat a => Maybe Int -> a -> ShowS
showFFloat (forall a. a -> Maybe a
Just Int
dp) Float
f String
""