mtlstats/src/Mtlstats/Prompt.hs

410 lines
12 KiB
Haskell
Raw Normal View History

2019-08-24 16:23:56 -04:00
{- |
mtlstats
2023-05-23 17:22:14 -04:00
Copyright (C) Rhéal Lamothe
2019-08-24 16:23:56 -04:00
<rheal.lamothe@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-}
module Mtlstats.Prompt (
-- * Prompt Functions
promptHandler,
promptControllerWith,
promptController,
2019-08-24 21:15:50 -04:00
strPrompt,
ucStrPrompt,
namePrompt,
2019-08-24 19:02:29 -04:00
numPrompt,
2020-04-15 22:07:56 -04:00
numPromptRange,
2019-12-31 22:44:37 -05:00
numPromptWithFallback,
2020-03-11 03:56:58 -04:00
dbNamePrompt,
2019-10-30 16:57:08 -04:00
selectPrompt,
2019-08-24 19:02:29 -04:00
-- * Individual prompts
getDBPrompt,
newSeasonPrompt,
2019-09-09 22:50:44 -04:00
playerNumPrompt,
playerNamePrompt,
2019-09-19 04:35:01 -04:00
playerPosPrompt,
goalieNumPrompt,
goalieNamePrompt,
2019-09-19 04:35:01 -04:00
selectPlayerPrompt,
2020-04-06 14:46:30 -04:00
selectActivePlayerPrompt,
2019-10-30 00:29:33 -04:00
selectGoaliePrompt,
2020-04-06 15:01:26 -04:00
selectActiveGoaliePrompt,
2020-02-11 23:00:13 -05:00
selectPositionPrompt,
playerToEditPrompt
2019-08-24 16:23:56 -04:00
) where
2023-05-25 19:36:03 -04:00
import Brick.Types (BrickEvent (VtyEvent), Location (Location), Widget)
import Brick.Widgets.Core (hBox, showCursor, str)
2019-08-24 16:23:56 -04:00
import Control.Monad (when)
2019-10-30 16:57:08 -04:00
import Control.Monad.Extra (whenJust)
2023-05-25 19:36:03 -04:00
import Control.Monad.State.Class (gets, modify)
import Data.Char (isAlphaNum, isDigit, toUpper)
2023-05-25 19:36:03 -04:00
import Graphics.Text.Width (safeWcswidth)
import Graphics.Vty.Input.Events
( Event (EvKey)
, Key (KBS, KChar, KEnter, KFun)
2023-05-25 19:36:03 -04:00
)
import Lens.Micro ((^.), (&), (.~), (?~), (%~))
2023-05-25 19:36:03 -04:00
import Lens.Micro.Mtl ((.=), use)
2019-08-24 16:23:56 -04:00
import Text.Read (readMaybe)
import Mtlstats.Actions
import Mtlstats.Config
2020-02-11 23:00:13 -05:00
import Mtlstats.Helpers.Position
2019-08-24 16:23:56 -04:00
import Mtlstats.Types
2019-09-19 04:35:01 -04:00
import Mtlstats.Util
2019-08-24 16:23:56 -04:00
-- | Event handler for a prompt
2023-05-25 19:36:03 -04:00
promptHandler :: Prompt -> Handler ()
promptHandler p (VtyEvent (EvKey KEnter [])) = do
val <- use inputBuffer
inputBuffer .= ""
2019-08-24 16:23:56 -04:00
promptAction p val
2023-05-25 19:36:03 -04:00
promptHandler p (VtyEvent (EvKey (KChar c) [])) =
modify $ inputBuffer %~ promptProcessChar p c
promptHandler _ (VtyEvent (EvKey KBS [])) =
2019-08-24 16:23:56 -04:00
modify removeChar
2023-05-25 19:36:03 -04:00
promptHandler p (VtyEvent (EvKey k m)) =
promptSpecialKey p k m
2019-08-24 16:23:56 -04:00
promptHandler _ _ = return ()
-- | Builds a controller out of a prompt with a header
promptControllerWith
2023-05-25 19:36:03 -04:00
:: (ProgState -> Widget () -> Widget ())
-- ^ The header
-> Prompt
-- ^ The prompt to use
-> Controller
-- ^ The resulting controller
promptControllerWith header prompt = Controller
2023-05-25 19:36:03 -04:00
{ drawController = \s -> header s $ drawPrompt prompt s
, handleController = promptHandler prompt
}
-- | Builds a controller out of a prompt
promptController
:: Prompt
-- ^ The prompt to use
-> Controller
-- ^ The resulting controller
2023-05-25 19:36:03 -04:00
promptController = promptControllerWith $ const id
2019-08-24 21:15:50 -04:00
-- | Builds a string prompt
strPrompt
:: String
-- ^ The prompt string
-> (String -> Action ())
-- ^ The callback function for the result
-> Prompt
strPrompt pStr act = Prompt
2023-05-25 19:36:03 -04:00
{ drawPrompt = drawSimplePrompt pStr
, promptProcessChar = \ch -> (++ [ch])
, promptAction = act
2023-05-25 19:36:03 -04:00
, promptSpecialKey = \_ _ -> return ()
2019-08-24 21:15:50 -04:00
}
-- | Creates an upper case string prompt
ucStrPrompt
:: String
-- ^ The prompt string
-> (String -> Action ())
-- ^ The callback function for the result
-> Prompt
2019-12-02 13:46:43 -05:00
ucStrPrompt pStr act = (strPrompt pStr act)
{ promptProcessChar = \ch -> (++ [toUpper ch]) }
-- | Creates a prompt which forces capitalization of input to
-- accomodate a player or goalie name
namePrompt
:: String
-- ^ The prompt string
-> (String -> Action ())
-- ^ The callback function for the result
-> Prompt
namePrompt pStr act = (strPrompt pStr act)
{ promptProcessChar = capitalizeName }
2019-08-24 16:23:56 -04:00
-- | Builds a numeric prompt
numPrompt
:: String
-- ^ The prompt string
-> (Int -> Action ())
-- ^ The callback function for the result
-> Prompt
2019-12-31 22:44:37 -05:00
numPrompt pStr = numPromptWithFallback pStr $ return ()
2020-04-15 22:07:56 -04:00
-- | Builds a numberic prompt with a range
numPromptRange
:: Int
-- ^ The minimum value
-> Int
-- ^ The maximum value
-> String
-- ^ The prompt string
-> (Int -> Action ())
-- ^ The callback function for the result
-> Prompt
numPromptRange nMin nMax pStr callback = numPrompt pStr $ \n ->
when (n >= nMin && n <= nMax) $ callback n
2019-12-31 22:44:37 -05:00
-- | Builds a numeric prompt with a fallback action
numPromptWithFallback
:: String
-- ^ The prompt string
-> Action ()
-- ^ The action to call on invalid (or blank) input
-> (Int -> Action ())
-- ^ The callback function for the result
-> Prompt
numPromptWithFallback pStr fallback act = Prompt
2023-05-25 19:36:03 -04:00
{ drawPrompt = drawSimplePrompt pStr
, promptProcessChar = \ch existing -> if isDigit ch
then existing ++ [ch]
else existing
2020-03-05 05:15:58 -05:00
, promptAction = maybe fallback act . readMaybe
2023-05-25 19:36:03 -04:00
, promptSpecialKey = \_ _ -> return ()
2019-08-24 16:23:56 -04:00
}
2019-08-24 19:02:29 -04:00
2020-03-11 03:56:58 -04:00
-- | Prompts for a database name
dbNamePrompt
:: String
-- ^ The prompt string
-> (String -> Action ())
-- ^ The callback to pass the result to
-> Prompt
dbNamePrompt pStr act = (strPrompt pStr act)
{ promptProcessChar = \ch -> if isAlphaNum ch || ch == '-'
then (++[toUpper ch])
else id
}
-- | Prompts the user for a filename to save a backup of the database
-- to
newSeasonPrompt :: Prompt
2020-03-12 02:44:41 -04:00
newSeasonPrompt = dbNamePrompt "Filename for new season: " $ \fn ->
if null fn
then modify backHome
else do
saveDatabase
modify
$ (dbName .~ fn)
. (progMode .~ NewSeason True)
2020-02-04 00:19:15 -05:00
2019-10-30 16:57:08 -04:00
-- | Builds a selection prompt
selectPrompt :: SelectParams a -> Prompt
selectPrompt params = Prompt
2023-05-25 19:36:03 -04:00
{ drawPrompt = \s -> let
sStr = s^.inputBuffer
pStr = spPrompt params ++ sStr
pWidth = safeWcswidth pStr
results = zip [1..maxFunKeys] $ spSearch params sStr (s^.database)
fmtRes = map
2019-10-30 16:57:08 -04:00
(\(n, (_, x)) -> let
desc = spElemDesc params x
2023-05-25 19:36:03 -04:00
in str $ "F" ++ show n ++ ") " ++ desc)
2019-10-30 16:57:08 -04:00
results
2023-05-25 19:36:03 -04:00
in hBox $
[ showCursor () (Location (0, pWidth)) $ str pStr
, str ""
, str $ spSearchHeader params
] ++ fmtRes
, promptProcessChar = spProcessChar params
2019-10-30 16:57:08 -04:00
, promptAction = \sStr -> if null sStr
then spCallback params Nothing
else do
db <- gets (^.database)
case spSearchExact params sStr db of
Nothing -> spNotFound params sStr
Just n -> spCallback params $ Just n
2023-05-25 19:36:03 -04:00
, promptSpecialKey = \key _ -> case key of
KFun rawK -> do
sStr <- use inputBuffer
db <- use database
2019-10-30 16:57:08 -04:00
let
2023-05-25 19:36:03 -04:00
n = pred rawK
2019-10-30 16:57:08 -04:00
results = spSearch params sStr db
when (n < maxFunKeys) $
2019-11-12 17:01:08 -05:00
whenJust (nth n results) $ \(sel, _) -> do
2019-10-30 16:57:08 -04:00
modify $ inputBuffer .~ ""
2019-11-12 17:01:08 -05:00
spCallback params $ Just sel
2019-10-30 16:57:08 -04:00
_ -> return ()
}
-- | Prompts for the database to load
getDBPrompt :: Prompt
2020-03-12 02:44:41 -04:00
getDBPrompt = dbNamePrompt "Season database to load: " $ \fn -> do
modify $ dbName .~ fn
loadDatabase
2019-09-09 13:04:39 -04:00
-- | Prompts for a new player's number
playerNumPrompt :: Prompt
playerNumPrompt = numPrompt "Player number: " $
modify . (progMode.createPlayerStateL.cpsNumber ?~)
2019-09-09 22:50:44 -04:00
-- | Prompts for a new player's name
playerNamePrompt :: Prompt
playerNamePrompt = namePrompt "Player name: " $
2019-09-09 22:50:44 -04:00
modify . (progMode.createPlayerStateL.cpsName .~)
2019-09-09 22:57:36 -04:00
-- | Prompts for a new player's position
playerPosPrompt :: Prompt
playerPosPrompt = selectPositionPrompt "Player position: " $
2019-09-09 22:57:36 -04:00
modify . (progMode.createPlayerStateL.cpsPosition .~)
-- | Prompts tor the goalie's number
goalieNumPrompt :: Prompt
goalieNumPrompt = numPrompt "Goalie number: " $
modify . (progMode.createGoalieStateL.cgsNumber ?~)
-- | Prompts for the goalie's name
goalieNamePrompt :: Prompt
goalieNamePrompt = namePrompt "Goalie name: " $
modify . (progMode.createGoalieStateL.cgsName .~)
2020-04-06 14:46:30 -04:00
-- | Selects a player using a specified search function (creating the
-- player if necessary)
selectPlayerPromptWith
:: (String -> [Player] -> [(Int, Player)])
-- ^ The search function
-> String
2019-09-19 04:35:01 -04:00
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the payer as
-- input)
-> Prompt
2020-04-06 14:46:30 -04:00
selectPlayerPromptWith sFunc pStr callback = selectPrompt SelectParams
{ spPrompt = pStr
, spSearchHeader = "Player select:"
2020-04-06 14:46:30 -04:00
, spSearch = \sStr db -> sFunc sStr (db^.dbPlayers)
, spSearchExact = \sStr db -> fst <$> playerSearchExact sStr (db^.dbPlayers)
, spElemDesc = playerSummary
, spProcessChar = capitalizeName
, spCallback = callback
, spNotFound = \sStr -> do
mode <- gets (^.progMode)
let
cps = newCreatePlayerState
& cpsName .~ sStr
& cpsSuccessCallback .~ do
modify $ progMode .~ mode
index <- pred . length <$> gets (^.database.dbPlayers)
callback $ Just index
& cpsFailureCallback .~ modify (progMode .~ mode)
modify $ progMode .~ CreatePlayer cps
2019-09-19 04:35:01 -04:00
}
2020-04-06 14:46:30 -04:00
-- | Selects a player (creating one if necessary)
selectPlayerPrompt
:: String
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the payer as
-- input)
-> Prompt
selectPlayerPrompt = selectPlayerPromptWith playerSearch
-- | Selects an active player (creating one if necessary)
selectActivePlayerPrompt
:: String
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the payer as
-- input)
-> Prompt
selectActivePlayerPrompt = selectPlayerPromptWith activePlayerSearch
2020-04-06 15:01:26 -04:00
-- | Selects a goalie with a specified search criteria (creating the
-- goalie if necessary)
selectGoaliePromptWith
:: (String -> [Goalie] -> [(Int, Goalie)])
-- ^ The search criteria
-> String
2019-10-30 00:29:33 -04:00
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the goalie as
-- input)
-> Prompt
2020-04-06 15:01:26 -04:00
selectGoaliePromptWith criteria pStr callback = selectPrompt SelectParams
2019-10-30 21:57:31 -04:00
{ spPrompt = pStr
, spSearchHeader = "Goalie select:"
2020-04-06 15:01:26 -04:00
, spSearch = \sStr db -> criteria sStr (db^.dbGoalies)
2019-10-30 21:57:31 -04:00
, spSearchExact = \sStr db -> fst <$> goalieSearchExact sStr (db^.dbGoalies)
, spElemDesc = goalieSummary
, spProcessChar = capitalizeName
2019-10-30 21:57:31 -04:00
, spCallback = callback
, spNotFound = \sStr -> do
mode <- gets (^.progMode)
let
cgs = newCreateGoalieState
& cgsName .~ sStr
& cgsSuccessCallback .~ do
modify $ progMode .~ mode
index <- pred . length <$> gets (^.database.dbGoalies)
callback $ Just index
& cgsFailureCallback .~ modify (progMode .~ mode)
modify $ progMode .~ CreateGoalie cgs
}
2019-10-30 00:29:33 -04:00
2020-04-06 15:01:26 -04:00
-- | Selects a goalie (creating one if necessary)
selectGoaliePrompt
:: String
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the goalie as
-- input)
-> Prompt
selectGoaliePrompt = selectGoaliePromptWith goalieSearch
-- | Selects an active goalie (creating one if necessary)
selectActiveGoaliePrompt
:: String
-- ^ The prompt string
-> (Maybe Int -> Action ())
-- ^ The callback to run (takes the index number of the goalie as
-- input)
-> Prompt
selectActiveGoaliePrompt = selectGoaliePromptWith activeGoalieSearch
2020-02-11 23:00:13 -05:00
-- | Selects (or creates) a player position
selectPositionPrompt
:: String
-- ^ The 'Prompt' string
-> (String -> Action ())
-- ^ The action to perform when a value is entered
-> Prompt
selectPositionPrompt pStr callback = selectPrompt SelectParams
{ spPrompt = pStr
, spSearchHeader = "Positions:"
, spSearch = posSearch
, spSearchExact = posSearchExact
, spElemDesc = id
, spProcessChar = \ch -> (++ [toUpper ch])
, spCallback = posCallback callback
, spNotFound = callback
}
playerToEditPrompt :: Prompt
2019-11-01 03:44:03 -04:00
playerToEditPrompt = selectPlayerPrompt "Player to edit: " $
modify . (progMode.editPlayerStateL.epsSelectedPlayer .~)
2023-05-25 19:36:03 -04:00
drawSimplePrompt :: String -> Renderer
drawSimplePrompt pStr s = let
fullStr = pStr ++ s^.inputBuffer
strWidth = safeWcswidth fullStr
in showCursor () (Location (0, strWidth)) $ str fullStr