Sha256: 01e46e137c282740a00c223c18a88950658620ff596950cc62d8c3e4605ce1a9

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# coding: utf-8

module TTY
  class Screen
    class Color
      # Initialize color support
      #
      # @api public
      def initialize(options = {})
        @output  = options.fetch(:output) { $stderr }
        @verbose = options.fetch(:verbose) { false }
      end

      # Detect if terminal supports color
      #
      # @return [Boolean]
      #   true when terminal supports color, false otherwise
      #
      # @api public
      def supports?
        return false unless tty?

        from_curses || from_tput || from_term || from_env
      end

      # Attempt to load curses to check color support
      #
      # @return [Boolean]
      #
      # @api private
      def from_curses(curses_class = nil)
        require 'curses'

        begin
          curses_class ||= Curses
          curses_class.init_screen
          curses_class.has_colors?
        ensure
          curses_class.close_screen
        end
      rescue LoadError
        warn 'no native curses support' if @verbose
        false
      end

      # Shell out to tput to check color support
      #
      # @api private
      def from_tput
        %x(tput colors 2>/dev/null).to_i > 2
      rescue Errno::ENOENT
        false
      end

      # Inspect environment $TERM variable for color support
      #
      # @api private
      def from_term
        if ENV['TERM'] == 'dumb'
          false
        elsif ENV['TERM'] =~ /^screen|^xterm|^vt100|color|ansi|cygwin|linux/i
          true
        else false
        end
      end

      def from_env
        ENV.include?('COLORTERM')
      end

      attr_reader :output

      def tty?
        output.tty?
      end
    end # Color
  end # Screen
end # TTY

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tty-screen-0.4.2 lib/tty/screen/color.rb
tty-screen-0.4.1 lib/tty/screen/color.rb