completely 0.1.2 → 0.3.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: c09f951fc28b18f24c2c28fd7c80a15b3731aa07456145ab905c63c2b0dc9634
4
- data.tar.gz: a9a25173ab3358e7eea42fe38552cbced8c6602bf5a0e9e6c414555420ae7af6
3
+ metadata.gz: 6f19143967a28107018c600c7f302bfccd2cf34d70a88be661f68e340e515c07
4
+ data.tar.gz: 6037bb0f6944627f97abf37c51f76d1db213b42402db092fa6c36fb257892c22
5
5
  SHA512:
6
- metadata.gz: b6edd13c5a0084485b7cb7114c45cf30d8c144075a3791aa51a8f5e7fc71df825cb03cb4786bc8fb3832f7bc1a46f06876614493184c4602d7e2e0dfbd3d4854
7
- data.tar.gz: f8eb6cb08636c4281b50d7542d3c92fc8c6914e10597605f48e29ae342b6fb8cd31fff84de9e81dc0a7011bd5e963469c11fe025719e537238883d0a5812b5d9
6
+ metadata.gz: c46ac169ab59595c67d3d8a8c320ea7fb56e78715fbc2dd55e4e20534c8543c66f0b62dda01a6802bbaf9f071a5ebd7ffa55a17c70443e05bba8ab8c3bedce71
7
+ data.tar.gz: ac9a7a7934e115957271ec1e5cc736c0df29368f18da6f9eb409069f9690cb2594d6b9bdbf6d4fd18388ce79f7285e66a357d4bb241083ca083fd57102c95746
data/README.md CHANGED
@@ -13,7 +13,10 @@ This tool is for you if:
13
13
 
14
14
  1. You develop your own command line tools.
15
15
  2. Your life feels empty without bash completions.
16
- 3. Bash completion scripts scare you.
16
+ 3. Bash completion scripts seem overly complex to you.
17
+
18
+ Note that if you are building bash command line scripts with [bashly][bashly],
19
+ then this functionality is already integrated with it.
17
20
 
18
21
  ---
19
22
 
@@ -29,15 +32,18 @@ $ gem install completely
29
32
  The `completely` command line works with a simple YAML configuration file as
30
33
  input, and generates a bash completions script as output.
31
34
 
32
- The configuration file is built like this:
35
+ The configuration file is built of blocks that look like this:
33
36
 
34
37
  ```yaml
35
38
  pattern:
36
- - --argument
37
- - --param
38
- - command
39
+ - --argument
40
+ - --param
41
+ - command
39
42
  ```
40
43
 
44
+ Each pattern contains an array of words (or functions) that will be suggested
45
+ for the auto complete process.
46
+
41
47
  You can save a sample YAML file by running:
42
48
 
43
49
  ```
@@ -58,6 +64,7 @@ mygit status:
58
64
  - --help
59
65
  - --verbose
60
66
  - --branch
67
+ - $(git branch 2> /dev/null)
61
68
 
62
69
  mygit init:
63
70
  - --bare
@@ -92,21 +99,66 @@ For more options (like setting input/output path), run
92
99
  $ completely --help
93
100
  ```
94
101
 
95
- ### Suggesting files and directories
102
+ ### Suggesting files, directories and other bash built-ins
96
103
 
97
- You may have noticed that the sample file contains two special entries:
104
+ In addition to specifying a simple array of completion words, you may use
105
+ the special syntax `<..>` to suggest more advanced functions.
98
106
 
99
- - `<file>`
100
- - `<directory>`
107
+ ```yaml
108
+ pattern:
109
+ - <file>
110
+ - <directory>
111
+ ```
101
112
 
102
- These patterns will add the list of files and directories
113
+ These suggestions will add the list of files and directories
103
114
  (when `<file>` is used) or just directories (when `<directory>` is used) to
104
115
  the list of suggestions.
105
116
 
