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

Commit

Permalink
Merge pull request #3483 from input-output-hk/adiemand/CBR-97/pretty-…
Browse files Browse the repository at this point in the history
…print-SlotId

[CBR-97] pretty print 'SlotId'
  • Loading branch information
CodiePP authored Aug 27, 2018
2 parents 4818274 + f24a64c commit e37addf
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions core/src/Pos/Core/Slotting/SlotId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import Universum
import Control.Lens (Iso', iso, lens, makeLensesFor)
import Data.Aeson.TH (defaultOptions, deriveJSON)
import Data.SafeCopy (base, deriveSafeCopySimple)
import Formatting (Format, bprint, build, ords, (%))
import Data.Text (pack)
import Data.Text.Lazy.Builder (fromText)
import Formatting (Format, bprint, build, later, (%))
import qualified Formatting.Buildable as Buildable

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

instance Buildable SlotId where
build SlotId {..} =
bprint (ords%" slot of "%ords%" epoch") (getSlotIndex siSlot) siEpoch
bprint (intords%" slot of "%intords%" epoch")
(getSlotIndex siSlot) (getEpochIndex siEpoch)

-- | temporary reimplementation of 'ords' from "Formatting"
-- because the original function converts the integer value to a real number
intords :: (Show n, Integral n) => Format r (n -> r)
intords = later go
where
fmt = fromText . pack . show
go n
| tens > 3 && tens < 21 = fmt n <> "th"
| otherwise =
fmt n <>
case n `mod` 10 of
1 -> "st"
2 -> "nd"
3 -> "rd"
_ -> "th"
where tens = n `mod` 100

instance NFData SlotId

Expand Down

0 comments on commit e37addf

Please sign in to comment.