pretty-simple 3.2.1.0 → 3.2.2.0
raw patch · 4 files changed
+38/−15 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Text.Pretty.Simple.Internal.OutputPrinter: renderRaibowParenFor :: MonadReader OutputOptions m => NestLevel -> Builder -> m Builder
+ Text.Pretty.Simple.Internal.OutputPrinter: removeTrailingSpacesInOtherBeforeNewLine :: [Output] -> [Output]
+ Text.Pretty.Simple.Internal.OutputPrinter: renderRainbowParenFor :: MonadReader OutputOptions m => NestLevel -> Builder -> m Builder
Files
- CHANGELOG.md +6/−0
- pretty-simple.cabal +1/−1
- src/Text/Pretty/Simple.hs +2/−2
- src/Text/Pretty/Simple/Internal/OutputPrinter.hs +29/−12
CHANGELOG.md view
@@ -1,4 +1,10 @@ +## 3.2.2.0++* Remove whitespace from the ends of lines.+ [#62](https://github.com/cdepillabout/pretty-simple/pull/62)+ Thanks Gaith Hallak ([@ghallak](https://github.com/ghallak))!+ ## 3.2.1.0 * Added `pTraceOpt` functions to `Debug.Pretty.Simple`.
pretty-simple.cabal view
@@ -1,5 +1,5 @@ name: pretty-simple-version: 3.2.1.0+version: 3.2.2.0 synopsis: pretty printer for data types with a 'Show' instance. description: Please see <https://github.com/cdepillabout/pretty-simple#readme README.md>. homepage: https://github.com/cdepillabout/pretty-simple
src/Text/Pretty/Simple.hs view
@@ -382,14 +382,14 @@ -- | Like 'pShow', but without color. -- -- >>> pShowNoColor [ Nothing, Just (1, "hello") ]--- "[ Nothing\n, Just \n ( 1\n , \"hello\" \n ) \n] "+-- "[ Nothing\n, Just\n ( 1\n , \"hello\"\n )\n]" pShowNoColor :: Show a => a -> Text pShowNoColor = pShowOpt defaultOutputOptionsNoColor -- | LIke 'pString', but without color. -- -- >>> pStringNoColor $ show [1, 2, 3]--- "[ 1\n, 2\n, 3\n] "+-- "[ 1\n, 2\n, 3\n]" pStringNoColor :: String -> Text pStringNoColor = pStringOpt defaultOutputOptionsNoColor
src/Text/Pretty/Simple/Internal/OutputPrinter.hs view
@@ -28,13 +28,13 @@ import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.Reader (MonadReader(ask, reader), runReader)-import Data.Char (isPrint, ord)+import Data.Char (isPrint, isSpace, ord) import Numeric (showHex) import Data.Foldable (fold) import Data.Text.Lazy (Text) import Data.Text.Lazy.Builder (Builder, fromString, toLazyText) import Data.Typeable (Typeable)-import Data.List (intercalate)+import Data.List (dropWhileEnd, intercalate) import Data.Maybe (fromMaybe) import Text.Read (readMaybe) import GHC.Generics (Generic)@@ -132,17 +132,17 @@ -- | Render a single 'Output' as a 'Builder', using the options specified in -- the 'OutputOptions'. renderOutput :: MonadReader OutputOptions m => Output -> m Builder-renderOutput (Output nest OutputCloseBrace) = renderRaibowParenFor nest "}"-renderOutput (Output nest OutputCloseBracket) = renderRaibowParenFor nest "]"-renderOutput (Output nest OutputCloseParen) = renderRaibowParenFor nest ")"-renderOutput (Output nest OutputComma) = renderRaibowParenFor nest ","+renderOutput (Output nest OutputCloseBrace) = renderRainbowParenFor nest "}"+renderOutput (Output nest OutputCloseBracket) = renderRainbowParenFor nest "]"+renderOutput (Output nest OutputCloseParen) = renderRainbowParenFor nest ")"+renderOutput (Output nest OutputComma) = renderRainbowParenFor nest "," renderOutput (Output _ OutputIndent) = do indentSpaces <- reader outputOptionsIndentAmount pure . mconcat $ replicate indentSpaces " " 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 nest OutputOpenBrace) = renderRainbowParenFor nest "{"+renderOutput (Output nest OutputOpenBracket) = renderRainbowParenFor nest "["+renderOutput (Output nest OutputOpenParen) = renderRainbowParenFor nest "(" renderOutput (Output _ (OutputOther string)) = do indentSpaces <- reader outputOptionsIndentAmount let spaces = replicate (indentSpaces + 2) ' '@@ -266,10 +266,10 @@ -- -- If 'outputOptionsColorOptions' is 'Nothing', then just return the input -- character. If it is 'Just', then return the input character colorized.-renderRaibowParenFor+renderRainbowParenFor :: MonadReader OutputOptions m => NestLevel -> Builder -> m Builder-renderRaibowParenFor nest string =+renderRainbowParenFor nest string = sequenceFold [useColorRainbowParens nest, pure string, useColorReset] useColorRainbowParens@@ -297,7 +297,8 @@ -- An sample of an optimization is 'removeStartingNewLine' which just removes a -- newline if it is the first item in an 'Output' list. modificationsOutputList :: [Output] -> [Output]-modificationsOutputList = shrinkWhitespaceInOthers . compressOthers . removeStartingNewLine+modificationsOutputList =+ removeTrailingSpacesInOtherBeforeNewLine . shrinkWhitespaceInOthers . compressOthers . removeStartingNewLine -- | Remove a 'OutputNewLine' if it is the first item in the 'Output' list. --@@ -306,6 +307,22 @@ removeStartingNewLine :: [Output] -> [Output] removeStartingNewLine ((Output _ OutputNewLine) : t) = t removeStartingNewLine outputs = outputs++-- | Remove trailing spaces from the end of a 'OutputOther' token if it is+-- followed by a 'OutputNewLine', or if it is the final 'Output' in the list.+-- This function assumes that there is a single 'OutputOther' before any+-- 'OutputNewLine' (and before the end of the list), so it must be run after+-- running 'compressOthers'.+--+-- >>> removeTrailingSpacesInOtherBeforeNewLine [Output 2 (OutputOther "foo "), Output 4 OutputNewLine]+-- [Output {outputNestLevel = NestLevel {unNestLevel = 2}, outputOutputType = OutputOther "foo"},Output {outputNestLevel = NestLevel {unNestLevel = 4}, outputOutputType = OutputNewLine}]+removeTrailingSpacesInOtherBeforeNewLine :: [Output] -> [Output]+removeTrailingSpacesInOtherBeforeNewLine [] = []+removeTrailingSpacesInOtherBeforeNewLine (Output nest (OutputOther string):[]) =+ (Output nest (OutputOther $ dropWhileEnd isSpace string)):[]+removeTrailingSpacesInOtherBeforeNewLine (Output nest (OutputOther string):nl@(Output _ OutputNewLine):t) =+ (Output nest (OutputOther $ dropWhileEnd isSpace string)):nl:removeTrailingSpacesInOtherBeforeNewLine t+removeTrailingSpacesInOtherBeforeNewLine (h:t) = h : removeTrailingSpacesInOtherBeforeNewLine t -- | If there are two subsequent 'OutputOther' tokens, combine them into just -- one 'OutputOther'.