|
@@ -5,6 +5,8 @@ module Bricoleur.Utils
|
|
, cDebug
|
|
, cDebug
|
|
, cWarn
|
|
, cWarn
|
|
, cError
|
|
, cError
|
|
|
|
+, cDie
|
|
|
|
+, bsErrorLn
|
|
|
|
|
|
, cOpenFile
|
|
, cOpenFile
|
|
|
|
|
|
@@ -19,6 +21,7 @@ module Bricoleur.Utils
|
|
) where
|
|
) where
|
|
|
|
|
|
import qualified Formatting as F
|
|
import qualified Formatting as F
|
|
|
|
+import qualified Data.ByteString.Lazy.Char8 as BS
|
|
import qualified Data.Text.Lazy as TL
|
|
import qualified Data.Text.Lazy as TL
|
|
import qualified Data.Text.Lazy.Builder as TL
|
|
import qualified Data.Text.Lazy.Builder as TL
|
|
import qualified Data.Text.Lazy.IO as TL
|
|
import qualified Data.Text.Lazy.IO as TL
|
|
@@ -30,22 +33,27 @@ import qualified System.Posix.Terminal as Posix
|
|
|
|
|
|
import Prelude (FilePath, IO, Either(Left), ($))
|
|
import Prelude (FilePath, IO, Either(Left), ($))
|
|
|
|
|
|
|
|
+-- | Produce a 'Left' value from a format string
|
|
throw :: F.Format (Either TL.Text r) a -> a
|
|
throw :: F.Format (Either TL.Text r) a -> a
|
|
throw f =
|
|
throw f =
|
|
F.runFormat f (\ b -> Left (TL.toLazyText b))
|
|
F.runFormat f (\ b -> Left (TL.toLazyText b))
|
|
|
|
|
|
|
|
+
|
|
stderr :: TL.Text -> IO ()
|
|
stderr :: TL.Text -> IO ()
|
|
stderr = TL.hPutStr Sys.stderr
|
|
stderr = TL.hPutStr Sys.stderr
|
|
|
|
|
|
|
|
+
|
|
-- | Write output to stdout
|
|
-- | Write output to stdout
|
|
cOutput :: TL.Text -> IO ()
|
|
cOutput :: TL.Text -> IO ()
|
|
cOutput = TL.putStrLn
|
|
cOutput = TL.putStrLn
|
|
|
|
|
|
|
|
+
|
|
-- | Write a debug message to stderr.
|
|
-- | Write a debug message to stderr.
|
|
cDebug :: F.Format (IO ()) a -> a
|
|
cDebug :: F.Format (IO ()) a -> a
|
|
cDebug msg = F.runFormat msg $ \ b ->
|
|
cDebug msg = F.runFormat msg $ \ b ->
|
|
TL.hPutStrLn Sys.stderr (TL.toLazyText b)
|
|
TL.hPutStrLn Sys.stderr (TL.toLazyText b)
|
|
|
|
|
|
|
|
+
|
|
-- | Write a warning message to stderr. If we are connected to a TTY,
|
|
-- | Write a warning message to stderr. If we are connected to a TTY,
|
|
-- then this will write in an orange color.
|
|
-- then this will write in an orange color.
|
|
cWarn :: F.Format (IO ()) a -> a
|
|
cWarn :: F.Format (IO ()) a -> a
|
|
@@ -57,9 +65,10 @@ cWarn msg = F.runFormat msg $ \b -> do
|
|
stderr "\x1b[39m\n"
|
|
stderr "\x1b[39m\n"
|
|
else TL.hPutStrLn Sys.stderr (TL.toLazyText b)
|
|
else TL.hPutStrLn Sys.stderr (TL.toLazyText b)
|
|
|
|
|
|
|
|
+
|
|
-- | Write an error message to stderr and exit. If we are connected to
|
|
-- | Write an error message to stderr and exit. If we are connected to
|
|
-- a TTY, this message will be in red.
|
|
-- a TTY, this message will be in red.
|
|
-cError :: F.Format (IO r) a -> a
|
|
|
|
|
|
+cError :: F.Format (IO ()) a -> a
|
|
cError msg = F.runFormat msg $ \b -> do
|
|
cError msg = F.runFormat msg $ \b -> do
|
|
isTTY <- Posix.queryTerminal Posix.stdOutput
|
|
isTTY <- Posix.queryTerminal Posix.stdOutput
|
|
if isTTY
|
|
if isTTY
|
|
@@ -67,13 +76,33 @@ cError msg = F.runFormat msg $ \b -> do
|
|
stderr (TL.toLazyText b)
|
|
stderr (TL.toLazyText b)
|
|
stderr "\x1b[39m\n"
|
|
stderr "\x1b[39m\n"
|
|
else TL.hPutStrLn Sys.stderr (TL.toLazyText b)
|
|
else TL.hPutStrLn Sys.stderr (TL.toLazyText b)
|
|
|
|
+
|
|
|
|
+-- | Write an error message to stderr and exit. If we are connected to
|
|
|
|
+-- a TTY, this message will be in red.
|
|
|
|
+cDie :: F.Format (IO r) a -> a
|
|
|
|
+cDie msg = F.runFormat msg $ \ b -> do
|
|
|
|
+ isTTY <- Posix.queryTerminal Posix.stdOutput
|
|
|
|
+ if isTTY
|
|
|
|
+ then do stderr "\x1b[91m"
|
|
|
|
+ stderr (TL.toLazyText b)
|
|
|
|
+ stderr "\x1b[39m\n"
|
|
|
|
+ else TL.hPutStrLn Sys.stderr (TL.toLazyText b)
|
|
Sys.exitFailure
|
|
Sys.exitFailure
|
|
|
|
|
|
|
|
|
|
|
|
+bsErrorLn :: BS.ByteString -> IO ()
|
|
|
|
+bsErrorLn bs = do
|
|
|
|
+ isTTY <- Posix.queryTerminal Posix.stdOutput
|
|
|
|
+ if isTTY
|
|
|
|
+ then do BS.hPutStr Sys.stderr "\x1b[91m"
|
|
|
|
+ BS.hPutStr Sys.stderr bs
|
|
|
|
+ BS.hPutStr Sys.stderr "\x1b[39m\n"
|
|
|
|
+ else BS.hPutStrLn Sys.stderr bs
|
|
|
|
+
|
|
cOpenFile :: TL.Text -> FilePath -> IO TL.Text
|
|
cOpenFile :: TL.Text -> FilePath -> IO TL.Text
|
|
cOpenFile purpose path = do
|
|
cOpenFile purpose path = do
|
|
exists <- Sys.doesFileExist path
|
|
exists <- Sys.doesFileExist path
|
|
if exists
|
|
if exists
|
|
then TL.readFile path
|
|
then TL.readFile path
|
|
- else cError ("Unable to open " F.% F.text F.%
|
|
|
|
- " file at " F.% F.string) purpose path
|
|
|
|
|
|
+ else cDie ("Unable to open " F.% F.text F.%
|
|
|
|
+ " file at " F.% F.string) purpose path
|