Class: RatatuiRuby::Event
- Inherits:
-
Object
- Object
- RatatuiRuby::Event
- Defined in:
- lib/ratatui_ruby/event.rb,
lib/ratatui_ruby/event/key.rb,
lib/ratatui_ruby/event/none.rb,
lib/ratatui_ruby/event/sync.rb,
lib/ratatui_ruby/event/mouse.rb,
lib/ratatui_ruby/event/paste.rb,
lib/ratatui_ruby/event/resize.rb,
lib/ratatui_ruby/event/key/dwim.rb,
lib/ratatui_ruby/event/key/media.rb,
lib/ratatui_ruby/event/focus_lost.rb,
lib/ratatui_ruby/event/key/system.rb,
lib/ratatui_ruby/event/focus_gained.rb,
lib/ratatui_ruby/event/key/modifier.rb,
lib/ratatui_ruby/event/key/character.rb,
lib/ratatui_ruby/event/key/navigation.rb
Overview
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:
– SPDX-SnippetBegin SPDX-FileCopyrightText: 2025 Kerrick Long SPDX-License-Identifier: MIT-0 ++
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
– SPDX-SnippetEnd ++
Predicates
Check event types with predicates without pattern matching:
– SPDX-SnippetBegin SPDX-FileCopyrightText: 2025 Kerrick Long SPDX-License-Identifier: MIT-0 ++
event = RatatuiRuby.poll_event
if event.key?
puts "Key pressed"
elsif event.none?
# Idle
elsif event.mouse?
puts "Mouse event"
end
– SPDX-SnippetEnd ++
Defined Under Namespace
Classes: FocusGained, FocusLost, Key, Mouse, None, Paste, Resize, Sync
Instance Method Summary collapse
-
#deconstruct_keys(keys) ⇒ Object
Deconstructs the event for pattern matching.
-
#focus_gained? ⇒ Boolean
Returns true if this is a FocusGained event.
-
#focus_lost? ⇒ Boolean
Returns true if this is a FocusLost event.
-
#key? ⇒ Boolean
Returns true if this is a Key event.
-
#method_missing(name, *args, **kwargs, &block) ⇒ Object
Responds to dynamic predicate methods for key checks.
-
#mouse? ⇒ Boolean
Returns true if this is a Mouse event.
-
#none? ⇒ Boolean
Returns true if this is a None event.
-
#paste? ⇒ Boolean
Returns true if this is a Paste event.
-
#resize? ⇒ Boolean
Returns true if this is a Resize event.
-
#respond_to_missing?(name, *args) ⇒ Boolean
Declares that this class responds to dynamic predicate methods.
-
#sync? ⇒ Boolean
Returns true if this is a Sync event.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs, &block) ⇒ Object
Responds to dynamic predicate methods for key checks. All non-Key events return false for any key predicate.
121 122 123 124 125 126 127 |
# 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 |
Instance Method Details
#deconstruct_keys(keys) ⇒ Object
Deconstructs the event for pattern matching.
Keys argument is unused but required by the protocol.
– SPDX-SnippetBegin SPDX-FileCopyrightText: 2025 Kerrick Long SPDX-License-Identifier: MIT-0 ++
case event
in type: :key, code:
puts "Key: #{code}"
end
– SPDX-SnippetEnd ++
150 151 152 |
# File 'lib/ratatui_ruby/event.rb', line 150 def deconstruct_keys(keys) {} end |
#focus_gained? ⇒ Boolean
Returns true if this is a FocusGained event.
105 106 107 |
# File 'lib/ratatui_ruby/event.rb', line 105 def focus_gained? false end |
#focus_lost? ⇒ Boolean
Returns true if this is a FocusLost event.
110 111 112 |
# File 'lib/ratatui_ruby/event.rb', line 110 def focus_lost? false end |
#key? ⇒ Boolean
Returns true if this is a Key event.
85 86 87 |
# File 'lib/ratatui_ruby/event.rb', line 85 def key? false end |
#mouse? ⇒ Boolean
Returns true if this is a Mouse event.
90 91 92 |
# File 'lib/ratatui_ruby/event.rb', line 90 def mouse? false end |
#none? ⇒ Boolean
Returns true if this is a None event.
80 81 82 |
# File 'lib/ratatui_ruby/event.rb', line 80 def none? false end |
#paste? ⇒ Boolean
Returns true if this is a Paste event.
100 101 102 |
# File 'lib/ratatui_ruby/event.rb', line 100 def paste? false end |
#resize? ⇒ Boolean
Returns true if this is a Resize event.
95 96 97 |
# File 'lib/ratatui_ruby/event.rb', line 95 def resize? false end |
#respond_to_missing?(name, *args) ⇒ Boolean
Declares that this class responds to dynamic predicate methods.
130 131 132 |
# File 'lib/ratatui_ruby/event.rb', line 130 def respond_to_missing?(name, *args) name.to_s.end_with?("?") || super end |
#sync? ⇒ Boolean
Returns true if this is a Sync event.
115 116 117 |
# File 'lib/ratatui_ruby/event.rb', line 115 def sync? false end |