Class: Fatty::ShellSession

Inherits:
Session
  • Object
show all
Defined in:
lib/fatty/session/shell_session.rb

Instance Attribute Summary collapse

Attributes inherited from Session

#counter, #id, #keymap, #terminal

Instance Method Summary collapse

Methods inherited from Session

#close, #handle_resize, #renderer, #screen

Methods included from Actionable

included

Constructor Details

#initialize(id: nil, prompt: "sh> ", on_accept: nil, completion_proc: nil, history_ctx: nil, history_path: :default) ⇒ ShellSession

Returns a new instance of ShellSession.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fatty/session/shell_session.rb', line 11

def initialize(id: nil, prompt: "sh> ", on_accept: nil, completion_proc: nil, history_ctx: nil, history_path: :default)
  keymap = Keymaps.emacs
  super(
    id: :shell,
    keymap: keymap,
  )
  @history = Fatty::History.for_path(history_path)
  @output_session = OutputSession.new(keymap: keymap, history: @history)
  @field = Fatty::InputField.new(
    prompt: prompt,
    history: @history,
    completion_proc: completion_proc,
    history_kind: :command,
    history_ctx: history_ctx,
  )
  @on_accept = on_accept
  @completion_proc = completion_proc
  @completion_range = nil
  @completion_path = nil
  @completion_popup_prefix = nil
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



9
10
11
# File 'lib/fatty/session/shell_session.rb', line 9

def field
  @field
end

#historyObject (readonly)

Returns the value of attribute history.



9
10
11
# File 'lib/fatty/session/shell_session.rb', line 9

def history
  @history
end

#on_acceptObject (readonly)

Returns the value of attribute on_accept.



9
10
11
# File 'lib/fatty/session/shell_session.rb', line 9

def on_accept
  @on_accept
end

#output_sessionObject (readonly)

Returns the value of attribute output_session.



9
10
11
# File 'lib/fatty/session/shell_session.rb', line 9

def output_session
  @output_session
end

Instance Method Details

#cursor_visible?Boolean

Returns:

  • (Boolean)


305
306
307
# File 'lib/fatty/session/shell_session.rb', line 305

def cursor_visible?
  !input_suppressed?
end

#init(terminal:) ⇒ Object

Session Protocol



37
38
39
40
41
42
43
44
# File 'lib/fatty/session/shell_session.rb', line 37

def init(terminal:)
  super
  output_session.init(terminal: terminal)
  [
    Command.terminal(:register_session, session: output_session),
    Command.session(output_session.id, :resize),
  ]
end

#pager_active?Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/fatty/session/shell_session.rb', line 301

def pager_active?
  output_session.pager_active?
end

#persist!Object

Save any state we want saved on quit, error, etc.



286
287
288
289
290
291
292
293
# File 'lib/fatty/session/shell_session.rb', line 286

def persist!
  return unless @history.respond_to?(:save!)

  Fatty.debug("ShellSession#persist!: saving history", tag: :history)
  @history.save!
rescue => e
  Fatty.error("ShellSession#persist!: failed to save history: #{e.class}: #{e.message}", tag: :history)
end

#tickObject

Called by Terminal#go on every loop iteration. Returns true if any visible state changed (dirty).



297
298
299
# File 'lib/fatty/session/shell_session.rb', line 297

def tick
  output_session.tick
end

#update(command) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fatty/session/shell_session.rb', line 46

def update(command)
  log_update(command)
  commands =
    case command.action
    when :terminal_paste
      field.act_on(:paste, command.payload.fetch(:text, ""), env: action_env(event: nil))
      []
    when :resize
      Command.session(output_session.id, :resize)
    when :popup_result
      apply_popup_result(command.payload)
    when :paste
      text = command.payload.fetch(:text, "").to_s
      env = action_env(event: nil)
      field.act_on(:paste, text, env: env)
      []
    when :popup_tab
      apply_completion_popup_tab(command.payload)
    when :popup_backtab
      apply_completion_popup_backtab(command.payload)
    when :key
      ev = command.payload.fetch(:event)
      if output_session.pager_active?
        Command.session(output_session.id, :key, event: ev)
      elsif ev.is_a?(Fatty::KeyEvent)
        action, args = resolve_action(ev)
        if action
          apply_action(action, args, event: ev)
        else
          case ev.key
          when :enter, :return
            apply_action(:submit_line, [], event: ev)
          else
            unrecognized_key_alert(ev)
          end
        end
      else
        []
      end
    else
      []
    end
  Array(commands)
end

#viewObject



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fatty/session/shell_session.rb', line 91

def view
  Fatty.debug(
    "ShellSession#view output_session=" \
      "#{output_session.class}:#{output_session.object_id}",
    tag: :render,
  )
  output_session.view
  if input_suppressed?
    renderer.clear_input_field
  else
    renderer.render_input_field(field)
  end
end