Class: Rich::Live
- Inherits:
-
Object
- Object
- Rich::Live
- Defined in:
- lib/rich/layout.rb
Overview
Live updating display
Constant Summary collapse
- DEFAULT_REFRESH =
Default refresh rate (Windows is slower)
Gem.win_platform? ? 0.2 : 0.1
Instance Attribute Summary collapse
-
#console ⇒ Console
readonly
Console for output.
-
#refresh_rate ⇒ Float
readonly
Refresh rate.
-
#transient ⇒ Boolean
readonly
Transient (clear on exit).
Instance Method Summary collapse
-
#initialize(console: nil, refresh_rate: DEFAULT_REFRESH, transient: false) ⇒ Live
constructor
A new instance of Live.
-
#refresh ⇒ Object
Refresh display if needed.
-
#start { ... } ⇒ Object
Start live display.
-
#stop ⇒ Object
Stop live display.
-
#update(renderable) ⇒ Object
Update the renderable content.
Constructor Details
#initialize(console: nil, refresh_rate: DEFAULT_REFRESH, transient: false) ⇒ Live
Returns a new instance of Live.
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rich/layout.rb', line 170 def initialize( console: nil, refresh_rate: DEFAULT_REFRESH, transient: false ) @console = console || Console.new @refresh_rate = refresh_rate @transient = transient @renderable = nil @started = false @lines_rendered = 0 @last_render = nil end |
Instance Attribute Details
#console ⇒ Console (readonly)
Returns Console for output.
159 160 161 |
# File 'lib/rich/layout.rb', line 159 def console @console end |
#refresh_rate ⇒ Float (readonly)
Returns Refresh rate.
162 163 164 |
# File 'lib/rich/layout.rb', line 162 def refresh_rate @refresh_rate end |
#transient ⇒ Boolean (readonly)
Returns Transient (clear on exit).
165 166 167 |
# File 'lib/rich/layout.rb', line 165 def transient @transient end |
Instance Method Details
#refresh ⇒ Object
Refresh display if needed
220 221 222 223 224 225 226 227 228 229 |
# File 'lib/rich/layout.rb', line 220 def refresh return unless @started return unless @renderable now = Time.now return if @last_render && now - @last_render < @refresh_rate render_update @last_render = now end |
#start { ... } ⇒ Object
Start live display
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/rich/layout.rb', line 193 def start @started = true @console.hide_cursor if block_given? begin yield self ensure stop end end end |
#stop ⇒ Object
Stop live display
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rich/layout.rb', line 207 def stop return unless @started if @transient && @lines_rendered > 0 # Clear rendered content @console.write("\e[#{@lines_rendered}A\e[J") end @console.show_cursor @started = false end |
#update(renderable) ⇒ Object
Update the renderable content
186 187 188 189 |
# File 'lib/rich/layout.rb', line 186 def update(renderable) @renderable = renderable refresh end |