From c7878e88da21a3411329c31540f784308f5f28a7 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Tue, 26 Aug 2025 16:57:32 +0100 Subject: [PATCH] lib:Hledger.Utils.IO: add warnIO --- hledger-lib/Hledger/Utils/IO.hs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/hledger-lib/Hledger/Utils/IO.hs b/hledger-lib/Hledger/Utils/IO.hs index aab2ef036..6e0695643 100644 --- a/hledger-lib/Hledger/Utils/IO.hs +++ b/hledger-lib/Hledger/Utils/IO.hs @@ -23,6 +23,7 @@ module Hledger.Utils.IO ( error', usageError, warn, + warnIO, ansiFormatError, ansiFormatWarning, printError, @@ -160,6 +161,7 @@ import System.Process (CreateProcess(..), StdStream(CreatePipe), creat import Text.Pretty.Simple (CheckColorTty(..), OutputOptions(..), defaultOutputOptionsDarkBg, defaultOutputOptionsNoColor, pShowOpt, pPrintOpt) import Hledger.Utils.Text (WideBuilder(WideBuilder)) +import Control.Monad.IO.Class (MonadIO, liftIO) -- Pretty showing/printing @@ -217,17 +219,20 @@ usageError = error' . (++ " (use -h to see usage)") ansiFormatError :: String -> String ansiFormatError = (<> sgrresetall) . ((sgrbrightred <> sgrbold) <>) --- | Show a message, with "Warning:" label, on stderr before returning the given value. --- Also do some ANSI styling of the first line when allowed (using unsafe IO). --- Currently we use this very sparingly in hledger; we prefer to either quietly work, --- or loudly raise an error. Varying output can make scripting harder, --- but on stderr, it shouldn't cause much hassle. +-- | Show a warning message on stderr before returning the given value. +-- Like trace, but prepends a "Warning:" label, and does some ANSI styling of the first line when allowed (using unsafe IO). +-- Currently we use this very sparingly in hledger; we prefer to either quietly work, or loudly raise an error. +-- Varying output can make scripting harder. But on stderr, it shouldn't cause much hassle. warn :: String -> a -> a -warn msg = trace msg' - where - msg' = - (if useColorOnStderrUnsafe then modifyFirstLine ansiFormatWarning else id) $ - "Warning: "<> msg +warn = trace . formatWarning + +-- | Like warn, but take extra care to sequence properly in IO. +warnIO :: MonadIO m => String -> m () +warnIO = liftIO . traceIO . formatWarning + +formatWarning = + (if useColorOnStderrUnsafe then modifyFirstLine ansiFormatWarning else id) . + ("Warning: " <>) -- | Apply standard ANSI SGR formatting (yellow, bold) suitable for console warning text. ansiFormatWarning :: String -> String