Skip to content

Instantly share code, notes, and snippets.

Created May 6, 2017 03:50
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/076e99cdf355abe42e572299419e2ba2 to your computer and use it in GitHub Desktop.
Save anonymous/076e99cdf355abe42e572299419e2ba2 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)
import List exposing (map)
import String exposing (join, length)
import Date exposing (fromTime, hour, minute, second)
-- 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 =
hour (fromTime time)
min =
minute (fromTime time)
sec =
second (fromTime time)
in
( { model | time = joinTimes hr min sec }, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
every Time.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