Skip to content

Commit

Permalink
Add 'Nothing' to WithDisabled; make usage more principled
Browse files Browse the repository at this point in the history
  • Loading branch information
tbidne committed Mar 10, 2024
1 parent b411eeb commit c335dd6
Show file tree
Hide file tree
Showing 19 changed files with 428 additions and 374 deletions.
4 changes: 2 additions & 2 deletions shrun.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ library
Shrun.Configuration.Data.FileLogging
Shrun.Configuration.Data.MergedConfig
Shrun.Configuration.Data.Notify
Shrun.Configuration.Data.WithDisable
Shrun.Configuration.Data.WithDisabled
Shrun.Configuration.Legend
Shrun.Configuration.Toml
Shrun.Data.Command
Expand Down Expand Up @@ -167,7 +167,7 @@ test-suite unit
Unit.Generators
Unit.Prelude
Unit.Shrun.Configuration.Args
Unit.Shrun.Configuration.Data.WithDisable
Unit.Shrun.Configuration.Data.WithDisabled
Unit.Shrun.Configuration.Legend
Unit.Shrun.Logging.Formatting
Unit.Shrun.Logging.Generators
Expand Down
45 changes: 20 additions & 25 deletions src/Shrun/Configuration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ import Shrun.Configuration.Data.MergedConfig
),
)
import Shrun.Configuration.Data.Notify (mergeNotifyLogging)
import Shrun.Configuration.Data.WithDisable
( WithDisable,
alternativeDefault,
alternativeEmpty,
defaultIfDisabled,
emptyIfDisabled,
)
import Shrun.Configuration.Data.WithDisabled (WithDisabled, (<>?))
import Shrun.Configuration.Data.WithDisabled qualified as WD
import Shrun.Configuration.Legend qualified as Legend
import Shrun.Configuration.Toml (Toml)
import Shrun.Data.Command (Command (MkCommand))
Expand Down Expand Up @@ -78,24 +73,24 @@ mergeConfig args mToml = do
$ MkMergedConfig
{ coreConfig =
MkCoreConfigP
{ timeout = emptyIfDisabled (args ^. (#coreConfig % #timeout)),
init = emptyIfDisabled (args ^. (#coreConfig % #init)),
{ timeout = WD.toMaybe (args ^. (#coreConfig % #timeout)),
init = WD.toMaybe (args ^. (#coreConfig % #init)),
keyHide =
defaultIfDisabled KeyHideOff (args ^. (#coreConfig % #keyHide)),
WD.fromWithDisabled KeyHideOff (args ^. (#coreConfig % #keyHide)),
pollInterval =
defaultIfDisabled
WD.fromWithDisabled
defaultPollInterval
(args ^. (#coreConfig % #pollInterval)),
cmdLogSize =
defaultIfDisabled
WD.fromWithDisabled
defaultCmdLogSize
(args ^. (#coreConfig % #cmdLogSize)),
timerFormat =
defaultIfDisabled
WD.fromWithDisabled
defaultTimerFormat
(args ^. (#coreConfig % #timerFormat)),
cmdNameTrunc =
emptyIfDisabled (args ^. (#coreConfig % #cmdNameTrunc)),
WD.toMaybe (args ^. (#coreConfig % #cmdNameTrunc)),
cmdLogging,
fileLogging =
mergeFileLogging
Expand Down Expand Up @@ -128,31 +123,31 @@ mergeConfig args mToml = do
{ coreConfig =
MkCoreConfigP
{ timeout =
altNothing #timeout (toml ^. (#coreConfig % #timeout)),
plusNothing #timeout (toml ^. (#coreConfig % #timeout)),
init =
altNothing #init (toml ^. (#coreConfig % #init)),
plusNothing #init (toml ^. (#coreConfig % #init)),
keyHide =
altDefault
plusDefault
KeyHideOff
#keyHide
(toml ^. (#coreConfig % #keyHide)),
pollInterval =
altDefault
plusDefault
defaultPollInterval
#pollInterval
(toml ^. (#coreConfig % #pollInterval)),
cmdLogSize =
altDefault
plusDefault
defaultCmdLogSize
#cmdLogSize
(toml ^. (#coreConfig % #cmdLogSize)),
timerFormat =
altDefault
plusDefault
defaultTimerFormat
#timerFormat
(toml ^. (#coreConfig % #timerFormat)),
cmdNameTrunc =
altNothing
plusNothing
#cmdNameTrunc
(toml ^. (#coreConfig % #cmdNameTrunc)),
cmdLogging,
Expand All @@ -170,8 +165,8 @@ mergeConfig args mToml = do
where
cmdsText = args ^. #commands

altDefault :: a -> Lens' CoreConfigArgs (WithDisable (Maybe a)) -> Maybe a -> a
altDefault defA l = alternativeDefault defA (args ^. (#coreConfig % l))
plusDefault :: a -> Lens' CoreConfigArgs (WithDisabled a) -> Maybe a -> a
plusDefault defA l r = WD.fromWithDisabled defA $ (args ^. (#coreConfig % l)) <>? r

altNothing :: Lens' CoreConfigArgs (WithDisable (Maybe a)) -> Maybe a -> Maybe a
altNothing l = alternativeEmpty (args ^. (#coreConfig % l))
plusNothing :: Lens' CoreConfigArgs (WithDisabled a) -> Maybe a -> Maybe a
plusNothing l r = WD.toMaybe $ (args ^. (#coreConfig % l)) <>? r
3 changes: 1 addition & 2 deletions src/Shrun/Configuration/Args.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ import Shrun.Configuration.Data.FileLogging
import Shrun.Configuration.Data.Notify
( NotifyP (MkNotifyP, action, system, timeout),
)
import Shrun.Configuration.Data.WithDisable (WithDisable (With))
import Shrun.Prelude

defaultArgs :: NESeq Text -> Args
defaultArgs commands =
MkArgs
{ configPath = mempty,
cmdLog = With False,
cmdLog = mempty,
coreConfig =
MkCoreConfigP
{ timeout = mempty,
Expand Down
22 changes: 14 additions & 8 deletions src/Shrun/Configuration/Args/Parsing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ import Shrun.Configuration.Args.Parsing.Core qualified as Core
import Shrun.Configuration.Args.Parsing.Utils qualified as Utils
import Shrun.Configuration.Args.TH (getDefaultConfigTH)
import Shrun.Configuration.Data.Core (CoreConfigArgs)
import Shrun.Configuration.Data.WithDisable (WithDisable)
import Shrun.Configuration.Data.WithDisabled (WithDisabled)
import Shrun.Prelude
import Shrun.Utils qualified as U

-- | CLI args.
data Args = MkArgs
{ -- | Optional config file.
configPath :: WithDisable (Maybe OsPath),
configPath :: WithDisabled OsPath,
-- | Whether to log commands.
cmdLog :: WithDisable Bool,
cmdLog :: WithDisabled (),
-- | Core config.
coreConfig :: CoreConfigArgs,
-- | List of commands.
Expand Down Expand Up @@ -115,8 +115,8 @@ defaultConfig = OA.infoOption (unpack txt) (OA.long "default-config" <> Utils.mk
txt = T.unlines $$getDefaultConfigTH
help = "Writes a default config.toml file to stdout."

configParser :: Parser (WithDisable (Maybe OsPath))
configParser = Utils.withDisableParser mainParser "config"
configParser :: Parser (WithDisabled OsPath)
configParser = Utils.withDisabledParser mainParser "config"
where
mainParser =
OA.optional
Expand All @@ -137,17 +137,23 @@ configParser = Utils.withDisableParser mainParser "config"
"--config and the automatic XDG lookup."
]

cmdLogParser :: Parser (WithDisable Bool)
cmdLogParser = Utils.withDisableParser mainParser "cmd-log"
cmdLogParser :: Parser (WithDisabled ())
cmdLogParser = Utils.withDisabledParser mainParser "cmd-log"
where
mainParser =
switchParser =
OA.switch
( mconcat
[ OA.short 'l',
OA.long "cmd-log",
Utils.mkHelp helpTxt
]
)
mainParser = do
b <- switchParser
pure
$ if b
then Just ()
else Nothing
helpTxt =
mconcat
[ "The default behavior is to swallow logs for the commands ",
Expand Down
10 changes: 5 additions & 5 deletions src/Shrun/Configuration/Args/Parsing/CmdLogging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Shrun.Configuration.Data.CmdLogging
stripControl
),
)
import Shrun.Configuration.Data.WithDisable (WithDisable)
import Shrun.Configuration.Data.WithDisabled (WithDisabled)
import Shrun.Data.StripControl (StripControl)
import Shrun.Data.StripControl qualified as StripControl
import Shrun.Data.Truncation (LineTruncation)
Expand All @@ -33,9 +33,9 @@ cmdLoggingParser = do
lineTrunc
}

cmdLogStripControlParser :: Parser (WithDisable (Maybe StripControl))
cmdLogStripControlParser :: Parser (WithDisabled StripControl)
cmdLogStripControlParser =
Utils.withDisableParser mainParser "cmd-log-strip-control"
Utils.withDisabledParser mainParser "cmd-log-strip-control"
where
mainParser =
OA.optional
Expand All @@ -60,8 +60,8 @@ cmdLogStripControlParser =
" This option is experimental and subject to change."
]

cmdLogLineTruncParser :: Parser (WithDisable (Maybe LineTruncation))
cmdLogLineTruncParser = Utils.withDisableParser mainParser "cmd-log-line-trunc"
cmdLogLineTruncParser :: Parser (WithDisabled LineTruncation)
cmdLogLineTruncParser = Utils.withDisabledParser mainParser "cmd-log-line-trunc"
where
mainParser =
OA.optional
Expand Down
30 changes: 15 additions & 15 deletions src/Shrun/Configuration/Args/Parsing/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import Shrun.Configuration.Data.Core
timerFormat
),
)
import Shrun.Configuration.Data.WithDisable (WithDisable)
import Shrun.Configuration.Data.WithDisabled (WithDisabled)
import Shrun.Data.KeyHide (KeyHide (KeyHideOn))
import Shrun.Data.PollInterval
( PollInterval,
Expand Down Expand Up @@ -69,8 +69,8 @@ coreParser = do
notify
}

timeoutParser :: Parser (WithDisable (Maybe Timeout))
timeoutParser = Utils.withDisableParser mainParser "timeout"
timeoutParser :: Parser (WithDisabled Timeout)
timeoutParser = Utils.withDisabledParser mainParser "timeout"
where
mainParser =
OA.optional
Expand All @@ -90,8 +90,8 @@ timeoutParser = Utils.withDisableParser mainParser "timeout"
"2h3s. Defaults to no timeout."
]

initParser :: Parser (WithDisable (Maybe Text))
initParser = Utils.withDisableParser mainParser "init"
initParser :: Parser (WithDisabled Text)
initParser = Utils.withDisabledParser mainParser "init"
where
mainParser =
OA.optional
Expand All @@ -109,8 +109,8 @@ initParser = Utils.withDisableParser mainParser "init"
"to 'shrun \". ~/.bashrc && foo\" \". ~/.bashrc && bar\"'."
]

keyHideParser :: Parser (WithDisable (Maybe KeyHide))
keyHideParser = Utils.withDisableParser mainParser "key-hide"
keyHideParser :: Parser (WithDisabled KeyHide)
keyHideParser = Utils.withDisabledParser mainParser "key-hide"
where
mainParser =
OA.optional
Expand All @@ -130,8 +130,8 @@ keyHideParser = Utils.withDisableParser mainParser "key-hide"
"unaffected."
]

pollIntervalParser :: Parser (WithDisable (Maybe PollInterval))
pollIntervalParser = Utils.withDisableParser mainParser "poll-interval"
pollIntervalParser :: Parser (WithDisabled PollInterval)
pollIntervalParser = Utils.withDisabledParser mainParser "poll-interval"
where
mainParser =
OA.optional
Expand Down Expand Up @@ -164,8 +164,8 @@ pollIntervalParser = Utils.withDisableParser mainParser "poll-interval"
. showt
. view #unPollInterval

cmdLogSizeParser :: Parser (WithDisable (Maybe (Bytes B Natural)))
cmdLogSizeParser = Utils.withDisableParser mainParser "cmd-log-size"
cmdLogSizeParser :: Parser (WithDisabled (Bytes B Natural))
cmdLogSizeParser = Utils.withDisabledParser mainParser "cmd-log-size"
where
mainParser =
OA.optional
Expand All @@ -186,8 +186,8 @@ cmdLogSizeParser = Utils.withDisableParser mainParser "cmd-log-size"
"across lines. The default is 1024."
]

timerFormatParser :: Parser (WithDisable (Maybe TimerFormat))
timerFormatParser = Utils.withDisableParser mainParser "timer-format"
timerFormatParser :: Parser (WithDisabled TimerFormat)
timerFormatParser = Utils.withDisabledParser mainParser "timer-format"
where
mainParser =
OA.optional
Expand All @@ -203,8 +203,8 @@ timerFormatParser = Utils.withDisableParser mainParser "timer-format"
"'2 hours, 3 seconds'."
]

cmdNameTruncParser :: Parser (WithDisable (Maybe (Truncation TCmdName)))
cmdNameTruncParser = Utils.withDisableParser mainParser "cmd-name-trunc"
cmdNameTruncParser :: Parser (WithDisabled (Truncation TCmdName))
cmdNameTruncParser = Utils.withDisabledParser mainParser "cmd-name-trunc"
where
mainParser =
OA.optional
Expand Down
18 changes: 9 additions & 9 deletions src/Shrun/Configuration/Args/Parsing/FileLogging.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Shrun.Configuration.Data.FileLogging
( FileLoggingArgs,
FileLoggingP (MkFileLoggingP, mode, path, sizeMode, stripControl),
)
import Shrun.Configuration.Data.WithDisable (WithDisable)
import Shrun.Configuration.Data.WithDisabled (WithDisabled)
import Shrun.Data.FileMode (FileMode)
import Shrun.Data.FileMode qualified as FileMode
import Shrun.Data.FilePathDefault (FilePathDefault)
Expand All @@ -37,8 +37,8 @@ fileLoggingParser = do
sizeMode
}

fileLogParser :: Parser (WithDisable (Maybe FilePathDefault))
fileLogParser = Utils.withDisableParser mainParser "file-log"
fileLogParser :: Parser (WithDisabled FilePathDefault)
fileLogParser = Utils.withDisabledParser mainParser "file-log"
where
mainParser =
OA.optional
Expand All @@ -61,9 +61,9 @@ fileLogParser = Utils.withDisableParser mainParser "file-log"
"directory e.g. ~/.config/shrun/shrun.log."
]

fileLogStripControlParser :: Parser (WithDisable (Maybe StripControl))
fileLogStripControlParser :: Parser (WithDisabled StripControl)
fileLogStripControlParser =
Utils.withDisableParser mainParser "file-log-strip-control"
Utils.withDisabledParser mainParser "file-log-strip-control"
where
mainParser =
OA.optional
Expand All @@ -81,8 +81,8 @@ fileLogStripControlParser =
"Defaults to all."
]

fileLogModeParser :: Parser (WithDisable (Maybe FileMode))
fileLogModeParser = Utils.withDisableParser mainParser "file-log-mode"
fileLogModeParser :: Parser (WithDisabled FileMode)
fileLogModeParser = Utils.withDisabledParser mainParser "file-log-mode"
where
mainParser =
OA.optional
Expand All @@ -96,8 +96,8 @@ fileLogModeParser = Utils.withDisableParser mainParser "file-log-mode"
)
helpTxt = "Mode in which to open the log file. Defaults to write."

fileLogSizeModeParser :: Parser (WithDisable (Maybe FileSizeMode))
fileLogSizeModeParser = Utils.withDisableParser mainParser "file-log-size-mode"
fileLogSizeModeParser :: Parser (WithDisabled FileSizeMode)
fileLogSizeModeParser = Utils.withDisabledParser mainParser "file-log-size-mode"
where
mainParser =
OA.optional
Expand Down
Loading

0 comments on commit c335dd6

Please sign in to comment.