Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit b754d06

Browse files
committed
[CBR-97] pretty print 'SlotId'
Signed-off-by: Alexander Diemand <[email protected]>
1 parent cf4c65e commit b754d06

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

core/src/Pos/Core/Slotting/SlotId.hs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import Universum
2828
import Control.Lens (Iso', iso, lens, makeLensesFor)
2929
import Data.Aeson.TH (defaultOptions, deriveJSON)
3030
import Data.SafeCopy (base, deriveSafeCopySimple)
31-
import Formatting (Format, bprint, build, ords, (%))
31+
import Data.Text (pack)
32+
import Data.Text.Lazy.Builder (fromText)
33+
import Formatting (Format, bprint, build, later, (%))
3234
import qualified Formatting.Buildable as Buildable
3335

3436
import Pos.Binary.Class (Cons (..), Field (..), deriveSimpleBi)
@@ -50,7 +52,25 @@ data SlotId = SlotId
5052

5153
instance Buildable SlotId where
5254
build SlotId {..} =
53-
bprint (ords%" slot of "%ords%" epoch") (getSlotIndex siSlot) siEpoch
55+
bprint (intords%" slot of "%intords%" epoch")
56+
(getSlotIndex siSlot) (getEpochIndex siEpoch)
57+
58+
-- | temporary reimplementation of 'ords' from "Formatting"
59+
-- because the original function converts the integer value to a real number
60+
intords :: (Show n, Integral n) => Format r (n -> r)
61+
intords = later go
62+
where
63+
fmt = fromText . pack . show
64+
go n
65+
| tens > 3 && tens < 21 = fmt n <> "th"
66+
| otherwise =
67+
fmt n <>
68+
case n `mod` 10 of
69+
1 -> "st"
70+
2 -> "nd"
71+
3 -> "rd"
72+
_ -> "th"
73+
where tens = n `mod` 100
5474

5575
instance NFData SlotId
5676

0 commit comments

Comments
 (0)