Created
April 9, 2016 18:22
-
-
Save TGOlson/fd9d668d924265ee56c3f916c8174c11 to your computer and use it in GitHub Desktop.
Simple typing interface with FRP
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
module ReactionTest | |
( main | |
) where | |
import Control.Event.Handler | |
import Reactive.Banana | |
import Reactive.Banana.Frameworks | |
import System.IO | |
import System.Console.ANSI | |
import Control.Monad | |
main :: IO () | |
main = do | |
-- Setup the fancy UI | |
hSetEcho stdin False | |
hSetBuffering stdin NoBuffering | |
showCursor | |
resetScreen | |
(addHandler, pushEvent) <- newAddHandler | |
network <- compile $ sentenceNetwork addHandler | |
actuate network | |
forever $ getChar >>= pushEvent | |
resetScreen :: IO () | |
resetScreen = setCursorPosition 0 0 >> clearScreen | |
data CharAction = DEL | ADD Char | |
sentenceNetwork :: AddHandler Char -> MomentIO () | |
sentenceNetwork addHandler = do | |
eChar <- fromAddHandler addHandler | |
let eCharAction = toCharAction <$> eChar | |
bString <- accumB "" $ handleCharAction <$> eCharAction | |
eStringChange <- changes bString | |
reactimate' $ fmap logString <$> eStringChange | |
where | |
toCharAction '\DEL' = DEL | |
toCharAction c = ADD c | |
handleCharAction DEL = init | |
handleCharAction (ADD c) = \s -> s ++ pure c | |
logString s = resetScreen >> putStrLn s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment