-- |
-- Module      : Data.Hourglass.Utils
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- some padding / formatting functions
--
module Data.Hourglass.Utils where

-- | pad a number to 2 digits
pad2 :: (Show a, Ord a, Num a, Integral a) => a -> String
pad2 :: a -> String
pad2 a
v | a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
100   = a -> String
forall a. (Show a, Ord a, Num a, Integral a) => a -> String
pad2 (a
v a -> a -> a
forall a. Integral a => a -> a -> a
`mod` a
100)
       | a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
10    = a -> String
forall a. Show a => a -> String
show a
v
       | Bool
otherwise  = Char
'0' Char -> String -> String
forall a. a -> [a] -> [a]
: a -> String
forall a. Show a => a -> String
show a
v

-- | pad a number to 4 digits
pad4 :: (Show a, Ord a, Num a, Integral a) => a -> String
pad4 :: a -> String
pad4 a
v | a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
1000  = a -> String
forall a. Show a => a -> String
show a
v
       | a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
100   = Char
'0' Char -> String -> String
forall a. a -> [a] -> [a]
: a -> String
forall a. Show a => a -> String
show a
v
       | a
v a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= a
10    = Char
'0'Char -> String -> String
forall a. a -> [a] -> [a]
:Char
'0' Char -> String -> String
forall a. a -> [a] -> [a]
: a -> String
forall a. Show a => a -> String
show a
v
       | Bool
otherwise  = Char
'0'Char -> String -> String
forall a. a -> [a] -> [a]
:Char
'0'Char -> String -> String
forall a. a -> [a] -> [a]
:Char
'0'Char -> String -> String
forall a. a -> [a] -> [a]
: a -> String
forall a. Show a => a -> String
show a
v

-- | Pad a number to at least N digits.
--
-- if the number is greater, no truncation happens.
padN :: (Show a, Ord a, Num a, Integral a) => Int -> a -> String
padN :: Int -> a -> String
padN Int
n a
v
    | Int
vlen Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n = String
vs
    | Bool
otherwise = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
vlen) Char
'0' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
vs
  where vs :: String
vs = a -> String
forall a. Show a => a -> String
show a
v
        vlen :: Int
vlen = String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
vs