-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountDown.purs
73 lines (59 loc) · 1.61 KB
/
CountDown.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Test.Examples.CountDown where
import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Aff (Aff, Milliseconds(..), launchAff_)
import Effect.Aff as Aff
import Marionette as Mar
import Marionette.Controllers.ControlAPI (ControlAPI)
import Marionette.Controllers.ControlAPI as RecAPI
import Marionette.Renderers.Commander (KeyPrompt(..))
import Marionette.Renderers.Commander as Comm
data State
= Init
| CountingDown Int
| LaunchSpaceShip
derive instance Eq State
data Msg = Start | Tick
control :: ControlAPI Msg State -> Msg -> Aff Unit
control api = case _ of
Start -> do
api.modifyState_ case _ of
Init -> CountingDown 10
st -> st
api.sendMsg Tick
Tick -> do
api.modifyState_ case _ of
CountingDown 1 -> LaunchSpaceShip
CountingDown n -> CountingDown (n - 1)
st -> st
Aff.delay (Milliseconds 1000.0)
api.sendMsg Tick
view :: State -> Comm.CliSurface Msg
view = case _ of
Init -> Comm.CliSurface
( Comm.TextOutput
""
)
( Comm.KeyInput (KeyPrompt "Press the 's' key to start!") case _ of
{ name: "s" } -> Just Start
_ -> Nothing
)
CountingDown n -> Comm.CliSurface
(Comm.TextOutput $ show n <> "...")
(Comm.NoInput)
LaunchSpaceShip -> Comm.CliSurface
(Comm.TextOutput $ "boom!")
(Comm.NoInput)
initialState :: State
initialState = Init
program :: Mar.Program Msg State
program =
{ initialState
, renderer: Comm.mkRenderer_ view
, controller: RecAPI.mkController control
}
main :: Effect Unit
main = launchAff_ do
_ <- Mar.runProgram program Mar.defaultConfig
pure unit