diff --git a/src/Elm/Utils.hs b/src/Elm/Utils.hs index 7bee46656..1fe365762 100644 --- a/src/Elm/Utils.hs +++ b/src/Elm/Utils.hs @@ -59,7 +59,9 @@ run :: (MonadError String m, MonadIO m) => String -> [String] -> m String run command args = do result <- liftIO (unwrappedRun command args) case result of - Right out -> return out + Right out -> + return out + Left err -> throwError (context (message err)) where @@ -70,6 +72,7 @@ run command args = case err of CommandFailed stderr stdout -> stdout ++ stderr + MissingExe msg -> msg @@ -79,11 +82,18 @@ unwrappedRun command args = do (exitCode, stdout, stderr) <- readProcessWithExitCode command args "" return $ case exitCode of - ExitSuccess -> Right stdout - ExitFailure code - | code == 127 -> Left (missingExe command) -- UNIX - | code == 9009 -> Left (missingExe command) -- Windows - | otherwise -> Left (CommandFailed stdout stderr) + ExitSuccess -> + Right stdout + + ExitFailure code -> + if code == 127 then + Left (missingExe command) -- UNIX + + else if code == 9009 then + Left (missingExe command) -- Windows + + else + Left (CommandFailed stdout stderr) missingExe :: String -> CommandError diff --git a/src/Reporting/Report.hs b/src/Reporting/Report.hs index 918c7fbda..f1c7ad4c9 100644 --- a/src/Reporting/Report.hs +++ b/src/Reporting/Report.hs @@ -13,7 +13,8 @@ import Data.Aeson ((.=)) import qualified Data.Aeson.Types as Json import System.IO (Handle) import Text.PrettyPrint.ANSI.Leijen - ( Doc, (<>), displayS, displayIO, dullcyan, fillSep, hardline, renderPretty, text + ( Doc, SimpleDoc(..), (<>), displayS, displayIO, dullcyan, fillSep + , hardline, renderPretty, text ) import qualified Reporting.Region as R @@ -83,15 +84,38 @@ messageBar tag location = -- RENDER DOCS +toHandle :: Handle -> String -> R.Region -> Report -> String -> IO () +toHandle handle location region rprt source = + displayIO + handle + (renderPretty 1 80 (toDoc location region rprt source)) + + toString :: String -> R.Region -> Report -> String -> String toString location region rprt source = displayS - (renderPretty 1 80 (toDoc location region rprt source)) + (stripAnsi (renderPretty 1 80 (toDoc location region rprt source))) "" -toHandle :: Handle -> String -> R.Region -> Report -> String -> IO () -toHandle handle location region rprt source = - displayIO - handle - (renderPretty 1 80 (toDoc location region rprt source)) +stripAnsi :: SimpleDoc -> SimpleDoc +stripAnsi simpleDoc = + case simpleDoc of + SFail -> + SFail + + SEmpty -> + SEmpty + + SChar chr subDoc -> + SChar chr (stripAnsi subDoc) + + SText n str subDoc -> + SText n str (stripAnsi subDoc) + + SLine n subDoc -> + SLine n (stripAnsi subDoc) + + SSGR _ subDoc -> + stripAnsi subDoc +