Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'prettyprinter' library #67

Merged
merged 31 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
20d8a2d
Use 'prettyprinter' library for printing
georgefst Jun 9, 2020
4625f30
Fix multi-line 'other's
georgefst Jun 9, 2020
da4ff87
Simplify and document annotation processing
georgefst Jun 25, 2020
4b981a5
Remove unused TemplateHaskell pragma
georgefst Jun 25, 2020
48a2359
More compactness - fixes 3 of the tests
georgefst Jun 25, 2020
19b1c18
Simplify further
georgefst Jun 26, 2020
07f82ca
Clean up and document
georgefst Jun 26, 2020
11f99b2
Import Data.Monoid on older GHCs
georgefst Jun 26, 2020
3b44727
Fix CPP syntax
georgefst Jun 26, 2020
3d2460e
Haddock-ify a comment
georgefst Jun 27, 2020
c1b084d
Fix remaining tests
georgefst Jun 27, 2020
677eb44
Use colour in executable and example
georgefst Jun 29, 2020
4840dad
Use 'dropWhileEnd' instead of reversing list twice
georgefst Jun 29, 2020
f6ae384
Remove ViewPatterns
georgefst Jun 29, 2020
3472c6d
Remove redundant $
georgefst Jun 29, 2020
e06aeeb
Use smarter layout algorithm
georgefst Jun 29, 2020
0d4f41e
Include escape codes in output of 'pString' etc.
georgefst Jun 29, 2020
aa13ebc
Add missing case in 'isSimple'
georgefst Jun 30, 2020
95a86f6
Make 'pString' lazy again
georgefst Jun 30, 2020
c1cc680
[skip ci] Fix typo
georgefst Jun 30, 2020
f5700ca
Bump prettyprinter-ansi-terminal version
georgefst Jul 31, 2020
0de7092
Remove ansi-terminal dependency
georgefst Jul 31, 2020
846b488
Use the new prettyprinter module names
georgefst Jul 31, 2020
88f6a16
[skip ci] Alphabetise extensions
georgefst Jul 31, 2020
2cc2093
Add stack extra-deps
georgefst Jul 31, 2020
21f9804
Add note about stack extra-deps.
cdepillabout Aug 11, 2020
56ad278
Update stack.yaml.lock.
cdepillabout Aug 11, 2020
5d78be9
Add CI workflows.
cdepillabout Aug 11, 2020
aea27b3
Make sure to compile with the correct flag.
cdepillabout Aug 11, 2020
c91b35e
Update the build status shield on the README.
cdepillabout Aug 11, 2020
9566022
Remove .travis.yml.
cdepillabout Aug 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pretty-simple.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ library
, Text.Pretty.Simple.Internal.Color
, Text.Pretty.Simple.Internal.Expr
, Text.Pretty.Simple.Internal.ExprParser
, Text.Pretty.Simple.Internal.ExprToOutput
, Text.Pretty.Simple.Internal.Output
, Text.Pretty.Simple.Internal.OutputPrinter
build-depends: base >= 4.8 && < 5
, ansi-terminal >= 0.6
, containers
, mtl >= 2.2
, prettyprinter >= 1.6.1
, prettyprinter-ansi-terminal >= 1.1.1.2
, text >= 1.2
, transformers >= 0.4
default-language: Haskell2010
Expand Down
13 changes: 6 additions & 7 deletions src/Text/Pretty/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,15 @@ import Control.Applicative
#endif

import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Foldable (toList)
import Data.Text.Lazy (Text)
import Data.Text.Lazy.IO as LText
import System.IO (Handle, stdout)
import Data.Text.Prettyprint.Doc.Render.Terminal (renderIO, renderLazy)

import Text.Pretty.Simple.Internal
(CheckColorTty(..), OutputOptions(..), StringOutputStyle(..),
defaultColorOptionsDarkBg, defaultColorOptionsLightBg,
defaultOutputOptionsDarkBg, defaultOutputOptionsLightBg,
defaultOutputOptionsNoColor, hCheckTTY, expressionParse,
expressionsToOutputs, render)
defaultOutputOptionsNoColor, hCheckTTY, layoutString)

