rainbow-0.28.0.4: 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 Source #

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

Minimal complete definition

render

Methods

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

Instances

Renderable String Source #

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

Renderable ByteString Source #

Lazy ByteString is converted to strict chunks.

Renderable ByteString Source #

Strict ByteString is left as-is.

Renderable Text Source #

Converts a lazy Text to UTF-8 ByteStrings.

Methods

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

Renderable Text Source #

Converts a strict Text to a UTF-8 ByteString.

Methods

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

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

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

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]) Source #

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 Source #

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 () Source #

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 () Source #

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.