Skip to content

Commit

Permalink
Improved formatting. Switch to prettyprinter library. Tracing.
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jul 21, 2021
1 parent fc46e19 commit 08ea636
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "optparse-applicative"]
path = optparse-applicative
url = git@github.com:input-output-hk/optparse-applicative.git
[submodule "criterion"]
path = criterion
url = git@github.com:input-output-hk/criterion.git
2 changes: 2 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ packages:
nix/workbench/cardano-topology
bench/tx-generator
nix/workbench/locli
optparse-applicative
criterion

package cardano-api
ghc-options: -Werror
Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ library
Cardano.CLI.Mary.RenderValue
Cardano.CLI.Mary.ValueParser

Cardano.CLI.Render

Cardano.CLI.TopHandler

other-modules: Paths_cardano_cli
Expand Down Expand Up @@ -133,6 +135,7 @@ library
, ouroboros-network
, parsec
, plutus-ledger-api
, prettyprinter
, shelley-spec-ledger
, small-steps
, split
Expand Down
37 changes: 31 additions & 6 deletions cardano-cli/src/Cardano/CLI/Parsers.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
{-# LANGUAGE OverloadedStrings #-}

module Cardano.CLI.Parsers
( opts
, pref
) where

import Cardano.Prelude
import Prelude (String)

import Options.Applicative
import qualified Options.Applicative as Opt

import Data.Function (id)
import Cardano.CLI.Byron.Parsers (backwardsCompatibilityCommands, parseByronCommands)
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
import Prettyprinter.Render.Util.SimpleDocTree

import qualified Data.Text as T
import qualified Options.Applicative as Opt

command' :: String -> String -> Parser a -> Mod CommandFields a
command' c descr p =
Expand All @@ -29,7 +36,25 @@ opts =
)

pref :: ParserPrefs
pref = Opt.prefs showHelpOnEmpty
pref = Opt.prefs $ mempty
<> showHelpOnEmpty
<> helpHangUsageOverflow 10
<> helpRenderHelp customRenderHelp

-- | Convert a help text to 'String'.
customRenderHelp :: Int -> ParserHelp -> String
customRenderHelp cols
= T.unpack
. ("<html>\n" <>)
. ("<body>\n" <>)
. ("<pre>\n" <>)
. (<> "\n</html>")
. (<> "\n</body>")
. (<> "\n</pre>")
. renderSimplyDecorated id (\(AnnTrace _ name) x -> "<span name=" <> show name <> ">" <> x <> "</span>")
. treeForm
. layoutSmart (LayoutOptions (AvailablePerLine cols 1.0))
. helpText

parseClientCommand :: Parser ClientCommand
parseClientCommand =
Expand Down
58 changes: 58 additions & 0 deletions cardano-cli/src/Cardano/CLI/Render.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{-# LANGUAGE OverloadedStrings #-}

module Cardano.CLI.Render
( renderHtmlShowS
) 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 ds = id
. ("<html>\n" <>)
. ("<body>\n" <>)
. ("<pre>\n" <>)
. (<> "\n</pre>")
. (<> "\n</body>")
. (<> "\n</html>")
. go [] ds
where
go :: [Ann] -> SimpleDocStream Ann -> ShowS
go anns 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
2 changes: 1 addition & 1 deletion cardano-node/src/Cardano/Node/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,4 @@ parserHelpOptions = fromMaybe mempty . OptI.unChunk . OptI.fullDesc (Opt.prefs m
-- | Render the help pretty document.
renderHelpDoc :: Int -> OptI.Doc -> String
renderHelpDoc cols =
(`OptI.displayS` "") . OptI.renderPretty 1.0 cols
(`OptI.renderShowS` "") . OptI.layoutPretty (OptI.LayoutOptions (OptI.AvailablePerLine cols 1.0))
1 change: 1 addition & 0 deletions criterion
Submodule criterion added at fb2e7b
1 change: 1 addition & 0 deletions moo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zsh: command not found: transaction
1 change: 1 addition & 0 deletions optparse-applicative
Submodule optparse-applicative added at ca02c8

0 comments on commit 08ea636

Please sign in to comment.