117
+ You may use any of the below keywords to add additional suggestions:
118
+
119
+ | Keyword | Meaning
120
+ |---------------|---------------------
121
+ | `<alias>` | Alias names
122
+ | `<arrayvar>` | Array variable names
123
+ | `<binding>` | Readline key binding names
124
+ | `<builtin>` | Names of shell builtin commands
125
+ | `<command>` | Command names
126
+ | `<directory>` | Directory names
127
+ | `<disabled>` | Names of disabled shell builtins
128
+ | `<enabled>` | Names of enabled shell builtins
129
+ | `<export>` | Names of exported shell variables
130
+ | `<file>` | File names
131
+ | `<function>` | Names of shell functions
132
+ | `<group>` | Group names
133
+ | `<helptopic>` | Help topics as accepted by the help builtin
134
+ | `<hostname>` | Hostnames, as taken from the file specified by the HOSTFILE shell variable
135
+ | `<job>` | Job names
136
+ | `<keyword>` | Shell reserved words
137
+ | `<running>` | Names of running jobs
138
+ | `<service>` | Service names
139
+ | `<signal>` | Signal names
140
+ | `<stopped>` | Names of stopped jobs
141
+ | `<user>` | User names
142
+ | `<variable>` | Names of all shell variables
143
+
106
144
  For those interested in the technical details, any word between `<...>` will
107
145
  simply be added using the [`compgen -A action`][compgen] function, so you can
108
146
  in fact use any of its supported arguments.
109
147
 
148
+ ### Suggesting custom dynamic suggestions
149
+
150
+ You can also use any command that outputs a whitespace-delimited list as a
151
+ suggestions list, by wrapping it in `$(..)`. For example, in order to add git
152
+ branches to your suggestions, use the following:
153
+
154
+ ```yaml
155
+ mygit:
156
+ - $(git branch 2> /dev/null)
157
+ ```
158
+
159
+ The `2> /dev/null` is used so that if the command is executed in a directory
160
+ without a git repository, it will still behave as expected.
161
+
110
162
 
111
163
  ## Using the generated completion scripts
112
164
 
@@ -152,3 +204,4 @@ to contribute, feel free to [open an issue][issues].
152
204
 
153
205
  [issues]: https://github.com/DannyBen/completely/issues
154
206
  [compgen]: https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html
207
+ [bashly]: https://bashly.dannyb.co/
@@ -37,6 +37,7 @@ module Completely
37
37
 
38
38
  def preview_command
39
39
  puts script
40
+ syntax_warning unless completions.valid?
40
41
  end
41
42
 
42
43
  def generate_command
@@ -44,6 +45,7 @@ module Completely
44
45
  output = wrap ? wrapper_function(wrap) : script
45
46
  File.write output_path, output
46
47
  say "Saved !txtpur!#{output_path}"
48
+ syntax_warning unless completions.valid?
47
49
  end
48
50
 
49
51
  private
@@ -76,5 +78,10 @@ module Completely
76
78
  @completions ||= Completions.load(config_path, function_name: args['--function'])
77
79
  end
78
80
 
81
+ def syntax_warning
82
+ say! "\n!txtred!WARNING:\nYour configuration is invalid."
83
+ say! "!txtred!All patterns must start with the same word."
84
+ end
85
+
79
86
  end
80
87
  end
@@ -7,7 +7,13 @@ module Completely
7
7
 
8
8
  class << self
9
9
  def load(config_path, function_name: nil)
10
- new YAML.load_file(config_path), function_name: function_name
10
+ begin
11
+ data = YAML.load_file config_path, aliases: true
12
+ rescue ArgumentError
13
+ data = YAML.load_file config_path
14
+ end
15
+
16
+ new data, function_name: function_name
11
17
  end
12
18
  end
13
19
 
@@ -15,6 +21,14 @@ module Completely
15
21
  @config, @function_name = config, function_name
16
22
  end
17
23
 
24
+ def patterns
25
+ @patterns ||= patterns!
26
+ end
27
+
28
+ def valid?
29
+ pattern_prefixes.uniq.count == 1
30
+ end
31
+
18
32
  def script
19
33
  ERB.new(template, trim_mode: '%-').result(binding)
20
34
  end
@@ -32,6 +46,12 @@ module Completely
32
46
 
33
47
  private
34
48
 
49
+ def patterns!
50
+ config.map do |text, completions|
51
+ Pattern.new text, completions
52
+ end.sort_by { |pattern| -pattern.length }
53
+ end
54
+
35
55
  def template_path
36
56
  @template_path ||= File.expand_path("template.erb", __dir__)
37
57
  end
@@ -41,16 +61,16 @@ module Completely
41
61
  end
42
62
 
43
63
  def command
