{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE LambdaCase #-}
module Imj.Input.Blocking
(
getKeyThenFlush
) where
import Imj.Prelude
import System.IO( getChar, hReady, stdin )
import Data.Char( ord )
import Data.List( reverse )
import Imj.Geo.Discrete.Types( Direction(..) )
import Imj.Input.Types
getKeyThenFlush :: IO Key
getKeyThenFlush = do
chars <- getAllChars
let res = fromString chars
return res
fromString :: String -> Key
fromString =
\case
[] -> error "should not be empty"
[c] -> case ord c of
27 -> Escape
_ -> AlphaNum c
c:l -> case ord c of
27 -> case l of
a:b:_ -> case a of
'[' -> case b of
'A' -> Arrow Up
'B' -> Arrow Down
'C' -> Arrow RIGHT
'D' -> Arrow LEFT
_ -> Unknown
_ -> Unknown
_ -> Unknown
_ -> AlphaNum c
getAllChars :: IO String
getAllChars =
reverse <$> getKey' ""
where getKey' chars = do
char <- getChar
more <- hReady stdin
(if more
then
getKey'
else
return) (char:chars)