diff options
author | Akshay <[email protected]> | 2020-11-22 16:37:48 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-11-22 16:37:48 +0000 |
commit | 589a9c03eeee61b6e1bc9b6ac145c177a59258be (patch) | |
tree | 022b87b2a254423fb0f39ebc108207078e92d1c4 | |
parent | 19a06028be67598e718ea13daa65dd82576ee8f4 (diff) |
add app config, word generation
-rw-r--r-- | src/Base.elm | 22 | ||||
-rw-r--r-- | src/Data.elm | 21 | ||||
-rw-r--r-- | src/Main.elm | 95 |
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 | |||
31 | type alias Config = | ||
32 | { punctuation : Bool | ||
33 | , capitals : Bool | ||
34 | , length : Int | ||
35 | } | ||
36 | |||
37 | |||
38 | type 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 @@ | |||
1 | module Data exposing (punctuations, randomWord, textData) | 1 | module Data exposing (defaultConfig, randomParagraph) |
2 | 2 | ||
3 | import Base exposing (Config) | ||
3 | import Random | 4 | import Random |
4 | import Utils exposing (listIdx, triple) | 5 | import Utils exposing (listIdx, triple) |
5 | 6 | ||
6 | 7 | ||
7 | type alias Config = | 8 | defaultConfig : Config |
8 | { punctuation : Bool | 9 | defaultConfig = |
9 | , capitals : Bool | 10 | Config True True 50 |
10 | , length : Int | ||
11 | } | ||
12 | 11 | ||
13 | 12 | ||
14 | textData : List String | 13 | textData : List String |
@@ -22,9 +21,9 @@ punctuations = | |||
22 | ", ! . ? ... ; :" |> String.split " " | 21 | ", ! . ? ... ; :" |> String.split " " |
23 | 22 | ||
24 | 23 | ||
25 | randomBool : Random.Generator Bool | 24 | falseWeightedBool : Random.Generator Bool |
26 | randomBool = | 25 | falseWeightedBool = |
27 | Random.uniform True [ False ] | 26 | Random.weighted ( 20, True ) [ ( 80, False ) ] |
28 | 27 | ||
29 | 28 | ||
30 | randomWord : Config -> Random.Generator ( Int, Bool, Bool ) | 29 | randomWord : 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 (..) | |||
3 | import Array exposing (..) | 3 | import Array exposing (..) |
4 | import Base exposing (..) | 4 | import Base exposing (..) |
5 | import Browser | 5 | import Browser |
6 | import Html exposing (..) | 6 | import Css |
7 | import Html.Attributes exposing (..) | 7 | import Css.Global exposing (global, selector) |
8 | import Html.Events exposing (onInput) | 8 | import Data exposing (..) |
9 | import Html | ||
10 | import Html.Styled exposing (..) | ||
11 | import Html.Styled.Attributes exposing (..) | ||
12 | import Html.Styled.Events exposing (onClick, onInput) | ||
13 | import Random | ||
14 | import Styles exposing (styledButton, styledInput, styledTextBox) | ||
9 | import Task | 15 | import Task |
10 | import Time | 16 | import Time |
11 | import Utils exposing (isJust, isNothing) | 17 | import Utils exposing (isJust, isNothing) |
@@ -15,44 +21,36 @@ import Views exposing (..) | |||
15 | main = | 21 | main = |
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 | ||
24 | type Msg | ||
25 | = Started Time.Posix | ||
26 | | Finished Time.Posix | ||
27 | | CorrectInput | ||
28 | | CorrectSoFar | ||
29 | | NextWord | ||
30 | | WrongInput | ||
31 | | InputChanged String | ||
32 | | Redo | ||
33 | |||
34 | |||
35 | subscriptions : Model -> Sub Msg | 30 | subscriptions : Model -> Sub Msg |
36 | subscriptions model = | 31 | subscriptions model = |
37 | Sub.none | 32 | Sub.none |
38 | 33 | ||
39 | 34 | ||
40 | generateWords : Array Word | 35 | stringsToWords : List String -> List Word |
41 | generateWords = | 36 | stringsToWords 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) | 40 | generateWords : Config -> Cmd Msg |
41 | generateWords config = | ||
42 | Random.generate LoadWords (randomParagraph config) | ||
43 | |||
44 | |||
45 | defaultModel : Model | ||
46 | defaultModel = | ||
47 | Model Nothing Nothing Array.empty Nothing defaultConfig 0 "" | ||
46 | 48 | ||
47 | 49 | ||
48 | init : () -> ( Model, Cmd Msg ) | 50 | init : () -> ( Model, Cmd Msg ) |
49 | init _ = | 51 | init _ = |
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 | ||
74 | lastWord : Model -> Bool | 72 | lastWord : Model -> Bool |
75 | lastWord model = | 73 | lastWord model = |
76 | model.currentWord == model.length - 1 | 74 | model.currentWord == model.config.length - 1 |
77 | 75 | ||
78 | 76 | ||
79 | wordEnded : String -> Bool | 77 | wordEnded : 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 | ||
186 | currentContents : Model -> String | 211 | currentContents : Model -> String |
@@ -193,14 +218,14 @@ currentContents model = | |||
193 | view : Model -> Html Msg | 218 | view : Model -> Html Msg |
194 | view model = | 219 | view 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 | ||