-----------------------------------------------------------------------------
-- |
-- Copyright   :  (C) 2011-2019 Edward Kmett
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  Edward Kmett <ekmett@gmail.com>
-- Stability   :  experimental
-- Portability :  non-portable
--
-- Utility functions to augment the prettyprinter library's interface.
----------------------------------------------------------------------------

module Text.Trifecta.Util.Pretty
  ( AnsiStyle
  , renderIO
  -- * Rendering
  , char
  -- * Styles
  , bold
  , debold
  , underlined
  , deunderline
  -- * Compatibility shims
  , renderPretty
  , columns
  ) where


import Prettyprinter
import Prettyprinter.Render.Terminal
import Prettyprinter.Render.Terminal.Internal (ansiBold, ansiUnderlining)

char :: Char -> Doc a
char :: forall a. Char -> Doc a
char = forall a ann. Pretty a => a -> Doc ann
pretty

renderPretty :: Double -> Int -> Doc AnsiStyle -> SimpleDocStream AnsiStyle
renderPretty :: Double -> Int -> Doc AnsiStyle -> SimpleDocStream AnsiStyle
renderPretty Double
ribbonFraction Int
page
  = forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutSmart LayoutOptions { layoutPageWidth :: PageWidth
layoutPageWidth = Int -> Double -> PageWidth
AvailablePerLine Int
page Double
ribbonFraction }

debold, deunderline :: AnsiStyle
debold :: AnsiStyle
debold = forall a. Monoid a => a
mempty { ansiBold :: Maybe Bold
ansiBold = forall a. Maybe a
Nothing }
deunderline :: AnsiStyle
deunderline = forall a. Monoid a => a
mempty { ansiUnderlining :: Maybe Underlined
ansiUnderlining = forall a. Maybe a
Nothing}

columns :: (Maybe Int -> Doc AnsiStyle) -> Doc AnsiStyle
columns :: (Maybe Int -> Doc AnsiStyle) -> Doc AnsiStyle
columns Maybe Int -> Doc AnsiStyle
f = forall ann. (PageWidth -> Doc ann) -> Doc ann
pageWidth (Maybe Int -> Doc AnsiStyle
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. PageWidth -> Maybe Int
toMaybeInt) where
  toMaybeInt :: PageWidth -> Maybe Int
toMaybeInt (AvailablePerLine Int
cpl Double
_) = forall a. a -> Maybe a
Just Int
cpl
  toMaybeInt PageWidth
Unbounded = forall a. Maybe a
Nothing