diff --git a/prettyprinter-ansi-terminal/prettyprinter-ansi-terminal.cabal b/prettyprinter-ansi-terminal/prettyprinter-ansi-terminal.cabal index 6c941ffe..ac0e73d8 100644 --- a/prettyprinter-ansi-terminal/prettyprinter-ansi-terminal.cabal +++ b/prettyprinter-ansi-terminal/prettyprinter-ansi-terminal.cabal @@ -20,6 +20,15 @@ source-repository head type: git location: https://github.com/quchen/prettyprinter +flag text + description: While it's a core value of @prettyprinter@ packages to use @Text@, there are rare + circumstances (mostly when @prettyprinter@ arises as a dependency of + test suites of packages like @bytestring@ or @text@ themselves) when + this is inconvenient. In this case one can disable this flag, so that + @prettyprinter-ansi-terminal@ fallbacks to @String@. + default: True + manual: True + library exposed-modules: Data.Text.Prettyprint.Doc.Render.Terminal , Data.Text.Prettyprint.Doc.Render.Terminal.Internal @@ -37,8 +46,17 @@ library build-depends: base >= 4.5 && < 5 , ansi-terminal >= 0.4.0 - , text >= 1.2 , prettyprinter >= 1.7.0 + if flag(text) + build-depends: text >= 1.2 + else + -- A fake text package, emulating the same API, but backed by String + hs-source-dirs: src-text + other-modules: + Data.Text + , Data.Text.IO + , Data.Text.Lazy + , Data.Text.Lazy.Builder if impl(ghc >= 8.0) ghc-options: -Wcompat diff --git a/prettyprinter-ansi-terminal/src-text/Data/Text.hs b/prettyprinter-ansi-terminal/src-text/Data/Text.hs new file mode 100644 index 00000000..abfe82fa --- /dev/null +++ b/prettyprinter-ansi-terminal/src-text/Data/Text.hs @@ -0,0 +1,46 @@ +-- Provide a fake API, mimicking Data.Text from text package, +-- but actually backed by type Text = String. It is used only in rare +-- circumstances, when prettyprinter is built with -text flag. +-- + +{-# OPTIONS_GHC -fno-warn-missing-signatures #-} + +module Data.Text where + +import Prelude hiding (head, length, null, replicate) +import qualified Data.Char +import qualified Data.List + +type Text = String +cons = (:) +dropWhileEnd = Data.List.dropWhileEnd +head = Data.List.head +intercalate = Data.List.intercalate +length = Data.List.length :: [Char] -> Int +lines = Data.List.lines +map = Data.List.map +null = Data.List.null :: [Char] -> Bool +pack = id +replicate = (Data.List.concat .) . Data.List.replicate +singleton = (:[]) +snoc xs x = xs ++ [x] +stripEnd = dropWhileEnd Data.Char.isSpace +unlines = Data.List.unlines +unpack = id +words = Data.List.words + +uncons :: Text -> Maybe (Char, Text) +uncons [] = Nothing +uncons (x : xs) = Just (x, xs) + +splitOn :: Text -> Text -> [Text] +splitOn pat src + | null pat = error "splitOn: empty pattern" + | otherwise = go [] src + where + go acc [] = [reverse acc] + go acc xs@(y : ys) + | pat `Data.List.isPrefixOf` xs + = reverse acc : go [] (drop (length pat) xs) + | otherwise + = go (y : acc) ys diff --git a/prettyprinter-ansi-terminal/src-text/Data/Text/IO.hs b/prettyprinter-ansi-terminal/src-text/Data/Text/IO.hs new file mode 100644 index 00000000..705d4c1a --- /dev/null +++ b/prettyprinter-ansi-terminal/src-text/Data/Text/IO.hs @@ -0,0 +1,13 @@ +-- Provide a fake API, mimicking Data.Text.IO from text package, +-- but actually backed by type Text = String. It is used only in rare +-- circumstances, when prettyprinter is built with -text flag. +-- + +{-# OPTIONS_GHC -fno-warn-missing-signatures #-} + +module Data.Text.IO where + +import qualified System.IO + +hPutStr = System.IO.hPutStr +putStrLn = System.IO.putStrLn diff --git a/prettyprinter-ansi-terminal/src-text/Data/Text/Lazy.hs b/prettyprinter-ansi-terminal/src-text/Data/Text/Lazy.hs new file mode 100644 index 00000000..f66d8974 --- /dev/null +++ b/prettyprinter-ansi-terminal/src-text/Data/Text/Lazy.hs @@ -0,0 +1,15 @@ +-- Provide a fake API, mimicking Data.Text.Lazy from text package, +-- but actually backed by type Text = String. It is used only in rare +-- circumstances, when prettyprinter is built with -text flag. +-- + +{-# OPTIONS_GHC -fno-warn-missing-signatures #-} + +module Data.Text.Lazy where + +import Data.Text as T + +type Text = T.Text +length = T.length +lines = T.lines +toStrict = id diff --git a/prettyprinter-ansi-terminal/src-text/Data/Text/Lazy/Builder.hs b/prettyprinter-ansi-terminal/src-text/Data/Text/Lazy/Builder.hs new file mode 100644 index 00000000..690533c9 --- /dev/null +++ b/prettyprinter-ansi-terminal/src-text/Data/Text/Lazy/Builder.hs @@ -0,0 +1,13 @@ +-- Provide a fake API, mimicking Data.Text.Lazy.Builder from text package, +-- but actually backed by type Builder = String. It is used only in rare +-- circumstances, when prettyprinter is built with -text flag. +-- + +{-# OPTIONS_GHC -fno-warn-missing-signatures #-} + +module Data.Text.Lazy.Builder where + +type Builder = String +fromText = id +singleton = (:[]) +toLazyText = id