Skip to content

Commit

Permalink
command completion support
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday committed Oct 23, 2015
1 parent def7726 commit b38220d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions exe/formatron
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ require 'formatron/generators/bootstrap/cli'
require 'formatron/generators/instance/cli'
require 'formatron/configuration/deploy_cli'
require 'formatron/configuration/destroy_cli'
require 'formatron/completion/completion_script_cli'
require 'formatron/completion/complete_cli'

Formatron::CLI.include Formatron::Generators::Credentials::CLI
Formatron::CLI.include Formatron::Generators::Bootstrap::CLI
Formatron::CLI.include Formatron::Generators::Instance::CLI
Formatron::CLI.include Formatron::Configuration::DeployCLI
Formatron::CLI.include Formatron::Configuration::DestroyCLI
Formatron::CLI.include Formatron::Completion::CompletionScriptCLI
Formatron::CLI.include Formatron::Completion::CompleteCLI
cli = Formatron::CLI.new
cli.run
48 changes: 48 additions & 0 deletions lib/formatron/completion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Formatron
# command completion utilities
module Completion
def self.normalize_commands(commands)
commands.map { |command| command.split.join ' ' }
end

def self.filter_commands(entered, commands)
commands.select! do |command|
command.start_with? entered
end
end

def self.shift_commands(entered, current_word, commands)
length = entered.split.length
commands.map! do |command|
components = command.split
if length > 0
components.shift(length - 1)
components.shift if current_word.eql? ''
end
components[0]
end
end

def self.complete(entered, current_word, commands)
commands = normalize_commands commands
filter_commands entered, commands
shift_commands entered, current_word, commands
commands.uniq.join ' '
end

# rubocop:disable Metrics/LineLength
def self.completion_script(prefix)
<<-EOH.gsub(/^ {8}/, '')
_formatron_complete() {
COMPREPLY=()
local word="${COMP_WORDS[COMP_CWORD]}"
local completions=$(#{prefix} ${COMP_WORDS[0]} complete "${COMP_LINE}" "$word")
COMPREPLY=( $(compgen -W "$completions" -- "$word") )
}
complete -F _formatron_complete formatron
EOH
end
# rubocop:enable Metrics/LineLength
end
end
30 changes: 30 additions & 0 deletions lib/formatron/completion/complete_cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative '../completion'

module Formatron
module Completion
# CLI command for completion
module CompleteCLI
def complete_action(c)
c.action do |args|
entered = args[0].split
entered.shift
entered.shift if entered[0].eql? 'help'
entered = entered.join ' '
current_word = args[1] || ''
print Completion.complete entered, current_word, defined_commands.keys
end
end

def complete_formatron_command
command :complete do |c|
c.syntax = 'formatron complete CURRENT_LINE CURRENT_WORD'
c.summary = 'check the supplied line and word and suggest ' \
'a list of command completions'
c.description = 'check the supplied line and word and suggest ' \
'a list of command completions'
complete_action c
end
end
end
end
end
25 changes: 25 additions & 0 deletions lib/formatron/completion/completion_script_cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require_relative '../completion'

module Formatron
module Completion
# CLI command for completion enabling script
module CompletionScriptCLI
def completion_script_action(c)
c.action do |args|
print Completion.completion_script args[0]
end
end

def completion_script_formatron_command
command :'completion-script' do |c|
c.syntax = 'formatron completion-script PREFIX'
c.summary = 'Print a bash completion script to ' \
'enable command completion'
c.description = 'Print a bash completion script to ' \
'enable command completion'
completion_script_action c
end
end
end
end
end

0 comments on commit b38220d

Please sign in to comment.