{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK not-home #-}
#include "version-compatibility-macros.h"
module Prettyprinter.Render.Terminal.Internal (
AnsiStyle(..),
Color(..),
color, colorDull,
bgColor, bgColorDull,
bold, italicized, underlined,
Intensity(..),
Bold(..),
Underlined(..),
Italicized(..),
renderLazy, renderStrict,
renderIO,
putDoc, hPutDoc,
) where
import Control.Applicative
import Data.IORef
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TLB
import qualified System.Console.ANSI as ANSI
import System.IO (Handle, hPutChar, stdout)
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Render.Util.Panic
#if !(SEMIGROUP_MONOID_SUPERCLASS)
import Data.Semigroup
#endif
#if !(MIN_VERSION_base(4,6,0))
modifyIORef' :: IORef a -> (a -> a) -> IO ()
modifyIORef' ref f = do
x <- readIORef ref
let x' = f x
x' `seq` writeIORef ref x'
#endif
data Color = Black | Red | Green | Yellow | Blue | Magenta | Cyan | White
deriving (Eq, Ord, Show)
data Intensity = Vivid | Dull
deriving (Eq, Ord, Show)
data Layer = Foreground | Background
deriving (Eq, Ord, Show)
data Bold = Bold deriving (Eq, Ord, Show)
data Underlined = Underlined deriving (Eq, Ord, Show)
data Italicized = Italicized deriving (Eq, Ord, Show)
color :: Color -> AnsiStyle
color c = mempty { ansiForeground = Just (Vivid, c) }
bgColor :: Color -> AnsiStyle
bgColor c = mempty { ansiBackground = Just (Vivid, c) }
colorDull :: Color -> AnsiStyle
colorDull c = mempty { ansiForeground = Just (Dull, c) }
bgColorDull :: Color -> AnsiStyle
bgColorDull c = mempty { ansiBackground = Just (Dull, c) }
bold :: AnsiStyle
bold = mempty { ansiBold = Just Bold }
italicized :: AnsiStyle
italicized = mempty { ansiItalics = Just Italicized }
underlined :: AnsiStyle
underlined = mempty { ansiUnderlining = Just Underlined }
renderLazy :: SimpleDocStream AnsiStyle -> TL.Text
renderLazy =
let push x = (x :)
unsafePeek [] = panicPeekedEmpty
unsafePeek (x:_) = x
unsafePop [] = panicPoppedEmpty
unsafePop (x:xs) = (x, xs)
go :: [AnsiStyle] -> SimpleDocStream AnsiStyle -> TLB.Builder
go s sds = case sds of
SFail -> panicUncaughtFail
SEmpty -> mempty
SChar c rest -> TLB.singleton c <> go s rest
SText _ t rest -> TLB.fromText t <> go s rest
SLine i rest -> TLB.singleton '\n' <> TLB.fromText (T.replicate i " ") <> go s rest
SAnnPush style rest ->
let currentStyle = unsafePeek s
newStyle = style <> currentStyle
in TLB.fromText (styleToRawText newStyle) <> go (push style s) rest
SAnnPop rest ->
let (_currentStyle, s') = unsafePop s
newStyle = unsafePeek s'
in TLB.fromText (styleToRawText newStyle) <> go s' rest
in TLB.toLazyText . go [mempty]
renderIO :: Handle -> SimpleDocStream AnsiStyle -> IO ()
renderIO h sdoc = do
styleStackRef <- newIORef [mempty]
let push x = modifyIORef' styleStackRef (x :)
unsafePeek = readIORef styleStackRef >>= \tok -> case tok of
[] -> panicPeekedEmpty
x:_ -> pure x
unsafePop = readIORef styleStackRef >>= \tok -> case tok of
[] -> panicPoppedEmpty
x:xs -> writeIORef styleStackRef xs >> pure x
let go = \sds -> case sds of
SFail -> panicUncaughtFail
SEmpty -> pure ()
SChar c rest -> do
hPutChar h c
go rest
SText _ t rest -> do
T.hPutStr h t
go rest
SLine i rest -> do
hPutChar h '\n'
T.hPutStr h (T.replicate i (T.singleton ' '))
go rest
SAnnPush style rest -> do
currentStyle <- unsafePeek
let newStyle = style <> currentStyle
push newStyle
T.hPutStr h (styleToRawText newStyle)
go rest
SAnnPop rest -> do
_currentStyle <- unsafePop
newStyle <- unsafePeek
T.hPutStr h (styleToRawText newStyle)
go rest
go sdoc
readIORef styleStackRef >>= \stack -> case stack of
[] -> panicStyleStackFullyConsumed
[_] -> pure ()
xs -> panicStyleStackNotFullyConsumed (length xs)
panicStyleStackFullyConsumed :: void
panicStyleStackFullyConsumed
= error ("There is no empty style left at the end of rendering" ++
" (but there should be). Please report this as a bug.")
panicStyleStackNotFullyConsumed :: Int -> void
panicStyleStackNotFullyConsumed len
= error ("There are " <> show len <> " styles left at the" ++
"end of rendering (there should be only 1). Please report" ++
" this as a bug.")
data AnsiStyle = SetAnsiStyle
{ ansiForeground :: Maybe (Intensity, Color)
, ansiBackground :: Maybe (Intensity, Color)
, ansiBold :: Maybe Bold
, ansiItalics :: Maybe Italicized
, ansiUnderlining :: Maybe Underlined
} deriving (Eq, Ord, Show)
instance Semigroup AnsiStyle where
cs1 <> cs2 = SetAnsiStyle
{ ansiForeground = ansiForeground cs1 <|> ansiForeground cs2
, ansiBackground = ansiBackground cs1 <|> ansiBackground cs2
, ansiBold = ansiBold cs1 <|> ansiBold cs2
, ansiItalics = ansiItalics cs1 <|> ansiItalics cs2
, ansiUnderlining = ansiUnderlining cs1 <|> ansiUnderlining cs2 }
instance Monoid AnsiStyle where
mempty = SetAnsiStyle Nothing Nothing Nothing Nothing Nothing
mappend = (<>)
styleToRawText :: AnsiStyle -> Text
styleToRawText = T.pack . ANSI.setSGRCode . stylesToSgrs
where
stylesToSgrs :: AnsiStyle -> [ANSI.SGR]
stylesToSgrs (SetAnsiStyle fg bg b i u) = catMaybes
[ Just ANSI.Reset
, fmap (\(intensity, c) -> ANSI.SetColor ANSI.Foreground (convertIntensity intensity) (convertColor c)) fg
, fmap (\(intensity, c) -> ANSI.SetColor ANSI.Background (convertIntensity intensity) (convertColor c)) bg
, fmap (\_ -> ANSI.SetConsoleIntensity ANSI.BoldIntensity) b
, fmap (\_ -> ANSI.SetItalicized True) i
, fmap (\_ -> ANSI.SetUnderlining ANSI.SingleUnderline) u
]
convertIntensity :: Intensity -> ANSI.ColorIntensity
convertIntensity = \i -> case i of
Vivid -> ANSI.Vivid
Dull -> ANSI.Dull
convertColor :: Color -> ANSI.Color
convertColor = \c -> case c of
Black -> ANSI.Black
Red -> ANSI.Red
Green -> ANSI.Green
Yellow -> ANSI.Yellow
Blue -> ANSI.Blue
Magenta -> ANSI.Magenta
Cyan -> ANSI.Cyan
White -> ANSI.White
renderStrict :: SimpleDocStream AnsiStyle -> Text
renderStrict = TL.toStrict . renderLazy
putDoc :: Doc AnsiStyle -> IO ()
putDoc = hPutDoc stdout
hPutDoc :: Handle -> Doc AnsiStyle -> IO ()
hPutDoc h doc = renderIO h (layoutPretty defaultLayoutOptions doc)