rainbow-0.30.0.2: Print text to terminal with colors and effects

Safe HaskellNone
LanguageHaskell2010

Rainbow.Translate

Description

This module contains functions that convert a Chunk into ByteStrings. Ordinarily everything you need from this module is exported from Rainbow.

Synopsis

Documentation

class Renderable a where #

Items that can be rendered. render returns a difference list.

Methods

render :: a -> [ByteString] -> [ByteString] #

Instances
Renderable String #

Strings are converted first to a strict Text and then to a strict ByteString.

Instance details

Defined in Rainbow.Translate

Methods

render :: String -> [ByteString] -> [ByteString] #

Renderable ByteString #

Lazy ByteString is converted to strict chunks.

Instance details

Defined in Rainbow.Translate

Renderable ByteString #

Strict ByteString is left as-is.

Instance details

Defined in Rainbow.Translate

Renderable Text #

Converts a lazy Text to UTF-8 ByteStrings.

Instance details

Defined in Rainbow.Translate

Methods

render :: Text -> [ByteString] -> [ByteString] #

Renderable Text #

Converts a strict Text to a UTF-8 ByteString.

Instance details

Defined in Rainbow.Translate

Methods

render :: Text -> [ByteString] -> [ByteString] #

params :: Show a => [a] -> [ByteString] -> [ByteString] #

byteStringMakerFromEnvironment :: Renderable a => IO (Chunk a -> [ByteString] -> [ByteString]) #

Spawns a subprocess to read the output of tput colors. If this says there are at least 256 colors are available, returns toByteStringsColors256. Otherwise, if there are at least 8 colors available, returns toByteStringsColors8. Otherwise, returns toByteStringsColors0.

If any IO exceptions arise during this process, they are discarded and toByteStringsColors0 is returned.

byteStringMakerFromHandle :: Renderable a => Handle -> IO (Chunk a -> [ByteString] -> [ByteString]) #

Like byteStringMakerFromEnvironment but also consults a provided Handle. If the Handle is not a terminal, toByteStringsColors0 is returned. Otherwise, the value of byteStringMakerFromEnvironment is returned.

chunksToByteStrings #

Arguments

:: (Chunk a -> [ByteString] -> [ByteString])

Function that converts Chunk to ByteString. This function, when applied to a Chunk, returns a difference list.

-> [Chunk a] 
-> [ByteString] 

Convert a list of Chunk to a list of ByteString. The length of the returned list may be longer than the length of the input list.

So, for example, to print a bunch of chunks to standard output using 256 colors:

module PrintMyChunks where

import qualified Data.ByteString as BS
import Rainbow

myChunks :: [Chunk String]
myChunks = [ chunk "Roses" & fore red, chunk "\n",
             chunk "Violets" & fore blue, chunk "\n" ]

myPrintedChunks :: IO ()
myPrintedChunks = mapM_ BS.putStr
                . chunksToByteStrings toByteStringsColors256
                $ myChunks

To use the highest number of colors that this terminal supports:

myPrintedChunks' :: IO ()
myPrintedChunks' = do
  printer <- byteStringMakerFromEnvironment
  mapM_ BS.putStr
    . chunksToByteStrings printer
    $ myChunks

putChunk :: Renderable a => Chunk a -> IO () #

Writes a Chunk to standard output. Spawns a child process to read the output of tput colors to determine how many colors to use, for every single chunk. Therefore, this is not going to win any speed awards. You are better off using chunksToByteStrings and the functions in Data.ByteString to print your Chunks if you are printing a lot of them.

putChunkLn :: Renderable a => Chunk a -> IO () #

Writes a Chunk to standard output, and appends a newline. Spawns a child process to read the output of tput colors to determine how many colors to use, for every single chunk. Therefore, this is not going to win any speed awards. You are better off using chunksToByteStrings and the functions in Data.ByteString to print your Chunks if you are printing a lot of them.