-- $setup
-- >>> import Data.Text.Lazy (unpack)
Expand Down Expand Up @@ -519,7 +517,9 @@ pHPrintStringOpt checkColorTty outputOptions handle str = do
case checkColorTty of
CheckColorTty -> hCheckTTY handle outputOptions
NoCheckColorTty -> pure outputOptions
liftIO $ LText.hPutStrLn handle $ pStringOpt realOutputOpts str
liftIO $ do
renderIO handle $ layoutString realOutputOpts str
putStrLn ""

-- | Like 'pShow' but takes 'OutputOptions' to change how the
-- pretty-printing is done.
Expand All @@ -529,8 +529,7 @@ pShowOpt outputOptions = pStringOpt outputOptions . show
-- | Like 'pString' but takes 'OutputOptions' to change how the
-- pretty-printing is done.
pStringOpt :: OutputOptions -> String -> Text
pStringOpt outputOptions =
render outputOptions . toList . expressionsToOutputs . expressionParse
pStringOpt outputOptions = renderLazy . layoutString outputOptions

-- $colorOptions
--
Expand Down
2 changes: 0 additions & 2 deletions src/Text/Pretty/Simple/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ module Text.Pretty.Simple.Internal
import Text.Pretty.Simple.Internal.Color as X
import Text.Pretty.Simple.Internal.ExprParser as X
import Text.Pretty.Simple.Internal.Expr as X
import Text.Pretty.Simple.Internal.ExprToOutput as X
import Text.Pretty.Simple.Internal.Output as X
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two modules have gone, as we now render straight from Expr.

The remaining OutputPrinter should perhaps be renamed ExprPrinter, since the old Output type that was being printed there no longer even exists.

import Text.Pretty.Simple.Internal.OutputPrinter as X
161 changes: 70 additions & 91 deletions src/Text/Pretty/Simple/Internal/Color.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ module Text.Pretty.Simple.Internal.Color
import Control.Applicative
#endif

import Data.Text.Lazy.Builder (Builder, fromString)
import Data.Text.Prettyprint.Doc.Render.Terminal
(AnsiStyle, Color(..), bold, colorDull, color)
import Data.Typeable (Typeable)
import GHC.Generics (Generic)
import System.Console.ANSI
(Color(..), ColorIntensity(..), ConsoleIntensity(..),
ConsoleLayer(..), SGR(..), setSGRCode)

