-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
def7726
commit b38220d
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |