completely 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cd3ae4ec92d7318bd4ef32fdd465d9bff3341931f5f965d2db8f263c1b95e38b
4
+ data.tar.gz: 01ccb2fa2e1f050b7a45944b552590fad6aab9bafc90ef4cb98b8d6284200392
5
+ SHA512:
6
+ metadata.gz: fd06256b9cb4c42364d118ea1392b180ef662e98b2ea9c6b24ca815f5fa5cf1f16d6c0a6792090c9bc5f376ab7dfb04e9b8c732cb83292140acb59676d6090a9
7
+ data.tar.gz: b1985849259627803ebe3f7c56b4adc98137456b172fb36cbf1379b4677257dca6cc671d85a12106596fb246de02aad07c5e9d51c5dd0abc7301518d9b7529b2
data/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # Completely - Bash Completions Generator
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/completely.svg)](https://badge.fury.io/rb/completely)
4
+ [![Build Status](https://github.com/DannyBen/completely/workflows/Test/badge.svg)](https://github.com/DannyBen/completely/actions?query=workflow%3ATest)
5
+
6
+ ---
7
+
8
+ Completely is a command line utility and a Ruby library that lets you generate
9
+ bash completion scripts from simple YAML configuration.
10
+
11
+ This tool is for you it:
12
+
13
+ 1. You develop your own command line tools.
14
+ 2. Your life feels empty without bash completions.
15
+ 3. Bash completion scripts scare you.
16
+
17
+ ---
18
+
19
+
20
+ ## Install
21
+
22
+ ```
23
+ $ gem install completely
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ The `completely` command line works with a simple YAML configuration file as
29
+ input, and generates a bash completions script as output.
30
+
31
+ The configuration file is built like this:
32
+
33
+ ```yaml
34
+ pattern:
35
+ - --argument
36
+ - --param
37
+ - command
38
+ ```
39
+
40
+ You can save a sample YAML file by running:
41
+
42
+ ```
43
+ $ completely new
44
+ ```
45
+
46
+ This will generate a file named `completely.yaml` with this content:
47
+
48
+ ```yaml
49
+ mygit:
50
+ - --help
51
+ - --version
52
+ - status
53
+ - init
54
+ - commit
55
+
56
+ mygit status:
57
+ - --help
58
+ - --verbose
59
+ - --branch
60
+
61
+ mygit init:
62
+ - --bare
63
+ - <directory>
64
+
65
+ mygit commit:
66
+ - <file>
67
+ - --help
68
+ - --message
69
+ - --all
70
+ - -a
71
+ - --quiet
72
+ - -q
73
+ ```
74
+
75
+ Each pattern in this configuration file will be checked against the user's
76
+ input, and if the input **starts with** a matching pattern, the list that
77
+ follows it will be suggested as completions.
78
+
79
+ To generate the bash script, simply run:
80
+
81
+ ```
82
+ $ completely generate
83
+
84
+ # or, to just preview it without saving:
85
+ $ completely preview
86
+ ```
87
+
88
+ For more options (like setting input/output path), run
89
+
90
+ ```
91
+ $ completely --help
92
+ ```
93
+
94
+ ### Suggesting files and directories
95
+
96
+ You may have noticed that the sample file contains two special entries:
97
+
98
+ - `<file>`
99
+ - `<directory>`
100
+
101
+ These patterns will add the list of files and directories
102
+ (when `<file>` is used) or just directories (when `<directory` is used) to
103
+ the list of suggestions.
104
+
105
+ For those interested in the technical details, any word between `<...>` will
106
+ simply be added using the [`compgen -A action`][compgen] function, so you can
107
+ in fact use any of its supported arguments.
108
+
109
+
110
+ ### Using the generated completion scripts
111
+
112
+ In order to enable the completions, simply source the generated script:
113
+
114
+ ```
115
+ $ source completely.bash
116
+ ```
117
+
118
+ You may wish to add this to your `~/.bashrc` file to enable this for future
119
+ sessions (just be sure to use absolute path).
120
+
121
+
122
+ ## Contributing / Support
123
+
124
+ If you experience any issue, have a question or a suggestion, or if you wish
125
+ to contribute, feel free to [open an issue][issues].
126
+
127
+ ---
128
+
129
+ [issues]: https://github.com/DannyBen/completely/issues
130
+ [compgen]: https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html
data/bin/completely ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ require 'completely'
3
+ require 'completely/cli'
4
+ require 'colsole'
5
+ include Colsole
6
+
7
+ runner = Completely::CLI.runner
8
+
9
+ begin
10
+ exit runner.run ARGV
11
+
12
+ rescue => e
13
+ puts e.backtrace.reverse if ENV['DEBUG']
14
+ say! "!undred!#{e.class}!txtrst!\n#{e.message}"
15
+ exit 1
16
+ end
data/lib/completely.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'requires'
2
+
3
+ if ENV['BYEBUG']
4
+ require 'byebug'
5
+ require 'lp'
6
+ end
7
+
8
+ # requires 'completely'
9
+ require 'completely/completions'
@@ -0,0 +1,11 @@
1
+ require 'mister_bin'
2
+ require 'completely/command'
3
+ require 'completely/version'
4
+
5
+ module Completely
6
+ class CLI
7
+ def self.runner
8
+ MisterBin::Runner.new handler: Command
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,72 @@
1
+ module Completely
2
+ class Command < MisterBin::Command
3
+ help "Bash completion script generator"
4
+ version Completely::VERSION
5
+
6
+ usage "completely new [CONFIG_PATH]"
7
+ usage "completely preview [CONFIG_PATH --function NAME]"
8
+ usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME]"
9
+ usage "completely (-h|--help|--version)"
10
+
11
+ command "new", "Create a new sample YAML configuration file"
12
+ command "preview", "Generate the bash completion script to STDOUT"
13
+ command "generate", "Generate the bash completion script to a file"
14
+
15
+ option "-f --function NAME", "Modify the name of the function in the generated script"
16
+
17
+ param "CONFIG_PATH", "Path to the YAML configuration file"
18
+ param "OUTPUT_PATH", "Path to the output bash script"
19
+
20
+ example "completely new"
21
+ example "completely new input.yaml"
22
+ example "completely preview --function my_completions"
23
+ example "completely generate"
24
+ example "completely generate input.yaml"
25
+ example "completely generate input.yaml output.sh -f my_completions"
26
+
27
+ def new_command
28
+ if File.exist? config_path
29
+ raise "File already exists: #{config_path}"
30
+ else
31
+ File.write config_path, sample
32
+ say "Saved !txtpur!#{config_path}"
33
+ end
34
+ end
35
+
36
+ def preview_command
37
+ puts script
38
+ end
39
+
40
+ def generate_command
41
+ File.write output_path, script
42
+ say "Saved !txtpur!#{output_path}"
43
+ end
44
+
45
+ private
46
+
47
+ def config_path
48
+ @config_path ||= args['CONFIG_PATH'] || "completely.yaml"
49
+ end
50
+
51
+ def output_path
52
+ @output_path ||= args['OUTPUT_PATH'] || "completely.bash"
53
+ end
54
+
55
+ def sample
56
+ @sample ||= File.read(sample_path)
57
+ end
58
+
59
+ def sample_path
60
+ @sample_path ||= File.expand_path("sample.yaml", __dir__)
61
+ end
62
+
63
+ def script
64
+ @script ||= completions.script
65
+ end
66
+
67
+ def completions
68
+ @completions ||= Completions.load(config_path, function_name: args['--function'])
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+ require 'erb'
3
+
4
+ module Completely
5
+ class Completions
6
+ attr_reader :config, :function_name
7
+
8
+ class << self
9
+ def load(config_path, function_name: nil)
10
+ new YAML.load_file(config_path), function_name: function_name
11
+ end
12
+ end
13
+
14
+ def initialize(config, function_name: nil)
15
+ @config, @function_name = config, function_name
16
+ end
17
+
18
+ def script
19
+ ERB.new(template, trim_mode: '%-').result(binding)
20
+ end
21
+
22
+ private
23
+
24
+ def template_path
25
+ @template_path ||= File.expand_path("template.erb", __dir__)
26
+ end
27
+
28
+ def template
29
+ @template ||= File.read(template_path)
30
+ end
31
+
32
+ def command
33
+ @command ||= config.keys.first
34
+ end
35
+
36
+ def patterns
37
+ @patterns ||= config.to_a.sort_by { |k, v| -k.size }.to_h
38
+ end
39
+
40
+ def function_name
41
+ @function_name ||= "_#{command}_completions"
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ mygit:
2
+ - --help
3
+ - --version
4
+ - status
5
+ - init
6
+ - commit
7
+
8
+ mygit status:
9
+ - --help
10
+ - --verbose
11
+ - --branch
12
+
13
+ mygit init:
14
+ - --bare
15
+ - <directory>
16
+
17
+ mygit commit:
18
+ - <file>
19
+ - --help
20
+ - --message
21
+ - --all
22
+ - -a
23
+ - --quiet
24
+ - -q
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This bash completions script was generated by
4
+ # completely (https://github.com/dannyben/completely)
5
+ # Modifying it manually is not recommended
6
+ <%= function_name %>() {
7
+ local cur=${COMP_WORDS[COMP_CWORD]}
8
+
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")) ;;
16
+ % end
17
+ esac
18
+ }
19
+
20
+ complete -F <%= function_name %> <%= command %>
@@ -0,0 +1,3 @@
1
+ module Completely
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: completely
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colsole
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mister_bin
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: requires
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ description: Generate bash completion scripts using simple YAML configuration
56
+ email: db@dannyben.com
57
+ executables:
58
+ - completely
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - bin/completely
64
+ - lib/completely.rb
65
+ - lib/completely/cli.rb
66
+ - lib/completely/command.rb
67
+ - lib/completely/completions.rb
68
+ - lib/completely/sample.yaml
69
+ - lib/completely/template.erb
70
+ - lib/completely/version.rb
71
+ homepage: https://github.com/dannyben/completely
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.7.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.2.16
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Bash Completions Generator
94
+ test_files: []