A peer-to-peer stack for code collaboration
Only print colors when ANSI is supported
Thomas Scholtesc134d9153531
diff --git a/.weeder.yaml b/.weeder.yamlindex 34ee2b6..299b19a 100644--- a/.weeder.yaml+++ b/.weeder.yaml@@ -39,7 +39,6 @@- module:- name: Radicle.Tests- identifier:- - ansi- assertReplInteraction- atom- intdiff --git a/exe/RadicleExe.hs b/exe/RadicleExe.hsindex 628b250..d63f221 100644--- a/exe/RadicleExe.hs+++ b/exe/RadicleExe.hs@@ -1,12 +1,14 @@module RadicleExe (main) where-import Options.Applicativeimport Prelude (String)import Protolude hiding (TypeError, option, sourceFile)-import Radicle++import Options.Applicativeimport System.Directory (doesFileExist)+import Radicleimport Radicle.Internal.HttpStorage+import Radicle.Internal.Pretty (putPrettyAnsi)main :: IO ()main = do@@ -19,9 +21,9 @@ main = do(result, _state) <- runLang bindings progcase result ofLeft (LangError _ Exit) -> pure ()- Left e -> do putStrLn $ renderAnsi e+ Left e -> do putPrettyAnsi eexitWith (ExitFailure 1)- Right v -> putStrLn $ renderAnsi v+ Right v -> putPrettyAnsi velse dosrc <- readSource (sourceFile opts')hist <- case histFile opts' ofdiff --git a/package.yaml b/package.yamlindex 5cbfa51..d54641e 100644--- a/package.yaml+++ b/package.yaml@@ -22,6 +22,7 @@ library:source-dirs: srcdependencies:- aeson+ - ansi-terminal- bytestring- containers- cryptonitediff --git a/src/Radicle.hs b/src/Radicle.hsindex 66f8c4f..ac1cdb0 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -71,7 +71,6 @@ module Radicle, renderPretty, renderPrettyDef, renderCompactPretty- , renderAnsi-- ** Re-exports, PageWidth(..), Prettydiff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hsindex ef5ded8..93883e6 100644--- a/src/Radicle/Internal/Effects.hs+++ b/src/Radicle/Internal/Effects.hs@@ -6,8 +6,6 @@ import Protolude hiding (TypeError, toList)import qualified Data.Map as Mapimport qualified Data.Text as T-import Data.Text.Prettyprint.Doc (reAnnotate)-import Data.Text.Prettyprint.Doc.Render.Terminal (putDoc)import qualified Data.Time as Timeimport GHC.Exts (IsList(..))import System.Console.Haskeline@@ -57,7 +55,7 @@ repl histFile preFileName preCode bindings = do$ void $ interpretMany preFileName preCodecase r ofLeft (LangError _ Exit) -> pure ()- Left e -> putDoc $ reAnnotate toAnsi $ prettyV e+ Left e -> putPrettyAnsi eRight () -> pure ()completion :: Monad m => CompletionFunc m@@ -82,7 +80,7 @@ replPrimFns = fromList $ allDocs $, "Pretty-prints a value.", \case[x] -> do- putStrS (renderAnsi x)+ putPrettyAnsi xpure nilxs -> throwErrorHere $ WrongNumberOfArgs "print!" 1 (length xs))@@ -144,10 +142,10 @@ replPrimFns = fromList $ allDocs $(\err -> case err ofLangError _ Exit -> pure (pure nil)LangError _ (Impossible _) -> do- putStrS (renderAnsi err)+ putPrettyAnsi errthrowError err_ -> do- putStrS (renderAnsi err)+ putPrettyAnsi errput stpure (loop action))go = dodiff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hsindex c4f6ef8..cc6150e 100644--- a/src/Radicle/Internal/Effects/Capabilities.hs+++ b/src/Radicle/Internal/Effects/Capabilities.hs@@ -9,6 +9,7 @@ import Protoludeimport Data.Text.Prettyprint.Doc (PageWidth)import Data.Time+import System.Console.ANSI (hSupportsANSI)import System.Console.Haskeline hiding (catch)import System.IO (isEOF)#ifdef ghcjs_HOST_OS@@ -33,12 +34,18 @@ instance (MonadException m) => Stdin (InputT m) whereclass (Monad m) => Stdout m whereputStrS :: Text -> m ()+ -- | Return true if we can output ANSI control characters with+ -- 'putStrS'.+ supportsANSI :: m Boolinstance {-# OVERLAPPABLE #-} Stdout m => Stdout (Lang m) whereputStrS = lift . putStrS+ supportsANSI = lift supportsANSIinstance Stdout IO whereputStrS = putStrLn+ supportsANSI = hSupportsANSI stdoutinstance (MonadException m, Monad m) => Stdout (InputT m) whereputStrS = outputStrLn . toS+ supportsANSI = pure Trueclass (Monad m) => Exit m whereexitS :: m ()diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 22afa46..ad0144a 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -18,6 +18,7 @@ import Text.Megaparsec.Pos (sourcePosPretty)import qualified Radicle.Internal.Annotation as Annimport Radicle.Internal.Core+import Radicle.Internal.Effects.Capabilities (Stdout(..))import Radicle.Internal.Typeclass PrettyV a where@@ -139,6 +140,17 @@ renderPretty pg = renderStrict . layoutSmart (LayoutOptions pg) . prettyVrenderPrettyDef :: PrettyV v => v -> TextrenderPrettyDef = renderStrict . layoutSmart defaultLayoutOptions . prettyV+-- | Pretty print the value with colors if @m@ supports it.+putPrettyAnsi :: (PrettyV v, Stdout m) => v -> m ()+putPrettyAnsi value = do+ supportsANSI' <- supportsANSI+ let docStream = layoutSmart defaultLayoutOptions $ prettyV value+ if supportsANSI'+ then+ putStrS $ Term.renderStrict $ reAnnotateS toAnsi docStream+ else+ putStrS $ renderStrict docStream+toAnsi :: Type -> Term.AnsiStyletoAnsi = \caseTKeyword -> color Magenta@@ -147,12 +159,3 @@ toAnsi = \caseTBoolean -> color CyanTRef -> color Red_ -> mempty---- | Like 'renderPrettyDef', but includes escape sequences for terminal colours.------ Examples:------ >>> renderAnsi (asValue (List [String "hi", Keyword (Ident "there")]))--- "\ESC[0m(\ESC[0;92m\"hi\"\ESC[0m \ESC[0;95m:there\ESC[0m)\ESC[0m"-renderAnsi :: PrettyV v => v -> Text-renderAnsi = Term.renderStrict . reAnnotateS toAnsi . layoutSmart defaultLayoutOptions . prettyVdiff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex c8934ea..a29f959 100644--- a/test/spec/Radicle/Internal/TestCapabilities.hs+++ b/test/spec/Radicle/Internal/TestCapabilities.hs@@ -141,6 +141,7 @@ instance {-# OVERLAPPING #-} Stdin TestLang whereinstance {-# OVERLAPPING #-} Stdout TestLang whereputStrS t = lift $modify (\ws -> ws { worldStateStdout = t:worldStateStdout ws })+ supportsANSI = pure Falseinstance {-# OVERLAPPING #-} ReadFile TestLang wherereadFileS fn = dodiff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 36f8a7e..fe9104f 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -599,11 +599,6 @@ test_pretty =pp = renderPrettyDef vv_ = parseTest ppin v_ == Right v- , testCase "renderAnsi renders colours" $ do- renderAnsi (kw "hello") @?= "\ESC[0;95m:hello\ESC[0m"- renderAnsi (asValue (String "hello")) @?= "\ESC[0;92m\"hello\"\ESC[0m"- renderAnsi (int 42) @?= "\ESC[0;93m42\ESC[0m"- renderAnsi (asValue (Boolean True)) @?= "\ESC[0;96m#t\ESC[0m"]whereapl cols = AvailablePerLine cols 1@@ -842,9 +837,4 @@ assertReplInteraction input expected = doRight _ -> pure ()-- In addition to the output of the lines tested, tests get-- printed, so we take only the last few output lines.- reverse (take (length expected) output) @=? map ansi expected--ansi :: Text -> Text-ansi t = case parseTest t of- Left _ -> panic "Error adding colour codes to value string: original string does not parse."- Right v -> renderAnsi v+ reverse (take (length expected) output) @=? expected