Skip to content

Commit

Permalink
Merge pull request #26 from Wizek/master
Browse files Browse the repository at this point in the history
Indent pretty-printed strings and custom show instances
  • Loading branch information
cdepillabout committed Mar 8, 2018
2 parents e009c93 + ac38afb commit e2f1a36
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/Text/Pretty/Simple.hs
Expand Up @@ -204,6 +204,35 @@ pStringNoColor = pStringOpt defaultOutputOptionsNoColor
-- ( "hello"
-- , "bye"
-- )
--
-- Lines in strings get indented
--
-- >>> pPrintOpt defaultOutputOptionsNoColor (1, (2, "foo\nbar\nbaz", 3))
-- ( 1
-- ,
-- ( 2
-- , "foo
-- bar
-- baz"
-- , 3
-- )
-- )
--
-- Lines get indented even in custom show instances
--
-- >>> data Foo = Foo
-- >>> instance Show Foo where show _ = "foo\nbar\nbaz"
-- >>> pPrintOpt defaultOutputOptionsNoColor (1, (2, Foo, 3))
-- ( 1
-- ,
-- ( 2
-- , foo
-- bar
-- baz
-- , 3
-- )
-- )

pPrintOpt :: (MonadIO m, Show a) => OutputOptions -> a -> m ()
pPrintOpt outputOptions = liftIO . LText.putStrLn . pShowOpt outputOptions

Expand Down
27 changes: 24 additions & 3 deletions src/Text/Pretty/Simple/Internal/OutputPrinter.hs
Expand Up @@ -31,6 +31,7 @@ import Data.Foldable (fold, foldlM)
import Data.Text.Lazy (Text)
import Data.Text.Lazy.Builder (Builder, fromString, toLazyText)
import Data.Typeable (Typeable)
import Data.List (intercalate)
import GHC.Generics (Generic)

import Text.Pretty.Simple.Internal.Color
Expand Down Expand Up @@ -108,23 +109,43 @@ renderOutput (Output _ OutputNewLine) = pure "\n"
renderOutput (Output nest OutputOpenBrace) = renderRaibowParenFor nest "{"
renderOutput (Output nest OutputOpenBracket) = renderRaibowParenFor nest "["
renderOutput (Output nest OutputOpenParen) = renderRaibowParenFor nest "("
renderOutput (Output _ (OutputOther string)) =
renderOutput (Output _ (OutputOther string)) = do
indentSpaces <- reader outputOptionsIndentAmount
let spaces = replicate (indentSpaces + 2) ' '
-- TODO: This probably shouldn't be a string to begin with.
pure $ fromString string
pure $ fromString $ indentSubsequentLinesWith spaces string
renderOutput (Output _ (OutputStringLit string)) = do
indentSpaces <- reader outputOptionsIndentAmount
let spaces = replicate (indentSpaces + 2) ' '

sequenceFold
[ useColorQuote
, pure "\""
, useColorReset
, useColorString
-- TODO: This probably shouldn't be a string to begin with.
, pure $ fromString string
, pure $ fromString $ indentSubsequentLinesWith spaces string
, useColorReset
, useColorQuote
, pure "\""
, useColorReset
]

-- |
-- >>> indentSubsequentLinesWith " " "aaa"
-- "aaa"
--
-- >>> indentSubsequentLinesWith " " "aaa\nbbb\nccc"
-- "aaa\n bbb\n ccc"
--
-- >>> indentSubsequentLinesWith " " ""
-- ""
indentSubsequentLinesWith :: String -> String -> String
indentSubsequentLinesWith indent input =
intercalate "\n" $ (start ++) $ map (indent ++) $ end
where (start, end) = splitAt 1 $ lines input


-- | Produce a 'Builder' corresponding to the ANSI escape sequence for the
-- color for the @\"@, based on whether or not 'outputOptionsColorOptions' is
-- 'Just' or 'Nothing', and the value of 'colorQuote'.
Expand Down

0 comments on commit e2f1a36

Please sign in to comment.