Skip to content

Instantly share code, notes, and snippets.

@nrnrnr
Created August 9, 2022 21:01
Show Gist options
  • Save nrnrnr/1440511a4f3785ded41a27d2ca90b004 to your computer and use it in GitHub Desktop.
Save nrnrnr/1440511a4f3785ded41a27d2ca90b004 to your computer and use it in GitHub Desktop.
Simple substitute for Outputable
{-# LANGUAGE OverloadedStrings #-}
module GHC.Utils.Indentable
( IndentedBuilder
, Indentation
, newline
, indentBy
, indent
, (<+>)
)
where
import GHC.Prelude
import Data.ByteString.Builder
import Data.Monoid
import Data.Semigroup
import Data.String
type Indentation = Builder
newtype IndentedBuilder = IB (Indentation -> Builder)
newline :: IndentedBuilder
newline = IB $ \indent -> "\n" <> indent
instance Semigroup IndentedBuilder where
IB f <> IB f' = IB $ \indent -> f indent <> f' indent
instance IsString IndentedBuilder where
fromString s = IB $ const (fromString s)
instance Monoid IndentedBuilder where
mempty = IB $ const mempty
indentBy :: Indentation -> IndentedBuilder -> IndentedBuilder
indentBy moreIndent (IB f) = IB (\indent -> f (indent <> moreIndent))
indent :: Indentation -> IndentedBuilder -> Builder
indent indentation (IB f) = f indentation
(<+>) :: IndentedBuilder -> IndentedBuilder -> IndentedBuilder
s <+> s' = s <> " " <> s'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment