Skip to content

Commit

Permalink
Adds list pretty printer
Browse files Browse the repository at this point in the history
  • Loading branch information
solomon-b committed Dec 5, 2022
1 parent 9b81e8b commit 46e62eb
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/CofreeBot/Bot/Behaviors/Lists.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ listsBot' = Bot $ \s -> \case
t' <- fmap snd $ runBot listItemBot t action
pure ("List Updated", Map.insert name t' s)
DeleteList name -> pure ("List deleted", Map.delete name s)
ShowList name -> pure (T.pack $ show $ Map.lookup name s, s)
ShowList name -> pure (prettyListM name $ Map.lookup name s, s)

prettyList :: T.Text -> IntMap T.Text -> T.Text
prettyList name list = name <> ":\n" <> foldr (\(i, x) acc -> T.pack (show i) <> ". " <> x <> "\n" <> acc) mempty (IntMap.toList list)

prettyListM :: T.Text -> Maybe (IntMap T.Text) -> T.Text
prettyListM name = \case
Nothing -> "List '" <> name <> "' not found."
Just l -> prettyList name l

listsBot :: Monad m => Bot m (Map T.Text (IntMap T.Text)) T.Text T.Text
listsBot = dimap (parseOnly parseListAction) indistinct $ emptyBot \/ listsBot'
Expand Down

0 comments on commit 46e62eb

Please sign in to comment.