aboutsummaryrefslogtreecommitdiff
path: root/tprint/src/Options/Utils.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tprint/src/Options/Utils.hs')
-rw-r--r--tprint/src/Options/Utils.hs48
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 @@
1module Options.Utils
2 ( rCI
3 , rTime
4 , pRange
5 ) where
6
7import Options.Applicative
8
9import Data.Char
10import Data.Maybe
11import Data.List
12import Text.Read
13
14import Data.Time
15
16import Thermoprint.Client (Range(..))
17
18rCI :: (Read a, Show a) => ReadM a
19rCI = 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
28rTime :: ReadM UTCTime
29rTime = 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
37pRange :: 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))
42pRange 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