Sha256: 664a5a89a957a73ea2eca151ab02d0edf99da354f9621b8ad59c4057511b63b7

Contents?: true

Size: 1.61 KB

Versions: 32

Compression:

Stored size: 1.61 KB

Contents

require 'highline/import'

module Kafo
  class ColorScheme

    def self.colors_possible?
      ::ENV['TERM'] && !`which tput 2> /dev/null`.empty? && `tput colors`.to_i > 0
    end

    def initialize(options={})
      @background = options[:background].nil? ? :dark : options[:background]
      @colors = options[:colors].nil? ? self.class.colors_possible? : options[:colors]
    end

    def setup
      if @colors
        HighLine.color_scheme = build_color_scheme
        HighLine.use_color = true
      else
        HighLine.use_color = false
      end
    end

    private

    def build_color_scheme
      HighLine::ColorScheme.new do |cs|
        color_hash.keys.each do |key|
          cs[key] = color_hash[key]
        end
      end
    end

    def color_hash
      @color_hash ||= {
          :headline => build_color(:yellow),
          :title => build_color(:yellow),
          :horizontal_line => build_color(:white),
          :important => build_color(:white),
          :question => build_color(:green),
          :info => build_color(:cyan),
          :cancel => build_color(:red),
          :run => build_color(:green),

          :bad => build_color(:red),
          :good => build_color(:green),
      }
    end

    def build_color(color)
      bright = @background.to_s == 'bright'
      color = convert_bright_to_dark(color) if bright

      attributes = [ color ]
      attributes.unshift :bold unless bright
      attributes
    end

    def convert_bright_to_dark(color)
      case color
        when :white
          :black
        when :cyan
          :blue
        else
          color
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
kafo-0.7.6 lib/kafo/color_scheme.rb
kafo-0.9.1 lib/kafo/color_scheme.rb
kafo-0.7.5 lib/kafo/color_scheme.rb
kafo-0.7.4 lib/kafo/color_scheme.rb
kafo-0.9.0 lib/kafo/color_scheme.rb
kafo-0.8.2 lib/kafo/color_scheme.rb
kafo-0.8.1 lib/kafo/color_scheme.rb
kafo-0.8.0 lib/kafo/color_scheme.rb
kafo-0.7.3 lib/kafo/color_scheme.rb
kafo-0.7.2 lib/kafo/color_scheme.rb
kafo-0.7.1 lib/kafo/color_scheme.rb
kafo-0.7.0 lib/kafo/color_scheme.rb