Class: Etna::GenerateCompletionScript
- Inherits:
-
Command
- Object
- Command
- Etna::GenerateCompletionScript
show all
- Defined in:
- lib/etna/generate_autocompletion_script.rb
Instance Attribute Summary
Attributes inherited from Command
#parent
Instance Method Summary
collapse
Methods inherited from Command
#completions, #fill_in_missing_params, #find_command, #initialize, parent_scope, #setup
#command_name, #completions_for, #desc, #flag_argspec, #flag_as_parameter, included, #parse_flags, #usage
Constructor Details
This class inherits a constructor from Etna::Command
Instance Method Details
#enable_flags(flags_container) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/etna/generate_autocompletion_script.rb', line 59
def enable_flags(flags_container)
boolean_flags = flags_container.boolean_flags
string_flags = flags_container.string_flags
flags = boolean_flags + string_flags
write %Q(all_flag_completion_names="$all_flag_completion_names #{flags.join(' ')} ")
write %Q(string_flag_completion_names="$string_flag_completion_names #{string_flags.join(' ')} ")
string_flags.each do |flag|
write %Q(declare _completions_for_#{flag_as_parameter(flag)}="#{completions_for(flag_as_parameter(flag)).join(' ')}")
end
end
|
#execute ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/etna/generate_autocompletion_script.rb', line 98
def execute
name = File.basename(program_name)
write <<-EOF
#!/usr/bin/env bash
function _#{name}_completions() {
_#{name}_inner_completions "${COMP_WORDS[@]:1:COMP_CWORD}"
}
function _#{name}_inner_completions() {
local all_flag_completion_names=''
local string_flag_completion_names=''
local all_completion_names=''
local i=''
local a=''
EOF
generate_for_scope(parent)
write <<-EOF
}
complete -o default -F _#{name}_completions #{name}
EOF
File.open("#{name}.completion", 'w') { |f| f.write(@script) }
end
|
#generate_flag_handling ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/etna/generate_autocompletion_script.rb', line 28
def generate_flag_handling
write %Q(elif [[ -z "$(echo $all_flag_completion_names | xargs)" ]]; then)
write "return"
write %Q(elif [[ "$all_flag_completion_names" =~ $1\\ ]]; then)
write %Q(all_flag_completion_names="${all_flag_completion_names//$1\\ /}")
write 'a=$1'
write 'shift'
write %Q(if [[ "$string_flag_completion_names" =~ $a\\ ]]; then)
write 'if [[ "$#" == "1" ]]; then'
write %Q(a="${a//--/}")
write %Q(a="${a//-/_}")
write %Q(i="_completions_for_$a")
write %Q(all_completion_names="${!i}")
write 'COMPREPLY=($(compgen -W "$all_completion_names" -- "$1"))'
write 'return'
write 'fi'
write 'shift'
write 'fi'
end
|
#generate_for_command(command) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/etna/generate_autocompletion_script.rb', line 8
def generate_for_command(command)
completions = command.completions
completions.each do |c|
generate_start_match(c, false)
write "fi"
write "shift"
end
enable_flags(command.class)
write 'while [[ "$#" != "0" ]]; do'
generate_start_match([])
generate_flag_handling
write "else"
write "return"
write 'fi'
write 'done'
write "return"
end
|
#generate_for_scope(scope) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/etna/generate_autocompletion_script.rb', line 71
def generate_for_scope(scope)
enable_flags(scope.class)
write 'while [[ "$#" != "0" ]]; do'
generate_start_match(scope.subcommands.keys)
scope.subcommands.each do |name, command|
write %Q(elif [[ "$1" == "#{name}" ]]; then)
write 'shift'
if command.class.included_modules.include?(CommandExecutor)
generate_for_scope(command)
else
generate_for_command(command)
end
end
generate_flag_handling
write "else"
write "return"
write "fi"
write 'done'
end
|
#generate_start_match(completions, include_flags = true) ⇒ Object
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/etna/generate_autocompletion_script.rb', line 48
def generate_start_match(completions, include_flags=true)
write 'if [[ "$#" == "1" ]]; then'
write %Q(all_completion_names="#{completions.join(' ')}")
write %Q(all_completion_names="$all_completion_names $all_flag_completion_names") if include_flags
write %Q(if [[ -z "$(echo $all_completion_names | xargs)" ]]; then)
write 'return'
write 'fi'
write 'COMPREPLY=($(compgen -W "$all_completion_names" -- "$1"))'
write 'return'
end
|
#program_name ⇒ Object
94
95
96
|
# File 'lib/etna/generate_autocompletion_script.rb', line 94
def program_name
$PROGRAM_NAME
end
|
#write(string) ⇒ Object
125
126
127
128
129
|
# File 'lib/etna/generate_autocompletion_script.rb', line 125
def write(string)
@script ||= ""
@script << string
@script << "\n"
end
|