Class: Branding::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/branding/canvas.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: 0, height: 0) ⇒ Canvas

Returns a new instance of Canvas.



15
16
17
18
19
20
# File 'lib/branding/canvas.rb', line 15

def initialize(width: 0, height: 0)
  @width = width
  @height = height
  @rows, @cols = self.class.terminal_size
  @pixel_buffer = []
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



5
6
7
# File 'lib/branding/canvas.rb', line 5

def cols
  @cols
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/branding/canvas.rb', line 5

def height
  @height
end

#rowsObject (readonly)

Returns the value of attribute rows.



5
6
7
# File 'lib/branding/canvas.rb', line 5

def rows
  @rows
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/branding/canvas.rb', line 5

def width
  @width
end

Class Method Details

.terminal_sizeObject



7
8
9
10
11
12
13
# File 'lib/branding/canvas.rb', line 7

def self.terminal_size
  # TODO: make sure we can get this on linux

  `stty size`.split.map(&:to_i)
rescue
  [40, 100]
end

Instance Method Details

#load(pixels, algo: :normal) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/branding/canvas.rb', line 22

def load(pixels, algo: :normal)
  case algo
  when :normal
    klass = Pixel
  when :hires
    klass = Pixel2x
  when :hicolor
    raise 'Hi-Color coming soon!'
  else
    raise "Unknown pixel algo `#{algo}`"
  end

  klass.load_strategy(pixels, width: width, height: height) do |pixel|
    @pixel_buffer << pixel
  end
end

#max_widthObject



49
50
51
# File 'lib/branding/canvas.rb', line 49

def max_width
  @max_width ||= [@width, @cols].min
end


39
40
41
42
43
44
45
46
47
# File 'lib/branding/canvas.rb', line 39

def print
  @pixel_buffer.each_with_index do |pixel, idx|
    next if (idx % width * pixel.width) >= cols

    STDOUT.puts(ANSI.reset) if idx % max_width == 0

    STDOUT.print(pixel)
  end
end