Class: Fatty::Session
- Inherits:
-
Object
- Object
- Fatty::Session
- Includes:
- Actionable
- Defined in:
- lib/fatty/session.rb
Overview
Base class for stateful runtime components.
Charm/Bubbletea-style contract:
init(terminal:) => commands update(message) => commands view(screen:, renderer:, terminal:) => renders only (no return contract)
Where commands is an Array (possibly empty). Terminal is responsible for
executing commands after each update cycle.
Direct Known Subclasses
AlertSession, ISearchSession, InputSession, KeyTestSession, ModalSession, OutputSession, SearchSession
Instance Attribute Summary collapse
-
#counter ⇒ Object
readonly
Returns the value of attribute counter.
-
#keymap ⇒ Object
readonly
Returns the value of attribute keymap.
-
#terminal ⇒ Object
readonly
Returns the value of attribute terminal.
-
#views ⇒ Object
readonly
Returns the value of attribute views.
Instance Method Summary collapse
- #add_view(view) ⇒ Object
- #close ⇒ Object
-
#handle_action(_action, _args, event:) ⇒ Object
Subclasses override this to react to resolved actions.
- #handle_resize ⇒ Object
-
#init(terminal:) ⇒ Object
Called once when the session becomes active (e.g. pushed).
-
#initialize(keymap: nil, views: []) ⇒ Session
constructor
A new instance of Session.
- #inspect ⇒ Object
-
#keymap_contexts ⇒ Object
Subclasses can override to vary contexts dynamically (paging/isearch/popup/etc).
-
#persist! ⇒ Object
Save any state we want saved on quit, error, etc.
- #resolve_action(ev) ⇒ Object
- #tick ⇒ Object
-
#update(message) ⇒ Object
Handle a message and return commands.
-
#view(screen:, renderer:) ⇒ Object
Render the session.
Methods included from Actionable
Constructor Details
Instance Attribute Details
#counter ⇒ Object (readonly)
Returns the value of attribute counter.
17 18 19 |
# File 'lib/fatty/session.rb', line 17 def counter @counter end |
#keymap ⇒ Object (readonly)
Returns the value of attribute keymap.
17 18 19 |
# File 'lib/fatty/session.rb', line 17 def keymap @keymap end |
#terminal ⇒ Object (readonly)
Returns the value of attribute terminal.
17 18 19 |
# File 'lib/fatty/session.rb', line 17 def terminal @terminal end |
#views ⇒ Object (readonly)
Returns the value of attribute views.
17 18 19 |
# File 'lib/fatty/session.rb', line 17 def views @views end |
Instance Method Details
#add_view(view) ⇒ Object
29 30 31 32 |
# File 'lib/fatty/session.rb', line 29 def add_view(view) @views << view view end |
#close ⇒ Object
122 123 124 |
# File 'lib/fatty/session.rb', line 122 def close nil end |
#handle_action(_action, _args, event:) ⇒ Object
Subclasses override this to react to resolved actions.
93 94 95 |
# File 'lib/fatty/session.rb', line 93 def handle_action(_action, _args, event:) [] end |
#handle_resize ⇒ Object
126 127 128 |
# File 'lib/fatty/session.rb', line 126 def handle_resize [] end |
#init(terminal:) ⇒ Object
Called once when the session becomes active (e.g. pushed). Subclasses may override to kick off timers/async work, etc.
36 37 38 39 |
# File 'lib/fatty/session.rb', line 36 def init(terminal:) @terminal = terminal [] end |
#inspect ⇒ Object
25 26 27 |
# File 'lib/fatty/session.rb', line 25 def inspect "#{self.class.name}:#{object_id}" end |
#keymap_contexts ⇒ Object
Subclasses can override to vary contexts dynamically (paging/isearch/popup/etc). Must return an Array of symbols in precedence order.
88 89 90 |
# File 'lib/fatty/session.rb', line 88 def keymap_contexts [:input] end |
#persist! ⇒ Object
Save any state we want saved on quit, error, etc.
72 73 |
# File 'lib/fatty/session.rb', line 72 def persist! end |
#resolve_action(ev) ⇒ Object
79 80 81 82 83 |
# File 'lib/fatty/session.rb', line 79 def resolve_action(ev) return [nil, []] unless keymap keymap.resolve_action(ev, contexts: keymap_contexts) end |
#tick ⇒ Object
75 76 77 |
# File 'lib/fatty/session.rb', line 75 def tick false end |
#update(message) ⇒ Object
Handle a message and return commands.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fatty/session.rb', line 42 def update() Fatty.debug("#{self.class}#update(message -> #{})", tag: :session) commands = case [0] when :key ev = [1] action, args = resolve_action(ev) Fatty.debug( "#{self.class}#update: key ev=#{ev.inspect} action=#{action.inspect} args=#{args.inspect}", tag: :session, ) if action handle_action(action, args, event: ev) else update_key(ev) end when :cmd Fatty.debug("#{self.class}#update: cmd message=#{.inspect}", tag: :session) update_cmd([1], [2]) else Fatty.warn("#{self.class}#update: unknown message[0]=#{[0].inspect}", tag: :session) [] end commands end |
#view(screen:, renderer:) ⇒ Object
Render the session.
By default, renders all views belonging to the session, ordered by z-index. Subclasses can override, but should not mutate state here.
116 117 118 119 120 |
# File 'lib/fatty/session.rb', line 116 def view(screen:, renderer:) views.sort_by(&:z).each do |v| v.render(screen:, renderer:, terminal:, session: self) end end |