class Chamomile::Renderer
FPS-throttled terminal renderer with alt-screen diff, inline, and full modes.
Constants
- ALT_SCREEN_OFF
- ALT_SCREEN_ON
- CLEAR_SCREEN
- CURSOR_HOME
- HIDE_CURSOR
- SHOW_CURSOR
- SYNC_END
- SYNC_START
Attributes
Public Class Methods
Source
# File lib/chamomile/renderer.rb, line 17 def initialize(output: $stdout, fps: 60) @output = output @alt_screen = false @inline = false @width = 80 @height = 24 @prev_lines = [] @inline_lines = 0 @mutex = Mutex.new @fps = fps @min_interval = fps.positive? ? 1.0 / fps : 0 @last_render = 0.0 @pending_view = nil @timer_thread = nil end
Public Instance Methods
Source
# File lib/chamomile/renderer.rb, line 111 def apply_cursor_shape(shape) code = case shape when :block then 2 when :underline then 4 when :bar then 6 when :blinking_block then 1 when :blinking_underline then 3 when :blinking_bar then 5 else 0 # default end @output.write("\e[#{code} q") end
Source
# File lib/chamomile/renderer.rb, line 80 def clear @output.write(CLEAR_SCREEN) @output.flush end
Source
# File lib/chamomile/renderer.rb, line 33 def enter_alt_screen @output.write(ALT_SCREEN_ON) @alt_screen = true @inline = false end
Source
# File lib/chamomile/renderer.rb, line 44 def enter_inline_mode @inline = true @inline_lines = 0 end
Source
# File lib/chamomile/renderer.rb, line 39 def exit_alt_screen @output.write(ALT_SCREEN_OFF) @alt_screen = false end
Source
# File lib/chamomile/renderer.rb, line 129 def force_render(view_output) view_string = resolve_view(view_output) @mutex.synchronize do flush_render(view_string) @last_render = Process.clock_gettime(Process::CLOCK_MONOTONIC) @pending_view = nil cancel_timer end end
Force an immediate render, bypassing FPS throttle
Source
# File lib/chamomile/renderer.rb, line 49 def hide_cursor @output.write(HIDE_CURSOR) end
Source
# File lib/chamomile/renderer.rb, line 107 def move_cursor(row, col) @output.write("\e[#{row};#{col}H") end
Source
# File lib/chamomile/renderer.rb, line 85 def println(str) @output.puts(str) end
Source
# File lib/chamomile/renderer.rb, line 90 def println_above(str) @mutex.synchronize do if @inline && @inline_lines.positive? buf = +"" buf << "\e[#{@inline_lines}A" # Move up buf << "\e[L" # Insert line buf << str buf << "\n" buf << "\e[#{@inline_lines}B" # Move back down @output.write(buf) @output.flush else @output.puts(str) end end end
Print a line above the rendered area (for inline mode logging, etc.)
Source
# File lib/chamomile/renderer.rb, line 62 def render(view_output) view_string = resolve_view(view_output) @mutex.synchronize do now = Process.clock_gettime(Process::CLOCK_MONOTONIC) elapsed = now - @last_render if elapsed >= @min_interval flush_render(view_string) @last_render = now @pending_view = nil cancel_timer else @pending_view = view_string schedule_timer(@min_interval - elapsed) end end end
Source
# File lib/chamomile/renderer.rb, line 57 def resize(width, height) @width = width @height = height end
Source
# File lib/chamomile/renderer.rb, line 53 def show_cursor @output.write(SHOW_CURSOR) end
Source
# File lib/chamomile/renderer.rb, line 139 def stop @mutex.synchronize do cancel_timer end end
Source
# File lib/chamomile/renderer.rb, line 124 def write_window_title(title) @output.write("\e]2;#{title}\a") end