-- | These options are for colorizing the output of functions like 'pPrint'.
--
Expand All @@ -39,15 +37,15 @@ import System.Console.ANSI
--
-- If you don't want to use a color for one of the options, use 'colorNull'.
data ColorOptions = ColorOptions
{ colorQuote :: Builder
{ colorQuote :: AnsiStyle
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colours are now expressed with AnsiStyle. This should actually be easier for users to work with, but is obviously a breaking change.

-- ^ Color to use for quote characters (@\"@) around strings.
, colorString :: Builder
, colorString :: AnsiStyle
-- ^ Color to use for strings.
, colorError :: Builder
-- ^ (currently not used)
, colorNum :: Builder
, colorError :: AnsiStyle
-- ^ Color for errors, e.g. unmatched brackets.
, colorNum :: AnsiStyle
-- ^ Color to use for numbers.
, colorRainbowParens :: [Builder]
, colorRainbowParens :: [AnsiStyle]
-- ^ A list of 'Builder' colors to use for rainbow parenthesis output. Use
-- '[]' if you don't want rainbow parenthesis. Use just a single item if you
-- want all the rainbow parenthesis to be colored the same.
Expand Down Expand Up @@ -75,26 +73,26 @@ defaultColorOptionsDarkBg =

-- | Default color for 'colorQuote' for dark backgrounds. This is
-- 'colorVividWhiteBold'.
defaultColorQuoteDarkBg :: Builder
defaultColorQuoteDarkBg :: AnsiStyle
defaultColorQuoteDarkBg = colorVividWhiteBold

-- | Default color for 'colorString' for dark backgrounds. This is
-- 'colorVividBlueBold'.
defaultColorStringDarkBg :: Builder
defaultColorStringDarkBg :: AnsiStyle
defaultColorStringDarkBg = colorVividBlueBold

-- | Default color for 'colorError' for dark backgrounds. This is
-- 'colorVividRedBold'.
defaultColorErrorDarkBg :: Builder
defaultColorErrorDarkBg :: AnsiStyle
defaultColorErrorDarkBg = colorVividRedBold

-- | Default color for 'colorNum' for dark backgrounds. This is
-- 'colorVividGreenBold'.
defaultColorNumDarkBg :: Builder
defaultColorNumDarkBg :: AnsiStyle
defaultColorNumDarkBg = colorVividGreenBold

-- | Default colors for 'colorRainbowParens' for dark backgrounds.
defaultColorRainbowParensDarkBg :: [Builder]
defaultColorRainbowParensDarkBg :: [AnsiStyle]
defaultColorRainbowParensDarkBg =
[ colorVividMagentaBold
, colorVividCyanBold
Expand Down Expand Up @@ -132,26 +130,26 @@ defaultColorOptionsLightBg =

-- | Default color for 'colorQuote' for light backgrounds. This is
-- 'colorVividWhiteBold'.
defaultColorQuoteLightBg :: Builder
defaultColorQuoteLightBg :: AnsiStyle
defaultColorQuoteLightBg = colorVividBlackBold

-- | Default color for 'colorString' for light backgrounds. This is
-- 'colorVividBlueBold'.
defaultColorStringLightBg :: Builder
defaultColorStringLightBg :: AnsiStyle
defaultColorStringLightBg = colorVividBlueBold

-- | Default color for 'colorError' for light backgrounds. This is
-- 'colorVividRedBold'.
defaultColorErrorLightBg :: Builder
defaultColorErrorLightBg :: AnsiStyle
defaultColorErrorLightBg = colorVividRedBold

-- | Default color for 'colorNum' for light backgrounds. This is
-- 'colorVividGreenBold'.
defaultColorNumLightBg :: Builder
defaultColorNumLightBg :: AnsiStyle
defaultColorNumLightBg = colorVividGreenBold

-- | Default colors for 'colorRainbowParens' for light backgrounds.
defaultColorRainbowParensLightBg :: [Builder]
defaultColorRainbowParensLightBg :: [AnsiStyle]
defaultColorRainbowParensLightBg =
[ colorVividMagentaBold
, colorVividCyanBold
Expand All @@ -167,141 +165,122 @@ defaultColorRainbowParensLightBg =
-- Vivid Bold Colors --
-----------------------

colorVividBlackBold :: Builder
colorVividBlackBold :: AnsiStyle
colorVividBlackBold = colorBold `mappend` colorVividBlack

colorVividBlueBold :: Builder
colorVividBlueBold :: AnsiStyle
colorVividBlueBold = colorBold `mappend` colorVividBlue

colorVividCyanBold :: Builder
colorVividCyanBold :: AnsiStyle
colorVividCyanBold = colorBold `mappend` colorVividCyan

colorVividGreenBold :: Builder
colorVividGreenBold :: AnsiStyle
colorVividGreenBold = colorBold `mappend` colorVividGreen

colorVividMagentaBold :: Builder
colorVividMagentaBold :: AnsiStyle
colorVividMagentaBold = colorBold `mappend` colorVividMagenta

colorVividRedBold :: Builder
colorVividRedBold :: AnsiStyle
colorVividRedBold = colorBold `mappend` colorVividRed

colorVividWhiteBold :: Builder
colorVividWhiteBold :: AnsiStyle
colorVividWhiteBold = colorBold `mappend` colorVividWhite

colorVividYellowBold :: Builder
colorVividYellowBold :: AnsiStyle
colorVividYellowBold = colorBold `mappend` colorVividYellow

-----------------------
-- Dull Bold Colors --
-----------------------

colorDullBlackBold :: Builder
colorDullBlackBold :: AnsiStyle
colorDullBlackBold = colorBold `mappend` colorDullBlack

colorDullBlueBold :: Builder
colorDullBlueBold :: AnsiStyle
colorDullBlueBold = colorBold `mappend` colorDullBlue

colorDullCyanBold :: Builder
colorDullCyanBold :: AnsiStyle
colorDullCyanBold = colorBold `mappend` colorDullCyan

colorDullGreenBold :: Builder
colorDullGreenBold :: AnsiStyle
colorDullGreenBold = colorBold `mappend` colorDullGreen

colorDullMagentaBold :: Builder
colorDullMagentaBold :: AnsiStyle
colorDullMagentaBold = colorBold `mappend` colorDullMagenta

colorDullRedBold :: Builder
colorDullRedBold :: AnsiStyle
colorDullRedBold = colorBold `mappend` colorDullRed

colorDullWhiteBold :: Builder
colorDullWhiteBold :: AnsiStyle
colorDullWhiteBold = colorBold `mappend` colorDullWhite

colorDullYellowBold :: Builder
colorDullYellowBold :: AnsiStyle
colorDullYellowBold = colorBold `mappend` colorDullYellow

------------------
-- Vivid Colors --
------------------

colorVividBlack :: Builder
colorVividBlack = colorHelper Vivid Black
colorVividBlack :: AnsiStyle
colorVividBlack = color Black

colorVividBlue :: Builder
colorVividBlue = colorHelper Vivid Blue
colorVividBlue :: AnsiStyle
colorVividBlue = color Blue

colorVividCyan :: Builder
colorVividCyan = colorHelper Vivid Cyan
colorVividCyan :: AnsiStyle
colorVividCyan = color Cyan

colorVividGreen :: Builder
colorVividGreen = colorHelper Vivid Green
colorVividGreen :: AnsiStyle
colorVividGreen = color Green

colorVividMagenta :: Builder
colorVividMagenta = colorHelper Vivid Magenta
colorVividMagenta :: AnsiStyle
colorVividMagenta = color Magenta

colorVividRed :: Builder
colorVividRed = colorHelper Vivid Red
colorVividRed :: AnsiStyle
colorVividRed = color Red

colorVividWhite :: Builder
colorVividWhite = colorHelper Vivid White
colorVividWhite :: AnsiStyle
colorVividWhite = color White

colorVividYellow :: Builder
colorVividYellow = colorHelper Vivid Yellow
colorVividYellow :: AnsiStyle
colorVividYellow = color Yellow

------------------
-- Dull Colors --
------------------

colorDullBlack :: Builder
colorDullBlack = colorHelper Dull Black
colorDullBlack :: AnsiStyle
colorDullBlack = colorDull Black

colorDullBlue :: Builder
colorDullBlue = colorHelper Dull Blue
colorDullBlue :: AnsiStyle
colorDullBlue = colorDull Blue

colorDullCyan :: Builder
colorDullCyan = colorHelper Dull Cyan
colorDullCyan :: AnsiStyle
colorDullCyan = colorDull Cyan

colorDullGreen :: Builder
colorDullGreen = colorHelper Dull Green
colorDullGreen :: AnsiStyle
colorDullGreen = colorDull Green

colorDullMagenta :: Builder
colorDullMagenta = colorHelper Dull Magenta
colorDullMagenta :: AnsiStyle
colorDullMagenta = colorDull Magenta

colorDullRed :: Builder
colorDullRed = colorHelper Dull Red
colorDullRed :: AnsiStyle
colorDullRed = colorDull Red

colorDullWhite :: Builder
colorDullWhite = colorHelper Dull White
colorDullWhite :: AnsiStyle
colorDullWhite = colorDull White

colorDullYellow :: Builder
colorDullYellow = colorHelper Dull Yellow
colorDullYellow :: AnsiStyle
colorDullYellow = colorDull Yellow

--------------------
-- Special Colors --
--------------------

-- | Change the intensity to 'BoldIntensity'.
colorBold :: Builder
colorBold = setSGRCodeBuilder [SetConsoleIntensity BoldIntensity]

-- | 'Reset' the console color back to normal.
colorReset :: Builder
colorReset = setSGRCodeBuilder [Reset]
colorBold :: AnsiStyle
colorBold = bold

-- | Empty string.
colorNull :: Builder
colorNull = ""

-------------
-- Helpers --
-------------

-- | Helper for creating a 'Builder' for an ANSI escape sequence color based on
-- a 'ColorIntensity' and a 'Color'.
colorHelper :: ColorIntensity -> Color -> Builder
colorHelper colorIntensity color =
setSGRCodeBuilder [SetColor Foreground colorIntensity color]

-- | Convert a list of 'SGR' to a 'Builder'.
setSGRCodeBuilder :: [SGR] -> Builder
setSGRCodeBuilder = fromString . setSGRCode

colorNull :: AnsiStyle
colorNull = mempty
Loading