safe-coloured-text 0.2.0.2 → 0.4.0.0
raw patch · 5 files changed
Files
- CHANGELOG.md +32/−0
- safe-coloured-text.cabal +3/−2
- src/Text/Colour.hs +23/−20
- src/Text/Colour/Chunk.hs +42/−26
- src/Text/Colour/Code.hs +8/−0
CHANGELOG.md view
@@ -1,5 +1,37 @@ # Changelog for safe-coloured-text +## [0.4.0.0] - 2026-04-12++### Added++* Strikethrough support (`chunkStrikethrough`, `strikethrough`, SGR 9/29)+* Reverse video support (`chunkSwapForegroundBackground`, `swapForegroundBackground`, SGR 7/27)+* Concealed/hidden text support (`chunkConcealed`, `concealed`, SGR 8/28)+* Overline support (`chunkOverlined`, `overlined`, SGR 53/55)++### Fixed++* `chunkBlinking` is now rendered in `chunkSGR` (was stored but never emitted)++## [0.3.0.2] - 2024-06-23++### Added++* `unwordsChunks`+* `chunkWidth`++## [0.3.0.1] - 2024-03-26++### Added++* `unlinesChunks`++## [0.3.0.0] - 2024-03-26++### Removed++* Removed deprecated functions+ ## [0.2.0.2] - 2024-03-26 ### Changed
safe-coloured-text.cabal view
@@ -1,11 +1,11 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.35.2.+-- This file has been generated from package.yaml by hpack version 0.37.0. -- -- see: https://github.com/sol/hpack name: safe-coloured-text-version: 0.2.0.2+version: 0.4.0.0 synopsis: Safely output coloured text category: User Interfaces homepage: https://github.com/NorfairKing/safe-coloured-text#readme@@ -17,6 +17,7 @@ license-file: LICENSE build-type: Simple extra-source-files:+ LICENSE CHANGELOG.md source-repository head
src/Text/Colour.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE OverloadedStrings #-}+ -- | Safe Coloured Text -- -- This module is responsible for defining, building, and rendering coloured text.@@ -24,6 +26,10 @@ bold, faint, italic,+ strikethrough,+ swapForegroundBackground,+ concealed,+ overlined, underline, doubleUnderline, noUnderline,@@ -66,6 +72,10 @@ -- * Rendering + -- ** Composing chunks+ unlinesChunks,+ unwordsChunks,+ -- ** Rendering chunks to strict bytestring in UTF8 renderChunksUtf8BS, renderChunkUtf8BS,@@ -86,14 +96,6 @@ renderChunksBuilder, renderChunkBuilder, - -- ** Decrecated rendering chunks to strict bytestring in UTF8- renderChunksBS,- renderChunkBS,-- -- ** Deprecated rendering chunks to lazy bytestring builders in UTF8- renderChunks,- renderChunk,- -- * IO TerminalCapabilities (..), @@ -102,12 +104,11 @@ putChunksLocaleWith, hPutChunksUtf8With, hPutChunksLocaleWith,- putChunksWith,- hPutChunksWith, ) where import qualified Data.ByteString.Builder as SBB+import Data.List (intercalate) import qualified Data.Text.IO as TIO import System.IO import Text.Colour.Capabilities@@ -119,12 +120,7 @@ -- | Print a list of chunks to stdout with given 'TerminalCapabilities' in an encoding according to the system's locale. putChunksLocaleWith :: TerminalCapabilities -> [Chunk] -> IO ()-putChunksLocaleWith tc = hPutChunksWith tc stdout---- | Deprecated synonym of 'putChunksUtf8With'-putChunksWith :: TerminalCapabilities -> [Chunk] -> IO ()-putChunksWith = putChunksUtf8With-{-# DEPRECATED putChunksWith "Use putChunksLocaleWith, or putChunksUtf8With if you must." #-}+putChunksLocaleWith tc = hPutChunksLocaleWith tc stdout -- | Print a list of chunks to the given 'Handle' with given 'TerminalCapabilities'. hPutChunksUtf8With :: TerminalCapabilities -> Handle -> [Chunk] -> IO ()@@ -134,7 +130,14 @@ hPutChunksLocaleWith :: TerminalCapabilities -> Handle -> [Chunk] -> IO () hPutChunksLocaleWith tc h cs = TIO.hPutStr h $ renderChunksText tc cs --- | Deprecated synonym of 'hPutChunksUtf8With'-hPutChunksWith :: TerminalCapabilities -> Handle -> [Chunk] -> IO ()-hPutChunksWith = hPutChunksUtf8With-{-# DEPRECATED hPutChunksWith "Use hPutChunksLocaleWith, or hPutChunksUtf8With if you must." #-}+-- | Render lines of chunks.+--+-- This puts newlines ("\n") at the end of every list of chunks.+unlinesChunks :: [[Chunk]] -> [Chunk]+unlinesChunks = concatMap (<> [chunk "\n"])++-- | Render lines of chunks.+--+-- This puts newlines (" ") inbetween the list of chunks.+unwordsChunks :: [[Chunk]] -> [Chunk]+unwordsChunks = intercalate [" "]
src/Text/Colour/Chunk.hs view
@@ -10,6 +10,7 @@ import Data.Maybe import Data.String import Data.Text (Text)+import qualified Data.Text as T import qualified Data.Text.Encoding as TE import qualified Data.Text.Lazy as LT import qualified Data.Text.Lazy.Builder as LTB@@ -24,6 +25,10 @@ data Chunk = Chunk { chunkText :: !Text, chunkItalic :: !(Maybe Bool),+ chunkStrikethrough :: !(Maybe Bool),+ chunkSwapForegroundBackground :: !(Maybe Bool),+ chunkConcealed :: !(Maybe Bool),+ chunkOverlined :: !(Maybe Bool), chunkConsoleIntensity :: !(Maybe ConsoleIntensity), chunkUnderlining :: !(Maybe Underlining), chunkBlinking :: !(Maybe Blinking),@@ -37,11 +42,21 @@ instance IsString Chunk where fromString = chunk . fromString +-- TODO This is not correct because text-width is correct but it's a+-- good place to put this so we can fix it later and it'll get fixed+-- everywhere.+chunkWidth :: Chunk -> Int+chunkWidth = T.length . chunkText+ plainChunk :: TerminalCapabilities -> Chunk -> Bool plainChunk tc Chunk {..} =- let Chunk _ _ _ _ _ _ _ = undefined+ let Chunk _ _ _ _ _ _ _ _ _ _ _ = undefined in and [ isNothing chunkItalic,+ isNothing chunkStrikethrough,+ isNothing chunkSwapForegroundBackground,+ isNothing chunkConcealed,+ isNothing chunkOverlined, isNothing chunkConsoleIntensity, isNothing chunkUnderlining, isNothing chunkBlinking,@@ -56,53 +71,33 @@ Colour24Bit {} -> tc < With24BitColours -- | Render chunks directly to a UTF8-encoded 'Bytestring'.-renderChunksUtf8BS :: Foldable f => TerminalCapabilities -> f Chunk -> ByteString+renderChunksUtf8BS :: (Foldable f) => TerminalCapabilities -> f Chunk -> ByteString renderChunksUtf8BS tc = TE.encodeUtf8 . renderChunksText tc --- | Deprecated synonym for 'renderChunksUtf8BS'-renderChunksBS :: Foldable f => TerminalCapabilities -> f Chunk -> ByteString-renderChunksBS = renderChunksUtf8BS-{-# DEPRECATED renderChunksBS "Use renderChunksText, or renderChunksUtf8BS if you must." #-}- -- | Render chunks to a UTF8-encoded 'ByteString' 'Bytestring.Builder'.-renderChunksUtf8BSBuilder :: Foldable f => TerminalCapabilities -> f Chunk -> ByteString.Builder+renderChunksUtf8BSBuilder :: (Foldable f) => TerminalCapabilities -> f Chunk -> ByteString.Builder renderChunksUtf8BSBuilder tc = foldMap (renderChunkUtf8BSBuilder tc) --- | Deprecated synonym for 'renderChunksUtf8BSBuilder'-renderChunks :: Foldable f => TerminalCapabilities -> f Chunk -> ByteString.Builder-renderChunks = renderChunksUtf8BSBuilder-{-# DEPRECATED renderChunks "Use renderChunksBuilder, or renderChunksUtf8BSBuilder if you must." #-}- -- | Render chunks directly to strict 'Text'.-renderChunksText :: Foldable f => TerminalCapabilities -> f Chunk -> Text+renderChunksText :: (Foldable f) => TerminalCapabilities -> f Chunk -> Text renderChunksText tc = LT.toStrict . renderChunksLazyText tc -- | Render chunks directly to lazy 'LT.Text'.-renderChunksLazyText :: Foldable f => TerminalCapabilities -> f Chunk -> LT.Text+renderChunksLazyText :: (Foldable f) => TerminalCapabilities -> f Chunk -> LT.Text renderChunksLazyText tc = LTB.toLazyText . renderChunksBuilder tc -- | Render chunks to a lazy 'LT.Text' 'Text.Builder'-renderChunksBuilder :: Foldable f => TerminalCapabilities -> f Chunk -> Text.Builder+renderChunksBuilder :: (Foldable f) => TerminalCapabilities -> f Chunk -> Text.Builder renderChunksBuilder tc = foldMap (renderChunkBuilder tc) -- | Render a chunk directly to a UTF8-encoded 'Bytestring'. renderChunkUtf8BS :: TerminalCapabilities -> Chunk -> ByteString renderChunkUtf8BS tc = TE.encodeUtf8 . renderChunkText tc --- | Deprecated synonym for 'renderChunkUtf8BS'-renderChunkBS :: TerminalCapabilities -> Chunk -> ByteString-renderChunkBS = renderChunkUtf8BS-{-# DEPRECATED renderChunkBS "Use renderChunkText, or renderChunkUtf8BS if you must." #-}- -- | Render a chunk directly to a UTF8-encoded 'Bytestring' 'ByteString.Builder'. renderChunkUtf8BSBuilder :: TerminalCapabilities -> Chunk -> ByteString.Builder renderChunkUtf8BSBuilder tc = LTE.encodeUtf8Builder . renderChunkLazyText tc --- | Deprecated synonym for 'renderChunkUtf8BSBuilder'-renderChunk :: TerminalCapabilities -> Chunk -> ByteString.Builder-renderChunk = renderChunkUtf8BSBuilder-{-# DEPRECATED renderChunk "Use renderChunkBuilder, or renderChunkUtf8BSBuilder if you must." #-}- -- | Render a chunk directly to strict 'Text'. renderChunkText :: TerminalCapabilities -> Chunk -> Text renderChunkText tc = LT.toStrict . renderChunkLazyText tc@@ -127,7 +122,12 @@ chunkSGR tc Chunk {..} = catMaybes [ SetItalic <$> chunkItalic,+ SetStrikethrough <$> chunkStrikethrough,+ SetSwapForegroundBackground <$> chunkSwapForegroundBackground,+ SetConcealed <$> chunkConcealed,+ SetOverlined <$> chunkOverlined, SetUnderlining <$> chunkUnderlining,+ SetBlinking <$> chunkBlinking, SetConsoleIntensity <$> chunkConsoleIntensity, chunkForeground >>= colourSGR tc Foreground, chunkBackground >>= colourSGR tc Background@@ -139,6 +139,10 @@ Chunk { chunkText = t, chunkItalic = Nothing,+ chunkStrikethrough = Nothing,+ chunkSwapForegroundBackground = Nothing,+ chunkConcealed = Nothing,+ chunkOverlined = Nothing, chunkConsoleIntensity = Nothing, chunkUnderlining = Nothing, chunkBlinking = Nothing,@@ -160,6 +164,18 @@ italic :: Chunk -> Chunk italic chu = chu {chunkItalic = Just True}++strikethrough :: Chunk -> Chunk+strikethrough chu = chu {chunkStrikethrough = Just True}++swapForegroundBackground :: Chunk -> Chunk+swapForegroundBackground chu = chu {chunkSwapForegroundBackground = Just True}++concealed :: Chunk -> Chunk+concealed chu = chu {chunkConcealed = Just True}++overlined :: Chunk -> Chunk+overlined chu = chu {chunkOverlined = Just True} underline :: Chunk -> Chunk underline chu = chu {chunkUnderlining = Just SingleUnderline}
src/Text/Colour/Code.hs view
@@ -71,6 +71,10 @@ data SGR = Reset | SetItalic !Bool+ | SetStrikethrough !Bool+ | SetSwapForegroundBackground !Bool+ | SetConcealed !Bool+ | SetOverlined !Bool | SetUnderlining !Underlining | SetBlinking !Blinking | SetConsoleIntensity !ConsoleIntensity@@ -95,6 +99,10 @@ sgrToCSIParams = \case Reset -> [] -- [0] would be fine too SetItalic b -> [if b then 3 else 23]+ SetStrikethrough b -> [if b then 9 else 29]+ SetSwapForegroundBackground b -> [if b then 7 else 27]+ SetConcealed b -> [if b then 8 else 28]+ SetOverlined b -> [if b then 53 else 55] SetUnderlining u -> [ case u of SingleUnderline -> 4