completely 0.4.3 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 207f7cf5e1b259c6cb7e9006f59cbf22b2096456707ef8d42edddbc6b0dc4e4e
4
- data.tar.gz: 51e59317f307d3da3e09f7a30092970607000be5c7a11e4276e378dd0a41bb78
3
+ metadata.gz: 133a4cb7fec5b868e4c98e594bc6ffc228c25eee4b85a58801f03fc78d9b1cbf
4
+ data.tar.gz: 5073efb37aba0c9601875d05a9b9acce469d71d182f91c8729f18052bdcfc915
5
5
  SHA512:
6
- metadata.gz: 4eaaacc3785c367510802ad2945057d1ecc50fbfd8ddbcba08234a80b73c12adbe6157c0fd9726e9eb2a9f043470bccac9fdd45bea767edce94b4b47e0dc6912
7
- data.tar.gz: fc79892cbbe2647aa5647ea68d6fe29ef3b91b0686565e16ce9e6c784157539ab728dce24c4991234c1a1756facca1b0aa019c9654a1fa90b91a7d8476ea3de8
6
+ metadata.gz: 63029b5f2cfd9362a7daa0e0d940e5f296d42962c2d0e9b2bda2297fe0c95b5abd3e6c9029f6c3781932b7fa2d2e122b0672ca6708774c375bcb277db48e5cf3
7
+ data.tar.gz: 9ec01adf53afe6d2aaa14ca6a369754adb76ef0043a51663183807eee3d0266c74c18bfd2518ff2d5bf1a5ba04216e0ac9fe0af3a3435b3ee2816115570cc568
data/README.md CHANGED
@@ -32,7 +32,13 @@ or with homebrew:
32
32
  ```bash
33
33
  $ brew install brew-gem
34
34
  $ brew gem install completely
35
- ````
35
+ ```
36
+
37
+ or with Docker:
38
+
39
+ ```bash
40
+ $ alias completely='docker run --rm -it --user $(id -u):$(id -g) --volume "$PWD:/app" dannyben/completely'
41
+ ```
36
42
 
37
43
  ## Using the `completely` command line
38
44
 
@@ -91,6 +97,9 @@ Each pattern in this configuration file will be checked against the user's
91
97
  input, and if the input **starts with** a matching pattern, the list that
92
98
  follows it will be suggested as completions.
93
99
 
100
+ Note that the suggested completions will not show flags (string that start with
101
+ a hyphen `-`) unless the input ends with a hyphen.
102
+
94
103
  To generate the bash script, simply run:
95
104
 
