Class: Muby::Style::ColorPair

Inherits:
Object
  • Object
show all
Defined in:
lib/muby/style.rb

Constant Summary collapse

@@instances =
[]
@@initialized =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ ColorPair

Returns a new instance of ColorPair.



46
47
48
49
# File 'lib/muby/style.rb', line 46

def initialize(number)
  @number = number
  @used_at = Time.new
end

Instance Attribute Details

#bgObject

Returns the value of attribute bg.



44
45
46
# File 'lib/muby/style.rb', line 44

def bg
  @bg
end

#fgObject

Returns the value of attribute fg.



44
45
46
# File 'lib/muby/style.rb', line 44

def fg
  @fg
end

#numberObject

Returns the value of attribute number.



44
45
46
# File 'lib/muby/style.rb', line 44

def number
  @number
end

#used_atObject

Returns the value of attribute used_at.



44
45
46
# File 'lib/muby/style.rb', line 44

def used_at
  @used_at
end

Class Method Details

.get_pair(fg, bg) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/muby/style.rb', line 24

def self.get_pair(fg, bg)
  init_pairs
  
  found = @@instances.find do |color|
    color.bg == bg && color.fg == fg
  end

  found = @@instances.find do |color|
    color.bg.nil? && color.fg.nil?
  end unless found
  
  found = @@instances.sort do |c1, c2|
    c1.used_at <=> c2.used_at
  end.first unless found
  
  found.use(fg, bg)
  
  found
end

.init_pairsObject



16
17
18
19
20
21
22
23
# File 'lib/muby/style.rb', line 16

def self.init_pairs
  unless @@initialized
    1.upto(Ncurses.COLOR_PAIRS - 1) do |n|
      @@instances << ColorPair.new(n)
    end
	  @@initialized = true
  end
end

Instance Method Details

#to_iObject



51
52
53
# File 'lib/muby/style.rb', line 51

def to_i
  Ncurses.COLOR_PAIR(@number)
end

#use(fg, bg) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/muby/style.rb', line 55

def use(fg, bg)
  @used_at = Time.new
  if (bg != @bg || fg != @fg)
    @bg = bg
    @fg = fg
    Ncurses.init_pair(@number, @fg, @bg)
  end
end