rainbow-0.34.2.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

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

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

Uses setupTermFromEnv to obtain the terminal's color capability. 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 the terminfo database could not be read (that is, if SetupTermError is returned), then return toByteStringsColors0.

byteStringMakerFromHandle :: Handle -> IO (Chunk -> [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 -> [ByteString] -> [ByteString])

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

-> [Chunk] 
-> [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

hPutChunks :: Handle -> [Chunk] -> IO () Source #

Writes a list of chunks to the given Handle.

First uses byteStringMakerFromEnvironment to determine how many colors to use. Then creates a list of ByteString using chunksToByteStrings and then writes them to the given Handle.

hPutChunksLn :: Handle -> [Chunk] -> IO () Source #

Writes a list of chunks to the given Handle, followed by a newline character.

First uses byteStringMakerFromEnvironment to determine how many colors to use. Then creates a list of ByteString using chunksToByteStrings and then writes them to the given Handle.

putChunks :: [Chunk] -> IO () Source #

Writes a list of chunks to standard output.

First uses byteStringMakerFromEnvironment to determine how many colors to use. Then creates a list of ByteString using chunksToByteStrings and then writes them to standard output.

putChunksLn :: [Chunk] -> IO () Source #

Writes a list of chunks to standard output, followed by a newline.

First uses byteStringMakerFromEnvironment to determine how many colors to use. Then creates a list of ByteString using chunksToByteStrings and then writes them to standard output.

putChunk :: Chunk -> IO () Source #

Writes a Chunk to standard output. Uses byteStringMakerFromEnvironment each time you apply it, so this might be inefficient. 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 :: Chunk -> IO () Source #

Writes a Chunk to standard output, and appends a newline. Uses byteStringMakerFromEnvironment each time you apply it, so this might be inefficient. You are better off using chunksToByteStrings and the functions in Data.ByteString to print your Chunks if you are printing a lot of them.