Class: Fatty::PromptSession

Inherits:
ModalSession show all
Defined in:
lib/fatty/session/prompt_session.rb

Constant Summary collapse

PROMPT_POPUP_MAX_WIDTH =
120
PROMPT_POPUP_MIN_WIDTH =
20
PROMPT_POPUP_MARGIN =
2

Instance Attribute Summary collapse

Attributes inherited from ModalSession

#win

Attributes inherited from Session

#counter, #keymap, #terminal

Instance Method Summary collapse

Methods inherited from ModalSession

#close, #handle_resize

Methods inherited from Session

#close, #handle_resize, #persist!, #renderer, #screen, #tick

Methods included from Actionable

included

Constructor Details

#initialize(initial: "", prompt: "> ", title: "Prompt", message: nil, kind: nil, history_ctx: nil, history_path: :default, history: nil, save_history: true) ⇒ PromptSession

Returns a new instance of PromptSession.



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

def initialize(initial: "", prompt: "> ", title: "Prompt", message: nil, kind: nil,
               history_ctx: nil, history_path: :default, history: nil, save_history: true)
  super(keymap: Keymaps.emacs)
  @title = title&.to_s
  @message = message&.to_s
  @kind = kind&.to_sym
  @save_history = !!save_history
  @history = history || Fatty::History.for_path(history_path)
  @field = Fatty::InputField.new(
    prompt: prompt,
    history_kind: :prompt,
    history: @history,
    history_ctx: history_ctx,
  )
  @field.buffer.replace(initial.to_s)
  @field.buffer.bol
  @win = nil
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



5
6
7
# File 'lib/fatty/session/prompt_session.rb', line 5

def field
  @field
end

#historyObject (readonly)

Returns the value of attribute history.



5
6
7
# File 'lib/fatty/session/prompt_session.rb', line 5

def history
  @history
end

#messageObject (readonly)

Returns the value of attribute message.



5
6
7
# File 'lib/fatty/session/prompt_session.rb', line 5

def message
  @message
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/fatty/session/prompt_session.rb', line 5

def title
  @title
end

Instance Method Details

#idObject



11
# File 'lib/fatty/session/prompt_session.rb', line 11

def id = :prompt

#init(terminal:) ⇒ Object

Session Protocol



36
37
38
39
40
# File 'lib/fatty/session/prompt_session.rb', line 36

def init(terminal:)
  super
  rebuild_windows!
  []
end

#stateObject

Other public API methods



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fatty/session/prompt_session.rb', line 111

def state
  [
    title.to_s.dup.freeze,
    message.to_s.dup.freeze,
    field.prompt_text.to_s.dup.freeze,
    field.buffer.text.to_s.dup.freeze,
    field.buffer.cursor,
    field.buffer.virtual_suffix.to_s.dup.freeze,
    renderer.screen.rows,
    renderer.screen.cols,
  ]
end

#update(command) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fatty/session/prompt_session.rb', line 42

def update(command)
  commands =
    case command.action
    when :key
      ev = command.payload.fetch(:event)
      action, args = resolve_action(ev)
      if action
        normalize_action_result(apply_action(action, args, event: ev))
      else
        []
      end
    when :terminal_paste
      field.act_on(:paste, command.payload.fetch(:text, ""), env: action_env(event: nil))
      []
    else
      []
    end
  Array(commands)
end

#viewObject



62
63
64
65
66
# File 'lib/fatty/session/prompt_session.rb', line 62

def view
  return unless @win

  renderer.render_prompt_popup(session: self)
end