Module: Fatty::PromptApi

Included in:
CallbackEnvironment
Defined in:
lib/fatty/api/prompt.rb

Instance Method Summary collapse

Instance Method Details

#prompt(prompt, initial: "", cancel_value: nil, history_key: nil, save_history: true) ⇒ Object

Create a popup to ask the user to enter an arbitrary string. These prompts will keep their own history based on the history_key, or if not history_key is given, the prompt text. If save_history is set to false, no history will be recorded, which can suppress writing sensitive values such as passwords to the history file.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
70
71
72
73
74
75
# File 'lib/fatty/api/prompt.rb', line 10

def prompt(prompt, initial: "", cancel_value: nil, history_key: nil, save_history: true)
  history_ctx = lambda do
    base =
      if @history_ctx.respond_to?(:call)
        @history_ctx.call
      elsif @history_ctx.is_a?(Hash)
        @history_ctx
      else
        {}
      end

    base.merge(prompt: (history_key || prompt).to_s)
  end

  popup = Fatty::PromptSession.new(
    title: "Prompt",
    message: prompt,
    prompt: "> ",
    initial: initial,
    kind: :terminal_prompt,
    save_history: save_history,
    history_ctx: history_ctx,
  )
  done = false
  result = nil

  acc_proc = ->(payload) do
    result = payload[:text]
    done = true
  end

  cancel_proc = -> do
    result = cancel_value
    done = true
  end

  owner = Terminal::PopupOwner.new(on_result: acc_proc, on_cancel: cancel_proc)

  begin
    terminal.apply_command(
      Fatty::Command.terminal(:push_modal, session: popup, owner: owner)
    )
    terminal.render_frame

    while !done && terminal.running?
      dirty = false
      command = terminal.event_source.next_event

      if command
        terminal.apply_command(command)
        dirty = true
      end

      begin
        dirty ||= !!terminal.tick_active_session
      rescue StandardError => e
        Fatty.error("PromptApi#prompt tick failed: #{e.class}: #{e.message}", tag: :terminal)
        dirty = true
      end
      terminal.render_frame if dirty
    end
  ensure
    terminal.render_frame
  end
  result
end