Skip to content

Commit e0f4dab

Browse files
committed
refactor completions library
1 parent a78b4c9 commit e0f4dab

File tree

5 files changed

+62
-47
lines changed

5 files changed

+62
-47
lines changed

lib/bashly/commands/add.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ def comp_command
6262
case format
6363
when "script"
6464
path = output || "#{Settings.target_dir}/completions.bash"
65-
add_lib Library::Completions.new(path, format: format)
65+
add_lib Library::CompletionsScript.new(path)
6666

6767
when "function"
6868
function = output || "send_completions"
6969
path = "#{Settings.source_dir}/lib/#{function}.sh"
70-
add_lib Library::Completions.new(path, format: format, function: function)
70+
add_lib Library::CompletionsFunction.new(path, function: function)
7171

7272
when "yaml"
7373
path = output || "#{Settings.target_dir}/completions.yml"
74-
add_lib Library::Completions.new(path, format: format)
74+
add_lib Library::CompletionsYAML.new(path)
7575

7676
else
7777
raise Error, "Unrecognized format: #{format}"

lib/bashly/library/completions.rb

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,15 @@ module Bashly
22
module Library
33
class Completions < Base
44
def content
5-
{
6-
path: target_path,
7-
content: file_content
8-
}
9-
end
10-
11-
def post_install_message
12-
case format
13-
when 'script'
14-
<<~EOF
15-
In order to enable completions, run:
16-
17-
!txtpur!$ source #{target_path}
18-
EOF
19-
20-
when 'function'
21-
<<~EOF
22-
In order to enable completions in your script, create a command or a flag (for example: !txtgrn!#{command.name} completions!txtrst! or !txtgrn!#{command.name} --completions!txtrst!) that calls the !txtgrn!#{function_name}!txtrst! function.
23-
24-
Your users can then run something like this to enable completions:
25-
26-
!txtpur!$ eval \"$(#{command.name} completions)\"
27-
EOF
28-
29-
else
30-
<<~EOF
31-
This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem.
32-
EOF
33-
end
34-
end
35-
36-
private
37-
38-
def function_name
39-
options[:function]
40-
end
41-
42-
def format
43-
options[:format]
5+
{ path: target_path, content: file_content }
446
end
457

468
def file_content
47-
case format
48-
when 'script' then command.completion_script
49-
when 'function' then command.completion_function(options[:function])
50-
else completions.to_yaml
51-
end
9+
raise NotImplementedError, "Please implement #file_content"
5210
end
5311

12+
protected
13+
5414
def completions
5515
@completions ||= command.completion_data
5616
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Bashly
2+
module Library
3+
class CompletionsFunction < Completions
4+
def file_content
5+
command.completion_function function_name
6+
end
7+
8+
def post_install_message
9+
<<~EOF
10+
In order to enable completions in your script, create a command or a flag (for example: !txtgrn!#{command.name} completions!txtrst! or !txtgrn!#{command.name} --completions!txtrst!) that calls the !txtgrn!#{function_name}!txtrst! function.
11+
12+
Your users can then run something like this to enable completions:
13+
14+
!txtpur!$ eval \"$(#{command.name} completions)\"
15+
EOF
16+
end
17+
18+
def function_name
19+
options[:function]
20+
end
21+
end
22+
end
23+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Bashly
2+
module Library
3+
class CompletionsScript < Completions
4+
def file_content
5+
command.completion_script
6+
end
7+
8+
def post_install_message
9+
<<~EOF
10+
In order to enable completions, run:
11+
12+
!txtpur!$ source #{target_path}
13+
EOF
14+
end
15+
end
16+
end
17+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Bashly
2+
module Library
3+
class CompletionsYAML < Completions
4+
def file_content
5+
completions.to_yaml
6+
end
7+
8+
def post_install_message
9+
<<~EOF
10+
This file can be converted to a completions script using the !txtgrn!completely!txtrst! gem.
11+
EOF
12+
end
13+
end
14+
end
15+
end

0 commit comments

Comments
 (0)