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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_rich/live.rb', line 78

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
  @event_queue = Queue.new
  @action_queue = Queue.new
  @event_thread = nil
  @params = {}
  if (log_path = ENV["RUBY_RICH_LOG"]).to_s.strip != ""
    FileUtils.mkdir_p(File.dirname(log_path))
    RubyRich.logger = Logger.new(log_path)
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



48
49
50
# File 'lib/ruby_rich/live.rb', line 48

def app
  @app
end

#layoutObject

Returns the value of attribute layout.



48
49
50
# File 'lib/ruby_rich/live.rb', line 48

def layout
  @layout
end

#listeningObject

Returns the value of attribute listening.



48
49
50
# File 'lib/ruby_rich/live.rb', line 48

def listening
  @listening
end

#mouseObject

Returns the value of attribute mouse.



76
77
78
# File 'lib/ruby_rich/live.rb', line 76

def mouse
  @mouse
end

#paramsObject

Returns the value of attribute params.



48
49
50
# File 'lib/ruby_rich/live.rb', line 48

def params
  @params
end

Class Method Details

.start(layout, refresh_rate: 30, mouse: false, alt_screen: false, autowrap: false, &proc) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_rich/live.rb', line 50

def start(layout, refresh_rate: 30, mouse: false, alt_screen: false, autowrap: false, &proc)
  setup_terminal(mouse: mouse, alt_screen: alt_screen, autowrap: autowrap)
  live = new(layout, refresh_rate)
  live.mouse = mouse
  proc.call(live) if proc
  live.run(proc)
rescue Interrupt
  live&.stop
rescue => e
  puts e.message
ensure
  live&.shutdown
  restore_terminal(mouse: mouse, alt_screen: alt_screen)
end

Instance Method Details

#find_layout(name) ⇒ Object



136
137
138
# File 'lib/ruby_rich/live.rb', line 136

def find_layout(name)
  @layout[name]
end

#find_panel(name) ⇒ Object



140
141
142
# File 'lib/ruby_rich/live.rb', line 140

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

#move_cursor(x, y) ⇒ Object



132
133
134
# File 'lib/ruby_rich/live.rb', line 132

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

#post(&block) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/ruby_rich/live.rb', line 111

def post(&block)
  return false unless block
  return false unless @running

  @action_queue << block
  true
end

#run(proc = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ruby_rich/live.rb', line 97

def run(proc = nil)
  start_event_thread if @listening
  while @running
    drain_action_queue
    break unless @running

    render_frame
    drain_event_queue if @listening
    sleep 1.0 / @refresh_rate
  end
rescue Interrupt
  @running = false
end

#shutdownObject



125
126
127
128
129
130
# File 'lib/ruby_rich/live.rb', line 125

def shutdown
  if @event_thread&.alive?
    @event_thread.kill
    @event_thread = nil
  end
end

#stopObject



119
120
121
122
123
# File 'lib/ruby_rich/live.rb', line 119

def stop
  @running = false
  shutdown
  RubyRich::Terminal.clear
end