class TTY::Color::Mode

Constants

METHODS
TERM_16
TERM_24BIT
TERM_256
TERM_52
TERM_64
TERM_8
TRUECOLORS

Public Class Methods

new(env) click to toggle source
# File lib/tty/color/mode.rb, line 31
def initialize(env)
  @env = env
end

Public Instance Methods

from_term() click to toggle source

Check TERM environment for colors

@return [NoValue, Integer]

@api private

# File lib/tty/color/mode.rb, line 57
def from_term
  case @env["TERM"]
  when TERM_24BIT then TRUECOLORS
  when /[-+](\d+)color/ then $1.to_i
  when /[-+](\d+)bit/   then 2**$1.to_i
  when TERM_256 then 256
  when TERM_64  then 64
  when TERM_52  then 52
  when TERM_16  then 16
  when TERM_8   then 8
  when /dummy/  then 0
  else NoValue
  end
end
from_tput() click to toggle source

Shell out to tput to check color support

@return [NoValue, Integer]

@api private

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

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

Detect supported colors

@return [Integer]

out of 0, 8, 16, 52, 66, 256, 2^24

@api public

# File lib/tty/color/mode.rb, line 41
def mode
  return 0 unless TTY::Color.tty?

  value = 8
  METHODS.each do |from_check|
    break if (value = public_send(from_check)) != NoValue
  end
  return 8 if value == NoValue
  value
end