module Paint
Constants
- RGB_COLORS
A list of color names, based on X11's rgb.txt Can be used with Paint.[] by passing a string containing the color name See Paint::Util::update_rgb_colors for generating
- RGB_COLORS_ANSI
A list of color names for standard ansi colors, needed for 16/8 color fallback mode See en.wikipedia.org/wiki/ANSI_escape_code#Colors
- RGB_COLORS_ANSI_BRIGHT
A list of color names for standard bright ansi colors, needed for 16 color fallback mode See en.wikipedia.org/wiki/ANSI_escape_code#Colors
- SHORTCUTS
Hash for defining color/effect shortcuts See README for details
- VERSION
Public Instance Methods
Paint::SomeModule –> Paint::SHORTCUTS
# File lib/paint/shortcuts.rb, line 13 def const_missing(mod_name) # get shortcuts shortcuts = SHORTCUTS[mod_name.to_s.gsub(/[A-Z]/,'_\0').downcase[1..-1].to_sym] || [] # create module class_eval "module #{mod_name}; end" mod = const_get(mod_name) eigen_mod = class << mod; self; end # 1.8 # define direct behaviour, class methods # mod.define_singleton_method :method_missing do |color_name, *args| eigen_mod.send:define_method, :method_missing do |color_name, *args| if color_code = shortcuts[color_name] if args.empty? then color_code else color_code + Array(args).join + NOTHING end else nil end end eigen_mod.send:define_method, :respond_to? do |color_name, *args| shortcuts.include?(color_name) || super(color_name, *args) end # define include behaviour, instance methods eigen_mod.send:define_method, :included do |_| shortcuts.each{ |color_name, color_code| define_method color_name do |*args| if args.empty? then color_code else color_code + Array(args).join + NOTHING end end } private(*shortcuts.keys) unless shortcuts.empty? end # include variations, defined in child modules mod.class_eval "module String; end" string = mod.const_get(:String) eigen_string = class << string; self; end # 1.8 eigen_string.send:define_method, :included do |_| shortcuts.each{ |color_name, color_code| define_method color_name do color_code + to_s + NOTHING end } end # OK, let's take it one level further ;) mod.class_eval "module Prefix; end" prefix_prefix = mod.const_get(:Prefix) eigen_prefix_prefix = class << prefix_prefix; self; end # 1.8 eigen_prefix_prefix.send:define_method, :const_missing do |prefix_name| class_eval "module #{prefix_name}; end" prefix = const_get(prefix_name) eigen_prefix = class << prefix; self; end # 1.8 eigen_prefix.send:define_method, :included do |_| define_method prefix_name.to_s.gsub(/[A-Z]/,'_\0').downcase[1..-1].to_sym do |color_name| shortcuts[color_name] && shortcuts[color_name] + to_s + NOTHING end end prefix end # :) mod end
Determine supported colors This is just a naive approach, based on some things I could test Please open issues if it does not work correctly for you
# File lib/paint/util.rb, line 38 def detect_mode if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ # windows if ENV['ANSICON'] 16 elsif ENV['ConEmuANSI'] == 'ON' 256 else 0 end else # case ENV['COLORTERM'] # when 'gnome-terminal' # 256 # else case ENV['TERM'] when /-256color$/, 'xterm' 256 when /-color$/, 'rxvt' 16 else # optimistic default 256 end # end end end
Tries to print all 256 colors
# File lib/paint/util.rb, line 12 def rainbow (0...256).each{ |color| print Paint[' ', 48, 5, color] # print empty bg color field } puts end
Removes any color and effect strings
# File lib/paint/util.rb, line 7 def unpaint(string) string.gsub(/\e\[(?:[0-9];?)+m/, '') end
Updates color names
# File lib/paint/util.rb, line 20 def update_rgb_colors(path = '/etc/X11/rgb.txt') if File.file?(path) Paint::RGB_COLORS.clear File.open(path, 'r') do |file| file.each_line{ |line| line.chomp =~ /(\d+)\s+(\d+)\s+(\d+)\s+(.*)$/ Paint::RGB_COLORS[$4] = [$1.to_i, $2.to_i, $3.to_i] if $4 } end else raise ArgumentError, "Not a valid file: #{path}" end end