Class: WindowBlessing::Buffer

Inherits:
Object
  • Object
show all
Includes:
Tools
Defined in:
lib/window_blessing/buffer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools

#buffer, #clone_value, #color, #fill_line, #gen_array2d, #gray_screen_color, #log, #overlapping_span, #overlay2d, #overlay_span, #range_length, #resize2d, #rgb_screen_color, #subarray2d, #window

Constructor Details

#initialize(size, options = {}) ⇒ Buffer

init-options

:bg_bufer => 2D array of 0-255 values
:fg_bufer => 2D array of 0-255 values
:contents => array of strings or string with new-lines

fill options (will override init-options)

:string, :bg, :fg -- see #fill


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/window_blessing/buffer.rb', line 34

def initialize(size, options={})
  @size = size

  @contents  = options[:contents]
  @fg_buffer = options[:fg_buffer]
  @bg_buffer = options[:bg_buffer]

  fill options
  normalize
  clean
end

Instance Attribute Details

#bgObject

used as the default color when resizing



17
18
19
# File 'lib/window_blessing/buffer.rb', line 17

def bg
  @bg
end

#bg_bufferObject

color buffers. 2D arrays. Each element is a number, 0-255.



20
21
22
# File 'lib/window_blessing/buffer.rb', line 20

def bg_buffer
  @bg_buffer
end

#contentsObject

Returns the value of attribute contents.



14
15
16
# File 'lib/window_blessing/buffer.rb', line 14

def contents
  @contents
end

#crop_areaObject

Returns the value of attribute crop_area.



22
23
24
# File 'lib/window_blessing/buffer.rb', line 22

def crop_area
  @crop_area
end

#dirty_areaObject (readonly)

Returns the value of attribute dirty_area.



23
24
25
# File 'lib/window_blessing/buffer.rb', line 23

def dirty_area
  @dirty_area
end

#fgObject

used as the default color when resizing



17
18
19
# File 'lib/window_blessing/buffer.rb', line 17

def fg
  @fg
end

#fg_bufferObject

color buffers. 2D arrays. Each element is a number, 0-255.



20
21
22
# File 'lib/window_blessing/buffer.rb', line 20

def fg_buffer
  @fg_buffer
end

#sizeObject (readonly)

Returns the value of attribute size.



13
14
15
# File 'lib/window_blessing/buffer.rb', line 13

def size
  @size
end

Class Method Details

.default_bgObject



25
# File 'lib/window_blessing/buffer.rb', line 25

def Buffer.default_bg; 0; end

.default_fgObject



26
# File 'lib/window_blessing/buffer.rb', line 26

def Buffer.default_fg; 7; end

Instance Method Details

#cleanObject



145
146
147
# File 'lib/window_blessing/buffer.rb', line 145

def clean
  @dirty_area = nil
end

#clearObject

DRAWING



159
160
161
162
163
# File 'lib/window_blessing/buffer.rb', line 159

def clear
  @contents = @bg_buffer = @fg_buffer = nil
  normalize
  self
end

#cropped(area) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/window_blessing/buffer.rb', line 95

def cropped(area)
  old_crop_area = @crop_area
  @crop_area = area | crop_area
  yield
  self
ensure
  @crop_area = old_crop_area
end

#cropped?Boolean

Returns:

  • (Boolean)


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

def cropped?
  crop_area != rect(size)
end

#dirty(area = internal_area) ⇒ Object



149
150
151
152
153
# File 'lib/window_blessing/buffer.rb', line 149

def dirty(area = internal_area)
  @dirty_area = (area & @dirty_area) | internal_area
  @on_dirty.call if @on_dirty
  @dirty_area
end

#dirty?Boolean

dirty?

Returns:

  • (Boolean)


141
142
143
# File 'lib/window_blessing/buffer.rb', line 141

def dirty?
  !!@dirty_area
end

#dirty_subbufferObject



134
135
136
# File 'lib/window_blessing/buffer.rb', line 134

def dirty_subbuffer
  @dirty_area && subbuffer(@dirty_area)
end

#draw_buffer(loc, buffer, source_area = nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/window_blessing/buffer.rb', line 208

def draw_buffer(loc, buffer, source_area = nil)
  source_area = buffer.internal_area | source_area | (crop_area - loc)
  return unless source_area.present?

  unless source_area == buffer.internal_area
    loc += source_area.loc
    buffer = buffer.subbuffer(source_area)
  end

  @contents  = overlay2d(loc, buffer.contents, contents)
  @fg_buffer = overlay2d(loc, buffer.fg_buffer, fg_buffer)
  @bg_buffer = overlay2d(loc, buffer.bg_buffer, bg_buffer)

  dirty rect(loc, buffer.size)
  self
