mtlstats/src/Mtlstats/Format.hs
2019-09-02 22:56:29 -04:00

104 lines
2.4 KiB
Haskell

{- |
mtlstats
Copyright (C) 2019 Rhéal Lamothe
<rheal.lamothe@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
module Mtlstats.Format
( padNum
, left
, right
, centre
, overlay
, month
) where
-- | Pad an 'Int' with leading zeroes to fit a certain character width
padNum
:: Int
-- ^ The width in characters
-> Int
-- ^ The value to format
-> String
padNum size n
| n < 0 = '-' : padNum (pred size) (-n)
| otherwise = let
str = show n
sLen = length str
pLen = size - sLen
pad = replicate pLen '0'
in pad ++ str
-- | Aligns text to the left within a field (clipping if necessary)
left
:: Int
-- ^ The width of the field
-> String
-- ^ The text to align
-> String
left n str = take n $ str ++ repeat ' '
-- | Aligns text to the right within a field (clipping if necessary)
right
:: Int
-- ^ The width of the field
-> String
-- ^ The text to align
-> String
right n str = reverse $ left n $ reverse str
-- | Aligns text to the centre within a field (clipping if necessary)
centre
:: Int
-- ^ The width of the field
-> String
-- ^ The text to align
-> String
centre n str = let
sLen = length str
pLen = (n - sLen) `div` 2
pad = replicate pLen ' '
in take n $ pad ++ str ++ repeat ' '
-- | Overlays one string on top of another
overlay
:: String
-- ^ The string on the top
-> String
-- ^ The string on the bottom
-> String
overlay (x:xs) (_:ys) = x : overlay xs ys
overlay xs [] = xs
overlay [] ys = ys
-- | Converts a number to a three character month (e.g. @"JAN"@)
month :: Int -> String
month 1 = "JAN"
month 2 = "FEB"
month 3 = "MAR"
month 4 = "APR"
month 5 = "MAY"
month 6 = "JUN"
month 7 = "JUL"
month 8 = "AUG"
month 9 = "SEP"
month 10 = "OCT"
month 11 = "NOV"
month 12 = "DEC"
month _ = ""