#include "version-compatibility-macros.h"
module Data.Text.Prettyprint.Doc.Render.Terminal.Internal where
import Control.Applicative
import Data.Maybe
import Data.Semigroup
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Builder as TLB
import qualified Data.Text.Lazy.IO as TL
import qualified System.Console.ANSI as ANSI
import System.IO (Handle, stdout)
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Render.Util.Panic
import Data.Text.Prettyprint.Doc.Render.Util.StackMachine
#if !(APPLICATIVE_MONAD)
import Control.Applicative
#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 doc
= let (resultBuilder, remainingStyles) = execStackMachine [mempty] (build doc)
in case remainingStyles of
[] -> error ("There is no empty style left at the end of rendering" ++
" (but there should be). Please report this as a bug.")
[_] -> TLB.toLazyText resultBuilder
xs -> error ("There are " <> show (length xs) <> " styles left at the" ++
"end of rendering (there should be only 1). Please report" ++
" this as a bug.")
build :: SimpleDocStream AnsiStyle -> StackMachine TLB.Builder AnsiStyle ()
build = \case
SFail -> panicUncaughtFail
SEmpty -> pure ()
SChar c x -> do
writeOutput (TLB.singleton c)
build x
SText _l t x -> do
writeOutput (TLB.fromText t)
build x
SLine i x -> do
writeOutput (TLB.singleton '\n')
writeOutput (TLB.fromText (T.replicate i " "))
build x
SAnnPush s x -> do
currentStyle <- unsafePeekStyle
let newStyle = s <> currentStyle
writeOutput (styleToRaw newStyle)
pushStyle newStyle
build x
SAnnPop x -> do
_currentStyle <- unsafePopStyle
newStyle <- unsafePeekStyle
writeOutput (styleToRaw newStyle)
build x
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 = (<>)
styleToRaw :: AnsiStyle -> TLB.Builder
styleToRaw = TLB.fromString . 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 = \case
Vivid -> ANSI.Vivid
Dull -> ANSI.Dull
convertColor :: Color -> ANSI.Color
convertColor = \case
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
renderIO :: Handle -> SimpleDocStream AnsiStyle -> IO ()
renderIO h sdoc = TL.hPutStrLn h (renderLazy sdoc)
putDoc :: Doc AnsiStyle -> IO ()
putDoc = hPutDoc stdout
hPutDoc :: Handle -> Doc AnsiStyle -> IO ()
hPutDoc h doc = renderIO h (layoutPretty defaultLayoutOptions doc)