Class: TTY::Color::Support

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/color/support.rb

Constant Summary collapse

SOURCES =
%w[from_term from_tput from_env from_curses].freeze
ENV_VARS =
%w[COLORTERM ANSICON].freeze
TERM_REGEX =
/
color|  # explicitly claims color support in the name
direct| # explicitly claims "direct color" (24 bit) support

#{Mode::TERM_256}|
#{Mode::TERM_64}|
#{Mode::TERM_52}|
#{Mode::TERM_16}|
#{Mode::TERM_8}|

^ansi(\.sys.*)?$|
^cygwin|
^linux|
^putty|
^rxvt|
^screen|
^tmux|
^xterm/xi.freeze

Instance Method Summary collapse

Constructor Details

#initialize(env, verbose: false) ⇒ Support

Initialize a color support



30
31
32
33
# File 'lib/tty/color/support.rb', line 30

def initialize(env, verbose: false)
  @env = env
  @verbose = verbose
end

Instance Method Details

#disabled?Boolean

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

Returns:

  • (Boolean)

    true when terminal color support has been disabled, false otherwise



58
59
60
61
# 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) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attempt to load curses to check color support

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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_envObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if environment specifies color variables



88
89
90
# File 'lib/tty/color/support.rb', line 88

def from_env
  ENV_VARS.any? { |key| @env.key?(key) } || NoValue
end

#from_termObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inspect environment $TERM variable for color support



66
67
68
69
70
71
72
# 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_tputObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Shell out to tput to check color support



77
78
79
80
81
82
83
# 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?Boolean

Detect if terminal supports color

Returns:

  • (Boolean)

    true when terminal supports color, false otherwise



41
42
43
44
45
46
47
48
49
50
# 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