Skip to content

Commit 6a32568

Browse files
committed
Generate recipe table
1 parent 94ee4d6 commit 6a32568

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/Swarm/DocGen.hs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import Data.Yaml.Aeson (prettyPrintParseException)
4444
import Swarm.Game.Display (displayChar)
4545
import Swarm.Game.Entity (Entity, EntityMap (entitiesByName), entityDisplay, entityName, loadEntities)
4646
import Swarm.Game.Entity qualified as E
47-
import Swarm.Game.Recipe (Recipe, loadRecipes, recipeInputs, recipeOutputs, recipeRequirements)
47+
import Swarm.Game.Recipe (Recipe, loadRecipes, recipeInputs, recipeOutputs, recipeRequirements, recipeTime, recipeWeight)
4848
import Swarm.Game.Robot (installedDevices, instantiateRobot, robotInventory)
4949
import Swarm.Game.Scenario (Scenario, loadScenario, scenarioRobots)
5050
import Swarm.Game.WorldGen (testWorld2Entites)
@@ -113,13 +113,16 @@ generateDocs = \case
113113
Capabilities -> simpleErrorHandle $ do
114114
entities <- loadEntities >>= guardRight "load entities"
115115
liftIO $ T.putStrLn $ capabilityPage address entities
116-
Recipes -> error "Recipes are not implemented"
117116
Entities -> simpleErrorHandle $ do
118117
let loadEntityList fp = left (from . prettyPrintParseException) <$> decodeFileEither fp
119118
let f = "entities.yaml"
120119
Just fileName <- liftIO $ getDataFileNameSafe f
121120
entities <- liftIO (loadEntityList fileName) >>= guardRight "load entities"
122121
liftIO $ T.putStrLn $ entitiesPage address entities
122+
Recipes -> simpleErrorHandle $ do
123+
entities <- loadEntities >>= guardRight "load entities"
124+
recipes <- loadRecipes entities >>= guardRight "load recipes"
125+
liftIO $ T.putStrLn $ recipePage address recipes
123126

124127
-- ----------------------------------------------------------------------------
125128
-- GENERATE KEYWORDS: LIST OF WORDS TO BE HIGHLIGHTED
@@ -360,6 +363,40 @@ entitiesPage _a es =
360363
]
361364
<> map entityToSection es
362365

366+
-- -------------
367+
-- RECIPES
368+
-- -------------
369+
370+
recipeHeader :: [Text]
371+
recipeHeader = ["In", "Out", "Required", "Time", "Weight"]
372+
373+
recipeRow :: PageAddress -> Recipe Entity -> [Text]
374+
recipeRow PageAddress {..} r =
375+
map
376+
escapeTable
377+
[ T.intercalate ", " (map formatCE $ view recipeInputs r)
378+
, T.intercalate ", " (map formatCE $ view recipeOutputs r)
379+
, T.intercalate ", " (map formatCE $ view recipeRequirements r)
380+
, tshow $ view recipeTime r
381+
, tshow $ view recipeWeight r
382+
]
383+
where
384+
formatCE (c, e) = T.unwords [tshow c, linkEntity $ view entityName e]
385+
linkEntity t =
386+
if T.null entityAddress
387+
then t
388+
else addLink (entityAddress <> "#" <> T.replace " " "-" t) t
389+
390+
recipeTable :: PageAddress -> [Recipe Entity] -> Text
391+
recipeTable a rs = T.unlines $ header <> map (listToRow mw) recipeRows
392+
where
393+
mw = maxWidths (recipeHeader : recipeRows)
394+
recipeRows = map (recipeRow a) rs
395+
header = [listToRow mw recipeHeader, separatingLine mw]
396+
397+
recipePage :: PageAddress -> [Recipe Entity] -> Text
398+
recipePage = recipeTable
399+
363400
-- ----------------------------------------------------------------------------
364401
-- GENERATE GRAPHVIZ: ENTITY DEPENDENCIES BY RECIPES
365402
-- ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)