{-# LANGUAGE CPP #-}
module Text.PrettyPrint.ANSI.Leijen.Internal where
import System.IO (Handle,hPutStr,hPutChar,stdout)
import System.Console.ANSI (Color(..), ColorIntensity(..), ConsoleLayer(..),
Underlining(..), ConsoleIntensity(..),
SGR(..), hSetSGR, setSGRCode)
import Data.String (IsString(..))
import Data.Maybe (catMaybes)
import qualified Data.Semigroup as Semi (Semigroup((<>)))
#if __GLASGOW_HASKELL__ >= 710
import Data.Monoid ((<>))
#elif __GLASGOW_HASKELL__ >= 704
import Data.Monoid (Monoid, mappend, mconcat, mempty, (<>))
#else
import Data.Monoid (Monoid, mappend, mconcat, mempty)
infixr 6 <>
#endif
infixr 6 <+>
infixr 5 </>,<//>,<$>,<$$>
list :: [Doc] -> Doc
list :: [Doc] -> Doc
list = Doc -> Doc -> Doc -> [Doc] -> Doc
encloseSep Doc
lbracket Doc
rbracket Doc
comma
tupled :: [Doc] -> Doc
tupled :: [Doc] -> Doc
tupled = Doc -> Doc -> Doc -> [Doc] -> Doc
encloseSep Doc
lparen Doc
rparen Doc
comma
semiBraces :: [Doc] -> Doc
semiBraces :: [Doc] -> Doc
semiBraces = Doc -> Doc -> Doc -> [Doc] -> Doc
encloseSep Doc
lbrace Doc
rbrace Doc
semi
encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc
encloseSep :: Doc -> Doc -> Doc -> [Doc] -> Doc
encloseSep Doc
left Doc
right Doc
sep [Doc]
ds
= case [Doc]
ds of
[] -> Doc
left Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
right
[Doc
d] -> Doc
left Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
d Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
right
[Doc]
_ -> Doc -> Doc
align ([Doc] -> Doc
cat ((Doc -> Doc -> Doc) -> [Doc] -> [Doc] -> [Doc]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
(<>) (Doc
left Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
: Doc -> [Doc]
forall a. a -> [a]
repeat Doc
sep) [Doc]
ds) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
right)
punctuate :: Doc -> [Doc] -> [Doc]
punctuate :: Doc -> [Doc] -> [Doc]
punctuate Doc
p [] = []
punctuate Doc
p [Doc
d] = [Doc
d]
punctuate Doc
p (Doc
d:[Doc]
ds) = (Doc
d Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
p) Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
: Doc -> [Doc] -> [Doc]
punctuate Doc
p [Doc]
ds
sep :: [Doc] -> Doc
sep :: [Doc] -> Doc
sep = Doc -> Doc
group (Doc -> Doc) -> ([Doc] -> Doc) -> [Doc] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
vsep
fillSep :: [Doc] -> Doc
fillSep :: [Doc] -> Doc
fillSep = (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold Doc -> Doc -> Doc
(</>)
hsep :: [Doc] -> Doc
hsep :: [Doc] -> Doc
hsep = (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold Doc -> Doc -> Doc
(<+>)
vsep :: [Doc] -> Doc
vsep :: [Doc] -> Doc
vsep = (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold Doc -> Doc -> Doc
(Text.PrettyPrint.ANSI.Leijen.Internal.<$>)
cat :: [Doc] -> Doc
cat :: [Doc] -> Doc
cat = Doc -> Doc
group (Doc -> Doc) -> ([Doc] -> Doc) -> [Doc] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
vcat
fillCat :: [Doc] -> Doc
fillCat :: [Doc] -> Doc
fillCat = (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold Doc -> Doc -> Doc
(<//>)
hcat :: [Doc] -> Doc
hcat :: [Doc] -> Doc
hcat = (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
(<>)
vcat :: [Doc] -> Doc
vcat :: [Doc] -> Doc
vcat = (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold Doc -> Doc -> Doc
(<$$>)
fold :: (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold :: (Doc -> Doc -> Doc) -> [Doc] -> Doc
fold Doc -> Doc -> Doc
f [] = Doc
empty
fold Doc -> Doc -> Doc
f [Doc]
ds = (Doc -> Doc -> Doc) -> [Doc] -> Doc
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Doc -> Doc -> Doc
f [Doc]
ds
#if __GLASGOW_HASKELL__ < 704
(<>) :: Doc -> Doc -> Doc
x <> y = x `beside` y
#endif
(<+>) :: Doc -> Doc -> Doc
Doc
x <+> :: Doc -> Doc -> Doc
<+> Doc
y = Doc
x Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
space Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
y
(</>) :: Doc -> Doc -> Doc
Doc
x </> :: Doc -> Doc -> Doc
</> Doc
y = Doc
x Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
softline Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
y
(<//>) :: Doc -> Doc -> Doc
Doc
x <//> :: Doc -> Doc -> Doc
<//> Doc
y = Doc
x Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
softbreak Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
y
(<$>) :: Doc -> Doc -> Doc
Doc
x <$> :: Doc -> Doc -> Doc
<$> Doc
y = Doc
x Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
line Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
y
(<$$>) :: Doc -> Doc -> Doc
Doc
x <$$> :: Doc -> Doc -> Doc
<$$> Doc
y = Doc
x Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
linebreak Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
y
softline :: Doc
softline :: Doc
softline = Doc -> Doc
group Doc
line
softbreak :: Doc
softbreak :: Doc
softbreak = Doc -> Doc
group Doc
linebreak
squotes :: Doc -> Doc
squotes :: Doc -> Doc
squotes = Doc -> Doc -> Doc -> Doc
enclose Doc
squote Doc
squote
dquotes :: Doc -> Doc
dquotes :: Doc -> Doc
dquotes = Doc -> Doc -> Doc -> Doc
enclose Doc
dquote Doc
dquote
braces :: Doc -> Doc
braces :: Doc -> Doc
braces = Doc -> Doc -> Doc -> Doc
enclose Doc
lbrace Doc
rbrace
parens :: Doc -> Doc
parens :: Doc -> Doc
parens = Doc -> Doc -> Doc -> Doc
enclose Doc
lparen Doc
rparen
angles :: Doc -> Doc
angles :: Doc -> Doc
angles = Doc -> Doc -> Doc -> Doc
enclose Doc
langle Doc
rangle
brackets :: Doc -> Doc
brackets :: Doc -> Doc
brackets = Doc -> Doc -> Doc -> Doc
enclose Doc
lbracket Doc
rbracket
enclose :: Doc -> Doc -> Doc -> Doc
enclose :: Doc -> Doc -> Doc -> Doc
enclose Doc
l Doc
r Doc
x = Doc
l Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
x Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
r
lparen :: Doc
lparen :: Doc
lparen = Char -> Doc
char Char
'('
rparen :: Doc
rparen :: Doc
rparen = Char -> Doc
char Char
')'
langle :: Doc
langle :: Doc
langle = Char -> Doc
char Char
'<'
rangle :: Doc
rangle :: Doc
rangle = Char -> Doc
char Char
'>'
lbrace :: Doc
lbrace :: Doc
lbrace = Char -> Doc
char Char
'{'
rbrace :: Doc
rbrace :: Doc
rbrace = Char -> Doc
char Char
'}'
lbracket :: Doc
lbracket :: Doc
lbracket = Char -> Doc
char Char
'['
rbracket :: Doc
rbracket :: Doc
rbracket = Char -> Doc
char Char
']'
squote :: Doc
squote :: Doc
squote = Char -> Doc
char Char
'\''
dquote :: Doc
dquote :: Doc
dquote = Char -> Doc
char Char
'"'
semi :: Doc
semi :: Doc
semi = Char -> Doc
char Char
';'
colon :: Doc
colon :: Doc
colon = Char -> Doc
char Char
':'
comma :: Doc
comma :: Doc
comma = Char -> Doc
char Char
','
space :: Doc
space :: Doc
space = Char -> Doc
char Char
' '
dot :: Doc
dot :: Doc
dot = Char -> Doc
char Char
'.'
backslash :: Doc
backslash :: Doc
backslash = Char -> Doc
char Char
'\\'
equals :: Doc
equals :: Doc
equals = Char -> Doc
char Char
'='
string :: String -> Doc
string :: String -> Doc
string String
"" = Doc
empty
string (Char
'\n':String
s) = Doc
line Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
string String
s
string String
s = case ((Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/=Char
'\n') String
s) of
(String
xs,String
ys) -> String -> Doc
text String
xs Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> String -> Doc
string String
ys
bool :: Bool -> Doc
bool :: Bool -> Doc
bool Bool
b = String -> Doc
text (Bool -> String
forall a. Show a => a -> String
show Bool
b)
int :: Int -> Doc
int :: Int -> Doc
int Int
i = String -> Doc
text (Int -> String
forall a. Show a => a -> String
show Int
i)
integer :: Integer -> Doc
integer :: Integer -> Doc
integer Integer
i = String -> Doc
text (Integer -> String
forall a. Show a => a -> String
show Integer
i)
float :: Float -> Doc
float :: Float -> Doc
float Float
f = String -> Doc
text (Float -> String
forall a. Show a => a -> String
show Float
f)
double :: Double -> Doc
double :: Double -> Doc
double Double
d = String -> Doc
text (Double -> String
forall a. Show a => a -> String
show Double
d)
rational :: Rational -> Doc
rational :: Rational -> Doc
rational Rational
r = String -> Doc
text (Rational -> String
forall a. Show a => a -> String
show Rational
r)
class Pretty a where
pretty :: a -> Doc
prettyList :: [a] -> Doc
prettyList = [Doc] -> Doc
list ([Doc] -> Doc) -> ([a] -> [Doc]) -> [a] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc
forall a. Pretty a => a -> Doc
pretty
instance Pretty a => Pretty [a] where
pretty :: [a] -> Doc
pretty = [a] -> Doc
forall a. Pretty a => [a] -> Doc
prettyList
instance Pretty Doc where
pretty :: Doc -> Doc
pretty = Doc -> Doc
forall a. a -> a
id
instance Pretty () where
pretty :: () -> Doc
pretty () = String -> Doc
text String
"()"
instance Pretty Bool where
pretty :: Bool -> Doc
pretty Bool
b = Bool -> Doc
bool Bool
b
instance Pretty Char where
pretty :: Char -> Doc
pretty Char
c = Char -> Doc
char Char
c
prettyList :: String -> Doc
prettyList String
s = String -> Doc
string String
s
instance Pretty Int where
pretty :: Int -> Doc
pretty Int
i = Int -> Doc
int Int
i
instance Pretty Integer where
pretty :: Integer -> Doc
pretty Integer
i = Integer -> Doc
integer Integer
i
instance Pretty Float where
pretty :: Float -> Doc
pretty Float
f = Float -> Doc
float Float
f
instance Pretty Double where
pretty :: Double -> Doc
pretty Double
d = Double -> Doc
double Double
d
instance (Pretty a,Pretty b) => Pretty (a,b) where
pretty :: (a, b) -> Doc
pretty (a
x,b
y) = [Doc] -> Doc
tupled [a -> Doc
forall a. Pretty a => a -> Doc
pretty a
x, b -> Doc
forall a. Pretty a => a -> Doc
pretty b
y]
instance (Pretty a,Pretty b,Pretty c) => Pretty (a,b,c) where
pretty :: (a, b, c) -> Doc
pretty (a
x,b
y,c
z)= [Doc] -> Doc
tupled [a -> Doc
forall a. Pretty a => a -> Doc
pretty a
x, b -> Doc
forall a. Pretty a => a -> Doc
pretty b
y, c -> Doc
forall a. Pretty a => a -> Doc
pretty c
z]
instance Pretty a => Pretty (Maybe a) where
pretty :: Maybe a -> Doc
pretty Maybe a
Nothing = Doc
empty
pretty (Just a
x) = a -> Doc
forall a. Pretty a => a -> Doc
pretty a
x
fillBreak :: Int -> Doc -> Doc
fillBreak :: Int -> Doc -> Doc
fillBreak Int
f Doc
x = Doc -> (Int -> Doc) -> Doc
width Doc
x (\Int
w ->
if (Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
f) then Int -> Doc -> Doc
nest Int
f Doc
linebreak
else String -> Doc
text (Int -> String
spaces (Int
f Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
w)))
fill :: Int -> Doc -> Doc
fill :: Int -> Doc -> Doc
fill Int
f Doc
d = Doc -> (Int -> Doc) -> Doc
width Doc
d (\Int
w ->
if (Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
f) then Doc
empty
else String -> Doc
text (Int -> String
spaces (Int
f Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
w)))
width :: Doc -> (Int -> Doc) -> Doc
width :: Doc -> (Int -> Doc) -> Doc
width Doc
d Int -> Doc
f = (Int -> Doc) -> Doc
column (\Int
k1 -> Doc
d Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> (Int -> Doc) -> Doc
column (\Int
k2 -> Int -> Doc
f (Int
k2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k1)))
indent :: Int -> Doc -> Doc
indent :: Int -> Doc -> Doc
indent Int
i Doc
d = Int -> Doc -> Doc
hang Int
i (String -> Doc
text (Int -> String
spaces Int
i) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
d)
hang :: Int -> Doc -> Doc
hang :: Int -> Doc -> Doc
hang Int
i Doc
d = Doc -> Doc
align (Int -> Doc -> Doc
nest Int
i Doc
d)
align :: Doc -> Doc
align :: Doc -> Doc
align Doc
d = (Int -> Doc) -> Doc
column (\Int
k ->
(Int -> Doc) -> Doc
nesting (\Int
i -> Int -> Doc -> Doc
nest (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) Doc
d))
data Doc = Fail
| Empty
| Char Char
| Text !Int String
| Line
| FlatAlt Doc Doc
| Cat Doc Doc
| Nest !Int Doc
| Union Doc Doc
| Column (Int -> Doc)
| Columns (Maybe Int -> Doc)
| Nesting (Int -> Doc)
| Color ConsoleLayer ColorIntensity
Color Doc
| Intensify ConsoleIntensity Doc
| Italicize Bool Doc
| Underline Underlining Doc
| RestoreFormat (Maybe (ColorIntensity, Color))
(Maybe (ColorIntensity, Color))
(Maybe ConsoleIntensity)
(Maybe Bool)
(Maybe Underlining)
data SimpleDoc = SFail
| SEmpty
| SChar Char SimpleDoc
| SText !Int String SimpleDoc
| SLine !Int SimpleDoc
| SSGR [SGR] SimpleDoc
instance Monoid Doc where
mempty :: Doc
mempty = Doc
empty
mappend :: Doc -> Doc -> Doc
mappend = Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
(Semi.<>)
mconcat :: [Doc] -> Doc
mconcat = [Doc] -> Doc
hcat
instance Semi.Semigroup Doc where
<> :: Doc -> Doc -> Doc
(<>) = Doc -> Doc -> Doc
beside
instance IsString Doc where
fromString :: String -> Doc
fromString = String -> Doc
text
empty :: Doc
empty :: Doc
empty = Doc
Empty
char :: Char -> Doc
char :: Char -> Doc
char Char
'\n' = Doc
line
char Char
c = Char -> Doc
Char Char
c
text :: String -> Doc
text :: String -> Doc
text String
"" = Doc
Empty
text String
s = Int -> String -> Doc
Text (String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s) String
s
line :: Doc
line :: Doc
line = Doc -> Doc -> Doc
FlatAlt Doc
Line Doc
space
linebreak :: Doc
linebreak :: Doc
linebreak = Doc -> Doc -> Doc
FlatAlt Doc
Line Doc
empty
hardline :: Doc
hardline :: Doc
hardline = Doc
Line
beside :: Doc -> Doc -> Doc
beside :: Doc -> Doc -> Doc
beside Doc
x Doc
y = Doc -> Doc -> Doc
Cat Doc
x Doc
y
nest :: Int -> Doc -> Doc
nest :: Int -> Doc -> Doc
nest Int
i Doc
x = Int -> Doc -> Doc
Nest Int
i Doc
x
column, nesting :: (Int -> Doc) -> Doc
column :: (Int -> Doc) -> Doc
column Int -> Doc
f = (Int -> Doc) -> Doc
Column Int -> Doc
f
nesting :: (Int -> Doc) -> Doc
nesting Int -> Doc
f = (Int -> Doc) -> Doc
Nesting Int -> Doc
f
columns :: (Maybe Int -> Doc) -> Doc
columns :: (Maybe Int -> Doc) -> Doc
columns Maybe Int -> Doc
f = (Maybe Int -> Doc) -> Doc
Columns Maybe Int -> Doc
f
group :: Doc -> Doc
group :: Doc -> Doc
group Doc
x = Doc -> Doc -> Doc
Union (Doc -> Doc
flatten Doc
x) Doc
x
flatAlt :: Doc -> Doc -> Doc
flatAlt :: Doc -> Doc -> Doc
flatAlt = Doc -> Doc -> Doc
FlatAlt
flatten :: Doc -> Doc
flatten :: Doc -> Doc
flatten (FlatAlt Doc
x Doc
y) = Doc
y
flatten (Cat Doc
x Doc
y) = Doc -> Doc -> Doc
Cat (Doc -> Doc
flatten Doc
x) (Doc -> Doc
flatten Doc
y)
flatten (Nest Int
i Doc
x) = Int -> Doc -> Doc
Nest Int
i (Doc -> Doc
flatten Doc
x)
flatten Doc
Line = Doc
Fail
flatten (Union Doc
x Doc
y) = Doc -> Doc
flatten Doc
x
flatten (Column Int -> Doc
f) = (Int -> Doc) -> Doc
Column (Doc -> Doc
flatten (Doc -> Doc) -> (Int -> Doc) -> Int -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc
f)
flatten (Columns Maybe Int -> Doc
f) = (Maybe Int -> Doc) -> Doc
Columns (Doc -> Doc
flatten (Doc -> Doc) -> (Maybe Int -> Doc) -> Maybe Int -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Int -> Doc
f)
flatten (Nesting Int -> Doc
f) = (Int -> Doc) -> Doc
Nesting (Doc -> Doc
flatten (Doc -> Doc) -> (Int -> Doc) -> Int -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc
f)
flatten (Color ConsoleLayer
l ColorIntensity
i Color
c Doc
x) = ConsoleLayer -> ColorIntensity -> Color -> Doc -> Doc
Color ConsoleLayer
l ColorIntensity
i Color
c (Doc -> Doc
flatten Doc
x)
flatten (Intensify ConsoleIntensity
i Doc
x) = ConsoleIntensity -> Doc -> Doc
Intensify ConsoleIntensity
i (Doc -> Doc
flatten Doc
x)
flatten (Italicize Bool
b Doc
x) = Bool -> Doc -> Doc
Italicize Bool
b (Doc -> Doc
flatten Doc
x)
flatten (Underline Underlining
u Doc
x) = Underlining -> Doc -> Doc
Underline Underlining
u (Doc -> Doc
flatten Doc
x)
flatten Doc
other = Doc
other
black :: Doc -> Doc
red :: Doc -> Doc
green :: Doc -> Doc
yellow :: Doc -> Doc
blue :: Doc -> Doc
magenta :: Doc -> Doc
cyan :: Doc -> Doc
white :: Doc -> Doc
dullblack :: Doc -> Doc
dullred :: Doc -> Doc
dullgreen :: Doc -> Doc
dullyellow :: Doc -> Doc
dullblue :: Doc -> Doc
dullmagenta :: Doc -> Doc
dullcyan :: Doc -> Doc
dullwhite :: Doc -> Doc
(Doc -> Doc
black, Doc -> Doc
dullblack) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
Black
(Doc -> Doc
red, Doc -> Doc
dullred) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
Red
(Doc -> Doc
green, Doc -> Doc
dullgreen) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
Green
(Doc -> Doc
yellow, Doc -> Doc
dullyellow) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
Yellow
(Doc -> Doc
blue, Doc -> Doc
dullblue) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
Blue
(Doc -> Doc
magenta, Doc -> Doc
dullmagenta) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
Magenta
(Doc -> Doc
cyan, Doc -> Doc
dullcyan) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
Cyan
(Doc -> Doc
white, Doc -> Doc
dullwhite) = Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
White
color :: Color -> Doc -> Doc
dullcolor :: Color -> Doc -> Doc
color :: Color -> Doc -> Doc
color = ConsoleLayer -> ColorIntensity -> Color -> Doc -> Doc
Color ConsoleLayer
Foreground ColorIntensity
Vivid
dullcolor :: Color -> Doc -> Doc
dullcolor = ConsoleLayer -> ColorIntensity -> Color -> Doc -> Doc
Color ConsoleLayer
Foreground ColorIntensity
Dull
colorFunctions :: Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions :: Color -> (Doc -> Doc, Doc -> Doc)
colorFunctions Color
what = (Color -> Doc -> Doc
color Color
what, Color -> Doc -> Doc
dullcolor Color
what)
onblack :: Doc -> Doc
onred :: Doc -> Doc
ongreen :: Doc -> Doc
onyellow :: Doc -> Doc
onblue :: Doc -> Doc
onmagenta :: Doc -> Doc
oncyan :: Doc -> Doc
onwhite :: Doc -> Doc
ondullblack :: Doc -> Doc
ondullred :: Doc -> Doc
ondullgreen :: Doc -> Doc
ondullyellow :: Doc -> Doc
ondullblue :: Doc -> Doc
ondullmagenta :: Doc -> Doc
ondullcyan :: Doc -> Doc
ondullwhite :: Doc -> Doc
(Doc -> Doc
onblack, Doc -> Doc
ondullblack) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
Black
(Doc -> Doc
onred, Doc -> Doc
ondullred) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
Red
(Doc -> Doc
ongreen, Doc -> Doc
ondullgreen) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
Green
(Doc -> Doc
onyellow, Doc -> Doc
ondullyellow) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
Yellow
(Doc -> Doc
onblue, Doc -> Doc
ondullblue) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
Blue
(Doc -> Doc
onmagenta, Doc -> Doc
ondullmagenta) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
Magenta
(Doc -> Doc
oncyan, Doc -> Doc
ondullcyan) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
Cyan
(Doc -> Doc
onwhite, Doc -> Doc
ondullwhite) = Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
White
oncolor :: Color -> Doc -> Doc
ondullcolor :: Color -> Doc -> Doc
oncolor :: Color -> Doc -> Doc
oncolor = ConsoleLayer -> ColorIntensity -> Color -> Doc -> Doc
Color ConsoleLayer
Background ColorIntensity
Vivid
ondullcolor :: Color -> Doc -> Doc
ondullcolor = ConsoleLayer -> ColorIntensity -> Color -> Doc -> Doc
Color ConsoleLayer
Background ColorIntensity
Dull
oncolorFunctions :: Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions :: Color -> (Doc -> Doc, Doc -> Doc)
oncolorFunctions Color
what = (Color -> Doc -> Doc
oncolor Color
what, Color -> Doc -> Doc
ondullcolor Color
what)
bold :: Doc -> Doc
bold :: Doc -> Doc
bold = ConsoleIntensity -> Doc -> Doc
Intensify ConsoleIntensity
BoldIntensity
debold :: Doc -> Doc
debold :: Doc -> Doc
debold = ConsoleIntensity -> Doc -> Doc
Intensify ConsoleIntensity
NormalIntensity
underline :: Doc -> Doc
underline :: Doc -> Doc
underline = Underlining -> Doc -> Doc
Underline Underlining
SingleUnderline
deunderline :: Doc -> Doc
deunderline :: Doc -> Doc
deunderline = Underlining -> Doc -> Doc
Underline Underlining
NoUnderline
plain :: Doc -> Doc
plain :: Doc -> Doc
plain Doc
Fail = Doc
Fail
plain e :: Doc
e@Doc
Empty = Doc
e
plain c :: Doc
c@(Char Char
_) = Doc
c
plain t :: Doc
t@(Text Int
_ String
_) = Doc
t
plain l :: Doc
l@Doc
Line = Doc
l
plain (FlatAlt Doc
x Doc
y) = Doc -> Doc -> Doc
FlatAlt (Doc -> Doc
plain Doc
x) (Doc -> Doc
plain Doc
y)
plain (Cat Doc
x Doc
y) = Doc -> Doc -> Doc
Cat (Doc -> Doc
plain Doc
x) (Doc -> Doc
plain Doc
y)
plain (Nest Int
i Doc
x) = Int -> Doc -> Doc
Nest Int
i (Doc -> Doc
plain Doc
x)
plain (Union Doc
x Doc
y) = Doc -> Doc -> Doc
Union (Doc -> Doc
plain Doc
x) (Doc -> Doc
plain Doc
y)
plain (Column Int -> Doc
f) = (Int -> Doc) -> Doc
Column (Doc -> Doc
plain (Doc -> Doc) -> (Int -> Doc) -> Int -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc
f)
plain (Columns Maybe Int -> Doc
f) = (Maybe Int -> Doc) -> Doc
Columns (Doc -> Doc
plain (Doc -> Doc) -> (Maybe Int -> Doc) -> Maybe Int -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Int -> Doc
f)
plain (Nesting Int -> Doc
f) = (Int -> Doc) -> Doc
Nesting (Doc -> Doc
plain (Doc -> Doc) -> (Int -> Doc) -> Int -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc
f)
plain (Color ConsoleLayer
_ ColorIntensity
_ Color
_ Doc
x) = Doc -> Doc
plain Doc
x
plain (Intensify ConsoleIntensity
_ Doc
x) = Doc -> Doc
plain Doc
x
plain (Italicize Bool
_ Doc
x) = Doc -> Doc
plain Doc
x
plain (Underline Underlining
_ Doc
x) = Doc -> Doc
plain Doc
x
plain (RestoreFormat Maybe (ColorIntensity, Color)
_ Maybe (ColorIntensity, Color)
_ Maybe ConsoleIntensity
_ Maybe Bool
_ Maybe Underlining
_) = Doc
Empty
data Docs = Nil
| Cons !Int Doc Docs
renderPretty :: Float -> Int -> Doc -> SimpleDoc
renderPretty :: Float -> Int -> Doc -> SimpleDoc
renderPretty = (Int -> Int -> Int -> SimpleDoc -> Bool)
-> Float -> Int -> Doc -> SimpleDoc
renderFits Int -> Int -> Int -> SimpleDoc -> Bool
fits1
renderSmart :: Float -> Int -> Doc -> SimpleDoc
renderSmart :: Float -> Int -> Doc -> SimpleDoc
renderSmart = (Int -> Int -> Int -> SimpleDoc -> Bool)
-> Float -> Int -> Doc -> SimpleDoc
renderFits Int -> Int -> Int -> SimpleDoc -> Bool
fitsR
renderFits :: (Int -> Int -> Int -> SimpleDoc -> Bool)
-> Float -> Int -> Doc -> SimpleDoc
renderFits :: (Int -> Int -> Int -> SimpleDoc -> Bool)
-> Float -> Int -> Doc -> SimpleDoc
renderFits Int -> Int -> Int -> SimpleDoc -> Bool
fits Float
rfrac Int
w Doc
x
= Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
0 Int
0 Maybe (ColorIntensity, Color)
forall a. Maybe a
Nothing Maybe (ColorIntensity, Color)
forall a. Maybe a
Nothing Maybe ConsoleIntensity
forall a. Maybe a
Nothing Maybe Bool
forall a. Maybe a
Nothing Maybe Underlining
forall a. Maybe a
Nothing (Int -> Doc -> Docs -> Docs
Cons Int
0 Doc
x Docs
Nil)
where
r :: Int
r = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
w (Float -> Int
forall a b. (RealFrac a, Integral b) => a -> b
round (Int -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
w Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
rfrac)))
best :: Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
n Int
k Maybe (ColorIntensity, Color)
mb_fc Maybe (ColorIntensity, Color)
mb_bc Maybe ConsoleIntensity
mb_in Maybe Bool
mb_it Maybe Underlining
mb_un Docs
Nil = SimpleDoc
SEmpty
best Int
n Int
k Maybe (ColorIntensity, Color)
mb_fc Maybe (ColorIntensity, Color)
mb_bc Maybe ConsoleIntensity
mb_in Maybe Bool
mb_it Maybe Underlining
mb_un (Cons Int
i Doc
d Docs
ds)
= case Doc
d of
Doc
Fail -> SimpleDoc
SFail
Doc
Empty -> Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k Docs
ds
Char Char
c -> let k' :: Int
k' = Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 in Int -> SimpleDoc -> SimpleDoc
seq Int
k' (Char -> SimpleDoc -> SimpleDoc
SChar Char
c (Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k' Docs
ds))
Text Int
l String
s -> let k' :: Int
k' = Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l in Int -> SimpleDoc -> SimpleDoc
seq Int
k' (Int -> String -> SimpleDoc -> SimpleDoc
SText Int
l String
s (Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k' Docs
ds))
Doc
Line -> Int -> SimpleDoc -> SimpleDoc
SLine Int
i (Int -> Int -> Docs -> SimpleDoc
best_typical Int
i Int
i Docs
ds)
FlatAlt Doc
x Doc
_ -> Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
x Docs
ds)
Cat Doc
x Doc
y -> Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
x (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
y Docs
ds))
Nest Int
j Doc
x -> let i' :: Int
i' = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
j in Int -> SimpleDoc -> SimpleDoc
seq Int
i' (Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i' Doc
x Docs
ds))
Union Doc
x Doc
y -> Int -> Int -> SimpleDoc -> SimpleDoc -> SimpleDoc
nicest Int
n Int
k (Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
x Docs
ds))
(Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
y Docs
ds))
Column Int -> Doc
f -> Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i (Int -> Doc
f Int
k) Docs
ds)
Columns Maybe Int -> Doc
f -> Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i (Maybe Int -> Doc
f (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
w)) Docs
ds)
Nesting Int -> Doc
f -> Int -> Int -> Docs -> SimpleDoc
best_typical Int
n Int
k (Int -> Doc -> Docs -> Docs
Cons Int
i (Int -> Doc
f Int
i) Docs
ds)
Color ConsoleLayer
l ColorIntensity
t Color
c Doc
x -> [SGR] -> SimpleDoc -> SimpleDoc
SSGR [ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
l ColorIntensity
t Color
c] (Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
n Int
k Maybe (ColorIntensity, Color)
mb_fc' Maybe (ColorIntensity, Color)
mb_bc' Maybe ConsoleIntensity
mb_in Maybe Bool
mb_it Maybe Underlining
mb_un (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
x Docs
ds_restore))
where
mb_fc' :: Maybe (ColorIntensity, Color)
mb_fc' = case ConsoleLayer
l of { ConsoleLayer
Background -> Maybe (ColorIntensity, Color)
mb_fc; ConsoleLayer
Foreground -> (ColorIntensity, Color) -> Maybe (ColorIntensity, Color)
forall a. a -> Maybe a
Just (ColorIntensity
t, Color
c) }
mb_bc' :: Maybe (ColorIntensity, Color)
mb_bc' = case ConsoleLayer
l of { ConsoleLayer
Background -> (ColorIntensity, Color) -> Maybe (ColorIntensity, Color)
forall a. a -> Maybe a
Just (ColorIntensity
t, Color
c); ConsoleLayer
Foreground -> Maybe (ColorIntensity, Color)
mb_bc }
Intensify ConsoleIntensity
t Doc
x -> [SGR] -> SimpleDoc -> SimpleDoc
SSGR [ConsoleIntensity -> SGR
SetConsoleIntensity ConsoleIntensity
t] (Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
n Int
k Maybe (ColorIntensity, Color)
mb_fc Maybe (ColorIntensity, Color)
mb_bc (ConsoleIntensity -> Maybe ConsoleIntensity
forall a. a -> Maybe a
Just ConsoleIntensity
t) Maybe Bool
mb_it Maybe Underlining
mb_un (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
x Docs
ds_restore))
Italicize Bool
t Doc
x -> [SGR] -> SimpleDoc -> SimpleDoc
SSGR [Bool -> SGR
SetItalicized Bool
t] (Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
n Int
k Maybe (ColorIntensity, Color)
mb_fc Maybe (ColorIntensity, Color)
mb_bc Maybe ConsoleIntensity
mb_in (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
t) Maybe Underlining
mb_un (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
x Docs
ds_restore))
Underline Underlining
u Doc
x -> [SGR] -> SimpleDoc -> SimpleDoc
SSGR [Underlining -> SGR
SetUnderlining Underlining
u] (Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
n Int
k Maybe (ColorIntensity, Color)
mb_fc Maybe (ColorIntensity, Color)
mb_bc Maybe ConsoleIntensity
mb_in Maybe Bool
mb_it (Underlining -> Maybe Underlining
forall a. a -> Maybe a
Just Underlining
u) (Int -> Doc -> Docs -> Docs
Cons Int
i Doc
x Docs
ds_restore))
RestoreFormat Maybe (ColorIntensity, Color)
mb_fc' Maybe (ColorIntensity, Color)
mb_bc' Maybe ConsoleIntensity
mb_in' Maybe Bool
mb_it' Maybe Underlining
mb_un' -> [SGR] -> SimpleDoc -> SimpleDoc
SSGR [SGR]
sgrs (Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
n Int
k Maybe (ColorIntensity, Color)
mb_fc' Maybe (ColorIntensity, Color)
mb_bc' Maybe ConsoleIntensity
mb_in' Maybe Bool
mb_it' Maybe Underlining
mb_un' Docs
ds)
where
sgrs :: [SGR]
sgrs = SGR
Reset SGR -> [SGR] -> [SGR]
forall a. a -> [a] -> [a]
: [Maybe SGR] -> [SGR]
forall a. [Maybe a] -> [a]
catMaybes [
((ColorIntensity, Color) -> SGR)
-> Maybe (ColorIntensity, Color) -> Maybe SGR
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ColorIntensity -> Color -> SGR) -> (ColorIntensity, Color) -> SGR
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground)) Maybe (ColorIntensity, Color)
mb_fc',
((ColorIntensity, Color) -> SGR)
-> Maybe (ColorIntensity, Color) -> Maybe SGR
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ColorIntensity -> Color -> SGR) -> (ColorIntensity, Color) -> SGR
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Background)) Maybe (ColorIntensity, Color)
mb_bc',
(ConsoleIntensity -> SGR) -> Maybe ConsoleIntensity -> Maybe SGR
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ConsoleIntensity -> SGR
SetConsoleIntensity Maybe ConsoleIntensity
mb_in',
(Bool -> SGR) -> Maybe Bool -> Maybe SGR
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Bool -> SGR
SetItalicized Maybe Bool
mb_it',
(Underlining -> SGR) -> Maybe Underlining -> Maybe SGR
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Underlining -> SGR
SetUnderlining Maybe Underlining
mb_un'
]
where
best_typical :: Int -> Int -> Docs -> SimpleDoc
best_typical Int
n' Int
k' Docs
ds' = Int
-> Int
-> Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Docs
-> SimpleDoc
best Int
n' Int
k' Maybe (ColorIntensity, Color)
mb_fc Maybe (ColorIntensity, Color)
mb_bc Maybe ConsoleIntensity
mb_in Maybe Bool
mb_it Maybe Underlining
mb_un Docs
ds'
ds_restore :: Docs
ds_restore = Int -> Doc -> Docs -> Docs
Cons Int
i (Maybe (ColorIntensity, Color)
-> Maybe (ColorIntensity, Color)
-> Maybe ConsoleIntensity
-> Maybe Bool
-> Maybe Underlining
-> Doc
RestoreFormat Maybe (ColorIntensity, Color)
mb_fc Maybe (ColorIntensity, Color)
mb_bc Maybe ConsoleIntensity
mb_in Maybe Bool
mb_it Maybe Underlining
mb_un) Docs
ds
nicest :: Int -> Int -> SimpleDoc -> SimpleDoc -> SimpleDoc
nicest Int
n Int
k SimpleDoc
x SimpleDoc
y | Int -> Int -> Int -> SimpleDoc -> Bool
fits Int
w (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
n Int
k) Int
width SimpleDoc
x = SimpleDoc
x
| Bool
otherwise = SimpleDoc
y
where
width :: Int
width = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k) (Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n)
fits1 :: Int -> Int -> Int -> SimpleDoc -> Bool
fits1 :: Int -> Int -> Int -> SimpleDoc -> Bool
fits1 Int
_ Int
_ Int
w SimpleDoc
x | Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
False
fits1 Int
_ Int
_ Int
w SimpleDoc
SFail = Bool
False
fits1 Int
_ Int
_ Int
w SimpleDoc
SEmpty = Bool
True
fits1 Int
p Int
m Int
w (SChar Char
c SimpleDoc
x) = Int -> Int -> Int -> SimpleDoc -> Bool
fits1 Int
p Int
m (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) SimpleDoc
x
fits1 Int
p Int
m Int
w (SText Int
l String
s SimpleDoc
x) = Int -> Int -> Int -> SimpleDoc -> Bool
fits1 Int
p Int
m (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) SimpleDoc
x
fits1 Int
_ Int
_ Int
w (SLine Int
i SimpleDoc
x) = Bool
True
fits1 Int
p Int
m Int
w (SSGR [SGR]
_ SimpleDoc
x) = Int -> Int -> Int -> SimpleDoc -> Bool
fits1 Int
p Int
m Int
w SimpleDoc
x
fitsR :: Int -> Int -> Int -> SimpleDoc -> Bool
fitsR :: Int -> Int -> Int -> SimpleDoc -> Bool
fitsR Int
p Int
m Int
w SimpleDoc
x | Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
False
fitsR Int
p Int
m Int
w SimpleDoc
SFail = Bool
False
fitsR Int
p Int
m Int
w SimpleDoc
SEmpty = Bool
True
fitsR Int
p Int
m Int
w (SChar Char
c SimpleDoc
x) = Int -> Int -> Int -> SimpleDoc -> Bool
fitsR Int
p Int
m (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) SimpleDoc
x
fitsR Int
p Int
m Int
w (SText Int
l String
s SimpleDoc
x) = Int -> Int -> Int -> SimpleDoc -> Bool
fitsR Int
p Int
m (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) SimpleDoc
x
fitsR Int
p Int
m Int
w (SLine Int
i SimpleDoc
x) | Int
m Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
i = Int -> Int -> Int -> SimpleDoc -> Bool
fitsR Int
p Int
m (Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) SimpleDoc
x
| Bool
otherwise = Bool
True
fitsR Int
p Int
m Int
w (SSGR [SGR]
_ SimpleDoc
x) = Int -> Int -> Int -> SimpleDoc -> Bool
fitsR Int
p Int
m Int
w SimpleDoc
x
renderCompact :: Doc -> SimpleDoc
renderCompact :: Doc -> SimpleDoc
renderCompact Doc
x
= Int -> [Doc] -> SimpleDoc
scan Int
0 [Doc
x]
where
scan :: Int -> [Doc] -> SimpleDoc
scan Int
k [] = SimpleDoc
SEmpty
scan Int
k (Doc
d:[Doc]
ds) = case Doc
d of
Doc
Fail -> SimpleDoc
SFail
Doc
Empty -> Int -> [Doc] -> SimpleDoc
scan Int
k [Doc]
ds
Char Char
c -> let k' :: Int
k' = Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 in Int -> SimpleDoc -> SimpleDoc
seq Int
k' (Char -> SimpleDoc -> SimpleDoc
SChar Char
c (Int -> [Doc] -> SimpleDoc
scan Int
k' [Doc]
ds))
Text Int
l String
s -> let k' :: Int
k' = Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l in Int -> SimpleDoc -> SimpleDoc
seq Int
k' (Int -> String -> SimpleDoc -> SimpleDoc
SText Int
l String
s (Int -> [Doc] -> SimpleDoc
scan Int
k' [Doc]
ds))
FlatAlt Doc
x Doc
_ -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
xDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Doc
Line -> Int -> SimpleDoc -> SimpleDoc
SLine Int
0 (Int -> [Doc] -> SimpleDoc
scan Int
0 [Doc]
ds)
Cat Doc
x Doc
y -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
xDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:Doc
yDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Nest Int
j Doc
x -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
xDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Union Doc
x Doc
y -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
yDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Column Int -> Doc
f -> Int -> [Doc] -> SimpleDoc
scan Int
k (Int -> Doc
f Int
kDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Columns Maybe Int -> Doc
f -> Int -> [Doc] -> SimpleDoc
scan Int
k (Maybe Int -> Doc
f Maybe Int
forall a. Maybe a
NothingDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Nesting Int -> Doc
f -> Int -> [Doc] -> SimpleDoc
scan Int
k (Int -> Doc
f Int
0Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Color ConsoleLayer
_ ColorIntensity
_ Color
_ Doc
x -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
xDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Intensify ConsoleIntensity
_ Doc
x -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
xDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Italicize Bool
_ Doc
x -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
xDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
Underline Underlining
_ Doc
x -> Int -> [Doc] -> SimpleDoc
scan Int
k (Doc
xDoc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
:[Doc]
ds)
RestoreFormat Maybe (ColorIntensity, Color)
_ Maybe (ColorIntensity, Color)
_ Maybe ConsoleIntensity
_ Maybe Bool
_ Maybe Underlining
_ -> Int -> [Doc] -> SimpleDoc
scan Int
k [Doc]
ds
displayS :: SimpleDoc -> ShowS
displayS :: SimpleDoc -> ShowS
displayS SimpleDoc
SFail = String -> ShowS
forall a. HasCallStack => String -> a
error (String -> ShowS) -> String -> ShowS
forall a b. (a -> b) -> a -> b
$ String
"@SFail@ can not appear uncaught in a " String -> ShowS
forall a. [a] -> [a] -> [a]
++
String
"rendered @SimpleDoc@"
displayS SimpleDoc
SEmpty = ShowS
forall a. a -> a
id
displayS (SChar Char
c SimpleDoc
x) = Char -> ShowS
showChar Char
c ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDoc -> ShowS
displayS SimpleDoc
x
displayS (SText Int
l String
s SimpleDoc
x) = String -> ShowS
showString String
s ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDoc -> ShowS
displayS SimpleDoc
x
displayS (SLine Int
i SimpleDoc
x) = String -> ShowS
showString (Char
'\n'Char -> ShowS
forall a. a -> [a] -> [a]
:Int -> String
indentation Int
i) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDoc -> ShowS
displayS SimpleDoc
x
displayS (SSGR [SGR]
s SimpleDoc
x) = String -> ShowS
showString ([SGR] -> String
setSGRCode [SGR]
s) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDoc -> ShowS
displayS SimpleDoc
x
displayIO :: Handle -> SimpleDoc -> IO ()
displayIO :: Handle -> SimpleDoc -> IO ()
displayIO Handle
handle SimpleDoc
simpleDoc
= SimpleDoc -> IO ()
display SimpleDoc
simpleDoc
where
display :: SimpleDoc -> IO ()
display SimpleDoc
SFail = String -> IO ()
forall a. HasCallStack => String -> a
error (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"@SFail@ can not appear uncaught in a " String -> ShowS
forall a. [a] -> [a] -> [a]
++
String
"rendered @SimpleDoc@"
display SimpleDoc
SEmpty = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
display (SChar Char
c SimpleDoc
x) = do{ Handle -> Char -> IO ()
hPutChar Handle
handle Char
c; SimpleDoc -> IO ()
display SimpleDoc
x}
display (SText Int
l String
s SimpleDoc
x) = do{ Handle -> String -> IO ()
hPutStr Handle
handle String
s; SimpleDoc -> IO ()
display SimpleDoc
x}
display (SLine Int
i SimpleDoc
x) = do{ Handle -> String -> IO ()
hPutStr Handle
handle (Char
'\n'Char -> ShowS
forall a. a -> [a] -> [a]
:Int -> String
indentation Int
i); SimpleDoc -> IO ()
display SimpleDoc
x}
display (SSGR [SGR]
s SimpleDoc
x) = do{ Handle -> [SGR] -> IO ()
hSetSGR Handle
handle [SGR]
s; SimpleDoc -> IO ()
display SimpleDoc
x}
instance Show Doc where
showsPrec :: Int -> Doc -> ShowS
showsPrec Int
d Doc
doc = SimpleDoc -> ShowS
displayS (Float -> Int -> Doc -> SimpleDoc
renderPretty Float
0.4 Int
80 Doc
doc)
putDoc :: Doc -> IO ()
putDoc :: Doc -> IO ()
putDoc Doc
doc = Handle -> Doc -> IO ()
hPutDoc Handle
stdout Doc
doc
hPutDoc :: Handle -> Doc -> IO ()
hPutDoc :: Handle -> Doc -> IO ()
hPutDoc Handle
handle Doc
doc = Handle -> SimpleDoc -> IO ()
displayIO Handle
handle (Float -> Int -> Doc -> SimpleDoc
renderPretty Float
0.4 Int
80 Doc
doc)
spaces :: Int -> String
spaces :: Int -> String
spaces Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = String
""
| Bool
otherwise = Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
n Char
' '
indentation :: Int -> String
indentation :: Int -> String
indentation Int
n = Int -> String
spaces Int
n