Skip to content

Commit

Permalink
Fix CLI without annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jul 21, 2021
1 parent e8555c5 commit 270f3ed
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 30 deletions.
3 changes: 1 addition & 2 deletions cardano-cli/src/Cardano/CLI/Parsers.hs
Expand Up @@ -11,7 +11,6 @@ import Cardano.CLI.Byron.Parsers (backwardsCompatibilityCommands, pars
import Cardano.CLI.Run (ClientCommand (..))
import Cardano.CLI.Shelley.Parsers (parseShelleyCommands)
import Options.Applicative
import Options.Applicative.Help.Ann
import Options.Applicative.Help.Types (helpText)
import Prelude (String)
import Prettyprinter
Expand Down Expand Up @@ -51,7 +50,7 @@ customRenderHelp cols
. (<> "\n</html>")
. (<> "\n</body>")
. (<> "\n</pre>")
. renderSimplyDecorated id (\(AnnTrace _ name) x -> "<span name=" <> show name <> ">" <> x <> "</span>")
. renderSimplyDecorated id (flip const)
. treeForm
. layoutSmart (LayoutOptions (AvailablePerLine cols 1.0))
. helpText
Expand Down
36 changes: 9 additions & 27 deletions cardano-cli/src/Cardano/CLI/Render.hs
Expand Up @@ -5,54 +5,36 @@ module Cardano.CLI.Render
) where

import Data.Function
import Data.Ord
import Options.Applicative.Help.Ann
import Prettyprinter
import Prettyprinter.Render.Util.Panic
import Text.Show

import qualified Data.List as L
import qualified Data.Text as T

renderOpenAnn :: Ann -> ShowS
renderOpenAnn ann = case ann of
AnnTrace _ s -> id
. ("<span trace=" <>)
. (show s <>)
. (">" <>)

renderCloseAnn :: Ann -> ShowS
renderCloseAnn ann = case ann of
AnnTrace _ _ -> id
. (<> "</span>")

-- | Render a 'SimpleDocStream' to a 'ShowS', useful to write 'Show' instances
-- based on the prettyprinter.
--
-- @
-- instance 'Show' MyType where
-- 'showsPrec' _ = 'renderHtmlShowS' . 'layoutPretty' 'defaultLayoutOptions' . 'pretty'
-- @
renderHtmlShowS :: SimpleDocStream Ann -> ShowS
renderHtmlShowS :: SimpleDocStream () -> ShowS
renderHtmlShowS ds = id
. ("<html>\n" <>)
. ("<body>\n" <>)
. ("<pre>\n" <>)
. (<> "\n</pre>")
. (<> "\n</body>")
. (<> "\n</html>")
. go [] ds
. go ds
where
go :: [Ann] -> SimpleDocStream Ann -> ShowS
go anns sds = case sds of
go :: SimpleDocStream () -> ShowS
go sds = case sds of
SFail -> panicUncaughtFail
SEmpty -> id
SChar c x -> showChar c . go anns x
SText _l t x -> showString (T.unpack t) . go anns x
SLine i x -> showString ('\n' : L.replicate i ' ') . go anns x
SAnnPush ann x -> onLevel ann (renderOpenAnn ann) . go (ann:anns) x
SAnnPop x -> case anns of
(a:as) -> go as x . onLevel a (renderCloseAnn a)
[] -> go [] x
onLevel :: Ann -> ShowS -> ShowS
onLevel (AnnTrace n _) f = if n >= 2 then f else id
SChar c x -> showChar c . go x
SText _l t x -> showString (T.unpack t) . go x
SLine i x -> showString ('\n' : L.replicate i ' ') . go x
SAnnPush _ x -> go x
SAnnPop x -> go x

0 comments on commit 270f3ed

Please sign in to comment.