Class: RubyRich::Live

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/live.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout, refresh_rate) ⇒ Live

Returns a new instance of Live.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby_rich/live.rb', line 63

def initialize(layout, refresh_rate)
  @layout = layout
  @layout.live = self
  @refresh_rate = refresh_rate
  @running = true
  @last_frame = Time.now
  @cursor = TTY::Cursor
  @render = CacheRender.new
  @console = RubyRich::Console.new
  @params = {}
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



37
38
39
# File 'lib/ruby_rich/live.rb', line 37

def app
  @app
end

#layoutObject

Returns the value of attribute layout.



37
38
39
# File 'lib/ruby_rich/live.rb', line 37

def layout
  @layout
end

#listeningObject

Returns the value of attribute listening.



37
38
39
# File 'lib/ruby_rich/live.rb', line 37

def listening
  @listening
end

#paramsObject

Returns the value of attribute params.



37
38
39
# File 'lib/ruby_rich/live.rb', line 37

def params
  @params
end

Class Method Details

.start(layout, refresh_rate: 30, &proc) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_rich/live.rb', line 39

def start(layout, refresh_rate: 30, &proc)
  setup_terminal
  live = new(layout, refresh_rate)
  proc.call(live) if proc
  live.run(proc)
rescue => e
  puts e.message
ensure
  restore_terminal
end

Instance Method Details

#find_layout(name) ⇒ Object



95
96
97
# File 'lib/ruby_rich/live.rb', line 95

def find_layout(name)
  @layout[name]
end

#find_panel(name) ⇒ Object



99
100
101
# File 'lib/ruby_rich/live.rb', line 99

def find_panel(name)
  @layout[name].content
end

#move_cursor(x, y) ⇒ Object



91
92
93
# File 'lib/ruby_rich/live.rb', line 91

def move_cursor(x,y)
  print @cursor.move_to(x, y)
end

#run(proc = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_rich/live.rb', line 75

def run(proc = nil)
  while @running
    render_frame
    if @listening
      event_data = @console.get_key()
      @layout.notify_listeners(event_data)
    end
    sleep 1.0 / @refresh_rate
  end
end

#stopObject



86
87
88
89
# File 'lib/ruby_rich/live.rb', line 86

def stop
  @running = false
  system("clear")
end