Sha256: b037fb61492bf3ce5fc5dd20860ae8b128d8cff942a6a95bfd5c3aa24a570146

Contents?: true

Size: 1.34 KB

Versions: 7

Compression:

Stored size: 1.34 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 = ::Colors

# FIXME some should be private
# TODO  add color-pair constants

  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, nb = index(fg), 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)
    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
  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

7 entries across 7 versions & 1 rubygems

Version Path
rubytext-0.1.4 lib/color.rb
rubytext-0.1.3 lib/color.rb
rubytext-0.1.2 lib/color.rb
rubytext-0.1.1 lib/color.rb
rubytext-0.1.0 lib/color.rb
rubytext-0.0.99 lib/color.rb
rubytext-0.0.98 lib/color.rb