end

#draw_rect(rectangle, options = {}) ⇒ Object

Just like #fill except all buffers are filled. Default values are used if not specified. options - see #fill options



204
205
206
# File 'lib/window_blessing/buffer.rb', line 204

def draw_rect(rectangle, options={})
  fill({:area => rectangle, :string => " ", :fg => Buffer.default_fg, :bg => Buffer.default_bg}.merge options)
end

#each_line(&block) ⇒ Object



46
47
48
# File 'lib/window_blessing/buffer.rb', line 46

def each_line(&block)
  @contents.zip(fg_buffer,bg_buffer).each &block
end

#fill(options = {}) ⇒ Object

Fills characters, foreground and/or background buffers with the specified values.

If one of the buffers is not specified to be filled, it is not changed.

For example, you can set the foreground color without changing the text:

fill :fg => rgb_screen_color(1,0,0)

options

:area => Rectangle # only fill the specified area (intersected with the crop_area)
:bg => background color OR 1d array of bg-color pattern - nil => don't touch bg
:fg => foreground color OR 1d array of fg-color pattern - nil => don't touch fb
:string => string - length 1 or more, use to fill-init @contents - nil => don't touch @contents


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/window_blessing/buffer.rb', line 178

def fill(options = {})
  area = crop_area
  area = area | options[:area] if options[:area]
  string = options[:string]
  fg = options[:fg]
  bg = options[:bg]
  fg = fg.to_screen_color if fg.kind_of?(Color)
  bg = bg.to_screen_color if bg.kind_of?(Color)

  return if area.size==point

  if area != internal_area
    @contents  = overlay2d(area.loc, gen_array2d(area.size, string), contents)  if string
    @fg_buffer = overlay2d(area.loc, gen_array2d(area.size, fg),     fg_buffer) if fg
    @bg_buffer = overlay2d(area.loc, gen_array2d(area.size, bg),     bg_buffer) if bg
  else
    @contents  = gen_array2d(size, string) if string
    @fg_buffer = gen_array2d(size, fg)     if fg
    @bg_buffer = gen_array2d(size, bg)     if bg
  end
  dirty area
  self
end

#inspectObject



108
109
110
# File 'lib/window_blessing/buffer.rb', line 108

def inspect
  "<Buffer size:#{size.inspect}>"
end

#internal_areaObject



116
117
118
# File 'lib/window_blessing/buffer.rb', line 116

def internal_area
  rect(size)
end

#normalize(range = 0..-1,, options = {}) ⇒ Object

options

:fg - color
:bg - color


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/window_blessing/buffer.rb', line 72

def normalize(range=0..-1, options={})
  ranged_size = size.clone
  ranged_size.y = range_length(range) || size.y
  @contents||=[]
  @fg_buffer||=[]
  @bg_buffer||=[]
  if @contents.kind_of?(String)
    @contents = @contents.split("\n")
    sanitize_contents
  end
  @contents[range]  = resize2d @contents[range] , ranged_size, " "
  @fg_buffer[range] = resize2d @fg_buffer[range], ranged_size, options[:fg] || Buffer.default_fg
  @bg_buffer[range] = resize2d @bg_buffer[range], ranged_size, options[:bg] || Buffer.default_bg
end

#on_dirty(&block) ⇒ Object



87
88
89
# File 'lib/window_blessing/buffer.rb', line 87

def on_dirty(&block)
  @on_dirty = block
end

#sanitize_contents(range = 0..-1)) ⇒ Object



65
66
67
# File 'lib/window_blessing/buffer.rb', line 65

def sanitize_contents(range=0..-1)
  @contents[range] = @contents[range].collect {|l| l.gsub(/[\x00-\x1f]/,'?')}
end

#subbuffer(area) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/window_blessing/buffer.rb', line 120

def subbuffer(area)
  area = internal_area | area
  return buffer unless area.present?

  x_range = area.x_range

  buffer area.size,
    :contents  => subarray2d(contents,area),
    :fg_buffer => subarray2d(fg_buffer,area),
    :bg_buffer => subarray2d(bg_buffer,area),
    :fg => fg,
    :bg => bg
end

#to_sObject



112
113
114
# File 'lib/window_blessing/buffer.rb', line 112

def to_s
  contents.join "\n"
end