Cleanup console input slightly

This commit is contained in:
2022-03-20 19:41:40 +00:00
parent ff93d1d041
commit 988a0de578
6 changed files with 39 additions and 41 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ handleTextInputEvent tev = handleTextInput text
text = unpack $ textInputEventText tev
handleTextInput :: String -> Universe -> IO (Maybe Universe)
handleTextInput text w
handleTextInput text w
| null (_menuLayers w) = return $ Just w
| otherwise = handleTextInMenu (head $ _menuLayers w) text w
+20 -26
View File
@@ -9,23 +9,18 @@ import Dodge.Debug.Terminal
import Dodge.Menu.PushPop
import Data.Maybe
import Control.Monad
--import Control.Lens
--import Control.Monad
import Control.Lens
import SDL
import Control.Lens ((%~))
--import qualified Debug.Trace
handleTextInMenu :: ScreenLayer -> [Char] -> Universe -> IO (Maybe Universe)
handleTextInMenu mState text = case mState of
InputScreen s help -> return . (popScreen' >=> pushScreen' (InputScreen new_text help))
where
new_text
-- Text events occur after key events so ignore the first
-- (key that opened the terminal menu)
| s == "" = ">"
| otherwise = s ++ text
_ -> return . Just
handleTextInMenu mState text = return . Just . case mState of
InputScreen {} -> menuLayers . ix 0 . scInput %~ updateText
_ -> id
where
updateText "" = ">"
updateText s = s ++ text
handlePressedKeyInMenu :: ScreenLayer -> Scancode -> Universe -> IO (Maybe Universe)
handlePressedKeyInMenu mState scode = case mState of
@@ -35,23 +30,22 @@ handlePressedKeyInMenu mState scode = case mState of
ColumnsScreen {} -> popScreen
WaitScreen {} -> return . Just
InputScreen s help -> case scode of
ScancodeEscape -> popScreen
-- Remove the menu layer (we readd it in applyTerminalString if the commmand fails)
ScancodeReturn -> return. Just . applyTerminalString (tail s) . (menuLayers %~ tail)
ScancodeTab -> autoCompleteTerminal s help
ScancodeBackspace
-> return . (popScreen' >=> pushScreen' (InputScreen (dropLast s) "Enter command"))
-- Ignore (text input now handled by handleText)
_ -> return . (popScreen' >=> pushScreen' (InputScreen s help))
ScancodeEscape -> popScreen
ScancodeReturn -> popScreen . applyTerminalString (tail s)
ScancodeTab -> autoCompleteTerminal s help
ScancodeBackspace -> return . Just . (menuLayers . ix 0 . scInput %~ doBackspace)
-- text input handled by handleText
_ -> return . Just
where
dropLast [_] = ['>']
dropLast (x:xs) = init (x:xs)
dropLast _ = ['>']
doBackspace [_] = ['>']
doBackspace (x:xs) = init (x:xs)
doBackspace _ = ['>']
optionListToEffects :: [MenuOption] -> (Universe -> IO (Maybe Universe)) -> Scancode
-> Universe -> IO (Maybe Universe)
optionListToEffects mos defaulteff sc = fromMaybe defaulteff .
lookup sc $ concatMap menuOptionToEffects mos
optionListToEffects mos defaulteff sc = fromMaybe defaulteff
. lookup sc
$ concatMap menuOptionToEffects mos
menuOptionToEffects :: MenuOption -> [(Scancode,Universe -> IO (Maybe Universe))]
menuOptionToEffects Toggle {_moKey = k, _moEff = eff} = [(k,eff)]