96
105
  ```bash
data/bin/completely CHANGED
@@ -8,9 +8,8 @@ runner = Completely::CLI.runner
8
8
 
9
9
  begin
10
10
  exit runner.run ARGV
11
-
12
11
  rescue => e
13
12
  puts e.backtrace.reverse if ENV['DEBUG']
14
13
  say! "!undred!#{e.class}!txtrst!\n#{e.message}"
15
14
  exit 1
16
- end
15
+ end
@@ -9,8 +9,8 @@ module Completely
9
9
  class CLI
10
10
  def self.runner
11
11
  runner = MisterBin::Runner.new version: Completely::VERSION,
12
- header: "Completely - Bash Completions Generator",
13
- footer: "Run !txtpur!completely COMMAND --help!txtrst! for more information"
12
+ header: 'Completely - Bash Completions Generator',
13
+ footer: 'Run !txtpur!completely COMMAND --help!txtrst! for more information'
14
14
 
15
15
  runner.route 'init', to: Commands::Init
16
16
  runner.route 'preview', to: Commands::Preview
@@ -3,22 +3,21 @@ require 'mister_bin'
3
3
  module Completely
4
4
  module Commands
5
5
  class Base < MisterBin::Command
6
-
7
6
  class << self
8
7
  def param_config_path
9
- param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
8
+ param 'CONFIG_PATH', "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
10
9
  end
11
10
 
12
11
  def option_function
13
- option "-f --function NAME", "Modify the name of the function in the generated script"
12
+ option '-f --function NAME', 'Modify the name of the function in the generated script'
14
13
  end
15
14
 
16
15
  def environment_config_path
17
- environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]"
16
+ environment 'COMPLETELY_CONFIG_PATH', 'Path to a completely configuration file [default: completely.yaml]'
18
17
  end
19
18
 
20
19
  def environment_debug
21
- environment "COMPLETELY_DEBUG", "It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested"
20
+ environment 'COMPLETELY_DEBUG', 'It not empty, the generated script will include an additional debugging snippet that outputs the compline and current word to a text file when a completion is requested'
22
21
  end
23
22
  end
24
23
 
@@ -46,7 +45,7 @@ module Completely
46
45
 
47
46
  def syntax_warning
48
47
  say! "\n!txtred!WARNING:\nYour configuration is invalid."
49
- say! "!txtred!All patterns must start with the same word."
48
+ say! '!txtred!All patterns must start with the same word.'
50
49
  end
51
50
  end
52
51
  end
@@ -3,19 +3,19 @@ require 'completely/commands/base'
3
3
  module Completely
4
4
  module Commands
5
5
  class Generate < Base
6
- help "Generate the bash completion script to a file"
6
+ help 'Generate the bash completion script to a file'
7
7
 
8
- usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]"
9
- usage "completely generate (-h|--help)"
8
+ usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]'
9
+ usage 'completely generate (-h|--help)'
10
10
 
11
11
  option_function
12
- option "-w --wrap NAME", "Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script"
12
+ option '-w --wrap NAME', 'Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script'
13
13
 
14
14
  param_config_path
15
- param "OUTPUT_PATH", "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable"
15
+ param 'OUTPUT_PATH', "Path to the output bash script. When not provided, the name of the input file will be used with a .bash extension\nCan also be set by an environment variable"
16
16
 
17
17
  environment_config_path
18
- environment "COMPLETELY_OUTPUT_PATH", "Path to the output bash script"
18
+ environment 'COMPLETELY_OUTPUT_PATH', 'Path to the output bash script'
19
19
  environment_debug
20
20
 
21
21
  def run
@@ -31,7 +31,6 @@ module Completely
31
31
  def wrapper_function(wrapper_name)
32
32
  completions.wrapper_function wrapper_name
33
33
  end
34
-
35
34
  end
36
35
  end
37
36
  end
@@ -3,21 +3,19 @@ require 'completely/commands/base'
3
3
  module Completely
4
4
  module Commands
5
5
  class Init < Base
6
- help "Create a new sample YAML configuration file"
6
+ help 'Create a new sample YAML configuration file'
7
7
 
8
- usage "completely init [CONFIG_PATH]"
9
- usage "completely init (-h|--help)"
8
+ usage 'completely init [CONFIG_PATH]'
9
+ usage 'completely init (-h|--help)'
10
10
 
11
11
  param_config_path
12
12
  environment_config_path
13
13
 
14
14
  def run
15
- if File.exist? config_path
16
- raise "File already exists: #{config_path}"
17
- else
18
- File.write config_path, sample
19
- say "Saved !txtpur!#{config_path}"
20
- end
15
+ raise "File already exists: #{config_path}" if File.exist? config_path
16
+
17
+ File.write config_path, sample
18
+ say "Saved !txtpur!#{config_path}"
21
19
  end
22
20
 
23
21
  private
@@ -27,9 +25,8 @@ module Completely
27
25
  end
28
26
 
29
27
  def sample_path
30
- @sample_path ||= File.expand_path "../templates/sample.yaml", __dir__
28
+ @sample_path ||= File.expand_path '../templates/sample.yaml', __dir__
31
29
  end
32
-
33
30
  end
34
31
  end
35
32
  end
@@ -3,10 +3,10 @@ require 'completely/commands/base'
3
3
  module Completely
4
4
  module Commands
5
5
  class Preview < Base
6
- help "Generate the bash completion script to STDOUT"
6
+ help 'Generate the bash completion script to STDOUT'
7
7
 
8
- usage "completely preview [CONFIG_PATH --function NAME]"
9
- usage "completely preview (-h|--help)"
8
+ usage 'completely preview [CONFIG_PATH --function NAME]'
9
+ usage 'completely preview (-h|--help)'
10
10
 
11
11
  option_function
12
12
  param_config_path
@@ -3,29 +3,32 @@ require 'completely/commands/base'
3
3
  module Completely
4
4
  module Commands
5
5
  class Test < Base
6
- summary "Test completions"
6
+ summary 'Test completions'
7
7
 
8
- help "This command can be used to test that your completions script responds with the right completions. It works by reading your completely.yaml file, generating a completions script, and generating a temporary testing script."
8
+ help 'This command can be used to test that your completions script responds with the right completions. It works by reading your completely.yaml file, generating a completions script, and generating a temporary testing script.'
9
9
 
10
- usage "completely test [--keep] COMPLINE"
11
- usage "completely test (-h|--help)"
10
+ usage 'completely test [--keep] COMPLINE'
11
+ usage 'completely test (-h|--help)'
12
12
 
13
- option "-k --keep", "Keep the temporary testing script in the current directory"
13
+ option '-k --keep', 'Keep the temporary testing script in the current directory'
14
14
 
15
- param "COMPLINE", "The command to test completions for. This will be handled as if a TAB was pressed immediately at the end of it, so the last word is considered the active cursor. If you wish to complete for the next word instead, end your command with a space."
15
+ param 'COMPLINE', 'The command to test completions for. This will be handled as if a TAB was pressed immediately at the end of it, so the last word is considered the active cursor. If you wish to complete for the next word instead, end your command with a space.'
16
16
 
17
17
  environment_config_path
18
18
  environment_debug
19
19
 
20
- example %q[completely test "mygit pu"]
21
- example %q[completely test "mygit pull "]
20
+ example 'completely test "mygit pu"'
21
+ example 'completely test "mygit pull "'
22
22
 
23
23
  def run
24
24
  puts tester.test(compline).join "\n"
25
+
25
26
  if args['--keep']
26
27
  File.write 'completely-tester.sh', tester_script
27
28
  puts 'saved completely-tester.sh'
28
29
  end
30
+
31
+ syntax_warning unless completions.valid?
29
32
  end
30
33
 
31
34
  private
@@ -45,7 +48,6 @@ module Completely
45
48
  def tester_script
46
49
  @tester_script ||= tester.tester_script compline
47
50
  end
48
-
49
51
  end
50
52
  end
51
53
  end
@@ -3,7 +3,7 @@ require 'erb'
3
3
 
4
4
  module Completely
5
5
  class Completions
6
- attr_reader :config, :function_name
6
+ attr_reader :config
7
7
 
8
8
  class << self
9
9
  def load(config_path, function_name: nil)
@@ -14,13 +14,14 @@ module Completely
14
14
  data = YAML.load_file config_path
15
15
  # :nocov:
16
16
  end
17
-
17
+
18
18
  new data, function_name: function_name
19
19
  end
20
20
  end
21
21
 
22
22
  def initialize(config, function_name: nil)
23
- @config, @function_name = config, function_name
23
+ @config = config
24
+ @function_name = function_name
24
25
  end
25
26
 
26
27
  def patterns
@@ -36,11 +37,11 @@ module Completely
36
37
  end
37
38
 
38
39
  def wrapper_function(name = nil)
39
- name ||= "send_completions"
40
+ name ||= 'send_completions'
40
41
 
41
42
  script_lines = script.split("\n").map do |line|
42
43
  clean_line = line.gsub("'") { "\\'" }
43
- %Q[ echo $'#{clean_line}']
44
+ " echo $'#{clean_line}'"
44
45
  end.join("\n")
45
46
 
46
47
  "#{name}() {\n#{script_lines}\n}"
@@ -53,13 +54,15 @@ module Completely
53
54
  private
54
55
 
55
56
  def patterns!
56
- config.map do |text, completions|
57
- Pattern.new text, completions
58
- end.sort_by { |pattern| -pattern.length }
57
+ result = config.map do |text, completions|
58
+ Pattern.new text, completions, pattern_function_name
59
+ end
60
+
61
+ result.sort_by { |pattern| -pattern.length }
59
62
  end
60
63
 
61
64
  def template_path
62
- @template_path ||= File.expand_path("templates/template.erb", __dir__)
65
+ @template_path ||= File.expand_path('templates/template.erb', __dir__)
63
66
  end
64
67
 
65
68
  def template
@@ -67,16 +70,19 @@ module Completely
67
70
  end
68
71
 
69
72
  def command
70
- @command ||= config.keys.first.split(' ').first
73
+ @command ||= config.keys.first.split.first
71
74
  end
72
75
 
73
76
  def function_name
74
77
  @function_name ||= "_#{command}_completions"
75
78
  end
76
79
 
77
- def pattern_prefixes
78
- patterns.map &:prefix
80
+ def pattern_function_name
81
+ @pattern_function_name ||= "#{function_name}_filter"
79
82
  end
80
83
 
84
+ def pattern_prefixes
85
+ patterns.map(&:prefix)
86
+ end
81
87
  end
82
- end
88
+ end
@@ -1,10 +1,11 @@
1
1
  module Completely
2
2
  class Pattern
3
- attr_reader :text, :completions
3
+ attr_reader :text, :completions, :function_name
4
4
 
5
- def initialize(text, completions)
5
+ def initialize(text, completions, function_name)
6
6
  @text = text
7
7
  @completions = completions || []
8
+ @function_name = function_name
8
9
  end
9
10
 
10
11
  def length
@@ -16,7 +17,7 @@ module Completely
16
17
  end
17
18
 
18
19
  def words
19
- @words ||= completions.reject { |w| w =~ /^<.*>$/ }
20
+ @words ||= completions.grep_v(/^<.*>$/)
20
21
  end
21
22
 
22
23
  def actions
@@ -27,21 +28,21 @@ module Completely
27
28
  end
28
29
 
29
30
  def prefix
30
- text.split(' ')[0]
31
+ text.split(/ |\*/).first
31
32
  end
32
33
 
33
34
  def case_string
34
35
  if text_without_prefix.empty?
35
- "*"
36
- elsif text_without_prefix.include? "*"
37
- %Q['#{text_without_prefix.gsub "*", "'*'"}']
36
+ '*'
37
+ elsif text_without_prefix.include? '*'
38
+ text_without_prefix.gsub(/([^*]+)/, "'\\1'")
38
39
  else
39
- %Q['#{text_without_prefix}'*]
40
+ "'#{text_without_prefix}'*"
40
41
  end
41
42
  end
42
43
 
43
44
  def text_without_prefix
44
- @text_without_prefix ||= text.split(' ')[1..-1].join ' '
45
+ @text_without_prefix ||= text[/^#{prefix}\s*(.*)/, 1]
45
46
  end
46
47
 
47
48
  def compgen
@@ -52,9 +53,9 @@ module Completely
52
53
 
53
54
  def compgen!
54
55
  result = []
55
- result << %Q[#{actions.join ' '}] if actions.any?
56
- result << %Q[-W "#{words.join ' '}"] if words.any?
56
+ result << actions.join(' ').to_s if actions.any?
57
+ result << %[-W "$(#{function_name} "#{words.join ' '}")"] if words.any?
57
58
  result.any? ? result.join(' ') : nil
58
59
  end
59
60
  end
60
- end
61
+ end
@@ -4,9 +4,28 @@
4
4
  # completely (https://github.com/dannyben/completely)
5
5
  # Modifying it manually is not recommended
6
6
 
7
+ <%= function_name %>_filter() {
8
+ local words="$1"
9
+ local cur=${COMP_WORDS[COMP_CWORD]}
10
+ local result=()
11
+
12
+ if [[ "${cur:0:1}" == "-" ]]; then
13
+ echo "$words"
14
+
15
+ else
16
+ for word in $words; do
17
+ [[ "${word:0:1}" != "-" ]] && result+=("$word")
18
+ done
19
+
20
+ echo "${result[*]}"
21
+
22
+ fi
23
+ }
24
+
7
25
  <%= function_name %>() {
8
26
  local cur=${COMP_WORDS[COMP_CWORD]}
9
- local compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
27
+ local compwords=("${COMP_WORDS[@]:1:$COMP_CWORD-1}")
28
+ local compline="${compwords[*]}"
10
29
 
11
30
  % if ENV['COMPLETELY_DEBUG']
12
31
  if [[ -n "$COMPLETELY_DEBUG" ]]; then
@@ -19,7 +38,7 @@
19
38
  % patterns.each do |pattern|
20
39
  % next if pattern.empty?
21
40
  <%= pattern.case_string %>)
22
- while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen <%= pattern.compgen %> -- "$cur" )
41
+ while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen <%= pattern.compgen %> -- "$cur" )
23
42
  ;;
24
43
 
25
44
  % end
@@ -5,12 +5,14 @@ module Completely
5
5
  class Tester
6
6
  attr_reader :script, :script_path, :function_name, :cword, :compline
7
7
 
8
- def initialize(script: nil, script_path: nil, function_name: )
9
- @script, @script_path, @function_name = script, script_path, function_name
8
+ def initialize(function_name:, script: nil, script_path: nil)
9
+ @script = script
10
+ @script_path = script_path
11
+ @function_name = function_name
10
12
  end
11
13
 
12
14
  def test(compline)
13
- Tempfile.create "completely-tester" do |f|
15
+ Tempfile.create 'completely-tester' do |f|
14
16
  f << tester_script(compline)
15
17
  f.flush
16
18
  `bash #{f.path}`
