Skip to content

Instantly share code, notes, and snippets.

Created May 6, 2017 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/53b18b595098ce6588397f8d68e01ab3 to your computer and use it in GitHub Desktop.
Save anonymous/53b18b595098ce6588397f8d68e01ab3 to your computer and use it in GitHub Desktop.
Elm Hex Clock
{
"version": "1.0.0",
"summary": "An Elm Hex Clock!",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 5.1.1",
"elm-lang/html": "2.0.0 <= v < 2.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
<html>
<head>
<style>
body {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<script>
var app = Elm.Main.fullscreen()
</script>
</body>
</html>
module Main exposing (..)
import Html exposing (Html, Attribute, div, text)
import Html.Attributes exposing (style)
import Time exposing (Time, every, hour, minute, second, inHours, inMinutes, inSeconds)
import List exposing (map)
import String exposing (join, length)
-- STYLES
styles : String -> Attribute msg
styles time =
style
[ ( "backgroundColor", time )
, ( "height", "100vh" )
, ( "width", "100vw" )
, ( "color", "#fff" )
, ( "display", "flex" )
, ( "justifyContent", "center" )
, ( "alignItems", "center" )
, ( "fontSize", "72px" )
]
-- HELPERS
joinTimes : Int -> Int -> Int -> String
joinTimes hrs min sec =
[ hrs, min, sec ]
|> map (\t -> toString t)
|> map
(\s ->
if length s < 2 then
"0" ++ s
else
s
)
|> join ""
toInt : Time -> Int
toInt time =
round time
-- MODEL/INIT
type alias Model =
{ time : String }
model : Model
model =
Model ""
init : ( Model, Cmd Msg )
init =
( Model "", Cmd.none )
-- UPDATE
type Msg
= Tick Time
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Tick time ->
let
hr =
rem (toInt (inHours time)) 24
min =
rem (toInt (inMinutes time)) 60
sec =
rem (toInt (inSeconds time)) 60
in
( { model | time = joinTimes hr min sec }, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
every second Tick
-- VIEW
view : Model -> Html a
view model =
let
{ time } =
model
in
div [ (styles time) ] [ text ("#" ++ time) ]
-- MAIN
main : Program Never Model Msg
main =
Html.program
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment