class RatatuiRuby::Event
Base class for all RatatuiRuby events.
Events represent terminal input: keyboard, mouse, resize, paste, focus changes. Returned by RatatuiRuby.poll_event. All events support Ruby 3.0+ pattern matching.
Event Types
-
Keyβ keyboard input -
Mouseβ mouse clicks, movement, wheel -
Resizeβ terminal resized -
Pasteβ clipboard paste -
FocusGainedβ terminal gained focus -
FocusLostβ terminal lost focus -
Noneβ no event available (Null Object)
Pattern Matching (Exhaustive)
Use case...in to dispatch on every possible event type. This ensures you handle every case without needing an else clause:
case RatatuiRuby.poll_event
in { type: :key, code: "q" }
break
in { type: :key, code: code, modifiers: }
handle_key(code, modifiers)
in { type: :mouse, kind: "down", x:, y: }
handle_click(x, y)
in { type: :mouse, kind:, x:, y: }
# handle other mouse activities
in { type: :resize, width:, height: }
handle_resize(width, height)
in { type: :paste, content: }
handle_paste(content)
in { type: :focus_gained }
handle_focus_gain
in { type: :focus_lost }
handle_focus_loss
in { type: :none }
# Idle
end
Predicates
Check event types with predicates without pattern matching:
event = RatatuiRuby.poll_event if event.key? puts "Key pressed" elsif event.none? # Idle elsif event.mouse? puts "Mouse event" end
Public Instance Methods
Source
# File lib/ratatui_ruby/event.rb, line 150 def deconstruct_keys(keys) {} end
Deconstructs the event for pattern matching.
Keys argument is unused but required by the protocol.
case event in type: :key, code: puts "Key: #{code}" end
Source
# File lib/ratatui_ruby/event.rb, line 105 def focus_gained? false end
Returns true if this is a FocusGained event.
Source
# File lib/ratatui_ruby/event.rb, line 110 def focus_lost? false end
Returns true if this is a FocusLost event.
Source
# File lib/ratatui_ruby/event.rb, line 85 def key? false end
Returns true if this is a Key event.
Source
# File lib/ratatui_ruby/event.rb, line 121 def method_missing(name, *args, **kwargs, &block) if name.to_s.end_with?("?") false else super end end
Responds to dynamic predicate methods for key checks. All non-Key events return false for any key predicate.
Source
# File lib/ratatui_ruby/event.rb, line 90 def mouse? false end
Returns true if this is a Mouse event.
Source
# File lib/ratatui_ruby/event.rb, line 80 def none? false end
Returns true if this is a None event.
Source
# File lib/ratatui_ruby/event.rb, line 100 def paste? false end
Returns true if this is a Paste event.
Source
# File lib/ratatui_ruby/event.rb, line 95 def resize? false end
Returns true if this is a Resize event.
Source
# File lib/ratatui_ruby/event.rb, line 130 def respond_to_missing?(name, *args) name.to_s.end_with?("?") || super end
Declares that this class responds to dynamic predicate methods.