Skip to content

Commit 41f0a42

Browse files
authored
Merge pull request #726 from bashly-framework/refactor/completion-settings
Refactor completions settings
2 parents f16450c + f3e6505 commit 41f0a42

25 files changed

Lines changed: 208 additions & 61 deletions

File tree

‎examples/completions-advanced/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ commands:
7474
## `settings.yml`
7575

7676
````yaml
77-
enable_completions: always
77+
completions: full
7878

7979
````
8080

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enable_completions: always
1+
completions: full

‎examples/completions/README.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ commands:
5959
## `settings.yml`
6060

6161
````yaml
62-
enable_completions: always
62+
completions: full
6363

6464
````
6565

@@ -104,5 +104,3 @@ download
104104

105105

106106
````
107-
108-

‎examples/completions/settings.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
enable_completions: always
1+
completions: full

‎lib/bashly/libraries/settings/settings.yml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,20 @@ env: development
158158
# - never # do not render this feature
159159
enable_header_comment: always
160160
enable_bash_version_bouncer: always
161-
enable_completions: never
162161
enable_view_markers: development
163162
enable_inspect_args: development
164163
enable_deps_array: always
165164
enable_env_var_names_array: always
166165
enable_sourcing: development
167166

167+
# Generate native runtime completions. Supported values:
168+
# - ~ or false # disable completions (default)
169+
# - minimal # generate the runtime completion engine without shell adapters
170+
# - bash or zsh # generate the runtime engine and one shell adapter
171+
# - bash,zsh # generate the runtime engine and selected shell adapters
172+
# - full # generate the runtime engine and all available shell adapters
173+
completions: ~
174+
168175

169176
#-------------------------------------------------------------------------------
170177
# DEVELOPER OPTIONS

‎lib/bashly/settings.rb‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
module Bashly
22
class Settings
3+
COMPLETION_SHELLS = %w[bash zsh].freeze
4+
35
class << self
46
include AssetHelper
57

68
attr_writer(
79
:argfile_var,
810
:commands_dir,
911
:compact_short_flags,
12+
:completions,
1013
:conjoined_flag_args,
1114
:config_path,
1215
:enable_bash_version_bouncer,
13-
:enable_completions,
1416
:enable_deps_array,
1517
:enable_env_var_names_array,
1618
:enable_header_comment,
@@ -51,6 +53,28 @@ def compact_short_flags
5153
@compact_short_flags ||= get :compact_short_flags
5254
end
5355

56+
def completions
57+
@completions ||= get :completions
58+
end
59+
60+
def completions?
61+
completions == 'minimal' || completion_shells.any?
62+
end
63+
64+
def completion_shells
65+
value = completions
66+
return [] if value.nil? || value == false || value == 'minimal'
67+
return COMPLETION_SHELLS if value == 'full'
68+
69+
shells = value.split(',', -1).map(&:strip) if value.is_a? String
70+
valid = shells&.any? && shells.all? { |shell| COMPLETION_SHELLS.include? shell }
71+
unique = shells&.uniq == shells
72+
return shells if valid && unique
73+
74+
raise ConfigurationError,
75+
"completions must be false, minimal, full, or a comma-separated list of: #{COMPLETION_SHELLS.join ', '}"
76+
end
77+
5478
def conjoined_flag_args
5579
@conjoined_flag_args ||= get :conjoined_flag_args
5680
end
@@ -69,10 +93,6 @@ def enable_bash_version_bouncer
6993
@enable_bash_version_bouncer ||= get :enable_bash_version_bouncer
7094
end
7195

72-
def enable_completions
73-
@enable_completions ||= get :enable_completions
74-
end
75-
7696
def enable_deps_array
7797
@enable_deps_array ||= get :enable_deps_array
7898
end
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
= view_marker
22

3+
completion_shells = Settings.completion_shells
4+
35
> send_completions() {
4-
> local completion_shell="${1:-bash}"
6+
> local completion_shell="${1:-{{ completion_shells.first }}}"
57
>
68
> case "$completion_shell" in
7-
> bash) send_completions_bash ;;
8-
> zsh) send_completions_zsh ;;
9+
completion_shells.each do |shell|
10+
> {{ shell }}) send_completions_{{ shell }} ;;
11+
end
912
> *)
1013
> printf 'unsupported shell: %s\n' "$completion_shell" >&2
1114
> return 1
1215
> ;;
1316
> esac
1417
> }
1518
>
16-
= render :completion_script_bash
17-
= render :completion_script_zsh
19+
completion_shells.each do |shell|
20+
= render :"completion_script_#{shell}"
21+
end

‎lib/bashly/views/command/completions.gtx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
> done <<<"$completion_output"
7575
> }
7676
>
77-
= render :completion_script
77+
= render :completion_script if Settings.completion_shells.any?
7878
= render :completion_function
7979
deep_commands.each do |command|
8080
= command.render :completion_function

‎lib/bashly/views/command/inspect_args.gtx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
> local k
55
>
66
> if ((${#args[@]})); then
7-
> readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
7+
> readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | LC_ALL=C sort)
88
> echo args:
99
> for k in "${sorted_keys[@]}"; do
1010
> echo "- \${args[$k]} = ${args[$k]}"
@@ -28,7 +28,7 @@ end
2828

2929
if Settings.enabled? :deps_array
3030
> if ((${#deps[@]})); then
31-
> readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
31+
> readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | LC_ALL=C sort)
3232
> echo
3333
> echo deps:
3434
> for k in "${sorted_keys[@]}"; do
@@ -40,7 +40,7 @@ end
4040

4141
if Settings.enabled? :env_var_names_array
4242
> if ((${#env_var_names[@]})); then
43-
> readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort)
43+
> readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | LC_ALL=C sort)
4444
> echo
4545
> echo "environment variables:"
4646
> for k in "${sorted_names[@]}"; do

‎lib/bashly/views/command/master_script.gtx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
= render :user_lib if user_lib.any?
99
= render :command_functions
1010
= render :parse_requirements
11-
= render :completions if Settings.enabled? :completions
11+
= render :completions if Settings.completions?
1212
= render :user_hooks
1313
= render :initialize
1414
= render :run

0 commit comments

Comments
 (0)