Module: RubyCoded::Plugins::CommandCompletion::StateExtension

Defined in:
lib/ruby_coded/plugins/command_completion/state_extension.rb

Overview

Mixed into Chat::State to add command-completion tracking.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 8

def self.included(base)
  base.attr_reader :command_completion_index
end

Instance Method Details

#accept_command_completion!Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 52

def accept_command_completion!
  suggestion = current_command_suggestion
  return unless suggestion

  cmd, = suggestion
  @input_buffer.clear
  @input_buffer << cmd
  @cursor_position = @input_buffer.length
  @command_completion_index = 0
end

#command_completion_active?Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 16

def command_completion_active?
  buf = @input_buffer
  buf.start_with?("/") && !buf.include?(" ") && !command_suggestions.empty?
end

#command_completion_downObject



45
46
47
48
49
50
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 45

def command_completion_down
  suggestions = command_suggestions
  return if suggestions.empty?

  @command_completion_index = (@command_completion_index + 1) % suggestions.size
end

#command_completion_upObject



38
39
40
41
42
43
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 38

def command_completion_up
  suggestions = command_suggestions
  return if suggestions.empty?

  @command_completion_index = (@command_completion_index - 1) % suggestions.size
end

#command_suggestionsObject

Returns an array of [command, description] pairs matching the current input buffer prefix.



23
24
25
26
27
28
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 23

def command_suggestions
  prefix = @input_buffer.downcase
  all_descriptions = merged_command_descriptions
  all_descriptions.select { |cmd, _| cmd.downcase.start_with?(prefix) }
                  .sort_by { |cmd, _| cmd.downcase }
end

#current_command_suggestionObject



30
31
32
33
34
35
36
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 30

def current_command_suggestion
  suggestions = command_suggestions
  return nil if suggestions.empty?

  idx = @command_completion_index % suggestions.size
  suggestions[idx]
end

#init_command_completionObject



12
13
14
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 12

def init_command_completion
  @command_completion_index = 0
end

#reset_command_completion_indexObject

Reset index when the buffer changes so selection stays coherent.



64
65
66
# File 'lib/ruby_coded/plugins/command_completion/state_extension.rb', line 64

def reset_command_completion_index
  @command_completion_index = 0
end