Skip to content

Instantly share code, notes, and snippets.

@kei-q
Created January 9, 2011 15:02
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 kei-q/771740 to your computer and use it in GitHub Desktop.
Save kei-q/771740 to your computer and use it in GitHub Desktop.
module DyreExample
( Config(..)
, defaultConfig
, dyreExample
) where
import qualified Config.Dyre as Dyre (wrapMain, defaultParams, Params(..))
import Config.Dyre.Relaunch (restoreTextState, relaunchMaster, saveTextState)
import System.IO (hFlush, stdout)
dyreExample = Dyre.wrapMain $ Dyre.defaultParams
{ Dyre.projectName = "dyreExample"
, Dyre.realMain = realMain
, Dyre.showError = showError
}
data Config = Config { message :: String, errorMsg :: Maybe String }
defaultConfig :: Config
defaultConfig = Config "Dyre Example v0.1" Nothing
data State = State { bufferLines :: [String] } deriving (Read, Show)
realMain config@Config{message = message, errorMsg = errorMsg } = do
(State buffer) <- restoreTextState $ State []
case errorMsg of
Nothing -> main' buffer
Just em -> putStrLn ("Error: " ++ em) >> main' buffer
where
main' buffer = do
putStrLn message
mapM putStrLn . reverse $ buffer
putStr "> " >> hFlush stdout
input <- getLine
case input of
"exit" -> return ()
"recompile" -> relaunchMaster Nothing
other -> saveTextState (State $ other:buffer) >> realMain config
showError :: Config -> String -> Config
showError cfg msg = cfg { errorMsg = Just msg }
import DyreExample
main = dyreExample $ defaultConfig { message = "modified" }
import DyreExample
main = dyreExample $ defaultConfig { message = "sample" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment