mirror of
https://github.com/CrystalSplitter/ghcitui.git
synced 2024-09-11 10:55:29 +03:00
This moves the ghcitui libs which don't require Brick to ghcitui-core, and the other library files to ghcitui-brick.
56 lines
1.3 KiB
Haskell
56 lines
1.3 KiB
Haskell
module Ghcitui.Util (showT, splitBy, linesToText, clamp, getNumDigits, formatDigits) where
|
|
|
|
import Data.Text (Text, breakOn, drop, length, pack)
|
|
import Prelude hiding (drop, length)
|
|
|
|
-- | Split text based on a delimiter.
|
|
splitBy
|
|
:: Text
|
|
-- ^ Delimeter.
|
|
-> Text
|
|
-- ^ Text to split on.
|
|
-> [Text]
|
|
splitBy "" source = [source]
|
|
splitBy delim source =
|
|
case breakOn delim source of
|
|
(l, "") -> [l]
|
|
(l, r) -> l : splitBy delim (drop (length delim) r)
|
|
|
|
-- | Convert Strings to Text.
|
|
linesToText :: [String] -> Text
|
|
linesToText = pack . Prelude.unlines
|
|
|
|
-- | 'show' but to Text.
|
|
showT :: (Show a) => a -> Text
|
|
showT = pack . show
|
|
|
|
-- Return the number of digits in a given integral
|
|
getNumDigits :: (Integral a) => a -> Int
|
|
getNumDigits 0 = 1
|
|
getNumDigits num = truncate (logBase 10 (fromIntegral num) :: Double) + 1
|
|
|
|
-- | Format digits into a string with padding.
|
|
formatDigits
|
|
:: Int
|
|
-- ^ Number of spaces
|
|
-> Int
|
|
-- ^ Number to format digits of
|
|
-> Text
|
|
-- ^ Formatted Text
|
|
formatDigits spacing num = pack (replicate amount ' ') <> pack (show num)
|
|
where
|
|
amount = spacing - getNumDigits num
|
|
|
|
clamp
|
|
:: (Ord a)
|
|
=> (a, a)
|
|
-- ^ The minimum and maximum (inclusive)
|
|
-> a
|
|
-- ^ Value to clamp
|
|
-> a
|
|
-- ^ Result
|
|
clamp (mi, mx) v
|
|
| v < mi = mi
|
|
| v > mx = mx
|
|
| otherwise = v
|