From 30142836d9bf512ddb5f27b69ddda960c77219f9 Mon Sep 17 00:00:00 2001 From: solomon Date: Sun, 20 Nov 2022 12:21:39 -0800 Subject: [PATCH] Updates Lists API --- cofree-bot.cabal | 1 + src/CofreeBot/Bot.hs | 5 +- src/CofreeBot/Bot/Behaviors/Lists.hs | 68 +++++++++++++++++++++------- 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/cofree-bot.cabal b/cofree-bot.cabal index 94b689f..1a8320c 100644 --- a/cofree-bot.cabal +++ b/cofree-bot.cabal @@ -103,3 +103,4 @@ library , process , random , vector + , xdg-basedir diff --git a/src/CofreeBot/Bot.hs b/src/CofreeBot/Bot.hs index af8666c..a68427c 100644 --- a/src/CofreeBot/Bot.hs +++ b/src/CofreeBot/Bot.hs @@ -51,9 +51,6 @@ import CofreeBot.Utils import CofreeBot.Utils.ListT import Control.Arrow qualified as Arrow import Control.Exception - ( catch, - throwIO, - ) import Control.Lens ( ifolded, view, @@ -78,8 +75,8 @@ import Data.These import Network.Matrix.Client import Network.Matrix.Client.Lens import System.Directory (createDirectoryIfMissing) +import System.Directory.Internal.Prelude (isDoesNotExistError) import System.IO -import System.IO.Error (isDoesNotExistError) import System.Random -------------------------------------------------------------------------------- diff --git a/src/CofreeBot/Bot/Behaviors/Lists.hs b/src/CofreeBot/Bot/Behaviors/Lists.hs index 855f3a7..761dceb 100644 --- a/src/CofreeBot/Bot/Behaviors/Lists.hs +++ b/src/CofreeBot/Bot/Behaviors/Lists.hs @@ -4,11 +4,15 @@ module CofreeBot.Bot.Behaviors.Lists ) where +-- TODO: I want a bot combinator ala Sessionize that allows you take +-- any bot and save its state to disk on updates. + -------------------------------------------------------------------------------- import CofreeBot.Bot import CofreeBot.Utils (indistinct) import Control.Applicative +import Control.Monad (void) import Data.Attoparsec.ByteString.Char8 ( isSpace, ) @@ -25,7 +29,7 @@ import Data.Text qualified as T data ListItemAction = Insert T.Text | Modify Int T.Text | Remove Int -data ListAction = CreateList T.Text | ModifyList T.Text ListItemAction | RemoveList T.Text | List T.Text +data ListAction = CreateList T.Text | ModifyList T.Text ListItemAction | DeleteList T.Text | ShowList T.Text listItemBot :: Monad m => Bot m (IntMap T.Text) ListItemAction T.Text listItemBot = Bot $ \s -> \case @@ -46,25 +50,57 @@ listsBot' = Bot $ \s -> \case let t = fromMaybe IntMap.empty $ Map.lookup name s t' <- fmap snd $ runBot listItemBot t action pure ("List Updated", Map.insert name t' s) - RemoveList name -> pure ("List deleted", Map.delete name s) - List name -> pure (T.pack $ show $ Map.lookup name s, s) + DeleteList name -> pure ("List deleted", Map.delete name s) + ShowList name -> pure (T.pack $ show $ Map.lookup name s, s) listsBot :: Monad m => Bot m (Map T.Text (IntMap T.Text)) T.Text T.Text listsBot = dimap (parseOnly parseListAction) indistinct $ emptyBot \/ listsBot' parseListAction :: Parser ListAction parseListAction = - parseCreateList <|> parseModifyList <|> parseRemoveList <|> parseListList + parseCreateList + <|> parseDeleteList + <|> parseAddListItem + <|> parseRemoveListItem + <|> parseUpdateListItem + <|> parseShowList where - parseCreateList = "create" *> skipSpace *> fmap CreateList (takeTill isSpace) - parseModifyList = - "add" - *> skipSpace - *> fmap ModifyList (takeTill isSpace) - <*> fmap Insert takeText - parseRemoveList = - "remove" - *> skipSpace - *> fmap ModifyList (takeTill isSpace) - <*> fmap Remove decimal - parseListList = "show" *> skipSpace *> fmap List takeText + parseName = ("📝" <|> "list") *> skipSpace *> takeTill isSpace + parseCreateList = do + name <- parseName + skipSpace + void $ "➕" <|> "create" + skipSpace + pure (CreateList name) + parseDeleteList = do + name <- parseName + skipSpace + void $ "➖" <|> "delete" + skipSpace + pure (DeleteList name) + parseShowList = do + name <- parseName + pure (ShowList name) + parseAddListItem = do + name <- parseName + skipSpace + void ("📝" <|> "add") + skipSpace + item <- takeText + pure (ModifyList name (Insert item)) + parseRemoveListItem = do + name <- parseName + skipSpace + void ("✔️" <|> "remove") + skipSpace + key <- decimal + pure (ModifyList name (Remove key)) + parseUpdateListItem = do + name <- parseName + skipSpace + void "update" + skipSpace + key <- decimal + skipSpace + item <- takeText + pure (ModifyList name (Modify key item))