Module: WindowBlessing::Tools

Includes:
GuiGeo::Tools
Included in:
WindowBlessing, Buffer, Color, EventedVariable, Window, WindowRedrawAreas
Defined in:
lib/window_blessing/tools.rb

Instance Method Summary collapse

Instance Method Details

#buffer(*args) ⇒ Object



122
# File 'lib/window_blessing/tools.rb', line 122

def buffer(*args); WindowBlessing::Buffer.new *args end

#clone_value(v) ⇒ Object



19
20
21
22
23
24
# File 'lib/window_blessing/tools.rb', line 19

def clone_value(v)
  case v
  when Fixnum, Bignum, Float, NilClass then v
  else v.clone
  end
end

#color(*args) ⇒ Object



120
# File 'lib/window_blessing/tools.rb', line 120

def color(*args); WindowBlessing::Color.new *args end

#fill_line(fill, length) ⇒ Object



79
80
81
82
83
# File 'lib/window_blessing/tools.rb', line 79

def fill_line(fill, length)
  line = fill * (length/fill.length)
  line = (line+fill)[0..length-1] if line.length != length
  line
end

#gen_array2d(size, fill) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/window_blessing/tools.rb', line 85

def gen_array2d(size, fill)
  fill = case fill
  when String, Array then fill
  else [fill]
  end

  a = (size.x * size.y)
  full = fill * ((a / fill.length) + 1)

  if fill.kind_of?(String)
    full.scan /.{#{size.x}}/
  else
    full.each_slice(size.x).collect {|a|a}
  end[0..size.y-1]

end

#gray_screen_color(g) ⇒ Object

g is in 0..1



110
111
112
113
114
115
116
117
# File 'lib/window_blessing/tools.rb', line 110

def gray_screen_color(g)
  g = (g*24.9).to_i
  case g
  when 0 then 0
  when 24 then 15
  else 232 + g
  end
end

#log(str) ⇒ Object



119
# File 'lib/window_blessing/tools.rb', line 119

def log(str); XtermLog.log "#{self.inspect}: #{str}" end

#overlapping_span(pos, span, length) ⇒ Object

returns pos, span on return, pos is within 0..length and pos + span.length is <= length



9
10
11
12
13
14
15
16
17
# File 'lib/window_blessing/tools.rb', line 9

def overlapping_span(pos, span, length)
  if pos <= -span.length || pos >= length || length <= 0
    return length, span.class.new
  elsif pos < 0
    span = span[-pos..-1]
    pos = 0
  end
  return pos, span[0..(length - pos - 1)]
end

#overlay2d(loc, source, target) ⇒ Object



43
44
45
46
47
# File 'lib/window_blessing/tools.rb', line 43

def overlay2d(loc, source, target)
  overlay_span(loc.y, source, target) do |s, t|
    overlay_span loc.x, s, t
  end
end

#overlay_span(pos, source_span, target_span, &block) ⇒ Object

if the block is provided, yields the source elements and the target elements they are overlaying, in order, one at a time



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/window_blessing/tools.rb', line 27

def overlay_span(pos, source_span, target_span, &block)
  pos, span = overlapping_span pos, source_span, target_span.length

  return target_span if span.length == 0

  if block
    span = span.each_with_index.collect {|s,i| yield s, target_span[i+pos]}
  end

  if pos == 0
    span + target_span[span.length..-1]
  else
    target_span[0..pos-1] + span + target_span[pos + span.length..-1]
  end
end

#range_length(range) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/window_blessing/tools.rb', line 124

def range_length(range)
  if (range.last < 0 && range.first >=0) || (range.last >=0 && range.first < 0) # length depends on the string or array the range is applied to
    nil
  else
    l = range.last - range.first
    l += 1 unless range.exclude_end?
    l
  end
end

#resize2d(array2d, size, blank_element) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/window_blessing/tools.rb', line 49

def resize2d(array2d, size, blank_element)
  array2d ||= []
  blank_element = [blank_element] unless blank_element.kind_of?(String)

  if array2d.length != size.y
    array2d = array2d[0..(size.y-1)]
    blank_line = blank_element * size.x
    array2d += (size.y - array2d.length).times.collect {blank_line.clone}
  end

  array2d.collect do |line|
    if line.length!=size.x
      line = line[0..(size.x-1)]
      line + blank_element * (size.x - line.length)
    else
      line
    end
  end
end

#rgb_screen_color(r, g, b) ⇒ Object

r, g, b are in 0..1



104
105
106
107
# File 'lib/window_blessing/tools.rb', line 104

def rgb_screen_color(r, g, b)
  return gray_screen_color(r) if r==g && g==b
  16 + (r*5.9).to_i * 36 + (g*5.9).to_i * 6 + (b*5.9).to_i
end

#subarray2d(array2d, area) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/window_blessing/tools.rb', line 70

def subarray2d(array2d, area)
  size = point(array2d[0].length,array2d.length)
  area = area | rect(size)
  x_range = area.x_range
  array2d[area.y_range].collect do |line|
    line[x_range]
  end
end

#window(*args) ⇒ Object



121
# File 'lib/window_blessing/tools.rb', line 121

def window(*args); WindowBlessing::Window.new *args end