Class: TTY::Color::Mode

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

Constant Summary collapse

TERM_24BIT =
/[+-]direct/.freeze
TRUECOLORS =

8 bits per RGB channel

2**24
TERM_256 =
/^(alacritty|iTerm\s?\d*\.app|kitty|mintty|ms-terminal|
nsterm|nsterm-build\d+|terminator|terminology(-[0-9.]+)?|
termite|vscode)$/x.freeze
TERM_64 =
/^(hpterm-color|wy370|wy370-105k|wy370-EPC|wy370-nk|
wy370-rv|wy370-tek|wy370-vb|wy370-w|wy370-wvb)$/x.freeze
TERM_52 =
/^(dg+ccc|dgunix+ccc|d430.*?[-+](dg|unix).*?[-+]ccc)$/x.freeze
TERM_16 =
/^(amiga-vnc|d430-dg|d430-unix|d430-unix-25|d430-unix-s|
d430-unix-sr|d430-unix-w|d430c-dg|d430c-unix|d430c-unix-25|
d430c-unix-s|d430c-unix-sr|d430c-unix-w|d470|d470-7b|d470-dg|
d470c|d470c-7b|d470c-dg|dg+color|dg\+fixed|dgunix\+fixed|
dgmode\+color|hp\+color|ncr260wy325pp|ncr260wy325wpp|
ncr260wy350pp|ncr260wy350wpp|nsterm-c|nsterm-c-acs|
nsterm-c-s|nsterm-c-s-7|nsterm-c-s-acs|nsterm\+c|
nsterm-7-c|nsterm-bce)$/x.freeze
TERM_8 =
/vt100|xnuppc|wy350/x.freeze
METHODS =
%w[from_term from_tput].freeze

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Mode

Returns a new instance of Mode.



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

def initialize(env)
  @env = env
end

Instance Method Details

#from_termNoValue, Integer

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 TERM environment for colors

Returns:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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_tputNoValue, Integer

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

Returns:



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

#modeInteger

Detect supported colors

Returns:

  • (Integer)

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



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