mirror of
https://github.com/NorfairKing/feedback.git
synced 2024-11-30 12:12:00 +03:00
even further optparse refactor
This commit is contained in:
parent
553943223e
commit
45274a7637
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user