module Monky.Examples.Utility
( loopFd
, convertUnit
, convertUnitB
, convertUnitSI
)
where
import Data.Text (Text)
import qualified Data.Text as T
import Formatting
import Control.Monad (unless)
import Control.Concurrent (threadWaitRead)
import System.Posix.Types (Fd)
import Monky.Modules
loopFd
:: h
-> Fd
-> ([MonkyOut] -> IO ())
-> (h -> IO [MonkyOut])
-> IO ()
loopFd h fd r f = do
threadWaitRead fd
out <- f h
unless (null out) (r out)
loopFd h fd r f
convertUnit :: Int -> Text -> Text -> Text -> Text -> Text
convertUnit = flip convertUnitT 1000 . fromIntegral
convertUnitB :: Integral a => a -> Text -> Text
convertUnitB rate b = convertUnitT (fromIntegral rate) 1024 (" " `T.append` b) "ki" "Mi" "Gi"
convertUnitSI :: Integral a => a -> Text -> Text
convertUnitSI rate b = convertUnitT (fromIntegral rate) 1000 b "k" "M" "G"
convertUnitT :: Float -> Int -> Text -> Text -> Text -> Text -> Text
convertUnitT rate step bs ks ms gs
| rate < fromIntegral (kf ) = sformat ((left 4 ' ' %. fixed 0) % stext) rate bs
| rate < fromIntegral (kf * 10 ) = sformat ((left 4 ' ' %. fixed 2) % stext) kv ks
| rate < fromIntegral (kf * 100 ) = sformat ((left 4 ' ' %. fixed 1) % stext) kv ks
| rate < fromIntegral (kf * 1000) = sformat ((left 4 ' ' %. fixed 0) % stext) kv ks
| rate < fromIntegral (mf * 10 ) = sformat ((left 4 ' ' %. fixed 2) % stext) mv ms
| rate < fromIntegral (mf * 100 ) = sformat ((left 4 ' ' %. fixed 1) % stext) mv ms
| rate < fromIntegral (mf * 1000) = sformat ((left 4 ' ' %. fixed 0) % stext) mv ms
| rate < fromIntegral (gf * 10 ) = sformat ((left 4 ' ' %. fixed 2) % stext) gv gs
| rate < fromIntegral (gf * 100 ) = sformat ((left 4 ' ' %. fixed 1) % stext) gv gs
| rate < fromIntegral (gf * 1000) = sformat ((left 4 ' ' %. fixed 0) % stext) gv gs
| otherwise = sformat ((left 4 ' ' %. expt 1) % stext) gv gs
where
kf = 1 * step
mf = kf * step
gf = mf * step
kv = rate / fromIntegral kf
mv = rate / fromIntegral mf
gv = rate / fromIntegral gf