prettyprinter-ansi-terminal-1.1.1.2/misc/0000755000000000000000000000000013112000052016447 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/0000755000000000000000000000000013112000052016303 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/Data/0000755000000000000000000000000013112000052017154 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/Data/Text/0000755000000000000000000000000013112000052020100 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/Data/Text/Prettyprint/0000755000000000000000000000000013112000052022444 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/Data/Text/Prettyprint/Doc/0000755000000000000000000000000013112000052023151 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/Data/Text/Prettyprint/Doc/Render/0000755000000000000000000000000013243466123024414 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/Data/Text/Prettyprint/Doc/Render/Terminal/0000755000000000000000000000000013243466123026167 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/test/0000755000000000000000000000000013141315073016511 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/test/Doctest/0000755000000000000000000000000013112000052020100 5ustar0000000000000000prettyprinter-ansi-terminal-1.1.1.2/src/Data/Text/Prettyprint/Doc/Render/Terminal.hs0000644000000000000000000000143113243466123026522 0ustar0000000000000000-- | Render 'SimpleDocStream' in a terminal. module Data.Text.Prettyprint.Doc.Render.Terminal ( -- * Styling AnsiStyle, Color(..), -- ** Font color color, colorDull, -- ** Background color bgColor, bgColorDull, -- ** Font style bold, italicized, underlined, -- ** Internal markers -- -- | These should only be used for writing adaptors to other libraries; for -- the average use case, use 'bold', 'bgColorDull', etc. Intensity(..), Bold(..), Underlined(..), Italicized(..), -- * Conversion to ANSI-infused 'Text' renderLazy, renderStrict, -- * Render directly to 'stdout' renderIO, -- ** Convenience functions putDoc, hPutDoc, ) where import Data.Text.Prettyprint.Doc.Render.Terminal.Internal prettyprinter-ansi-terminal-1.1.1.2/src/Data/Text/Prettyprint/Doc/Render/Terminal/Internal.hs0000644000000000000000000002766613243470317030320 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} #include "version-compatibility-macros.h" -- | __Warning:__ Internal module. May change arbitrarily between versions. module Data.Text.Prettyprint.Doc.Render.Terminal.Internal where import Control.Applicative import Control.Monad.ST import Data.IORef import Data.Maybe import Data.Semigroup import Data.STRef import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.IO as T import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Builder as TLB import qualified System.Console.ANSI as ANSI import System.IO (Handle, hPutChar, stdout) import Data.Text.Prettyprint.Doc import Data.Text.Prettyprint.Doc.Render.Util.Panic -- $setup -- -- (Definitions for the doctests) -- -- >>> :set -XOverloadedStrings -- >>> import qualified Data.Text.Lazy.IO as TL -- >>> import qualified Data.Text.Lazy as TL -- >>> import Data.Text.Prettyprint.Doc.Render.Terminal -- | The 8 ANSI terminal colors. data Color = Black | Red | Green | Yellow | Blue | Magenta | Cyan | White deriving (Eq, Ord, Show) -- | Dull or vivid coloring, as supported by ANSI terminals. data Intensity = Vivid | Dull deriving (Eq, Ord, Show) -- | Foreground (text) or background (paper) color data Layer = Foreground | Background deriving (Eq, Ord, Show) data Bold = Bold deriving (Eq, Ord, Show) data Underlined = Underlined deriving (Eq, Ord, Show) data Italicized = Italicized deriving (Eq, Ord, Show) -- | Style the foreground with a vivid color. color :: Color -> AnsiStyle color c = mempty { ansiForeground = Just (Vivid, c) } -- | Style the background with a vivid color. bgColor :: Color -> AnsiStyle bgColor c = mempty { ansiBackground = Just (Vivid, c) } -- | Style the foreground with a dull color. colorDull :: Color -> AnsiStyle colorDull c = mempty { ansiForeground = Just (Dull, c) } -- | Style the background with a dull color. bgColorDull :: Color -> AnsiStyle bgColorDull c = mempty { ansiBackground = Just (Dull, c) } -- | Render in __bold__. bold :: AnsiStyle bold = mempty { ansiBold = Just Bold } -- | Render in /italics/. italicized :: AnsiStyle italicized = mempty { ansiItalics = Just Italicized } -- | Render underlined. underlined :: AnsiStyle underlined = mempty { ansiUnderlining = Just Underlined } -- | @('renderLazy' doc)@ takes the output @doc@ from a rendering function -- and transforms it to lazy text, including ANSI styling directives for things -- like colorization. -- -- ANSI color information will be discarded by this function unless you are -- running on a Unix-like operating system. This is due to a technical -- limitation in Windows ANSI support. -- -- With a bit of trickery to make the ANSI codes printable, here is an example -- that would render colored in an ANSI terminal: -- -- >>> let render = TL.putStrLn . TL.replace "\ESC" "\\e" . renderLazy . layoutPretty defaultLayoutOptions -- >>> let doc = annotate (color Red) ("red" <+> align (vsep [annotate (color Blue <> underlined) ("blue+u" <+> annotate bold "bold" <+> "blue+u"), "red"])) -- >>> render (unAnnotate doc) -- red blue+u bold blue+u -- red -- >>> render doc -- \e[0;91mred \e[0;94;4mblue+u \e[0;94;1;4mbold\e[0;94;4m blue+u\e[0;91m -- red\e[0m -- -- Run the above via @echo -e '...'@ in your terminal to see the coloring. renderLazy :: SimpleDocStream AnsiStyle -> TL.Text renderLazy sdoc = runST (do styleStackRef <- newSTRef [mempty] outputRef <- newSTRef mempty let push x = modifySTRef' styleStackRef (x :) unsafePeek = readSTRef styleStackRef >>= \case [] -> panicPeekedEmpty x:_ -> pure x unsafePop = readSTRef styleStackRef >>= \case [] -> panicPeekedEmpty x:xs -> writeSTRef styleStackRef xs >> pure x writeOutput x = modifySTRef outputRef (<> x) let go = \case SFail -> panicUncaughtFail SEmpty -> pure () SChar c rest -> do writeOutput (TLB.singleton c) go rest SText _ t rest -> do writeOutput (TLB.fromText t) go rest SLine i rest -> do writeOutput (TLB.singleton '\n' <> TLB.fromText (T.replicate i " ")) go rest SAnnPush style rest -> do currentStyle <- unsafePeek let newStyle = style <> currentStyle push newStyle writeOutput (TLB.fromText (styleToRawText newStyle)) go rest SAnnPop rest -> do _currentStyle <- unsafePop newStyle <- unsafePeek writeOutput (TLB.fromText (styleToRawText newStyle)) go rest go sdoc readSTRef styleStackRef >>= \case [] -> panicStyleStackFullyConsumed [_] -> fmap TLB.toLazyText (readSTRef outputRef) xs -> panicStyleStackNotFullyConsumed (length xs) ) -- | @('renderIO' h sdoc)@ writes @sdoc@ to the handle @h@. -- -- >>> let render = renderIO System.IO.stdout . layoutPretty defaultLayoutOptions -- >>> let doc = annotate (color Red) ("red" <+> align (vsep [annotate (color Blue <> underlined) ("blue+u" <+> annotate bold "bold" <+> "blue+u"), "red"])) -- -- We render the 'unAnnotate'd version here, since the ANSI codes don’t display -- well in Haddock, -- -- >>> render (unAnnotate doc) -- red blue+u bold blue+u -- red -- -- This function behaves just like -- -- @ -- 'renderIO' h sdoc = 'TL.hPutStr' h ('renderLazy' sdoc) -- @ -- -- but will not generate any intermediate text, rendering directly to the -- handle. renderIO :: Handle -> SimpleDocStream AnsiStyle -> IO () renderIO h sdoc = do styleStackRef <- newIORef [mempty] let push x = modifyIORef' styleStackRef (x :) unsafePeek = readIORef styleStackRef >>= \case [] -> panicPeekedEmpty x:_ -> pure x unsafePop = readIORef styleStackRef >>= \case [] -> panicPeekedEmpty x:xs -> writeIORef styleStackRef xs >> pure x let go = \case SFail -> panicUncaughtFail SEmpty -> pure () SChar c rest -> do hPutChar h c go rest SText _ t rest -> do T.hPutStr h t go rest SLine i rest -> do hPutChar h '\n' T.hPutStr h (T.replicate i " ") go rest SAnnPush style rest -> do currentStyle <- unsafePeek let newStyle = style <> currentStyle push newStyle T.hPutStr h (styleToRawText newStyle) go rest SAnnPop rest -> do _currentStyle <- unsafePop newStyle <- unsafePeek T.hPutStr h (styleToRawText newStyle) go rest go sdoc readIORef styleStackRef >>= \case [] -> panicStyleStackFullyConsumed [_] -> pure () xs -> panicStyleStackNotFullyConsumed (length xs) panicStyleStackFullyConsumed :: void panicStyleStackFullyConsumed = error ("There is no empty style left at the end of rendering" ++ " (but there should be). Please report this as a bug.") panicStyleStackNotFullyConsumed :: Int -> void panicStyleStackNotFullyConsumed len = error ("There are " <> show len <> " styles left at the" ++ "end of rendering (there should be only 1). Please report" ++ " this as a bug.") -- $ -- >>> let render = renderIO System.IO.stdout . layoutPretty defaultLayoutOptions -- >>> let doc = annotate (color Red) ("red" <+> align (vsep [annotate (color Blue <> underlined) ("blue+u" <+> annotate bold "bold" <+> "blue+u"), "red"])) -- >>> render (unAnnotate doc) -- red blue+u bold blue+u -- red -- -- This test won’t work since I don’t know how to type \ESC for doctest :-/ -- -- >>> render doc -- -- \ESC[0;91mred \ESC[0;94;4mblue+u \ESC[0;94;1;4mbold\ESC[0;94;4m blue+u\ESC[0;91m -- -- red\ESC[0m -- | Render the annotated document in a certain style. Styles not set in the -- annotation will use the style of the surrounding document, or the terminal’s -- default if none has been set yet. -- -- @ -- style = 'color' 'Green' '<>' 'bold' -- styledDoc = 'annotate' style "hello world" -- @ data AnsiStyle = SetAnsiStyle { ansiForeground :: Maybe (Intensity, Color) -- ^ Set the foreground color, or keep the old one. , ansiBackground :: Maybe (Intensity, Color) -- ^ Set the background color, or keep the old one. , ansiBold :: Maybe Bold -- ^ Switch on boldness, or don’t do anything. , ansiItalics :: Maybe Italicized -- ^ Switch on italics, or don’t do anything. , ansiUnderlining :: Maybe Underlined -- ^ Switch on underlining, or don’t do anything. } deriving (Eq, Ord, Show) -- | Keep the first decision for each of foreground color, background color, -- boldness, italication, and underlining. If a certain style is not set, the -- terminal’s default will be used. -- -- Example: -- -- @ -- 'color' 'Red' '<>' 'color' 'Green' -- @ -- -- is red because the first color wins, and not bold because (or if) that’s the -- terminal’s default. instance Semigroup AnsiStyle where cs1 <> cs2 = SetAnsiStyle { ansiForeground = ansiForeground cs1 <|> ansiForeground cs2 , ansiBackground = ansiBackground cs1 <|> ansiBackground cs2 , ansiBold = ansiBold cs1 <|> ansiBold cs2 , ansiItalics = ansiItalics cs1 <|> ansiItalics cs2 , ansiUnderlining = ansiUnderlining cs1 <|> ansiUnderlining cs2 } -- | 'mempty' does nothing, which is equivalent to inheriting the style of the -- surrounding doc, or the terminal’s default if no style has been set yet. instance Monoid AnsiStyle where mempty = SetAnsiStyle Nothing Nothing Nothing Nothing Nothing mappend = (<>) styleToRawText :: AnsiStyle -> Text styleToRawText = T.pack . ANSI.setSGRCode . stylesToSgrs where stylesToSgrs :: AnsiStyle -> [ANSI.SGR] stylesToSgrs (SetAnsiStyle fg bg b i u) = catMaybes [ Just ANSI.Reset , fmap (\(intensity, c) -> ANSI.SetColor ANSI.Foreground (convertIntensity intensity) (convertColor c)) fg , fmap (\(intensity, c) -> ANSI.SetColor ANSI.Background (convertIntensity intensity) (convertColor c)) bg , fmap (\_ -> ANSI.SetConsoleIntensity ANSI.BoldIntensity) b , fmap (\_ -> ANSI.SetItalicized True) i , fmap (\_ -> ANSI.SetUnderlining ANSI.SingleUnderline) u ] convertIntensity :: Intensity -> ANSI.ColorIntensity convertIntensity = \case Vivid -> ANSI.Vivid Dull -> ANSI.Dull convertColor :: Color -> ANSI.Color convertColor = \case Black -> ANSI.Black Red -> ANSI.Red Green -> ANSI.Green Yellow -> ANSI.Yellow Blue -> ANSI.Blue Magenta -> ANSI.Magenta Cyan -> ANSI.Cyan White -> ANSI.White -- | @('renderStrict' sdoc)@ takes the output @sdoc@ from a rendering and -- transforms it to strict text. renderStrict :: SimpleDocStream AnsiStyle -> Text renderStrict = TL.toStrict . renderLazy -- | @('putDoc' doc)@ prettyprints document @doc@ to standard output using -- 'defaultLayoutOptions'. -- -- >>> putDoc ("hello" <+> "world") -- hello world -- -- @ -- 'putDoc' = 'hPutDoc' 'stdout' -- @ putDoc :: Doc AnsiStyle -> IO () putDoc = hPutDoc stdout -- | Like 'putDoc', but instead of using 'stdout', print to a user-provided -- handle, e.g. a file or a socket using 'defaultLayoutOptions'. -- -- > main = withFile "someFile.txt" (\h -> hPutDoc h (vcat ["vertical", "text"])) -- -- @ -- 'hPutDoc' h doc = 'renderIO' h ('layoutPretty' 'defaultLayoutOptions' doc) -- @ hPutDoc :: Handle -> Doc AnsiStyle -> IO () hPutDoc h doc = renderIO h (layoutPretty defaultLayoutOptions doc) prettyprinter-ansi-terminal-1.1.1.2/test/Doctest/Main.hs0000644000000000000000000000014013112000052021313 0ustar0000000000000000module Main (main) where import Test.DocTest main :: IO () main = doctest [ "src" , "-Imisc"] prettyprinter-ansi-terminal-1.1.1.2/LICENSE.md0000644000000000000000000000244313111007746017143 0ustar0000000000000000Copyright 2008, Daan Leijen and Max Bolingbroke, 2016 David Luposchainsky. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. This software is provided by the copyright holders "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright holders be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. prettyprinter-ansi-terminal-1.1.1.2/Setup.lhs0000755000000000000000000000011413112000052017323 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain prettyprinter-ansi-terminal-1.1.1.2/prettyprinter-ansi-terminal.cabal0000644000000000000000000000354613243472373024234 0ustar0000000000000000name: prettyprinter-ansi-terminal version: 1.1.1.2 cabal-version: >= 1.10 category: User Interfaces, Text synopsis: ANSI terminal backend for the »prettyprinter« package. description: See README.md license: BSD2 license-file: LICENSE.md extra-source-files: README.md , misc/version-compatibility-macros.h , CHANGELOG.md author: David Luposchainsky maintainer: David Luposchainsky bug-reports: http://github.com/quchen/prettyprinter/issues homepage: http://github.com/quchen/prettyprinter build-type: Simple tested-with: GHC==7.8.4, GHC==7.10.2, GHC==7.10.3, GHC==8.0.1, GHC==8.0.2 source-repository head type: git location: git://github.com/quchen/prettyprinter.git library exposed-modules: Data.Text.Prettyprint.Doc.Render.Terminal , Data.Text.Prettyprint.Doc.Render.Terminal.Internal ghc-options: -Wall hs-source-dirs: src include-dirs: misc default-language: Haskell2010 other-extensions: CPP , LambdaCase , OverloadedStrings build-depends: base >= 4.7 && < 5 , ansi-terminal >= 0.4.0 , text >= 1.2 , prettyprinter >= 1.1.1 if impl(ghc >= 8.0) ghc-options: -Wcompat if !impl(ghc >= 8.0) build-depends: semigroups >= 0.1 test-suite doctest type: exitcode-stdio-1.0 hs-source-dirs: test/Doctest main-is: Main.hs build-depends: base >= 4.7 && < 5 , doctest >= 0.9 ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010 if impl (ghc < 7.10) buildable: False -- Doctest cannot search folders in old versions it seems :-( prettyprinter-ansi-terminal-1.1.1.2/README.md0000644000000000000000000000061313113225130017002 0ustar0000000000000000ANSI terminal prettyprinter renderer ==================================== This package defines a renderer for documents generated by the `prettyprinter` package, suitable for displaying them on ANSI-compatible terminals, including colors, boldening, underlining and italication. For more information about the prettyprinter in general, refer to the main `prettyprinter` package documentation. prettyprinter-ansi-terminal-1.1.1.2/misc/version-compatibility-macros.h0000644000000000000000000000130013112000052024430 0ustar0000000000000000#ifndef VERSION_COMPATIBILITY_MACROS #define VERSION_COMPATIBILITY_MACROS #ifndef MIN_VERSION_base #error "MIN_VERSION_base macro not defined!" #endif -- These macros allow writing CPP compatibility hacks in a way that makes their -- purpose much clearer than just demanding a specific version of a library. #define APPLICATIVE_MONAD MIN_VERSION_base(4,8,0) #define FOLDABLE_TRAVERSABLE_IN_PRELUDE MIN_VERSION_base(4,8,0) #define MONOID_IN_PRELUDE MIN_VERSION_base(4,8,0) #define NATURAL_IN_BASE MIN_VERSION_base(4,8,0) #define SEMIGROUP_IN_BASE MIN_VERSION_base(4,9,0) #define MONAD_FAIL MIN_VERSION_base(4,9,0) #endif prettyprinter-ansi-terminal-1.1.1.2/CHANGELOG.md0000644000000000000000000000116313243470542017352 0ustar0000000000000000# 1.1.1.2 - Fix documentation claiming there would be a trailing newline in `renderIO` when there is none # 1.1.1.1 - `renderIO` now renders directly to STDOUT, instead of first building a textual rendering and then printing that to STDOUT. # 1.1.1 - Expose `AnsiStyle`’s constructors for adaptability # 1.1 - Overhauled the API significantly – Styles are now combined using the `Semigroup` instance from a number of readable primitives. # 1.0.1 Fix version shenanigans, since the prerelease was released to Hackage as version 1 already, so uploading the »new« version 1 did not work # 1 Initial release