1- {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2- {-# LANGUAGE FlexibleInstances #-}
31{-# LANGUAGE OverloadedStrings #-}
42-- | Some pretty printing, with specific constructors for the game
53module Startups.PrettyPrint where
@@ -11,45 +9,46 @@ import Data.Monoid
119import Data.Maybe (fromMaybe )
1210import Data.String
1311import Control.Monad.Error
14- import qualified Data.Sequence as Seq
15- import Data.Sequence (Seq )
1612import qualified Data.Text as T
1713import qualified Data.Foldable as F
1814import Data.List (intersperse )
1915import Control.Lens
2016import qualified Text.PrettyPrint.ANSI.Leijen as PP
2117
22- newtype PrettyDoc = PrettyDoc { getDoc :: Seq PrettyElement }
23- deriving (Eq , Monoid )
24-
25- data PrettyElement = RawText T. Text
26- | NewLine
27- | Space
28- | Emph PrettyDoc
29- | Colorize PColor PrettyDoc
30- | Indent Int PrettyDoc
31- | PDirection EffectDirection
32- | PNeighbor Neighbor
33- | PFund Funding
34- | PPoach Poacher
35- | PVictory VictoryPoint
36- | PPlayerCount PlayerCount
37- | PTurn Turn
38- | PResource Resource
39- | PAge Age
40- | PCompanyStage CompanyStage
41- | PConflict PoachingOutcome
42- | PCompany CompanyProfile
43- | PResearch ResearchType
44- | PCardType CardType
45- deriving Eq
18+ data PrettyDoc = RawText T. Text
19+ | NewLine
20+ | Space
21+ | Emph PrettyDoc
22+ | Colorize PColor PrettyDoc
23+ | Indent Int PrettyDoc
24+ | PDirection EffectDirection
25+ | PNeighbor Neighbor
26+ | PFund Funding
27+ | PPoach Poacher
28+ | PVictory VictoryPoint
29+ | PPlayerCount PlayerCount
30+ | PTurn Turn
31+ | PResource Resource
32+ | PAge Age
33+ | PCompanyStage CompanyStage
34+ | PConflict PoachingOutcome
35+ | PCompany CompanyProfile
36+ | PResearch ResearchType
37+ | PCardType CardType
38+ | PEmpty
39+ | PCat PrettyDoc PrettyDoc
40+ deriving Eq
41+
42+ instance Monoid PrettyDoc where
43+ mappend = PCat
44+ mempty = PEmpty
4645
4746data PColor = PColorCard CardType
4847 | PColorVictory VictoryType
4948 deriving Eq
5049
5150instance IsString PrettyDoc where
52- fromString = PrettyDoc . Seq. singleton . RawText . T. pack
51+ fromString = RawText . T. pack
5352
5453instance Error PrettyDoc where
5554 noMsg = mempty
@@ -61,38 +60,38 @@ class PrettyE a where
6160instance PrettyE PrettyDoc where
6261 pe = id
6362instance PrettyE T. Text where
64- pe = PrettyDoc . Seq. singleton . RawText
63+ pe = RawText
6564instance PrettyE EffectDirection where
66- pe = PrettyDoc . Seq. singleton . PDirection
65+ pe = PDirection
6766instance PrettyE Neighbor where
68- pe = PrettyDoc . Seq. singleton . PNeighbor
67+ pe = PNeighbor
6968instance PrettyE Funding where
70- pe = PrettyDoc . Seq. singleton . PFund
69+ pe = PFund
7170instance PrettyE Poacher where
72- pe = PrettyDoc . Seq. singleton . PPoach
71+ pe = PPoach
7372instance PrettyE VictoryPoint where
74- pe = PrettyDoc . Seq. singleton . PVictory
73+ pe = PVictory
7574instance PrettyE PlayerCount where
76- pe = PrettyDoc . Seq. singleton . PPlayerCount
75+ pe = PPlayerCount
7776instance PrettyE Turn where
78- pe = PrettyDoc . Seq. singleton . PTurn
77+ pe = PTurn
7978instance PrettyE Resource where
80- pe = PrettyDoc . Seq. singleton . PResource
79+ pe = PResource
8180instance PrettyE Age where
82- pe = PrettyDoc . Seq. singleton . PAge
81+ pe = PAge
8382instance PrettyE CompanyStage where
84- pe = PrettyDoc . Seq. singleton . PCompanyStage
83+ pe = PCompanyStage
8584instance PrettyE PoachingOutcome where
86- pe = PrettyDoc . Seq. singleton . PConflict
85+ pe = PConflict
8786instance PrettyE CompanyProfile where
88- pe = PrettyDoc . Seq. singleton . PCompany
87+ pe = PCompany
8988instance PrettyE ResearchType where
90- pe = PrettyDoc . Seq. singleton . PResearch
89+ pe = PResearch
9190instance PrettyE CardType where
92- pe = PrettyDoc . Seq. singleton . PCardType
91+ pe = PCardType
9392
94- instance (PrettyE a , F. Foldable f ) => PrettyE ( f a ) where
95- pe l = brackets $ sepBy (pchar ' ,' ) (map pe (F. toList l))
93+ foldPretty :: (PrettyE a , F. Foldable f ) => f a -> PrettyDoc
94+ foldPretty l = brackets $ sepBy (pchar ' ,' ) (map pe (F. toList l))
9695
9796victory :: VictoryPoint -> VictoryType -> PrettyDoc
9897victory v vc = withVictoryColor vc (pe v)
@@ -101,7 +100,7 @@ brackets :: PrettyDoc -> PrettyDoc
101100brackets e = pchar ' [' <> e <> pchar ' ]'
102101
103102space :: PrettyDoc
104- space = PrettyDoc ( Seq. singleton Space )
103+ space = Space
105104
106105sepBy :: F. Foldable f => PrettyDoc -> f PrettyDoc -> PrettyDoc
107106sepBy sep = mconcat . intersperse sep . F. toList
@@ -110,31 +109,31 @@ pchar :: Char -> PrettyDoc
110109pchar = pe . T. singleton
111110
112111withCardColor :: CardType -> PrettyDoc -> PrettyDoc
113- withCardColor c = PrettyDoc . Seq. singleton . Colorize (PColorCard c)
112+ withCardColor c = Colorize (PColorCard c)
114113
115114withVictoryColor :: VictoryType -> PrettyDoc -> PrettyDoc
116- withVictoryColor v = PrettyDoc . Seq. singleton . Colorize (PColorVictory v)
115+ withVictoryColor v = Colorize (PColorVictory v)
117116
118117numerical :: Integral n => n -> PrettyDoc
119118numerical = fromString . (show :: Integer -> String ) . fromIntegral
120119
121120emph :: PrettyDoc -> PrettyDoc
122- emph = PrettyDoc . Seq. singleton . Emph
121+ emph = Emph
123122
124123pcost :: Cost -> PrettyDoc
125124pcost (Cost r m) = F. foldMap pe r <> pe m
126125
127126indent :: Int -> PrettyDoc -> PrettyDoc
128- indent d = PrettyDoc . Seq. singleton . Indent d
127+ indent = Indent
129128
130129(<+>) :: PrettyDoc -> PrettyDoc -> PrettyDoc
131- a <+> b = a <> PrettyDoc ( Seq. singleton Space ) <> b
130+ a <+> b = a <> space <> b
132131
133132(</>) :: PrettyDoc -> PrettyDoc -> PrettyDoc
134133a </> b = a <> newline <> b
135134
136135newline :: PrettyDoc
137- newline = PrettyDoc ( Seq. singleton NewLine )
136+ newline = NewLine
138137
139138vcat :: [PrettyDoc ] -> PrettyDoc
140139vcat = mconcat . intersperse newline
@@ -167,20 +166,17 @@ cardName card = case card of
167166 CompanyCard c s _ _ -> pe c <+> pe s
168167
169168shortCard :: Card -> PrettyDoc
170- shortCard card = cardName card <+> pcost (card ^. cCost) <+> pe (map cardEffectShort (card ^. cEffect))
169+ shortCard card = cardName card <+> pcost (card ^. cCost) <+> foldPretty (map cardEffectShort (card ^. cEffect))
171170
172171longCard :: Card -> PrettyDoc
173172longCard c = shortCard c <> page
174173 <> if null grt
175174 then mempty
176- else mempty <+> " - Free:" <+> pe grt
175+ else mempty <+> " - Free:" <+> foldPretty grt
177176 where
178177 grt = c ^.. cFree . traverse . to pe
179178 page = fromMaybe mempty (c ^? cAge . to pe . to brackets)
180179
181- instance PP. Pretty PrettyDoc where
182- pretty = F. foldMap PP. pretty . getDoc
183-
184180prettyColor :: PColor -> PP. Doc -> PP. Doc
185181prettyColor (PColorCard c) = case c of
186182 BaseResource -> id
@@ -199,8 +195,10 @@ prettyColor (PColorVictory v) = case v of
199195 CommercialVictory -> PP. dullred
200196 CommunityVictory -> PP. magenta
201197
202- instance PP. Pretty PrettyElement where
198+ instance PP. Pretty PrettyDoc where
203199 pretty e = case e of
200+ PEmpty -> PP. empty
201+ PCat a b -> PP. pretty a <> PP. pretty b
204202 RawText t -> PP. string (T. unpack t)
205203 NewLine -> PP. linebreak
206204 Space -> PP. space
0 commit comments