aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-11-22 16:37:48 +0000
committerAkshay <[email protected]>2020-11-22 16:37:48 +0000
commit589a9c03eeee61b6e1bc9b6ac145c177a59258be (patch)
tree022b87b2a254423fb0f39ebc108207078e92d1c4
parent19a06028be67598e718ea13daa65dd82576ee8f4 (diff)
add app config, word generation
-rw-r--r--src/Base.elm22
-rw-r--r--src/Data.elm21
-rw-r--r--src/Main.elm95
3 files changed, 91 insertions, 47 deletions
diff --git a/src/Base.elm b/src/Base.elm
index c3557d3..480147a 100644
--- a/src/Base.elm
+++ b/src/Base.elm
@@ -22,7 +22,27 @@ type alias Model =
22 , end : Maybe Posix 22 , end : Maybe Posix
23 , words : Array Word 23 , words : Array Word
24 , accuracy : Maybe Float 24 , accuracy : Maybe Float
25 , length : Int 25 , config : Config
26 , currentWord : Int 26 , currentWord : Int
27 , inputBox : String 27 , inputBox : String
28 } 28 }
29
30
31type alias Config =
32 { punctuation : Bool
33 , capitals : Bool
34 , length : Int
35 }
36
37
38type Msg
39 = Started Time.Posix
40 | Finished Time.Posix
41 | CorrectInput
42 | CorrectSoFar
43 | NextWord
44 | WrongInput
45 | InputChanged String
46 | LoadWords (List String)
47 | WordLengthChanged Int
48 | Redo
diff --git a/src/Data.elm b/src/Data.elm
index c3e71ae..95c92c1 100644
--- a/src/Data.elm
+++ b/src/Data.elm
@@ -1,14 +1,13 @@
1module Data exposing (punctuations, randomWord, textData) 1module Data exposing (defaultConfig, randomParagraph)
2 2
3import Base exposing (Config)
3import Random 4import Random
4import Utils exposing (listIdx, triple) 5import Utils exposing (listIdx, triple)
5 6
6 7
7type alias Config = 8defaultConfig : Config
8 { punctuation : Bool 9defaultConfig =
9 , capitals : Bool 10 Config True True 50
10 , length : Int
11 }
12 11
13 12
14textData : List String 13textData : List String
@@ -22,9 +21,9 @@ punctuations =
22 ", ! . ? ... ; :" |> String.split " " 21 ", ! . ? ... ; :" |> String.split " "
23 22
24 23
25randomBool : Random.Generator Bool 24falseWeightedBool : Random.Generator Bool
26randomBool = 25falseWeightedBool =
27 Random.uniform True [ False ] 26 Random.weighted ( 20, True ) [ ( 80, False ) ]
28 27
29 28
30randomWord : Config -> Random.Generator ( Int, Bool, Bool ) 29randomWord : Config -> Random.Generator ( Int, Bool, Bool )
@@ -35,14 +34,14 @@ randomWord config =
35 34
36 punct = 35 punct =
37 if config.punctuation then 36 if config.punctuation then
38 randomBool 37 falseWeightedBool
39 38
40 else 39 else
41 Random.constant False 40 Random.constant False
42 41
43 caps = 42 caps =
44 if config.capitals then 43 if config.capitals then
45 randomBool 44 falseWeightedBool
46 45
47 else 46 else
48 Random.constant False 47 Random.constant False
diff --git a/src/Main.elm b/src/Main.elm
index a91e12b..620ef7c 100644
--- a/src/Main.elm
+++ b/src/Main.elm
@@ -3,9 +3,15 @@ module Main exposing (..)
3import Array exposing (..) 3import Array exposing (..)
4import Base exposing (..) 4import Base exposing (..)
5import Browser 5import Browser
6import Html exposing (..) 6import Css
7import Html.Attributes exposing (..) 7import Css.Global exposing (global, selector)
8import Html.Events exposing (onInput) 8import Data exposing (..)
9import Html
10import Html.Styled exposing (..)
11import Html.Styled.Attributes exposing (..)
12import Html.Styled.Events exposing (onClick, onInput)
13import Random
14import Styles exposing (styledButton, styledInput, styledTextBox)
9import Task 15import Task
10import Time 16import Time
11import Utils exposing (isJust, isNothing) 17import Utils exposing (isJust, isNothing)
@@ -15,44 +21,36 @@ import Views exposing (..)
15main = 21main =
16 Browser.element 22 Browser.element
17 { init = init 23 { init = init
18 , view = view 24 , view = view >> toUnstyled
19 , update = update 25 , update = update
20 , subscriptions = subscriptions 26 , subscriptions = subscriptions
21 } 27 }
22 28
23 29
24type Msg
25 = Started Time.Posix
26 | Finished Time.Posix
27 | CorrectInput
28 | CorrectSoFar
29 | NextWord
30 | WrongInput
31 | InputChanged String
32 | Redo
33
34
35subscriptions : Model -> Sub Msg 30subscriptions : Model -> Sub Msg
36subscriptions model = 31subscriptions model =
37 Sub.none 32 Sub.none
38 33
39 34
40generateWords : Array Word 35stringsToWords : List String -> List Word
41generateWords = 36stringsToWords ls =
42 "this is some sample text for the demoz" 37 List.map (\w -> Word w Todo) ls
43 |> String.split " " 38
44 |> Array.fromList 39
45 |> Array.map (\w -> Word w Todo) 40generateWords : Config -> Cmd Msg
41generateWords config =
42 Random.generate LoadWords (randomParagraph config)
43
44
45defaultModel : Model
46defaultModel =
47 Model Nothing Nothing Array.empty Nothing defaultConfig 0 ""
46 48
47 49
48init : () -> ( Model, Cmd Msg ) 50init : () -> ( Model, Cmd Msg )
49init _ = 51init _ =
50 let 52 ( defaultModel
51 words = 53 , generateWords defaultConfig
52 generateWords
53 in
54 ( Model Nothing Nothing words Nothing (Array.length words) 0 ""
55 , Cmd.none
56 ) 54 )
57 55
58 56
@@ -73,7 +71,7 @@ firstWord model =
73 71
74lastWord : Model -> Bool 72lastWord : Model -> Bool
75lastWord model = 73lastWord model =
76 model.currentWord == model.length - 1 74 model.currentWord == model.config.length - 1
77 75
78 76
79wordEnded : String -> Bool 77wordEnded : String -> Bool
@@ -129,6 +127,11 @@ update msg model =
129 , Cmd.none 127 , Cmd.none
130 ) 128 )
131 129
130 LoadWords ls ->
131 ( { model | words = Array.fromList <| stringsToWords ls }
132 , Cmd.none
133 )
134
132 InputChanged s -> 135 InputChanged s ->
133 let 136 let
134 cmd = 137 cmd =
@@ -145,6 +148,28 @@ update msg model =
145 , cmd 148 , cmd
146 ) 149 )
147 150
151 WordLengthChanged n ->
152 let
153 oldConfig =
154 model.config
155
156 newConfig =
157 { oldConfig | length = n }
158 in
159 ( { defaultModel | config = newConfig }
160 , generateWords newConfig
161 )
162
163 Redo ->
164 let
165 oldConfig =
166 model.config
167
168 newModel =
169 { defaultModel | config = oldConfig }
170 in
171 ( newModel, generateWords oldConfig )
172
148 NextWord -> 173 NextWord ->
149 if isJust model.end then 174 if isJust model.end then
150 ( model, Cmd.none ) 175 ( model, Cmd.none )
@@ -180,7 +205,7 @@ toPara words current content =
180 |> Array.map (\w -> span [ wordStyle w.status ] [ text (w.content ++ " ") ]) 205 |> Array.map (\w -> span [ wordStyle w.status ] [ text (w.content ++ " ") ])
181 |> Array.set current (span [ wordStyle CurrentWord ] [ text (content ++ " ") ]) 206 |> Array.set current (span [ wordStyle CurrentWord ] [ text (content ++ " ") ])
182 |> Array.toList 207 |> Array.toList
183 |> p [] 208 |> styledTextBox []
184 209
185 210
186currentContents : Model -> String 211currentContents : Model -> String
@@ -193,14 +218,14 @@ currentContents model =
193view : Model -> Html Msg 218view : Model -> Html Msg
194view model = 219view model =
195 pre [] 220 pre []
196 [ toPara model.words model.currentWord (currentContents model) 221 [ p [] [ text ("POS: " ++ viewProgress model) ]
197
198 -- , p [] [ text (Maybe.withDefault "XX" (Maybe.map displayTime model.begin)) ]
199 -- , p [] [ text (Maybe.withDefault "XX" (Maybe.map displayTime model.end)) ]
200 , p [] [ text ("POS: " ++ viewProgress (model.currentWord + 1) model.length) ]
201 , p [] [ text ("WPM: " ++ viewWpm model) ] 222 , p [] [ text ("WPM: " ++ viewWpm model) ]
202 , p [] [ text ("ACC: " ++ viewAccuracy model.words) ] 223 , p [] [ text ("ACC: " ++ viewAccuracy model.words) ]
203 , input [ onInput handleInputChanged, value model.inputBox ] [] 224 , viewWordLengthOptions
225 , toPara model.words model.currentWord (currentContents model)
226 , styledInput [ onInput handleInputChanged, value model.inputBox ] []
227 , styledButton [ onClick Redo ] [ text "redo" ]
228 , global [ selector "li:not(:last-child)::after" [ Css.property "content" "' / '" ] ]
204 ] 229 ]
205 230
206 231