module Shell.Utility.Log (
warn, notice, info, debug,
wrapWords,
) where
import Shell.Utility.Verbosity
import qualified System.IO as IO
import Control.Monad (when)
import qualified Data.List as List
warn :: Verbosity -> String -> IO ()
warn = atLevel normal $ IO.hPutStrLn IO.stderr . ("Warning: " ++)
notice :: Verbosity -> String -> IO ()
notice = atLevel normal putStrLn
info :: Verbosity -> String -> IO ()
info = atLevel verbose putStrLn
debug :: Verbosity -> String -> IO ()
debug = atLevel deafening putStrLn
atLevel ::
(Monad m, Ord verbosity) =>
verbosity -> (msg -> m ()) -> verbosity -> msg -> m ()
atLevel minVerbosity act verbosity msg =
when (verbosity >= minVerbosity) $ act msg
wrapWords :: Int -> [String] -> String
wrapWords width =
drop 1 . concat . snd .
List.mapAccumL
(\pos w ->
let len = length w
newPos = pos + 1 + len
in if newPos <= width
then (newPos, ' ':w)
else (len, '\n':w))
(-1)