From f7cfd5d8355e440ae0fe773b7065077645a27800 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 30 Nov 2019 11:52:06 -0500 Subject: [PATCH 1/4] allow lower case - allow strPrompt to accept lower case letters - implemented ucStrPrompt which forces characters to upper case --- src/Mtlstats/Prompt.hs | 39 +++++++++++++++++++++++++-------------- src/Mtlstats/Types.hs | 10 +++++----- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/Mtlstats/Prompt.hs b/src/Mtlstats/Prompt.hs index 85e585d..bf5930c 100644 --- a/src/Mtlstats/Prompt.hs +++ b/src/Mtlstats/Prompt.hs @@ -28,6 +28,7 @@ module Mtlstats.Prompt ( promptControllerWith, promptController, strPrompt, + ucStrPrompt, numPrompt, selectPrompt, -- * Individual prompts @@ -46,7 +47,7 @@ import Control.Monad.Extra (whenJust) import Control.Monad.Trans.State (gets, modify) import Data.Char (isDigit, toUpper) import Data.Foldable (forM_) -import Lens.Micro ((^.), (&), (.~), (?~)) +import Lens.Micro ((^.), (&), (.~), (?~), (%~)) import Lens.Micro.Extras (view) import Text.Read (readMaybe) import qualified UI.NCurses as C @@ -68,10 +69,8 @@ promptHandler p (C.EventCharacter '\n') = do val <- gets $ view inputBuffer modify $ inputBuffer .~ "" promptAction p val -promptHandler p (C.EventCharacter c) = let - c' = toUpper c - in when (promptCharCheck p c') $ - modify $ addChar c' +promptHandler p (C.EventCharacter c) = + modify $ inputBuffer %~ promptProcessChar p c promptHandler _ (C.EventSpecialKey C.KeyBackspace) = modify removeChar promptHandler p (C.EventSpecialKey k) = @@ -111,12 +110,22 @@ strPrompt -- ^ The callback function for the result -> Prompt strPrompt pStr act = Prompt - { promptDrawer = drawSimplePrompt pStr - , promptCharCheck = const True - , promptAction = act - , promptSpecialKey = const $ return () + { promptDrawer = drawSimplePrompt pStr + , promptProcessChar = \ch -> (++ [ch]) + , promptAction = act + , promptSpecialKey = const $ return () } +-- | Creates an upper case string prompt +ucStrPrompt + :: String + -- ^ The prompt string + -> (String -> Action ()) + -- ^ The callback function for the result + -> Prompt +ucStrPrompt pStr act = (ucStrPrompt pStr act) + { promptProcessChar = \ch -> (++ [toUpper ch]) } + -- | Builds a numeric prompt numPrompt :: String @@ -125,10 +134,12 @@ numPrompt -- ^ The callback function for the result -> Prompt numPrompt pStr act = Prompt - { promptDrawer = drawSimplePrompt pStr - , promptCharCheck = isDigit - , promptAction = \inStr -> forM_ (readMaybe inStr) act - , promptSpecialKey = const $ return () + { promptDrawer = drawSimplePrompt pStr + , promptProcessChar = \ch str -> if isDigit ch + then str ++ [ch] + else str + , promptAction = \inStr -> forM_ (readMaybe inStr) act + , promptSpecialKey = const $ return () } -- | Builds a selection prompt @@ -146,7 +157,7 @@ selectPrompt params = Prompt in "F" ++ show n ++ ") " ++ desc) results C.moveCursor row col - , promptCharCheck = const True + , promptProcessChar = \ch -> (++[ch]) , promptAction = \sStr -> if null sStr then spCallback params Nothing else do diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index 6403c90..de0c66e 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -599,13 +599,13 @@ instance ToJSON GameStats where -- | Defines a user prompt data Prompt = Prompt - { promptDrawer :: ProgState -> C.Update () + { promptDrawer :: ProgState -> C.Update () -- ^ Draws the prompt to the screen - , promptCharCheck :: Char -> Bool - -- ^ Determines whether or not the character is valid - , promptAction :: String -> Action () + , promptProcessChar :: Char -> String -> String + -- ^ Modifies the string based on the character entered + , promptAction :: String -> Action () -- ^ Action to perform when the value is entered - , promptSpecialKey :: C.Key -> Action () + , promptSpecialKey :: C.Key -> Action () -- ^ Action to perform when a special key is pressed } From 8af7974c8fa67c5e3c0f0c0b69090ce31c75d734 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 30 Nov 2019 12:54:50 -0500 Subject: [PATCH 2/4] made playerSearch and goalieSearch case insensitive --- src/Mtlstats/Types.hs | 8 +++++--- test/TypesSpec.hs | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index de0c66e..6997628 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -192,6 +192,7 @@ import Data.Aeson , (.!=) , (.=) ) +import Data.Char (toUpper) import Data.List (isInfixOf) import qualified Data.Map as M import Data.Maybe (listToMaybe) @@ -904,7 +905,7 @@ playerSearch -- ^ The matching players with their index numbers playerSearch sStr = filter match . zip [0..] - where match (_, p) = sStr `isInfixOf` (p^.pName) + where match (_, p) = map toUpper sStr `isInfixOf` map toUpper (p^.pName) -- | Searches for a player by exact match on name playerSearchExact @@ -967,8 +968,9 @@ goalieSearch -- ^ The list to search -> [(Int, Goalie)] -- ^ The search results with their corresponding index numbers -goalieSearch sStr = filter (\(_, goalie) -> sStr `isInfixOf` (goalie^.gName)) . - zip [0..] +goalieSearch sStr = + filter match . zip [0..] + where match (_, g) = map toUpper sStr `isInfixOf` map toUpper (g^.gName) -- | Searches a list of goalies for an exact match goalieSearchExact diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index 294d17d..1744669 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -591,7 +591,7 @@ playerSearchSpec = describe "playerSearch" $ mapM_ ps = [joe, bob, steve] in playerSearch sStr ps `shouldBe` expected) -- search, result - [ ( "Joe", [(0, joe)] ) + [ ( "joe", [(0, joe)] ) , ( "o", [(0, joe), (1, bob)] ) , ( "e", [(0, joe), (2, steve)] ) , ( "x", [] ) @@ -725,8 +725,8 @@ goalieSearchSpec = describe "goalieSearch" $ do goalieSearch "x" goalies `shouldBe` [] context "exact match" $ - it "should return Steve" $ - goalieSearch "Bob" goalies `shouldBe` [result 1] + it "should return Bob" $ + goalieSearch "bob" goalies `shouldBe` [result 1] goalieSearchExactSpec :: Spec goalieSearchExactSpec = describe "goalieSearchExact" $ do From 05af93996343c62a2743ffe1d31e7237b1a10ccd Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 30 Nov 2019 13:02:42 -0500 Subject: [PATCH 3/4] force player position to upper case --- src/Mtlstats/Prompt.hs | 2 +- src/Mtlstats/Prompt/EditPlayer.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mtlstats/Prompt.hs b/src/Mtlstats/Prompt.hs index bf5930c..5f43c06 100644 --- a/src/Mtlstats/Prompt.hs +++ b/src/Mtlstats/Prompt.hs @@ -191,7 +191,7 @@ playerNamePrompt = strPrompt "Player name: " $ -- | Prompts for a new player's position playerPosPrompt :: Prompt -playerPosPrompt = strPrompt "Player position: " $ +playerPosPrompt = ucStrPrompt "Player position: " $ modify . (progMode.createPlayerStateL.cpsPosition .~) -- | Prompts tor the goalie's number diff --git a/src/Mtlstats/Prompt/EditPlayer.hs b/src/Mtlstats/Prompt/EditPlayer.hs index dc6bb7e..ff44a79 100644 --- a/src/Mtlstats/Prompt/EditPlayer.hs +++ b/src/Mtlstats/Prompt/EditPlayer.hs @@ -51,7 +51,7 @@ editPlayerNamePrompt = strPrompt "Player name: " $ -- | Prompt to edit a player's position editPlayerPosPrompt :: Prompt -editPlayerPosPrompt = strPrompt "Player position: " $ +editPlayerPosPrompt = ucStrPrompt "Player position: " $ editPlayer . (pPosition .~) -- | Prompt to edit a player's year-to-date goals From 2f4e963e41f6f99a15b60d3276665987a133e59d Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 30 Nov 2019 21:09:24 -0500 Subject: [PATCH 4/4] update change log --- ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog.md b/ChangeLog.md index 48daafd..e2211a1 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -2,6 +2,7 @@ ## current - Bugfix: removed quotation marks from goalie names in report +- Allow lower case player names ## 0.7.0 - Shortened views to fit within 25 lines