-
Notifications
You must be signed in to change notification settings - Fork 0
/
cadice.hs
executable file
·75 lines (68 loc) · 2.06 KB
/
cadice.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE OverloadedStrings #-}
import Options.Applicative
import Data.Semigroup ((<>))
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Char
import Data.List
data Args = Args { wordlist :: String
, dicerolls :: String }
args :: Parser Args
args = Args
<$> strOption
( long "wordlist"
<> short 'l'
<> metavar "PATH"
<> help "Diceware word list" )
<*> strOption
( long "dicerolls"
<> short 'd'
<> value ""
<> metavar "ROLLS"
<> help "String of n*5 digits between 1 and 6, spaces optional" )
rolls :: T.Text -> IO [T.Text]
rolls "" = do
T.putStrLn "Please enter a dice roll sequence:"
xs <- T.getLine
rolls xs
rolls xs = if (T.length $ last rs) == dwIndexLength
then
return rs
else do
T.putStrLn $ T.pack $ "Rolls not a multiple of " ++
(show dwIndexLength)
return []
where
dwIndexLength = 5
chunk = (T.chunksOf dwIndexLength) . (T.filter (not . isSpace))
rs = chunk xs
search :: FilePath -> [T.Text] -> IO [T.Text]
search path rs = do
f <- T.readFile path
mapM (search' (T.lines f)) rs
where
search' :: [T.Text] -> T.Text -> IO T.Text
search' [] roll = do
T.putStrLn $ T.pack $ "Missing sequence " ++ T.unpack roll
return ""
search' (l:ls) roll = do
let ws = T.words l
if length ws == 2 && head ws == roll
then
return $ (head . tail) ws
else
search' ls roll
run :: Args -> IO ()
run as = do
rs <- rolls $ T.pack $ dicerolls as
ws <- search (wordlist as) rs
mapM_ T.putStr $ intersperse " " ws
main :: IO ()
main = run =<< execParser opts
where
versionOption = infoOption "ColdAsDice version 1.0" (long "version" <>
short 'V' <> help "Show version")
opts = info (args <**> helper <**> versionOption)
( fullDesc
<> progDesc "search words in a plaintext Diceware WORDLIST"
<> header "cadice - a Diceware convenience utility" )