even further optparse refactor

This commit is contained in:
Tom Sydney Kerckhove 2022-03-06 13:00:08 +01:00
parent 553943223e
commit 45274a7637
3 changed files with 40 additions and 29 deletions

View File

@ -24,6 +24,19 @@ import qualified Options.Applicative.Help as OptParse (string)
import Path import Path
import Path.IO import Path.IO
data LoopSettings = LoopSettings
{ loopSettingCommand :: !String,
loopSettingOutputSettings :: !OutputSettings
}
deriving (Show, Eq, Generic)
combineToLoopSettings :: Flags -> Environment -> Maybe OutputConfiguration -> LoopConfiguration -> IO LoopSettings
combineToLoopSettings Flags {..} Environment {..} mDefaultOutputConfig LoopConfiguration {..} = do
let loopSettingCommand = loopConfigCommand
let outputConfig = liftA2 (<>) loopConfigOutputConfiguration mDefaultOutputConfig
let loopSettingOutputSettings = combineToOutputSettings flagOutputFlags outputConfig
pure LoopSettings {..}
data OutputSettings = OutputSettings data OutputSettings = OutputSettings
{ outputSettingClear :: !Clear { outputSettingClear :: !Clear
} }
@ -65,16 +78,19 @@ instance HasCodec LoopConfiguration where
<*> optionalField "output" "output configuration for this loop" .= loopConfigOutputConfiguration <*> optionalField "output" "output configuration for this loop" .= loopConfigOutputConfiguration
where where
f = \case f = \case
Left c -> Left c -> makeLoopConfiguration c
LoopConfiguration
{ loopConfigCommand = c,
loopConfigOutputConfiguration = Nothing
}
Right loopConfig -> loopConfig Right loopConfig -> loopConfig
g loopConfig@(LoopConfiguration c mOutputConfig) = case mOutputConfig of g loopConfig@(LoopConfiguration c mOutputConfig) = case mOutputConfig of
Nothing -> Left c Nothing -> Left c
Just _ -> Right loopConfig Just _ -> Right loopConfig
makeLoopConfiguration :: String -> LoopConfiguration
makeLoopConfiguration c =
LoopConfiguration
{ loopConfigCommand = c,
loopConfigOutputConfiguration = Nothing
}
data OutputConfiguration = OutputConfiguration data OutputConfiguration = OutputConfiguration
{ outputConfigClear :: !(Maybe Clear) { outputConfigClear :: !(Maybe Clear)
} }

View File

@ -21,7 +21,7 @@ import UnliftIO
runFeedbackLoop :: IO () runFeedbackLoop :: IO ()
runFeedbackLoop = do runFeedbackLoop = do
Settings {..} <- getSettings LoopSettings {..} <- getLoopSettings
eventChan <- newChan eventChan <- newChan
outputChan <- newChan outputChan <- newChan
here <- getCurrentDir here <- getCurrentDir
@ -36,8 +36,8 @@ runFeedbackLoop = do
$ \event -> do $ \event -> do
writeChan eventChan event writeChan eventChan event
concurrently_ concurrently_
(processWorker settingCommand eventChan outputChan) (processWorker loopSettingCommand eventChan outputChan)
(outputWorker settingOutputSettings outputChan) (outputWorker loopSettingOutputSettings outputChan)
stopListeningAction stopListeningAction
eventFilter :: Path Abs Dir -> FS.Event -> Bool eventFilter :: Path Abs Dir -> FS.Event -> Bool

View File

@ -1,4 +1,3 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-} {-# LANGUAGE DerivingVia #-}
{-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
@ -13,25 +12,17 @@ import Feedback.Common.OptParse
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Text.Show.Pretty (pPrint) import Text.Show.Pretty (pPrint)
getSettings :: IO Settings getLoopSettings :: IO LoopSettings
getSettings = do getLoopSettings = do
flags <- getFlags flags <- getFlags
env <- getEnvironment env <- getEnvironment
config <- getConfiguration flags env config <- getConfiguration flags env
combineToSettings flags env config combineToSettings flags env config
data Settings = Settings combineToSettings :: Flags -> Environment -> Maybe Configuration -> IO LoopSettings
{ settingCommand :: !String, combineToSettings flags@Flags {..} environment mConf = do
settingOutputSettings :: !OutputSettings
}
deriving (Show, Eq, Generic)
-- | Combine everything to 'Settings'
combineToSettings :: Flags -> Environment -> Maybe Configuration -> IO Settings
combineToSettings Flags {..} Environment {} mConf = do
let loops = maybe M.empty configLoops mConf let loops = maybe M.empty configLoops mConf
let defaultOutputConfig = mConf >>= configOutputConfiguration loopConfig <- case M.lookup flagCommand loops of
(settingCommand, outputConfig) <- case M.lookup flagCommand loops of
Nothing -> do Nothing -> do
when (not (null loops)) $ when (not (null loops)) $
putStrLn $ putStrLn $
@ -40,16 +31,20 @@ combineToSettings Flags {..} Environment {} mConf = do
show flagCommand <> ",", show flagCommand <> ",",
"interpreting it as a standalone command." "interpreting it as a standalone command."
] ]
pure (flagCommand, defaultOutputConfig) pure $ makeLoopConfiguration flagCommand
Just LoopConfiguration {..} -> do Just config -> do
putStrLn $ putStrLn $
unwords unwords
[ "Interpreting", [ "Interpreting",
show flagCommand, show flagCommand,
"as the name of a configured loop." "as the name of a configured loop."
] ]
pure (loopConfigCommand, liftA2 (<>) loopConfigOutputConfiguration defaultOutputConfig) pure config
let settingOutputSettings = combineToOutputSettings flagOutputFlags outputConfig loopSets <-
let settings = Settings {..} combineToLoopSettings
when flagDebug $ pPrint settings flags
pure settings environment
(mConf >>= configOutputConfiguration)
loopConfig
when flagDebug $ pPrint loopSets
pure loopSets