Skip to content

Commit 654f0f4

Browse files
committed
Refactor PrettyPrint to remove useless types
1 parent 0bdcf97 commit 654f0f4

1 file changed

Lines changed: 57 additions & 59 deletions

File tree

Startups/PrettyPrint.hs

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2-
{-# LANGUAGE FlexibleInstances #-}
31
{-# LANGUAGE OverloadedStrings #-}
42
-- | Some pretty printing, with specific constructors for the game
53
module Startups.PrettyPrint where
@@ -11,45 +9,46 @@ import Data.Monoid
119
import Data.Maybe (fromMaybe)
1210
import Data.String
1311
import Control.Monad.Error
14-
import qualified Data.Sequence as Seq
15-
import Data.Sequence (Seq)
1612
import qualified Data.Text as T
1713
import qualified Data.Foldable as F
1814
import Data.List (intersperse)
1915
import Control.Lens
2016
import 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

4746
data PColor = PColorCard CardType
4847
| PColorVictory VictoryType
4948
deriving Eq
5049

5150
instance IsString PrettyDoc where
52-
fromString = PrettyDoc . Seq.singleton . RawText . T.pack
51+
fromString = RawText . T.pack
5352

5453
instance Error PrettyDoc where
5554
noMsg = mempty
@@ -61,38 +60,38 @@ class PrettyE a where
6160
instance PrettyE PrettyDoc where
6261
pe = id
6362
instance PrettyE T.Text where
64-
pe = PrettyDoc . Seq.singleton . RawText
63+
pe = RawText
6564
instance PrettyE EffectDirection where
66-
pe = PrettyDoc . Seq.singleton . PDirection
65+
pe = PDirection
6766
instance PrettyE Neighbor where
68-
pe = PrettyDoc . Seq.singleton . PNeighbor
67+
pe = PNeighbor
6968
instance PrettyE Funding where
70-
pe = PrettyDoc . Seq.singleton . PFund
69+
pe = PFund
7170
instance PrettyE Poacher where
72-
pe = PrettyDoc . Seq.singleton . PPoach
71+
pe = PPoach
7372
instance PrettyE VictoryPoint where
74-
pe = PrettyDoc . Seq.singleton . PVictory
73+
pe = PVictory
7574
instance PrettyE PlayerCount where
76-
pe = PrettyDoc . Seq.singleton . PPlayerCount
75+
pe = PPlayerCount
7776
instance PrettyE Turn where
78-
pe = PrettyDoc . Seq.singleton . PTurn
77+
pe = PTurn
7978
instance PrettyE Resource where
80-
pe = PrettyDoc . Seq.singleton . PResource
79+
pe = PResource
8180
instance PrettyE Age where
82-
pe = PrettyDoc . Seq.singleton . PAge
81+
pe = PAge
8382
instance PrettyE CompanyStage where
84-
pe = PrettyDoc . Seq.singleton . PCompanyStage
83+
pe = PCompanyStage
8584
instance PrettyE PoachingOutcome where
86-
pe = PrettyDoc . Seq.singleton . PConflict
85+
pe = PConflict
8786
instance PrettyE CompanyProfile where
88-
pe = PrettyDoc . Seq.singleton . PCompany
87+
pe = PCompany
8988
instance PrettyE ResearchType where
90-
pe = PrettyDoc . Seq.singleton . PResearch
89+
pe = PResearch
9190
instance 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

9796
victory :: VictoryPoint -> VictoryType -> PrettyDoc
9897
victory v vc = withVictoryColor vc (pe v)
@@ -101,7 +100,7 @@ brackets :: PrettyDoc -> PrettyDoc
101100
brackets e = pchar '[' <> e <> pchar ']'
102101

103102
space :: PrettyDoc
104-
space = PrettyDoc (Seq.singleton Space)
103+
space = Space
105104

106105
sepBy :: F.Foldable f => PrettyDoc -> f PrettyDoc -> PrettyDoc
107106
sepBy sep = mconcat . intersperse sep . F.toList
@@ -110,31 +109,31 @@ pchar :: Char -> PrettyDoc
110109
pchar = pe . T.singleton
111110

112111
withCardColor :: CardType -> PrettyDoc -> PrettyDoc
113-
withCardColor c = PrettyDoc . Seq.singleton . Colorize (PColorCard c)
112+
withCardColor c = Colorize (PColorCard c)
114113

115114
withVictoryColor :: VictoryType -> PrettyDoc -> PrettyDoc
116-
withVictoryColor v = PrettyDoc . Seq.singleton . Colorize (PColorVictory v)
115+
withVictoryColor v = Colorize (PColorVictory v)
117116

118117
numerical :: Integral n => n -> PrettyDoc
119118
numerical = fromString . (show :: Integer -> String) . fromIntegral
120119

121120
emph :: PrettyDoc -> PrettyDoc
122-
emph = PrettyDoc . Seq.singleton . Emph
121+
emph = Emph
123122

124123
pcost :: Cost -> PrettyDoc
125124
pcost (Cost r m) = F.foldMap pe r <> pe m
126125

127126
indent :: 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
134133
a </> b = a <> newline <> b
135134

136135
newline :: PrettyDoc
137-
newline = PrettyDoc (Seq.singleton NewLine)
136+
newline = NewLine
138137

139138
vcat :: [PrettyDoc] -> PrettyDoc
140139
vcat = mconcat . intersperse newline
@@ -167,20 +166,17 @@ cardName card = case card of
167166
CompanyCard c s _ _ -> pe c <+> pe s
168167

169168
shortCard :: 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

172171
longCard :: Card -> PrettyDoc
173172
longCard 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-
184180
prettyColor :: PColor -> PP.Doc -> PP.Doc
185181
prettyColor (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

Comments
 (0)