Class: Branding::Canvas
- Inherits:
-
Object
- Object
- Branding::Canvas
- Defined in:
- lib/branding/canvas.rb
Instance Attribute Summary collapse
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(width: 0, height: 0) ⇒ Canvas
constructor
A new instance of Canvas.
- #load(pixels, algo: :normal) ⇒ Object
- #max_width ⇒ Object
- #print ⇒ Object
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
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
5 6 7 |
# File 'lib/branding/canvas.rb', line 5 def cols @cols end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
5 6 7 |
# File 'lib/branding/canvas.rb', line 5 def height @height end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
5 6 7 |
# File 'lib/branding/canvas.rb', line 5 def rows @rows end |
#width ⇒ Object (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_size ⇒ Object
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_width ⇒ Object
49 50 51 |
# File 'lib/branding/canvas.rb', line 49 def max_width @max_width ||= [@width, @cols].min end |
#print ⇒ Object
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 |