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

Safe HaskellSafe-Inferred
LanguageHaskell2010

Rainbow.Translate

Synopsis

Documentation

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

toByteStringsColors0 :: Chunk -> [ByteString] -> [ByteString] Source

Convert a Chunk to a list of ByteString; do not show any colors. When applied to a Chunk, this function returns a difference list.

toByteStringsColors8 :: Chunk -> [ByteString] -> [ByteString] Source

Convert a Chunk to a list of ByteString; show eight colors. When applied to a Chunk, this function returns a difference list.

toByteStringsColors256 :: Chunk -> [ByteString] -> [ByteString] Source

Convert a Chunk to a list of ByteString; show 256 colors. When applied to a Chunk, this function returns a difference list.

byteStringMakerFromEnvironment :: IO (Chunk -> [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 :: 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:

{-# LANGUAGE OverloadedStrings #-}
module PrintMyChunks where

import qualified Data.ByteString as BS
import Rainbow

myChunks :: [Chunk]
myChunks = [ "Roses" <> fore red, "\n", "Violets" <> fore blue, "\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 :: Chunk -> 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 :: Chunk -> 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.