class TTY::Color::Support

Constants

ENV_VARS
SOURCES
TERM_REGEX

Public Class Methods

new(env, verbose: false) click to toggle source

Initialize a color support @api public

# File lib/tty/color/support.rb, line 30
def initialize(env, verbose: false)
  @env = env
  @verbose = verbose
end

Public Instance Methods

disabled?() click to toggle source

Detect if color support has been disabled with NO_COLOR ENV var.

@return [Boolean]

true when terminal color support has been disabled, false otherwise

@api public

# File lib/tty/color/support.rb, line 58
def disabled?
  no_color = @env["NO_COLOR"]
  !(no_color.nil? || no_color.empty?)
end
from_curses(curses_class = nil) click to toggle source

Attempt to load curses to check color support

@return [Boolean]

@api private

# File lib/tty/color/support.rb, line 97
def from_curses(curses_class = nil)
  return NoValue if TTY::Color.windows?

  require "curses"

  if defined?(Curses)
    curses_class ||= Curses
    curses_class.init_screen
    has_color = curses_class.has_colors?
    curses_class.close_screen
    return has_color
  end
  NoValue
rescue LoadError
  warn "no native curses support" if @verbose
  NoValue
end
from_env() click to toggle source

Check if environment specifies color variables

@api private

# File lib/tty/color/support.rb, line 88
def from_env
  ENV_VARS.any? { |key| @env.key?(key) } || NoValue
end
from_term() click to toggle source

Inspect environment $TERM variable for color support

@api private

# File lib/tty/color/support.rb, line 66
def from_term
  case @env["TERM"]
  when "dumb" then false
  when TERM_REGEX then true
  else NoValue
  end
end
from_tput() click to toggle source

Shell out to tput to check color support

@api private

# File lib/tty/color/support.rb, line 77
def from_tput
  return NoValue unless TTY::Color.command?("tput colors")

  `tput colors 2>/dev/null`.to_i > 2
rescue Errno::ENOENT
  NoValue
end
support?() click to toggle source

Detect if terminal supports color

@return [Boolean]

true when terminal supports color, false otherwise

@api public

# File lib/tty/color/support.rb, line 41
def support?
  return false unless TTY::Color.tty?
  return false if disabled?

  value = false
  SOURCES.each do |from_check|
    break if (value = public_send(from_check)) != NoValue
  end
  value == NoValue ? false : value
end