Class: LowFrame

Inherits:
Object
  • Object
show all
Defined in:
lib/support/low_frame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(renderer:, fps: 10, show_output: true) ⇒ LowFrame

Returns a new instance of LowFrame.



8
9
10
11
12
13
14
15
16
17
# File 'lib/support/low_frame.rb', line 8

def initialize(renderer:, fps: 10, show_output: true)
  @renderer = renderer
  @show_output = show_output

  # Millisecond duration of each frame. We lose a small amount of precision dropping the decimal.
  @frame_time = ((1.0 / fps) * 1000).to_i
  @last_frame = nil

  setup if renderer && show_output
end

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer.



6
7
8
# File 'lib/support/low_frame.rb', line 6

def renderer
  @renderer
end

#screen_sizeObject (readonly)

Returns the value of attribute screen_size.



6
7
8
# File 'lib/support/low_frame.rb', line 6

def screen_size
  @screen_size
end

Instance Method Details

#renderObject



19
20
21
22
23
24
# File 'lib/support/low_frame.rb', line 19

def render
  return unless @last_frame.nil? || (current_timestamp - @last_frame) >= @frame_time

  @last_frame = current_timestamp
  @renderer.render(screen_size: @screen_size)
end

#resizeObject



43
44
45
46
# File 'lib/support/low_frame.rb', line 43

def resize
  row_count, column_count = IO.console.winsize
  @screen_size = { row_count:, column_count: }
end

#setupObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/support/low_frame.rb', line 26

def setup
  print "\e[?25l" # Hide cursor.
  system 'clear'

  resize

  Signal.trap('WINCH') do
    resize
  end

  Signal.trap('INT') do
    print "\e[?25h\e[0m" # Show cursor and reset colors.
    system 'clear'
    exit
  end
end