--
module Hasura.Server.Init.Arg.PrettyPrinter
  ( (PP.<$>),
    PP.Doc,
    PP.text,
    mkEnvVarDoc,
    mkExamplesDoc,
  )
where

--------------------------------------------------------------------------------

import Hasura.Prelude
import Text.PrettyPrint.ANSI.Leijen qualified as PP

--------------------------------------------------------------------------------

mkEnvVarDoc :: [(String, String)] -> PP.Doc
mkEnvVarDoc :: [(String, String)] -> Doc
mkEnvVarDoc [(String, String)]
envVars =
  String -> Doc
PP.text String
"Environment variables: "
    Doc -> Doc -> Doc
PP.<$> Int -> Doc -> Doc
PP.indent Int
2 ([Doc] -> Doc
PP.vsep ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ ((String, String) -> Doc) -> [(String, String)] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (String, String) -> Doc
mkEnvVarLine [(String, String)]
envVars)
  where
    mkEnvVarLine :: (String, String) -> Doc
mkEnvVarLine (String
var, String
desc) =
      (Int -> Doc -> Doc
PP.fillBreak Int
40 (String -> Doc
PP.text String
var) Doc -> Doc -> Doc
PP.<+> String -> Doc
prettifyDesc String
desc) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
PP.hardline
    prettifyDesc :: String -> Doc
prettifyDesc = Doc -> Doc
PP.align (Doc -> Doc) -> (String -> Doc) -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc] -> Doc
PP.fillSep ([Doc] -> Doc) -> (String -> [Doc]) -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
PP.text ([String] -> [Doc]) -> (String -> [String]) -> String -> [Doc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words

mkExamplesDoc :: [[String]] -> PP.Doc
mkExamplesDoc :: [[String]] -> Doc
mkExamplesDoc [[String]]
exampleLines =
  String -> Doc
PP.text String
"Examples: " Doc -> Doc -> Doc
PP.<$> Int -> Doc -> Doc
PP.indent Int
2 ([Doc] -> Doc
PP.vsep [Doc]
examples)
  where
    examples :: [Doc]
examples = (String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map String -> Doc
PP.text ([String] -> [Doc]) -> [String] -> [Doc]
forall a b. (a -> b) -> a -> b
$ [String] -> [[String]] -> [String]
forall a. [a] -> [[a]] -> [a]
intercalate [String
""] [[String]]
exampleLines