Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Jan 20, 2017
0 parents commit 4e22833
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# elm-package generated files
elm-stuff
# elm-repl generated files
repl-temp-*
80 changes: 80 additions & 0 deletions Counter.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module Counter exposing (..)

import Html exposing (..)
import Color exposing (Color, red, green, blue, black)
import Html.Attributes exposing (..)
import Time
import Debug
import String exposing (concat)


-- MODEL

type alias Model =
{ num : Int
, limit : Int
, color : Color
, period : Time.Time
, start : Time.Time
}


init : Float -> Time.Time -> Model
init period startTime =
{ num = 0
, limit = 9
, color = blue
, period = Time.inSeconds period
, start = Time.inSeconds startTime
}


-- UPDATE

type Msg = Tick Time.Time

tickMsg : Time.Time -> Msg
tickMsg t = Tick t

update : Msg -> Model -> Model
update msg model =
case msg of
Tick time ->
let
delta = Debug.log "delta" <| Time.inSeconds <| time - model.start
in
if delta > (Debug.log "period" <| model.period)
then { model
| num = (model.num + 1) % model.limit
, start = time
}
else model



-- VIEW

view : Model -> Html Msg
view model =
let
rgb col =
let
rgba = Color.toRgb col
ts = toString
in concat ["rgb(", ts rgba.red, ",", ts rgba.green, ",", ts rgba.blue, ")"]
countStyle model =
[ ("font-size", "20px")
, ("font-family", "monospace")
, ("display", "inline-block")
, ("width", "50px")
, ("color", rgb model.color)
, ("text-align", "center")
]
in
div []
[ div [ style <| countStyle model ] [ text (toString <| model.num) ]
]




76 changes: 76 additions & 0 deletions CounterGrid.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module CounterGrid exposing (..)


import Html exposing (..)
import Html.Attributes exposing (..)
import Time
import Counter
import List
import Debug

-- MODEL

type alias Model =
{ counters : List Counter.Model
, cols : Int
}

init : List Float -> Time.Time -> (Model, Cmd Msg)
init periods start =
let
m = { counters = List.map (\p -> Counter.init p start) periods
, cols = 4
}
in
(m, Cmd.none)

-- UPDATE

type Msg = Tick Time.Time | CounterMsg Counter.Msg

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Tick time ->
({ model
| counters = List.map (Counter.update <| Counter.tickMsg time) model.counters
}, Cmd.none)
CounterMsg counterMsg ->
(model, Cmd.none)


-- VIEW

viewTable : Model -> Html Msg
viewTable model =
let
w = model.cols
h = ((List.length model.counters) // model.cols) + 1
n = List.length model.counters
row r = tr [] <| List.map (counterAt r) <| List.range 0 (w-1)
counterAt r i = case get model.counters (r*w + i) of
Nothing -> td [] []
Just cell -> td [] [Html.map CounterMsg <| Counter.view cell]
in
div []
[ table [style [("background", "black")]]
<| List.map row <| List.range 0 (h-1)
]

view : Model -> Html Msg
view model =
div []
[ viewTable model
]

get : List a -> Int -> Maybe a
-- get: Returns the element at index i.
get list i = List.drop i list |> List.head


-- APP

subscriptions : Model -> Sub Msg
subscriptions model = Sub.batch
[ Time.every Time.second Tick
]
16 changes: 16 additions & 0 deletions Main.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Main exposing (..)

import Html exposing (Html, program)
import CounterGrid as CounterGrid exposing (init, update, view, Model, Msg, subscriptions)

n = 18

main : Program Never Model Msg
main = program
{ init = init (List.take n rndss) 0
, view = view
, update = update
, subscriptions = subscriptions}

rndss = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
rnds = [1, 4, 6506, 651, 1125, 2228, 4854, 2221, 524, 3690, 2165, 6649, 7216, 5865, 113, 1179, 5156, 677, 4623, 772, 7658, 4631, 3548, 5222, 5692, 1582, 2067, 1474, 1184, 2622, 2397, 4178, 7143, 1058, 6251, 615, 3007, 1639, 2316, 7520, 2164, 4463, 1109, 5505, 5145, 3565, 5336, 6137, 4383, 1659, 6994, 5385, 3350, 4380, 4661, 2381, 4654, 5978, 5894, 4450, 367, 1093, 5619, 7516, 3326, 7425, 1741, 1731, 3072, 5422, 7793, 7139, 5643, 2660, 3151, 5240, 2578, 4478, 2256, 3403, 7856, 2287, 4523, 7586, 7627, 818, 4197, 4401, 4465, 6540, 2863, 7545, 5673, 2099, 5407, 6367, 1863, 827, 3375, 226, 1014, 5438]
15 changes: 15 additions & 0 deletions elm-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.0.0 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}

0 comments on commit 4e22833

Please sign in to comment.