Skip to content

Commit 48c7752

Browse files
committed
Normalize user input
1 parent c2ee473 commit 48c7752

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

message-index/helper-tool.hs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Main where
22

33
import Control.Monad (forM)
4+
import Data.Char (toLower, isSpace)
45
import Text.Read (readMaybe)
56
import System.Directory (createDirectory)
67
import System.FilePath ((</>))
@@ -9,6 +10,13 @@ import System.FilePath ((</>))
910
-- Querying the user about the diagnostic
1011
-------------------------------------------------------------------------------
1112

13+
-- | Remove leading and trailing whitespace, and convert to lower case.
14+
normalize :: String -> String
15+
normalize = fmap toLower . strip
16+
where
17+
strip = f . f
18+
f = reverse . dropWhile isSpace
19+
1220
-- Querying for the system: GHC / GHCup / Stack
1321

1422
data System = GHC | GHCup | Stack deriving Show
@@ -21,13 +29,13 @@ readSystem = do
2129
putStrLn " 3) Stack"
2230
putStr "Input (Default = GHC): "
2331
ln <- getLine
24-
case ln of
32+
case normalize ln of
2533
"1" -> pure GHC
26-
"GHC" -> pure GHC
34+
"ghc" -> pure GHC
2735
"2" -> pure GHCup
28-
"GHCup" -> pure GHCup
36+
"ghcup" -> pure GHCup
2937
"3" -> pure Stack
30-
"Stack" -> pure Stack
38+
"stack" -> pure Stack
3139
_ -> pure GHC
3240

3341
-- Querying for the error code
@@ -66,22 +74,21 @@ readSummary = do
6674
getLine
6775

6876
-- Severity
69-
data Severity = Error | Warning | NA deriving Show
77+
data Severity = Error | Warning deriving Show
7078

7179
readSeverity :: IO Severity
7280
readSeverity = do
7381
putStrLn "What is the severity of the diagnostic."
7482
putStrLn " 1) Error"
7583
putStrLn " 2) Warning"
76-
putStrLn " 3) N/A"
77-
putStr "Input (Default = N/A): "
84+
putStr "Input (Default = Error): "
7885
ln <- getLine
79-
case ln of
86+
case normalize ln of
8087
"1" -> pure Error
81-
"Error" -> pure Error
88+
"error" -> pure Error
8289
"2" -> pure Warning
83-
"Warning" -> pure Warning
84-
_ -> pure NA
90+
"warning" -> pure Warning
91+
_ -> pure Error
8592

8693
-- Warning flag
8794
type WarningFlag = String
@@ -170,7 +177,7 @@ createFiles tmpl = do
170177
let toplvl_index = unlines [ "---"
171178
, "title: " <> title tmpl
172179
, "summary: " <> summary tmpl
173-
, "severity: " <> case severity tmpl of { Warning -> "warning"; Error -> "error"; NA -> "na" }
180+
, "severity: " <> case severity tmpl of { Warning -> "warning"; Error -> "error" }
174181
, "introduced: " <> introduced tmpl
175182
, "---"
176183
, ""

0 commit comments

Comments
 (0)