Last active
July 31, 2017 10:33
-
-
Save jackwillis/b87e5650707d23882a6641ed2821630e to your computer and use it in GitHub Desktop.
RPG-style text prompt
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
name: RPGPrompt | |
version: 0.1.0.0 | |
synopsis: RPG-style text prompt | |
author: Jack Willis | |
maintainer: jack@attac.us | |
license: MIT | |
build-type: Simple | |
cabal-version: >=1.10 | |
executable RPGPrompt | |
main-is: Main.hs | |
-- other-modules: | |
-- other-extensions: | |
build-depends: base >= 4.7 && < 5, parsec >= 3.1.11, sleep >= 0.1.0.1 | |
hs-source-dirs: src | |
default-language: Haskell2010 |
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 Main where | |
import System.IO | |
import System.Sleep | |
import Text.Parsec | |
import Text.Parsec.String | |
data Prompt = TextChunk String | Pause | Continue deriving Show | |
-- '#'s represent pauses, '%'s are continues (wait for the player to hit enter) | |
-- Anything else gets shoved into a `TextChunk` | |
prompt :: Parser Prompt | |
prompt | |
= Pause <$ char '#' | |
<|> Continue <$ char '%' | |
<|> TextChunk <$> many (noneOf "%#") | |
prompts :: Parser [Prompt] | |
prompts = manyTill prompt eof | |
parsePrompts :: String -> Either ParseError [Prompt] | |
parsePrompts = parse prompts "user input" | |
process :: Either ParseError [Prompt] -> IO () | |
process e = case e of | |
(Left err) -> print err | |
(Right ps) -> mapM_ display ps | |
display :: Prompt -> IO () | |
display p = case p of | |
(TextChunk s) -> mapM_ (\c -> putChar c >> sleep 0.02) s | |
Pause -> sleep 0.5 | |
Continue -> putStr " [enter]" >> getLine >> return () | |
main = do | |
hSetBuffering stdout NoBuffering | |
process $ parsePrompts "Sounds good#.#.#.## What do you think?%Great!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment