Class: Noir::Command::Completion

Inherits:
Base::TerminalCommand show all
Defined in:
lib/noir/command/completion.rb

Class Method Summary collapse

Methods inherited from Base::TerminalCommand

sub_commands

Methods inherited from Base::Command

check_command_not_found, description, sub_commands

Class Method Details

.execute(*args) ⇒ Object



7
8
9
# File 'lib/noir/command/completion.rb', line 7

def execute *args
  puts suggestions(args)
end

.suggestions(list) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/noir/command/completion.rb', line 11

def suggestions list
  if list.size.zero?
    return suggestions_from_command Noir::Command, nil
  end

  commands  = [:Noir, :Command] + list

  matched_commands = [commands.first]
  commands = commands.drop(1)

  while true
    consts = eval(matched_commands.map(&:to_s).join('::')).constants(true)

    break if commands.size.zero?
    matched_command = consts.find{|c| c.to_s.downcase == commands.first.to_s.downcase}
    break if matched_command.nil?

    matched_commands << matched_command
    commands = commands.drop(1)
  end

  command_class = eval(matched_commands.map(&:to_s).join('::'))
  suggestions_from_command(command_class, commands.first)
end

.suggestions_from_command(klass, pattern = nil) ⇒ Object



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

def suggestions_from_command klass, pattern = nil
  suggests = klass.sub_commands.map(&:to_s).map(&:downcase)

  unless pattern.nil?
    suggests = suggests.select{|c| c.start_with? pattern.downcase}
  end

  return suggests
end