44
- @command ||= config.keys.first
45
- end
46
-
47
- def patterns
48
- @patterns ||= config.to_a.sort_by { |k, v| -k.size }.to_h
64
+ @command ||= config.keys.first.split(' ').first
49
65
  end
50
66
 
51
67
  def function_name
52
68
  @function_name ||= "_#{command}_completions"
53
69
  end
54
70
 
71
+ def pattern_prefixes
72
+ patterns.map &:prefix
73
+ end
74
+
55
75
  end
56
76
  end
@@ -0,0 +1,50 @@
1
+ module Completely
2
+ class Pattern
3
+ attr_reader :text, :completions
4
+
5
+ def initialize(text, completions)
6
+ @text = text
7
+ @completions = completions || []
8
+ end
9
+
10
+ def length
11
+ @length ||= text.size
12
+ end
13
+
14
+ def empty?
15
+ completions.empty?
16
+ end
17
+
18
+ def words
19
+ @words ||= completions.reject { |w| w =~ /^<.*>$/ }
20
+ end
21
+
22
+ def actions
23
+ @actions ||= completions.filter_map do |word|
24
+ action = word[/^<(.+)>$/, 1]
25
+ "-A #{action}" if action
26
+ end
27
+ end
28
+
29
+ def prefix
30
+ text.split(' ')[0]
31
+ end
32
+
33
+ def text_without_prefix
34
+ text.split(' ')[1..-1].join ' '
35
+ end
36
+
37
+ def compgen
38
+ @compgen ||= compgen!
39
+ end
40
+
41
+ private
42
+
43
+ def compgen!
44
+ result = []
45
+ result << %Q[#{actions.join ' '}] if actions.any?
46
+ result << %Q[-W "#{words.join ' '}"] if words.any?
47
+ result.any? ? result.join(' ') : nil
48
+ end
49
+ end
50
+ end
@@ -9,6 +9,7 @@ mygit status:
9
9
  - --help
10
10
  - --verbose
11
11
  - --branch
12
+ - $(git branch 2> /dev/null)
12
13
 
13
14
  mygit init:
14
15
  - --bare
@@ -5,14 +5,12 @@
5
5
  # Modifying it manually is not recommended
6
6
  <%= function_name %>() {
7
7
  local cur=${COMP_WORDS[COMP_CWORD]}
8
+ local comp_line="${COMP_WORDS[@]:1}"
8
9
 
9
- case "$COMP_LINE" in
10
- % patterns.each do |pattern, words|
11
- % next unless words
12
- % clean_words = words.reject { |w| w =~ /^<.*>$/ }.join " "
13
- % functions = words.filter_map { |w| func = w[/^<(.+)>$/, 1] ; "-A #{func}" if func }
14
- % flags = functions.any? ? "#{functions.join ' '} -W" : "-W"
15
- '<%= pattern %>'*) COMPREPLY=($(compgen <%= flags %> "<%= clean_words %>" -- "$cur")) ;;
10
+ case "$comp_line" in
11
+ % patterns.each do |pattern|
12
+ % next if pattern.empty?
13
+ '<%= pattern.text_without_prefix %>'*) COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) ;;
16
14
  % end
17
15
  esac
18
16
  }
@@ -1,3 +1,3 @@
1
1
  module Completely
2
- VERSION = "0.1.2"
2
+ VERSION = "0.3.1"
3
3
  end
data/lib/completely.rb CHANGED
@@ -3,5 +3,5 @@ if ENV['BYEBUG']
3
3
  require 'lp'
4
4
  end
5
5
 
6
- # requires 'completely'
6
+ require 'completely/pattern'
7
7
  require 'completely/completions'
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.1.2
4
+ version: 0.3.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: 2021-07-20 00:00:00.000000000 Z
11
+ date: 2022-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colsole
@@ -51,6 +51,7 @@ files:
51
51
  - lib/completely/cli.rb
52
52
  - lib/completely/command.rb
53
53
  - lib/completely/completions.rb
54
+ - lib/completely/pattern.rb
54
55
  - lib/completely/sample.yaml
55
56
  - lib/completely/template.erb
56
57
  - lib/completely/version.rb
@@ -73,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  - !ruby/object:Gem::Version
74
75
  version: '0'
75
76
  requirements: []
76
- rubygems_version: 3.2.16
77
+ rubygems_version: 3.2.15
77
78
  signing_key:
78
79
  specification_version: 4
79
80
  summary: Bash Completions Generator