module Logic.Judge.Writer
( Format(LaTeX, Plain)
, writeHeader
, writeBody
, writeFooter
, write
, plainprint
, prettyprint
) where
import "base" GHC.IO.Handle (Handle, hIsTerminalDevice)
import "base" GHC.IO.Handle.FD (stdout, stderr)
import "bytestring" Data.ByteString (hPut)
import "terminal-size" System.Console.Terminal.Size (size, width)
import qualified "ansi-wl-pprint" Text.PrettyPrint.ANSI.Leijen as PP
import qualified "utf8-string" Data.ByteString.UTF8 as UTF8
import Logic.Judge.Writer.Plain (Printable, pretty)
import Logic.Judge.Writer.LaTeX (LaTeX, latexHeader, latexFooter, latex)
data Format
= LaTeX
| Plain
deriving (Show, Read)
writeHeader :: Handle -> Format -> IO ()
writeHeader file format = case format of
LaTeX -> write file latexHeader
_ -> return ()
writeBody :: (LaTeX a, Printable a) => Handle -> Format -> a -> IO ()
writeBody file format = write file . case format of
LaTeX -> latex
Plain -> pretty
writeFooter :: Handle -> Format -> IO ()
writeFooter file format = case format of
LaTeX -> write file latexFooter
_ -> return ()
write :: Handle -> PP.Doc -> IO ()
write file doc = do
terminal <- hIsTerminalDevice file
if terminal
then prettyprint file doc
else plainprint file doc
prettyprint :: Handle -> PP.Doc -> IO ()
prettyprint file doc = do
columns <- maybe 79 width `fmap` size
PP.displayIO file
. (PP.renderPretty 1.0 columns)
. (PP.<> PP.line)
. PP.fill columns
$ doc
plainprint :: Handle -> PP.Doc -> IO ()
plainprint file doc = hPut file
. UTF8.fromString
. flip PP.displayS ""
. (PP.renderPretty 1.0 255)
. PP.plain
. (PP.<> PP.line)
$ doc