Skip to content

Instantly share code, notes, and snippets.

@danbst
Last active July 19, 2018 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbst/4600324 to your computer and use it in GitHub Desktop.
Save danbst/4600324 to your computer and use it in GitHub Desktop.
Netwire - console cursor moving
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