Module: Rubycom::Completions
- Defined in:
- lib/rubycom/completions.rb
Class Method Summary collapse
-
.register_completions(base) ⇒ String
Inserts a tab completion into the current user’s .bash_profile with a command entry to register the function for the current running ruby file.
-
.tab_complete(base, arguments, command_plugin) ⇒ Array
Discovers a list of possible matches to the given arguments Intended for use with bash tab completion.
Class Method Details
.register_completions(base) ⇒ String
Inserts a tab completion into the current user’s .bash_profile with a command entry to register the function for the current running ruby file
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rubycom/completions.rb', line 40 def self.register_completions(base) completion_function = "\n _\#{base}_complete() {\n COMPREPLY=()\n local completions=\"$(ruby \#{File.absolute_path($0)} tab_complete ${COMP_WORDS[*]} 2>/dev/null)\"\n COMPREPLY=( $(compgen -W \"$completions\") )\n }\n complete -o bashdefault -o default -o nospace -F _\#{base}_complete \#{$0.split('/').last}\n END\n\n already_registered = File.readlines(\"\#{Dir.home}/.bash_profile\").map { |line| line.include?(\"_\#{base}_complete()\") }.reduce(:|) rescue false\n if already_registered\n \"Completion function for \#{base} already registered.\"\n else\n File.open(\"\#{Dir.home}/.bash_profile\", 'a+') { |file|\n file.write(completion_function)\n }\n \"Registration complete, run 'source \#{Dir.home}/.bash_profile' to enable auto-completion.\"\n end\nend\n".gsub(/^ {6}/, '') |
.tab_complete(base, arguments, command_plugin) ⇒ Array
Discovers a list of possible matches to the given arguments Intended for use with bash tab completion
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rubycom/completions.rb', line 11 def self.tab_complete(base, arguments, command_plugin) return [] unless base.class == Module return [] unless command_plugin.class == Module arguments = [] if arguments.nil? args = (arguments.include?('tab_complete')) ? arguments[2..-1] : arguments matches = %w() if args.nil? || args.empty? matches = command_plugin.get_commands(base, false)[base.to_s.to_sym].map { |sym,_| sym.to_s } elsif args.length == 1 matches = command_plugin.get_commands(base, false)[base.to_s.to_sym].map { |sym,_| sym.to_s }.select { |word| !word.match(/^#{args[0]}/).nil? } if matches.size == 1 && matches[0] == args[0] matches = self.tab_complete(Kernel.const_get(args[0].to_sym), args[1..-1], command_plugin) end elsif args.length > 1 begin matches = self.tab_complete(Kernel.const_get(args[0].to_sym), args[1..-1], command_plugin) rescue Exception matches = %w() end end unless base.nil? matches = %w() if matches.nil? || matches.include?(args[0]) matches end |