{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

{-|
Module:      TextShow.Data.Ratio
Copyright:   (C) 2014-2017 Ryan Scott
License:     BSD-style (see the file LICENSE)
Maintainer:  Ryan Scott
Stability:   Provisional
Portability: GHC

'TextShow' instance for 'Ratio'.

/Since: 2/
-}
module TextShow.Data.Ratio () where

import GHC.Real (Ratio(..), ratioPrec, ratioPrec1)

import Prelude ()
import Prelude.Compat

import TextShow.Classes (TextShow(..), TextShow1(..), showbParen)
import TextShow.Data.Integral ()

-- | /Since: 2/
instance TextShow a => TextShow (Ratio a) where
    {-# SPECIALIZE instance TextShow Rational #-}
    showbPrec :: Int -> Ratio a -> Builder
showbPrec Int
p (a
numer :% a
denom) = Bool -> Builder -> Builder
showbParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ratioPrec) (Builder -> Builder) -> Builder -> Builder
forall a b. (a -> b) -> a -> b
$
           Int -> a -> Builder
forall a. TextShow a => Int -> a -> Builder
showbPrec Int
ratioPrec1 a
numer
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
" % "
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Int -> a -> Builder
forall a. TextShow a => Int -> a -> Builder
showbPrec Int
ratioPrec1 a
denom
    {-# INLINE showbPrec #-}

-- | /Since: 2/
instance TextShow1 Ratio where
    liftShowbPrec :: forall a.
(Int -> a -> Builder)
-> ([a] -> Builder) -> Int -> Ratio a -> Builder
liftShowbPrec Int -> a -> Builder
sp [a] -> Builder
_ Int
p (a
numer :% a
denom) = Bool -> Builder -> Builder
showbParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
ratioPrec) (Builder -> Builder) -> Builder -> Builder
forall a b. (a -> b) -> a -> b
$
           Int -> a -> Builder
sp Int
ratioPrec1 a
numer
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
" % "
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Int -> a -> Builder
sp Int
ratioPrec1 a
denom
    {-# INLINE liftShowbPrec #-}