Sha256: cb31d61d5d2d822e36997e5ca1f2a050fe32ced9eaa90150980bcb848d6d8b17

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# Colors are 'global' for now
Black, Blue, Cyan, Green, Magenta, Red, White, Yellow = 
  :black, :blue, :cyan, :green, :magenta, :red, :white, :yellow

Colors = [Black, Blue, Cyan, Green, Magenta, Red, White, Yellow]

class RubyText::Color
  Colors = [Black, Blue, Cyan, Green, Magenta, Red, White, Yellow]

  def self.sym2const(color)   # to curses constant
    X.const_get("COLOR_#{color.to_s.upcase}")
  end

  def self.index(color)
    Colors.find_index(color)  # "our" number
  end

  def self.pair(fg, bg)
    nf = index(fg)
    nb = index(bg)
    num = 8*nf + nb
    X.init_pair(num, sym2const(fg), sym2const(bg))
    num
  end
end

class RubyText::Window
  def self.colorize!(cwin, fg, bg)
    File.open("/tmp/cize.out", "w") do |f|
      f.puts "colorize: fg, bg = #{[fg, bg].inspect}"
    end
    cp = RubyText::Color.pair(fg, bg)
    cwin.color_set(cp)
    num = cwin.maxx * cwin.maxy
    cwin.setpos 0,0
    cwin.addstr(' '*num)
    cwin.setpos 0,0
    cwin.refresh
    debug "returning from colorize! call"
  rescue => err
    File.open("/tmp/#{__method__}.out", "w") do |f|
      f.puts err
      f.puts err.backtrace
    end
  end

  def set_colors(fg, bg)
    cp = RubyText::Color.pair(fg, bg)
    @cwin.color_set(cp)
  end

  def colorize!(fg, bg)
    set_colors(fg, bg)
    num = @cwin.maxx * @cwin.maxy
    self.home
    self.go(0, 0) { @cwin.addstr(' '*num) }
    @cwin.refresh
  end

  def fg=(sym)
    set_colors(sym, @bg)
  end

  def bg=(sym)
    set_colors(@fg, sym)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubytext-0.0.89 lib/color.rb
rubytext-0.0.88 lib/color.rb
rubytext-0.0.87 lib/color.rb