Skip to content

Commit 6da2b00

Browse files
committed
Implemented error-checking and displaying
checks that field have the right types of values in them and alerts the user if they do not
1 parent 2260acb commit 6da2b00

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

src/Demo/JS.hs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module Demo.JS ( readInputState
33
, writeInputState
44
, mkRandomInput
55
, sDrawButton
6+
, printHighError
7+
, printLowError
8+
, cullErrors
69
, sRandomButton
710
, drawList
811
, placeValues
@@ -14,6 +17,7 @@ module Demo.JS ( readInputState
1417

1518
import Control.Monad
1619
import Control.Applicative
20+
import Text.Read (readMaybe)
1721
import JavaScript.JQuery
1822
import JavaScript.Canvas hiding (Left, Right)
1923
import GHCJS.Types
@@ -48,10 +52,15 @@ sCellNum i = select (pack (template (cellMkName i)))
4852
++ n
4953
++ "\" type=\"text\" name=\"a\" /></div>"
5054

51-
getGenInfo :: IO (String, String)
52-
getGenInfo = do start <- fmap unpack (sStartHead >>= getVal)
53-
size <- fmap unpack (sNumCells >>= getVal)
54-
return (start, size)
55+
getGenInfo :: IO (Either String (Int, Int))
56+
getGenInfo =
57+
do start <- fmap unpack (sStartHead >>= getVal)
58+
size <- fmap unpack (sNumCells >>= getVal)
59+
case (readMaybe start, readMaybe size) of
60+
(Nothing,_) -> return (Left "\"Starting Index\" requires Integer")
61+
(Just _, Nothing) ->
62+
return (Left "\"Number of Memory Cells\" requires Integer")
63+
(Just i, Just s) -> return (Right (i,s))
5564

5665
placeValues :: Int -> Int -> IO ()
5766
placeValues start size =
@@ -166,9 +175,9 @@ mkCanvas = do
166175
return ()
167176

168177
displayOutput :: Either String Layout -> IO ()
169-
displayOutput l = cullError >> case l of
170-
Left er -> printError er
171-
Right ls -> drawList ls
178+
displayOutput l = cullErrors >> case l of
179+
Left er -> printLowError er
180+
Right ls -> drawList ls
172181

173182
withPadding :: (Double, Double) -> (Double, Double)
174183
withPadding (x,y) = (x - (2 * canvasXPadding), y - (2 * canvasYPadding))
@@ -283,8 +292,19 @@ drawElem c scale elem =
283292

284293
restore c
285294

286-
cullError = return ()
287-
printError a = return ()
295+
cullErrors = select "#lowError" >>= remove
296+
>> select "#highError" >>= remove
297+
>> return ()
298+
299+
printHighError = printError "highError" "#b"
300+
printLowError = printError "lowError" "#c"
301+
302+
printError a b e =
303+
do err <- select (pack
304+
("<p class=\"errors\" id=\"" ++ a ++ "\">Error: (" ++ e ++ ")</p>"))
305+
par <- select (pack b)
306+
appendJQuery err par
307+
return ()
288308

289309
drawTextCenter :: Coord -- location at which to center the text
290310
-> Double -- maximum width of the text

src/LinkedListDemo.hs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@ defaultNumCells = 14
1515

1616
main = do initializePage (defaultHeadIndex, defaultNumCells)
1717
(draw, rando, gener) <- mkSources
18-
wireButton draw sDrawButton readInputState
19-
wireButton rando sRandomButton mkRandomInput
20-
wireButton gener sCellGen generateCells
18+
wireButton draw sDrawButton (cullErrors >> readInputState)
19+
wireButton rando sRandomButton (cullErrors >> mkRandomInput)
20+
wireButton gener sCellGen (cullErrors >> generateCells)
2121
n <- compile (mkNetwork (draw, rando, gener))
2222
actuate n
2323

2424
generateCells :: IO ()
25-
generateCells = do (start,size) <- getGenInfo
26-
initializePage (read start, read size)
25+
generateCells =
26+
do genInfo <- getGenInfo
27+
case checkGenInfo =<< genInfo of
28+
Right (start,size) -> initializePage (start,size)
29+
Left err -> printHighError err
30+
31+
checkGenInfo :: (Int,Int) -> Either String (Int,Int)
32+
checkGenInfo (i,s)
33+
| i < 0 = Left "Starting Index cannot be negative"
34+
| s < 2 || s > 100 = Left "Number of Cells must be between 2 and 100"
35+
| otherwise = Right (i,s)
2736

2837
initializePage (start,size) =
2938
placeValues start size

static/linked_list.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,15 @@ div.buttons button {
194194

195195
div.break {
196196
height: 1em;
197+
}
198+
199+
.errors {
200+
color: red;
201+
font-weight: bold;
202+
display: table !important;
203+
padding: 0.3em;
204+
border-radius: 0.2em;
205+
margin-left: auto;
206+
margin-right: auto;
207+
background-color: white;
197208
}

0 commit comments

Comments
 (0)