Sha256: 78b41e7455b7cd563df9dbc02d2b13fd08b32ee07f4edc6757496c77063c0f38

Contents?: true

Size: 1.35 KB

Versions: 17

Compression:

Stored size: 1.35 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
    Curses.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
    Curses.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

17 entries across 17 versions & 1 rubygems

Version Path
rubytext-0.1.22 lib/color.rb
rubytext-0.1.21 lib/color.rb
rubytext-0.1.20 lib/color.rb
rubytext-0.1.19 lib/color.rb
rubytext-0.1.18 lib/color.rb
rubytext-0.1.17 lib/color.rb
rubytext-0.1.16 lib/color.rb
rubytext-0.1.15 lib/color.rb
rubytext-0.1.14 lib/color.rb
rubytext-0.1.13 lib/color.rb
rubytext-0.1.12 lib/color.rb
rubytext-0.1.11 lib/color.rb
rubytext-0.1.10 lib/color.rb
rubytext-0.1.8 lib/color.rb
rubytext-0.1.7 lib/color.rb
rubytext-0.1.6 lib/color.rb
rubytext-0.1.5 lib/color.rb