Last active
July 19, 2018 16:57
-
-
Save danbst/4600324 to your computer and use it in GitHub Desktop.
Netwire - console cursor moving
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Wire | |
import Prelude hiding ((.), id) | |
import System.Console.ANSI | |
import Data.Maybe | |
import Control.Applicative ((<$>)) | |
control whenInhibited whenProduced wire = loop wire clockSession | |
where | |
loop w' session' = do | |
(mx, w, session) <- stepSession w' session' () | |
case mx of | |
Left ex -> whenInhibited ex | |
Right x -> whenProduced x | |
loop w session | |
foreign import ccall unsafe "conio.h getch" c_getch :: IO Char | |
foreign import ccall unsafe "conio.h kbhit" c_kbhit :: IO Bool | |
keyPressed = do isKey <- c_kbhit | |
if isKey then Just <$> c_getch | |
else return Nothing | |
pressedKeyMaybe = mkFixM $ | |
\_ _ -> Right <$> keyPressed | |
moveCursor = mkFixM $ | |
\_ coords@(x,y) -> setCursorPosition y x >> return (Right coords) | |
cursorWire = accum (\(a,b) (c,d) -> (a+c, b+d)) (0,0) | |
. ( pure ((-1), 0 ) . when (== Just 'K') | |
<|> pure ( 1, 0 ) . when (== Just 'M') | |
<|> pure ( 0, (-1)) . when (== Just 'H') | |
<|> pure ( 0, 1 ) . when (== Just 'P') | |
) | |
. edge ((/=) 224 . fromEnum . fromMaybe ' ') . pressedKeyMaybe | |
main = control return (const (return ())) $ | |
moveCursor . cursorWire |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment