{- HLINT ignore "Redundant id" -}

module Cardano.CLI.Render
  ( customRenderHelp
  ) where

import           Data.Text (Text)
import           Options.Applicative
import           Options.Applicative.Help.Ann
import           Options.Applicative.Help.Types (helpText, renderHelp)
import           Prettyprinter
import           Prettyprinter.Render.Util.SimpleDocTree

import qualified Data.Text as T
import qualified System.Environment as IO
import qualified System.IO.Unsafe as IO

import           Cardano.Api (textShow)

cliHelpTraceEnabled :: Bool
cliHelpTraceEnabled :: Bool
cliHelpTraceEnabled = forall a. IO a -> a
IO.unsafePerformIO forall a b. (a -> b) -> a -> b
$ do
  Maybe String
mValue <- String -> IO (Maybe String)
IO.lookupEnv String
"CLI_HELP_TRACE"
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Maybe String
mValue forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just String
"1"
{-# NOINLINE cliHelpTraceEnabled #-}

-- | Convert a help text to 'String'.  When the CLI_HELP_TRACE environment variable is set
-- to '1', the output will be in HTML so that it can be viewed in a browser where developer
-- tools can be used to inspect tracing that aids in describing the structure of the output
-- document.
customRenderHelp :: Int -> ParserHelp -> String
customRenderHelp :: Int -> ParserHelp -> String
customRenderHelp = if Bool
cliHelpTraceEnabled
  then Int -> ParserHelp -> String
customRenderHelpAsHtml
  else Int -> ParserHelp -> String
customRenderHelpAsAnsi

customRenderHelpAsHtml :: Int -> ParserHelp -> String
customRenderHelpAsHtml :: Int -> ParserHelp -> String
customRenderHelpAsHtml Int
cols
  = Text -> String
T.unpack
  forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
wrapper
  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall out ann.
Monoid out =>
(Text -> out) -> (ann -> out -> out) -> SimpleDocTree ann -> out
renderSimplyDecorated forall a. a -> a
id Ann -> Text -> Text
renderElement
  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ann. SimpleDocStream ann -> SimpleDocTree ann
treeForm
  forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutSmart (PageWidth -> LayoutOptions
LayoutOptions (Int -> Double -> PageWidth
AvailablePerLine Int
cols Double
1.0))
  forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParserHelp -> Doc
helpText
  where
    renderElement :: Ann -> Text -> Text
    renderElement :: Ann -> Text -> Text
renderElement Ann
ann Text
x = if Bool
cliHelpTraceEnabled
      then case Ann
ann of
        AnnTrace Int
_ String
name -> Text
"<span name=" forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> Text
textShow String
name forall a. Semigroup a => a -> a -> a
<> Text
">" forall a. Semigroup a => a -> a -> a
<> Text
x forall a. Semigroup a => a -> a -> a
<> Text
"</span>"
        AnnStyle SetStyle
_ -> Text
x
      else Text
x
    wrapper :: Text -> Text
wrapper = if Bool
cliHelpTraceEnabled
      then forall a. a -> a
id
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"<html>\n" forall a. Semigroup a => a -> a -> a
<>)
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"<body>\n" forall a. Semigroup a => a -> a -> a
<>)
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
"<pre>\n" forall a. Semigroup a => a -> a -> a
<>)
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Semigroup a => a -> a -> a
<> Text
"\n</html>")
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Semigroup a => a -> a -> a
<> Text
"\n</body>")
        forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Semigroup a => a -> a -> a
<> Text
"\n</pre>")
      else forall a. a -> a
id

customRenderHelpAsAnsi :: Int -> ParserHelp -> String
customRenderHelpAsAnsi :: Int -> ParserHelp -> String
customRenderHelpAsAnsi = Int -> ParserHelp -> String
renderHelp