]> git.netwichtig.de Git - user/henk/code/haskell/diddohs.git/commitdiff
Changed format and structure a bit master v0.1
authorHendrik Jaeger <henk@frustcomp>
Fri, 14 Nov 2014 23:05:14 +0000 (00:05 +0100)
committerHendrik Jaeger <henk@frustcomp>
Fri, 14 Nov 2014 23:05:14 +0000 (00:05 +0100)
    modified:   src/Main.hs

src/Main.hs

index 6884d1462cd52cb50d96fb6fc36c7b7dbf42fba4..24fefa72f09082e0291a8d3d6353227664cda6ae 100644 (file)
@@ -11,7 +11,7 @@ import qualified Data.Map as Map
 import qualified Data.Text as T
 import qualified Data.Text.IO as TIO
 
-data Opt = Opt
+data Optionset = Optionset
     { optVerbose        :: Bool
     , optVersion        :: Bool
     , optHelp           :: Bool
@@ -23,8 +23,8 @@ data Opt = Opt
     , optEndDate        :: String
     }
 
-defaultOpts :: Opt
-defaultOpts = Opt
+defaultOpts :: Optionset
+defaultOpts = Optionset
     { optVerbose        = False
     , optVersion        = False
     , optHelp           = False
@@ -36,34 +36,34 @@ defaultOpts = Opt
     , optEndDate        = ""
     }
 
-availableOptions :: [OptDescr (Opt -> IO Opt)]
+availableOptions :: [OptDescr (Optionset -> IO Optionset)]
 availableOptions =
     [ Option ['h']    ["help"]
         (NoArg (\ _ -> putStrLn (usageInfo "Usage: diddohs [OPTION...]" availableOptions) >> exitSuccess))
         "Display program help"
     , Option ['v']    ["verbose"]
-        (NoArg (\ opts -> return opts { optVerbose = True }))
+        (NoArg (\ optset -> return optset { optVerbose = True }))
         "More detailed output"
     , Option ['V']    ["version"]
-        (NoArg (\ opts -> return opts { optVersion = True }))
+        (NoArg (\ optset -> return optset { optVersion = True }))
         "Display program version"
     , Option ['f']    ["file"]
-        (ReqArg (\ arg opts -> return opts { optInputFiles = optInputFiles opts ++ [arg]}) "FILE" )
+        (ReqArg (\ arg optset -> return optset { optInputFiles = optInputFiles optset ++ [arg]}) "FILE" )
         "Read from FILE"
     , Option ['w']    ["output"]
-        (ReqArg (\ arg opts -> return opts { optOutputFile = arg }) "FILE")
+        (ReqArg (\ arg optset -> return optset { optOutputFile = arg }) "FILE")
         "Write to FILE"
     , Option ['i']    ["informat"]
-        (ReqArg (\ arg opts -> return opts { optInputFormat = arg }) "FORMAT")
+        (ReqArg (\ arg optset -> return optset { optInputFormat = arg }) "FORMAT")
         "Timeformat used in input"
     , Option ['o']    ["outformat"]
-        (ReqArg (\ arg opts -> return opts { optOutputFormat = arg }) "FORMAT")
+        (ReqArg (\ arg optset -> return optset { optOutputFormat = arg }) "FORMAT")
         "Timeformat used in output"
     , Option ['s']    ["start"]
-        (ReqArg (\ arg opts -> return opts { optStartDate = arg }) "DATE")
+        (ReqArg (\ arg optset -> return optset { optStartDate = arg }) "DATE")
         "Start of reporting period"
     , Option ['e']    ["end"]
-        (ReqArg (\ arg opts -> return opts { optEndDate = arg }) "DATE")
+        (ReqArg (\ arg optset -> return optset { optEndDate = arg }) "DATE")
         "End of reporting period"
     ]
 
@@ -77,34 +77,44 @@ logentryMapToDiddoMap logmap = Map.mapWithKey toDddEntry logmap
             Nothing             -> fst $ Map.findMin logmap
 -- SECTION: Map of logentries to Map of DiddoEntries
 
+getLoglines :: [String] -> IO [T.Text]
+getLoglines []          = T.lines <$> TIO.getContents
+getLoglines files@(_:_) = T.lines . T.concat <$> mapM TIO.readFile files
+
 main :: IO ()
 main = do
     -- SECTION: option processing
-    (givenOptions,args,errs) <- getArgs >>= return . getOpt Permute availableOptions
+    (userOpts,args,errs) <-
+        getArgs >>= return . getOpt Permute availableOptions
 
     unless (null errs) $ do
         mapM_ (hPutStr stderr) errs
         exitFailure
 
-    effectiveOptions <- foldl (>>=) (return defaultOpts) givenOptions
+    opts <- foldl (>>=) (return defaultOpts) userOpts
 
     let
-        inDateFmt               = optInputFormat effectiveOptions
-        outDateFmt              = optOutputFormat effectiveOptions
+        inDateFmt   = optInputFormat opts
+        outDateFmt  = optOutputFormat opts
 
-        startDate               = parseToZonedTime inDateFmt $ optStartDate effectiveOptions
-        endDate                 = parseToZonedTime inDateFmt $ optEndDate effectiveOptions
+        startDate   = parseToZonedTime inDateFmt $ optStartDate opts
+        endDate     = parseToZonedTime inDateFmt $ optEndDate opts
     -- SECTION: option processing
 
-    loglines <- case optInputFiles effectiveOptions of
-            files@(_:_)     -> T.lines . T.concat <$> mapM TIO.readFile files
-            []              -> T.lines <$> TIO.getContents
+    loglines <- getLoglines $ optInputFiles opts
 
     let
-        timestampLogentryMap                        = Map.fromList $ map Diddo.parseDiddoLogline loglines
-        (_, _, startedTimestampLogentryMap)         = Map.splitLookup (zonedTimeToUTC startDate) timestampLogentryMap
-        (endedTimestampLogentryMap, lastEntry, _)   = Map.splitLookup (zonedTimeToUTC endDate) startedTimestampLogentryMap
-        timestampDiddoMap                           = logentryMapToDiddoMap timestampLogentryMap
+        timestampLogentryMap =
+            Map.fromList $ map Diddo.parseDiddoLogline loglines
+
+        (_, _, startedTimestampLogentryMap) =
+            Map.splitLookup (zonedTimeToUTC startDate) timestampLogentryMap
+
+        (endedTimestampLogentryMap, lastEntry, _) =
+            Map.splitLookup (zonedTimeToUTC endDate) startedTimestampLogentryMap
+
+        timestampDiddoMap =
+            logentryMapToDiddoMap timestampLogentryMap
 
     -- DEBUG
     mapM_ putStrLn args