class TTY::Color::Support
Constants
- ENV_VARS
- SOURCES
- TERM_REGEX
Public Class Methods
Source
# File lib/tty/color/support.rb, line 30 def initialize(env, verbose: false) @env = env @verbose = verbose end
Initialize a color support @api public
Public Instance Methods
Source
# File lib/tty/color/support.rb, line 58 def disabled? no_color = @env["NO_COLOR"] !(no_color.nil? || no_color.empty?) end
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
Source
# 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
Attempt to load curses to check color support
@return [Boolean]
@api private
Source
# File lib/tty/color/support.rb, line 88 def from_env ENV_VARS.any? { |key| @env.key?(key) } || NoValue end
Check if environment specifies color variables
@api private
Source
# 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
Inspect environment $TERM variable for color support
@api private
Source
# 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
Shell out to tput to check color support
@api private
Source
# 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
Detect if terminal supports color
@return [Boolean]
true when terminal supports color, false otherwise
@api public