Class: CliPix::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_pix/image.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  autoscale: true,
  size: nil,
  flop: false,
  flip: false,
  mode: :color,
  preprocess: false
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(image, options = {}) ⇒ Image

Returns a new instance of Image.



36
37
38
39
# File 'lib/cli_pix/image.rb', line 36

def initialize(image, options = {})
  @image = image
  @options = DEFAULT_OPTIONS.merge options
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



34
35
36
# File 'lib/cli_pix/image.rb', line 34

def image
  @image
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/cli_pix/image.rb', line 34

def options
  @options
end

Class Method Details

.from_blobObject



23
24
25
26
# File 'lib/cli_pix/image.rb', line 23

def read(blob, options = {})
  image = MiniMagick::Image.read(blob)
  self.new(image, options)
end

.open(url, options = {}) ⇒ Object Also known as: from_url, from_file



25
26
27
28
# File 'lib/cli_pix/image.rb', line 25

def open(url, options = {})
  image = MiniMagick::Image.open(url)
  self.new(image, options)
end

.read(blob, options = {}) ⇒ Object



19
20
21
22
# File 'lib/cli_pix/image.rb', line 19

def read(blob, options = {})
  image = MiniMagick::Image.read(blob)
  self.new(image, options)
end

Instance Method Details

#animate(framerate = 0.2) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/cli_pix/image.rb', line 49

def animate(framerate = 0.2)
  self.read do |text|
    # clear terminal
    clear_screen! 
    puts text
    # wait
    sleep framerate
  end
end

#displayObject



41
42
43
44
45
46
47
# File 'lib/cli_pix/image.rb', line 41

def display
  self.read do |text|
    # clear terminal
    clear_screen!
    puts text
  end
end

#readObject

yields each text frame to the block



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cli_pix/image.rb', line 60

def read
  process_image!
  if options[:preprocess]
    # | / - \  
    spinner = "|/-\\"
    text_frames = image.frames.map.with_index do |frame, index|
      print "Loading frame #{index + 1}/#{image.frames.length}... #{spinner[index % 4]} \r"
      if options[:mode] == :color
        convert_to_color(frame.get_pixels)
      else
        convert_to_ascii(frame.get_pixels)
      end
    end

    text_frames.each do |text_frame|
      yield text_frame
    end
  else
    image.frames.each do |frame|
      if options[:mode] == :color
        yield convert_to_color(frame.get_pixels)
      else
        yield convert_to_ascii(frame.get_pixels)
      end
    end
  end
end