{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE FlexibleInstances #-}

module HaskellWorks.Data.MQuery.Row where

import Text.PrettyPrint.ANSI.Leijen

import qualified Data.DList as DL

type MaxChars = Int

data Row a = Row MaxChars a

instance Pretty a => Pretty (Row (DL.DList a)) where
  pretty :: Row (DList a) -> Doc
pretty (Row MaxChars
maxChars DList a
xs) = [Doc] -> Doc
vcat (((Doc -> Doc
bold (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc
yellow) (String -> Doc
text String
"==> ") Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>) (Doc -> Doc) -> [Doc] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
`map` [Doc]
prettyRows)
    where prettyRows :: [Doc]
          prettyRows :: [Doc]
prettyRows = (\a
row -> String -> Doc
text (MaxChars -> String -> String
forall a. MaxChars -> [a] -> [a]
take MaxChars
maxChars (SimpleDoc -> String -> String
displayS (Doc -> SimpleDoc
renderCompact (a -> Doc
forall a. Pretty a => a -> Doc
pretty a
row)) []))) (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
`map` DList a -> [a]
forall a. DList a -> [a]
DL.toList DList a
xs

prettyRowOfString :: Show a => Row (DL.DList a) -> Doc
prettyRowOfString :: Row (DList a) -> Doc
prettyRowOfString (Row MaxChars
_ DList a
xs) = [Doc] -> Doc
vcat (((Doc -> Doc
bold (Doc -> Doc) -> (Doc -> Doc) -> Doc -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc -> Doc
yellow) (String -> Doc
text String
"==> ") Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<>) (Doc -> Doc) -> [Doc] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
`map` [Doc]
prettyRows)
  where prettyRows :: [Doc]
        prettyRows :: [Doc]
prettyRows = (String -> Doc
text (String -> Doc) -> (a -> String) -> a -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show) (a -> Doc) -> [a] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
`map` DList a -> [a]
forall a. DList a -> [a]
DL.toList DList a
xs