Class: Fatty::Session

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Actionable

included

Constructor Details

#initialize(keymap: nil, views: []) ⇒ Session

Returns a new instance of Session.



19
20
21
22
23
# File 'lib/fatty/session.rb', line 19

def initialize(keymap: nil, views: [])
  @keymap = keymap
  @views = Array(views)
  @counter = Counter.new
end

Instance Attribute Details

#counterObject (readonly)

Returns the value of attribute counter.



17
18
19
# File 'lib/fatty/session.rb', line 17

def counter
  @counter
end

#keymapObject (readonly)

Returns the value of attribute keymap.



17
18
19
# File 'lib/fatty/session.rb', line 17

def keymap
  @keymap
end

#terminalObject (readonly)

Returns the value of attribute terminal.



17
18
19
# File 'lib/fatty/session.rb', line 17

def terminal
  @terminal
end

#viewsObject (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

#closeObject



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_resizeObject



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

#inspectObject



25
26
27
# File 'lib/fatty/session.rb', line 25

def inspect
  "#{self.class.name}:#{object_id}"
end

#keymap_contextsObject

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

#tickObject



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(message)
  Fatty.debug("#{self.class}#update(message -> #{message})", tag: :session)

  commands =
    case message[0]
    when :key
      ev = message[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=#{message.inspect}", tag: :session)
      update_cmd(message[1], message[2])
    else
      Fatty.warn("#{self.class}#update: unknown message[0]=#{message[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