module CommandKit::Colors

Defines ANSI color codes.

## Examples

include CommandKit::Colors

def run
  puts colors.green("hello world")
end

### Printing color error messages

stderr.puts colors(stderr).red("error!")

## Environment Variables

## Alternatives

@see en.wikipedia.org/wiki/ANSI_escape_code

Public Instance Methods

ansi?(stream=stdout) click to toggle source

Checks if the stream supports ANSI output.

@param [IO] stream

@return [Boolean]

@note

When the env variable `TERM` is set to `dumb`, it will disable color
output. Color output will also be disabled if the given stream is not
a TTY.

@api public

# File lib/command_kit/colors.rb, line 534
def ansi?(stream=stdout)
  env['TERM'] != 'dumb' && stream.tty?
end
colors(stream=stdout) { |color| ... } click to toggle source

Returns the colors available for the given stream.

@param [IO] stream

@return [ANSI, PlainText]

The ANSI module or PlainText dummy module.

@example

puts colors.green("Hello world")

@example Using colors with stderr output:

stderr.puts colors(stderr).green("Hello world")

@api public

# File lib/command_kit/colors.rb, line 554
def colors(stream=stdout)
  color = if ansi?(stream) then ANSI
          else                  PlainText
          end

  yield color if block_given?
  return color
end