Class: GithubCLI::Command::Completion

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/github_cli/command/completion.rb

Constant Summary collapse

DEFAULT_INDENT =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#convert_value, #convert_values, #flatten_hash, #hash_without!, #longest_common_prefix, #pad, #truncate

Constructor Details

#initialize(command) ⇒ Completion

Returns a new instance of Completion


12
13
14
# File 'lib/github_cli/command/completion.rb', line 12

def initialize(command)
  @command = command
end

Instance Attribute Details

#commandObject

Command to be looked up.


10
11
12
# File 'lib/github_cli/command/completion.rb', line 10

def command
  @command
end

Instance Method Details

#find_command_possibilitiesObject

If there is more than one possible alternative, show all of them.


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/github_cli/command/completion.rb', line 20

def find_command_possibilities
  max_length = 1
  commands = Command.all.select do |cmd|
    prefix = Util.longest_common_prefix(command, cmd.namespace)
    if !prefix.empty? && (prefix.length >= max_length)
      max_length = prefix.length
      cmd
    end
  end
  commands.map(&:namespace).uniq.sort
end

#format_command_possibilities(options = {}) ⇒ Object

Options indent - Indent suggested commands by specified value.


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/github_cli/command/completion.rb', line 35

def format_command_possibilities(options={})
  possibilities = find_command_possibilities
  string = ""
  indent = options[:indent] || DEFAULT_INDENT
  if possibilities.size >= 1
    string << "\n\nDid you mean this?\n"
    possibilities.each do |possibility|
      string << " " * indent + "#{possibility}\n"
    end
  end
  string
end

#inspectObject

Returns a concise string representation of Completion instance


50
51
52
# File 'lib/github_cli/command/completion.rb', line 50

def inspect
  "#<#{self.class.inspect} @command=#{command.inspect}"
end