@@ -26,23 +28,20 @@ module Completely
26
28
 
27
29
  def set_compline_vars(compline)
28
30
  @compline = compline
29
- @cword = compline.split(' ').size - 1
31
+ @cword = compline.split.size - 1
30
32
  @cword += 1 if compline.end_with? ' '
31
33
  end
32
34
 
33
35
  def absolute_script_path
34
- @absolute_script_path ||= begin
35
- script_path ? File.expand_path(script_path) : nil
36
- end
36
+ @absolute_script_path ||= script_path ? File.expand_path(script_path) : nil
37
37
  end
38
38
 
39
39
  def template_path
40
- @template_path ||= File.expand_path "templates/tester-template.erb", __dir__
40
+ @template_path ||= File.expand_path 'templates/tester-template.erb', __dir__
41
41
  end
42
42
 
43
43
  def template
44
44
  @template ||= File.read template_path
45
45
  end
46
-
47
46
  end
48
- end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module Completely
2
- VERSION = "0.4.3"
3
- end
2
+ VERSION = '0.5.1'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: completely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-14 00:00:00.000000000 Z
11
+ date: 2022-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.7'
33
+ version: 0.7.2
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.7'
40
+ version: 0.7.2
41
41
  description: Generate bash completion scripts using simple YAML configuration
42
42
  email: db@dannyben.com
43
43
  executables:
@@ -64,7 +64,8 @@ files:
64
64
  homepage: https://github.com/dannyben/completely
65
65
  licenses:
66
66
  - MIT
67
- metadata: {}
67
+ metadata:
68
+ rubygems_mfa_required: 'true'
68
69
  post_install_message:
69
70
  rdoc_options: []
70
71
  require_paths:
@@ -80,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  - !ruby/object:Gem::Version
81
82
  version: '0'
82
83
  requirements: []
83
- rubygems_version: 3.3.14
84
+ rubygems_version: 3.3.26
84
85
  signing_key:
85
86
  specification_version: 4
86
87
  summary: Bash Completions Generator