Class: Legion::TTY::Components::SessionPicker

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/tty/components/session_picker.rb

Instance Method Summary collapse

Constructor Details

#initialize(session_store:) ⇒ SessionPicker

Returns a new instance of SessionPicker.



7
8
9
# File 'lib/legion/tty/components/session_picker.rb', line 7

def initialize(session_store:)
  @session_store = session_store
end

Instance Method Details

#select_with_prompt(output: $stdout) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/tty/components/session_picker.rb', line 11

def select_with_prompt(output: $stdout)
  sessions = @session_store.list
  return nil if sessions.empty?

  require 'tty-prompt'
  prompt = ::TTY::Prompt.new(output: output)
  choices = sessions.map do |s|
    { name: "#{s[:name]} (#{s[:message_count]} msgs, #{s[:saved_at]})", value: s[:name] }
  end
  choices << { name: '+ New session', value: :new }
  prompt.select('Select session:', choices, per_page: 10)
rescue ::TTY::Reader::InputInterrupt, Interrupt => e
  Legion::Logging.debug("session picker cancelled: #{e.message}") if defined?(Legion::Logging)
  nil
end