Class: Termin::ANSIColor::PPMReader

Inherits:
Object
  • Object
show all
Includes:
Termin::ANSIColor
Defined in:
lib/termin/ansicolor/ppm_reader.rb

Constant Summary

Constants included from Termin::ANSIColor

ATTRIBUTE_NAMES, COLORED_REGEXP, VERSION, VERSION_ARRAY, VERSION_BUILD, VERSION_MAJOR, VERSION_MINOR

Instance Method Summary collapse

Methods included from Termin::ANSIColor

attributes, #color, coloring=, coloring?, create_color_method, #on_color, #support?, term_ansicolor_attributes, #term_ansicolor_attributes, #uncolor

Constructor Details

#initialize(io, options = {}) ⇒ PPMReader

Returns a new instance of PPMReader.



6
7
8
9
10
# File 'lib/termin/ansicolor/ppm_reader.rb', line 6

def initialize(io, options = {})
  @io      = io
  @options = options
  @buffer  = ''
end

Instance Method Details

#each_rowObject



20
21
22
23
24
25
# File 'lib/termin/ansicolor/ppm_reader.rb', line 20

def each_row
  reset_io
  @height.times do
    yield parse_row
  end
end

#reset_ioObject



12
13
14
15
16
17
18
# File 'lib/termin/ansicolor/ppm_reader.rb', line 12

def reset_io
  begin
    @io.rewind
  rescue Errno::ESPIPE
  end
  parse_header
end

#to_aObject



27
28
29
# File 'lib/termin/ansicolor/ppm_reader.rb', line 27

def to_a
  enum_for(:each_row).to_a
end

#to_sObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/termin/ansicolor/ppm_reader.rb', line 31

def to_s
  result = ''
  each_row do |row|
    last_pixel = nil
    for pixel in row
      if pixel != last_pixel
        color = Attribute.nearest_rgb_color(pixel, @options)
        result << on_color(color)
        last_pixel = pixel
      end
      result << ' '
    end
    result << reset << "\n"
  end
  result
end