Class: RubyRich::Live

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

Constant Summary collapse

RESIZE_POLL_INTERVAL =
0.25

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.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ruby_rich/live.rb', line 98

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
  @wake_mutex = Mutex.new
  @wake_condition = ConditionVariable.new
  @dirty = true
  @last_terminal_size = 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.



68
69
70
# File 'lib/ruby_rich/live.rb', line 68

def app
  @app
end

#layoutObject

Returns the value of attribute layout.



68
69
70
# File 'lib/ruby_rich/live.rb', line 68

def layout
  @layout
end

#listeningObject

Returns the value of attribute listening.



68
69
70
# File 'lib/ruby_rich/live.rb', line 68

def listening
  @listening
end

#mouseObject

Returns the value of attribute mouse.



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

def mouse
  @mouse
end

#paramsObject

Returns the value of attribute params.



68
69
70
# File 'lib/ruby_rich/live.rb', line 68

def params
  @params
end

Class Method Details

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruby_rich/live.rb', line 70

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



173
174
175
# File 'lib/ruby_rich/live.rb', line 173

def find_layout(name)
  @layout[name]
end

#find_panel(name) ⇒ Object



177
178
179
# File 'lib/ruby_rich/live.rb', line 177

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

#move_cursor(x, y) ⇒ Object



169
170
171
# File 'lib/ruby_rich/live.rb', line 169

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

#post(&block) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/ruby_rich/live.rb', line 138

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

  @action_queue << block
  wake
  true
end

#refreshObject



147
148
149
150
151
152
153
# File 'lib/ruby_rich/live.rb', line 147

def refresh
  return false unless @running

  mark_dirty
  wake
  true
end

#run(proc = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ruby_rich/live.rb', line 121

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

    event_processed = @listening ? drain_event_queue : false
    if consume_dirty || action_processed || event_processed || terminal_size_changed?
      render_frame
    else
      wait_for_activity
    end
  end
rescue Interrupt
  @running = false
end

#shutdownObject



162
163
164
165
166
167
# File 'lib/ruby_rich/live.rb', line 162

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

#stopObject



155
156
157
158
159
160
# File 'lib/ruby_rich/live.rb', line 155

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