Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest

strategy:
matrix: { ruby: ['2.7', '3.0', '3.1'] }
matrix: { ruby: ['2.7', '3.0', '3.1', head] }

steps:
- name: Checkout code
Expand Down
19 changes: 19 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
inherit_gem:
rentacop: rentacop.yml

AllCops:
TargetRubyVersion: 2.7
Exclude:
- 'debug.rb'
- 'dev/**/*'

# Allow `set_compline_vars` here, it is more readable
Naming/AccessorMethodName:
Exclude:
- 'lib/completely/tester.rb'

# Allow long lines in specs and commands
Layout/LineLength:
Exclude:
- 'spec/**/*'
- 'lib/completely/commands/**/*'
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem 'byebug'
gem 'lp'
gem 'rentacop'
gem 'rspec'
gem 'rspec_approvals'
gem 'runfile'
Expand Down
3 changes: 1 addition & 2 deletions bin/completely
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ runner = Completely::CLI.runner

begin
exit runner.run ARGV

rescue => e
puts e.backtrace.reverse if ENV['DEBUG']
say! "!undred!#{e.class}!txtrst!\n#{e.message}"
exit 1
end
end
8 changes: 4 additions & 4 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env ruby

require "irb"
require "irb/completion"
require "completely"
require 'irb'
require 'irb/completion'
require 'completely'

include Completely

IRB.start
IRB.start
14 changes: 7 additions & 7 deletions completely.gemspec
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'date'
require 'completely/version'

Gem::Specification.new do |s|
s.name = 'completely'
s.version = Completely::VERSION
s.date = Date.today.to_s
s.summary = "Bash Completions Generator"
s.description = "Generate bash completion scripts using simple YAML configuration"
s.authors = ["Danny Ben Shitrit"]
s.summary = 'Bash Completions Generator'
s.description = 'Generate bash completion scripts using simple YAML configuration'
s.authors = ['Danny Ben Shitrit']
s.email = 'db@dannyben.com'
s.files = Dir['README.md', 'lib/**/*.*']
s.executables = ['completely']
s.homepage = 'https://github.com/dannyben/completely'
s.license = 'MIT'
s.required_ruby_version = ">= 2.7.0"
s.required_ruby_version = '>= 2.7.0'

s.add_runtime_dependency 'colsole', '~> 0.6'
s.add_runtime_dependency 'mister_bin', '~> 0.7'

s.metadata['rubygems_mfa_required'] = 'true'
end
4 changes: 2 additions & 2 deletions lib/completely/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module Completely
class CLI
def self.runner
runner = MisterBin::Runner.new version: Completely::VERSION,
header: "Completely - Bash Completions Generator",
footer: "Run !txtpur!completely COMMAND --help!txtrst! for more information"
header: 'Completely - Bash Completions Generator',
footer: 'Run !txtpur!completely COMMAND --help!txtrst! for more information'

runner.route 'init', to: Commands::Init
runner.route 'preview', to: Commands::Preview
Expand Down
11 changes: 5 additions & 6 deletions lib/completely/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
module Completely
module Commands
class Base < MisterBin::Command

class << self
def param_config_path
param "CONFIG_PATH", "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
param 'CONFIG_PATH', "Path to the YAML configuration file [default: completely.yaml]\nCan also be set by an environment variable"
end

def option_function
option "-f --function NAME", "Modify the name of the function in the generated script"
option '-f --function NAME', 'Modify the name of the function in the generated script'
end

def environment_config_path
environment "COMPLETELY_CONFIG_PATH", "Path to a completely configuration file [default: completely.yaml]"
environment 'COMPLETELY_CONFIG_PATH', 'Path to a completely configuration file [default: completely.yaml]'
end

def environment_debug
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"
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'
end
end

Expand Down Expand Up @@ -46,7 +45,7 @@ def config_basename

def syntax_warning
say! "\n!txtred!WARNING:\nYour configuration is invalid."
say! "!txtred!All patterns must start with the same word."
say! '!txtred!All patterns must start with the same word.'
end
end
end
Expand Down
13 changes: 6 additions & 7 deletions lib/completely/commands/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
module Completely
module Commands
class Generate < Base
help "Generate the bash completion script to a file"
help 'Generate the bash completion script to a file'

usage "completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]"
usage "completely generate (-h|--help)"
usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]'
usage 'completely generate (-h|--help)'

option_function
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"
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'

param_config_path
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"
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"

environment_config_path
environment "COMPLETELY_OUTPUT_PATH", "Path to the output bash script"
environment 'COMPLETELY_OUTPUT_PATH', 'Path to the output bash script'
environment_debug

