diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-03-01 07:34:23 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-03-01 07:34:23 +0100 |
commit | 61c983bd242ec45726ce572e85c069ca9274c71e (patch) | |
tree | 2bd74a9c80dd4f5c9e8a061894958cd9d19680b4 /tprint/src/Options | |
parent | fc83a7241e27574ef2bb81de206c59a9a854efe6 (diff) | |
download | thermoprint-61c983bd242ec45726ce572e85c069ca9274c71e.tar thermoprint-61c983bd242ec45726ce572e85c069ca9274c71e.tar.gz thermoprint-61c983bd242ec45726ce572e85c069ca9274c71e.tar.bz2 thermoprint-61c983bd242ec45726ce572e85c069ca9274c71e.tar.xz thermoprint-61c983bd242ec45726ce572e85c069ca9274c71e.zip |
Cleanup
Diffstat (limited to 'tprint/src/Options')
-rw-r--r-- | tprint/src/Options/Utils.hs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tprint/src/Options/Utils.hs b/tprint/src/Options/Utils.hs new file mode 100644 index 0000000..80b4a7e --- /dev/null +++ b/tprint/src/Options/Utils.hs | |||
@@ -0,0 +1,48 @@ | |||
1 | module Options.Utils | ||
2 | ( rCI | ||
3 | , rTime | ||
4 | , pRange | ||
5 | ) where | ||
6 | |||
7 | import Options.Applicative | ||
8 | |||
9 | import Data.Char | ||
10 | import Data.Maybe | ||
11 | import Data.List | ||
12 | import Text.Read | ||
13 | |||
14 | import Data.Time | ||
15 | |||
16 | import Thermoprint.Client (Range(..)) | ||
17 | |||
18 | rCI :: (Read a, Show a) => ReadM a | ||
19 | rCI = eitherReader rRep' | ||
20 | where | ||
21 | rRep' str = case mapMaybe readMaybe $ cases str of | ||
22 | [] -> Left $ "Could not parse `" ++ str ++ "'" | ||
23 | [x] -> Right x | ||
24 | xs -> Left $ "Ambiguous parse for `" ++ str ++ "': " ++ show xs | ||
25 | cases [] = [] | ||
26 | cases (c:cs) = [(c':cs') | c' <- [toLower c, toUpper c], cs' <- cases cs] | ||
27 | |||
28 | rTime :: ReadM UTCTime | ||
29 | rTime = eitherReader rTime' | ||
30 | where | ||
31 | rTime' str = maybe (Left $ "Could not parse `" ++ str ++ "' as a specification of time -- try the output of `date -u +'%Y-%m-%d %H:%M:%S' \"" ++ str ++ "\"`") Right | ||
32 | . foldr (<|>) Nothing | ||
33 | $ map (flip (parseTimeM True defaultTimeLocale) str) ( "%c" : "%s" : perms' [ [time, date, timezone] | time <- ["%T", "%X", "%I:%M:%S", "%r", "%X%Q", "%I:%M:%S%Q %p"], date <- ["%x", "%F"], timezone <- ["%z", "%Z"]] ) | ||
34 | perms' :: [[String]] -> [String] | ||
35 | perms' = map unwords . concatMap subsequences . concatMap permutations | ||
36 | |||
37 | pRange :: ReadM a | ||
38 | -> Mod OptionFields a -- ^ Modifiers applied to both min & max | ||
39 | -> Mod OptionFields a -- ^ Modifiers for minimum | ||
40 | -> Mod OptionFields a -- ^ Modifiers for maximum | ||
41 | -> Parser (Maybe (Range a)) | ||
42 | pRange r both min max = toRange <$> optional (option r (both <> min)) <*> optional (option r (both <> max)) | ||
43 | where | ||
44 | toRange Nothing Nothing = Nothing | ||
45 | toRange (Just min) Nothing = Just $ Min min | ||
46 | toRange Nothing (Just max) = Just $ Max max | ||
47 | toRange (Just min) (Just max) = Just $ min `Through` max | ||
48 | |||