shrun
Safe HaskellNone
LanguageGHC2021

Shrun.Logging.Formatting

Description

Provides Log formatting functionality.

Synopsis

High-level

formatConsoleLog :: (HasCallStack, HasCommands env, MonadAtomic m, MonadReader env m) => KeyHideSwitch -> ConsoleLoggingEnv -> Log -> m ConsoleLog Source #

Formats a log to be printed to the console.

formatFileLog :: (HasCallStack, HasCommands env, MonadAtomic m, MonadReader env m, MonadTime m) => KeyHideSwitch -> FileLoggingEnv -> Log -> m FileLog Source #

Formats a Log into a FileLog. Applies prefix and timestamp.

Final logs

formatConsoleMultiLineLogs :: (HasCallStack, HasCommands env, MonadAtomic m, MonadReader env m) => KeyHideSwitch -> ConsoleLoggingEnv -> NonEmpty Log -> m ConsoleLog Source #

Like formatConsoleLog, but for multiple logs. Concatenates all together with a newline.

formatFileMultiLineLogs :: (HasCallStack, HasCommands env, MonadAtomic m, MonadReader env m, MonadTime m) => KeyHideSwitch -> FileLoggingEnv -> NonEmpty Log -> m FileLog Source #

Like formatFileLog, but for multiple logs. Concatenates all together.

Low-level

logToColor :: Log -> Color Source #

Transforms log to a color based on its LogLevel.

logToPrefix :: Log -> Text Source #

Transforms log to a prefix based on its LogLevel.

levelToPrefix :: LogLevel -> Text Source #

Maps LogLevel to 'Prefix'.

Utils

concatWithLineTrunc :: Maybe (Truncation 'TruncLine, Maybe Int) -> Text -> Text -> Text Source #

Combines a prefix p and msg m with possible line truncation. If no truncation is given then concatWithLineTrunc is equivalent to p <> m. If we are given some line truncation l, then we derive

   k := l - prefix_len -- k is clamped to zero

and return

   prefix <> t'

where t' is t truncated to k chars. Notice the prefix is always included untarnished.

displayCmd :: CommandP1 -> KeyHideSwitch -> UnlinedText Source #

Pretty show for Command. If the command has a key, and KeyHideSwitch is KeyHideOff then we return the key. Otherwise we return the command itself.

>>> import Shrun.Command.Types (CommandP (MkCommandP), unsafeFromInt)
>>> import Shrun.Configuration.Data.CommonLogging.KeyHideSwitch (KeyHideSwitch (MkKeyHideSwitch))
>>> let idx = unsafeFromInt 1
>>> let mkCmd = MkCommandP idx
>>> let fmt k cmd kh = view #unUnlinedText $ displayCmd (mkCmd k cmd) kh
>>> fmt Nothing "some long command" (MkKeyHideSwitch true)
"some long command"
>>> fmt Nothing "some long command" (MkKeyHideSwitch false)
"some long command"
>>> fmt (Just "long") "some long command" (MkKeyHideSwitch true)
"some long command"
>>> fmt (Just "long") "some long command" (MkKeyHideSwitch false)
"long"

stripChars :: forall (t :: StripControlType). LogMessage -> StripControl t -> LogMessage Source #

Applies the given StripControl to the Text.

brackets :: Text -> Text Source #

Surrounds text with brackets, appending a space if the boolean is True.

Examples

Expand
>>> brackets "text"
"[text]"

formatCommandText :: Text -> UnlinedText Source #

Replace newlines with whitespace before stripping, so any strings separated by newlines do not get smashed together.