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, #views

Instance Method Summary collapse

Methods inherited from ModalSession

#clamp_height, #clamp_width, #close, #handle_resize, #max_height, #max_width, #rebuild_windows!

Methods inherited from Session

#add_view, #close, #handle_resize, #inspect, #persist!, #resolve_action, #tick, #update

Methods included from Actionable

included

Constructor Details

#initialize(initial: "", prompt: "> ", title: "Prompt", message: nil, kind: nil, history_ctx: nil, history_path: :default) ⇒ 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)
  super(keymap: Keymaps.emacs, views: [])
  @title = title&.to_s
  @message = message&.to_s
  @kind = kind&.to_sym

  @history = Fatty::History.for_path(history_path)
  @field = Fatty::InputField.new(
    prompt: prompt,
    history: @history,
    history_kind: :prompt,
    history_ctx: history_ctx,
  )
  @field.buffer.replace(initial.to_s)

  @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

#geometry(cols:, rows:) ⇒ Object

Return the outer width and height of the window for this modal, including any padding and borders.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fatty/session/prompt_session.rb', line 100

def geometry(cols:, rows:)
  max_w = max_width(
    cols: cols,
    margin: PROMPT_POPUP_MARGIN,
    min_width: PROMPT_POPUP_MIN_WIDTH,
  )
  max_h = max_height(rows: rows, margin: PROMPT_POPUP_MARGIN, min_height: 5)

  preferred_w = [(cols * 2 / 3).floor, 50].max

  message_width = @message.to_s.length
  prompt_width = @field.prompt_text.to_s.length + @field.buffer.text.to_s.length
  content_width = [message_width, prompt_width].max + 4

  width = clamp_width(
    [preferred_w, content_width].max,
    max_width: max_w,
    hard_max: PROMPT_POPUP_MAX_WIDTH,
  )

  extra_rows = 2
  extra_rows += 1 if @message && !@message.empty?
  height = clamp_height(
    extra_rows,
    max_height: max_h,
    min_height: 4,
  )

  [width, height]
end

#handle_action(action, args, event:) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fatty/session/prompt_session.rb', line 85

def handle_action(action, args, event:)
  env = action_env(event: event)

  result =
    with_virtual_suffix_sync do
      @field.act_on(action, *args, env: env)
    end
  result.is_a?(Array) ? result : []
rescue ActionError => e
  Fatty.error("PromptSession#handle_action: ActionError #{e.message}", tag: :session)
  []
end

#idObject



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

def id = :prompt

#init(terminal:) ⇒ Object

Framework and Session Hooks



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

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

#keymap_contextsObject



42
43
44
# File 'lib/fatty/session/prompt_session.rb', line 42

def keymap_contexts
  [:prompt, :text, :terminal]
end

#view(screen:, renderer:) ⇒ Object



46
47
48
49
50
# File 'lib/fatty/session/prompt_session.rb', line 46

def view(screen:, renderer:)
  return unless @win

  renderer.render_prompt_popup(session: self)
end

#with_virtual_suffix_syncObject



131
132
133
134
135
136
# File 'lib/fatty/session/prompt_session.rb', line 131

def with_virtual_suffix_sync
  @field.sync_virtual_suffix!
  result = yield
  @field.sync_virtual_suffix!
  result
end