-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
167 lines (142 loc) · 3.61 KB
/
Main.elm
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module Main exposing (main)
import Browser
import Browser.Dom as Dom
import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import Task
import String
import Time
import Binary
import Json.Decode as Json
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
type alias Model =
{ timestamp : Int
, command : String
}
init : () -> (Model, Cmd Msg)
init _ =
( { timestamp = 300
, command = ""
} , Cmd.none )
type Msg
= Tick Time.Posix
| RecordCommand String
| KeyDown Int
| Refocus
| Noop
focusCommandBox : Cmd Msg
focusCommandBox =
Task.attempt (\_ -> Noop) ( Dom.focus "txtCommand" )
onKeyDown : (Int -> msg) -> Attribute msg
onKeyDown tagger =
on "keydown" (Json.map tagger keyCode)
{-
Commands:
5 min/hour/m/h/hr
add 5 min/hour/h/m/hr
remove 5 min
minus 5 min
5 min
-5 min
+5 -> default to min
-5
5
show clock
show list
show history
history
list
-}
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Tick tick ->
if model.timestamp > 0 then
( { model | timestamp = model.timestamp - 1 }, Cmd.none )
else
( model, Cmd.none )
RecordCommand value ->
( { model | command = value }, Cmd.none )
KeyDown keycode ->
if keycode == 13 then
( { model | timestamp = model.timestamp + 300 }, Cmd.none )
else
( model, Cmd.none )
Refocus ->
( model, focusCommandBox )
Noop ->
( model, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
if model.timestamp > 0 then
Time.every 1000 Tick
else
Sub.none
secToHMS : Int -> ( Int, Int, Int )
secToHMS time =
let
h = time // 3600
rm = modBy 3600 time
m = rm // 60
s = modBy 60 rm
in
(h, m, s)
toBits : Int -> List Int
toBits a = Binary.fromDecimal a |> Binary.ensureSize 4 |> Binary.toIntegers
bitToDiv : Int -> Int -> Html Msg
bitToDiv n i = li [ classList [ ( "bit-dots", True )
, ( "on", i == 1 )
]
] [ ]
bitsDot : List Int -> List ( Html Msg )
bitsDot bits = List.indexedMap bitToDiv bits
renderDigit : Int -> Html Msg
renderDigit n =
div [ class "bits-group" ]
[ bitsDot ( toBits n ) |> ul []
]
renderNumber : Int -> Html Msg
renderNumber n =
let
l = n // 10
r = modBy 10 n
in
div [ class "number-block" ]
[ renderDigit l
, renderDigit r
]
padTime : Int -> String
padTime n =
String.padLeft 2 '0' ( String.fromInt n )
renderTimeString : Int -> Int -> Int -> String
renderTimeString h m s =
padTime h ++ ":" ++ padTime m ++ ":" ++ padTime s
view : Model -> Html Msg
view model =
let
(h, m, s) = secToHMS model.timestamp
in
div [ class "container" ]
[ div [ class "numbers-group" ]
[ renderNumber h
, renderNumber m
, renderNumber s
]
, h2 [ class "time-string" ] [ text ( renderTimeString h m s ) ]
, div [ class "command-box" ]
[ input [ autofocus True
, id "txtCommand"
, onInput RecordCommand
, onBlur Refocus
, onKeyDown KeyDown
, placeholder "No command implemented yet, just press ENTER to add 5 minutes"
] []
]
]