def run
Expand All @@ -31,7 +31,6 @@ def run
def wrapper_function(wrapper_name)
completions.wrapper_function wrapper_name
end

end
end
end
19 changes: 8 additions & 11 deletions lib/completely/commands/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
module Completely
module Commands
class Init < Base
help "Create a new sample YAML configuration file"
help 'Create a new sample YAML configuration file'

usage "completely init [CONFIG_PATH]"
usage "completely init (-h|--help)"
usage 'completely init [CONFIG_PATH]'
usage 'completely init (-h|--help)'

param_config_path
environment_config_path

def run
if File.exist? config_path
raise "File already exists: #{config_path}"
else
File.write config_path, sample
say "Saved !txtpur!#{config_path}"
end
raise "File already exists: #{config_path}" if File.exist? config_path

File.write config_path, sample
say "Saved !txtpur!#{config_path}"
end

private
Expand All @@ -27,9 +25,8 @@ def sample
end

def sample_path
@sample_path ||= File.expand_path "../templates/sample.yaml", __dir__
@sample_path ||= File.expand_path '../templates/sample.yaml', __dir__
end

end
end
end
6 changes: 3 additions & 3 deletions lib/completely/commands/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
module Completely
module Commands
class Preview < Base
help "Generate the bash completion script to STDOUT"
help 'Generate the bash completion script to STDOUT'

usage "completely preview [CONFIG_PATH --function NAME]"
usage "completely preview (-h|--help)"
usage 'completely preview [CONFIG_PATH --function NAME]'
usage 'completely preview (-h|--help)'

option_function
param_config_path
Expand Down
25 changes: 12 additions & 13 deletions lib/completely/commands/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
module Completely
module Commands
class Test < Base
summary "Test completions"
summary 'Test completions'

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."
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.'

usage "completely test [--keep] COMPLINE"
usage "completely test (-h|--help)"
usage 'completely test [--keep] COMPLINE'
usage 'completely test (-h|--help)'

option "-k --keep", "Keep the temporary testing script in the current directory"
option '-k --keep', 'Keep the temporary testing script in the current directory'

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."
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.'

environment_config_path
environment_debug

example %q[completely test "mygit pu"]
example %q[completely test "mygit pull "]
example 'completely test "mygit pu"'
example 'completely test "mygit pull "'

def run
puts tester.test(compline).join "\n"
if args['--keep']
File.write 'completely-tester.sh', tester_script
puts 'saved completely-tester.sh'
end
return unless args['--keep']

File.write 'completely-tester.sh', tester_script
puts 'saved completely-tester.sh'
end

private
Expand All @@ -45,7 +45,6 @@ def tester
def tester_script
@tester_script ||= tester.tester_script compline
end

end
end
end
26 changes: 14 additions & 12 deletions lib/completely/completions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

module Completely
class Completions
attr_reader :config, :function_name
attr_reader :config

class << self
def load(config_path, function_name: nil)
Expand All @@ -14,13 +14,14 @@ def load(config_path, function_name: nil)
data = YAML.load_file config_path
# :nocov:
end

new data, function_name: function_name
end
end

def initialize(config, function_name: nil)
@config, @function_name = config, function_name
@config = config
@function_name = function_name
end

def patterns
Expand All @@ -36,11 +37,11 @@ def script
end

def wrapper_function(name = nil)
name ||= "send_completions"
name ||= 'send_completions'

script_lines = script.split("\n").map do |line|
clean_line = line.gsub("'") { "\\'" }
%Q[ echo $'#{clean_line}']
" echo $'#{clean_line}'"
end.join("\n")

"#{name}() {\n#{script_lines}\n}"
Expand All @@ -53,21 +54,23 @@ def tester
private

def patterns!
config.map do |text, completions|
result = config.map do |text, completions|
Pattern.new text, completions, pattern_function_name
end.sort_by { |pattern| -pattern.length }
end

result.sort_by { |pattern| -pattern.length }
end

def template_path
@template_path ||= File.expand_path("templates/template.erb", __dir__)
@template_path ||= File.expand_path('templates/template.erb', __dir__)
end

def template
@template ||= File.read(template_path)
end

def command
@command ||= config.keys.first.split(' ').first
@command ||= config.keys.first.split.first
end

def function_name
Expand All @@ -79,8 +82,7 @@ def pattern_function_name
end

def pattern_prefixes
patterns.map &:prefix
patterns.map(&:prefix)
end

end
end
end
Loading