Text.Format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS -fno-warn-orphans #-}

module Text.Format
  (now,later,HoleyMonoid
  ,module Control.Category
  ,format
  ,integer
  ,int
  ,str
  ,pad
  ,epoch
  ,pico
--  ,b64
  ,hex)
  where

import           Control.Category ((.),id)
import           Data.Default
import           Data.HoleyMonoid
import           Data.String
import           Data.String.ToString
import           Data.Time
import           Numeric
import           Prelude hiding ((.),id,map)

-- | Format a string.
format :: HoleyMonoid m m a -> a
format = run

instance (IsString m, a ~ r) => IsString (HoleyMonoid m r a) where
  fromString = now . fromString

-- | Format an int.
int :: HoleyMonoid String r (Int -> r)
int = later show

-- | Format an integer.
integer :: HoleyMonoid String r (Integer -> r)
integer = later show

-- | Format a string.
str :: ToString string => HoleyMonoid String r (string -> r)
str = later toString

-- | Pad the output of a holey monoid.
pad :: Char -> Int -> HoleyMonoid String r (a -> r) -> HoleyMonoid String r (a -> r)
pad c i = map (reverse . take i . (++ replicate i c) . reverse)

-- | Print the epoch seconds of a time.
epoch :: FormatTime t => HoleyMonoid Int r (t -> r)
epoch = later (read . formatTime def "%s")

-- | Print the picoseconds of a time.
pico :: FormatTime t => HoleyMonoid Int r (t -> r)
pico = later (read . formatTime def "%q")

-- | Base-64 encode a number.
-- b64 :: Integral i => HoleyMonoid i r (a -> r) -> HoleyMonoid String r (a -> r)
-- b64 = map (UTF8.toString . base64EncodeIntegral) where
--   base64EncodeIntegral = B64.encode . toBS where
--   toBS = B.pack . fmap (fromIntegral . fromEnum) . flip showInt ""

-- | Show a number at base 16.
hex :: (Show i,Integral i) => HoleyMonoid i r (a -> r) -> HoleyMonoid String r (a -> r)
hex = map (flip showHex "")
70:11: Warning: Use section
Found:
(flip showHex "")
Why not:
(`showHex` "")