Skip to content

Commit be45d2d

Browse files
committed
initial completions implementation
1 parent 79fdb4a commit be45d2d

File tree

6 files changed

+43
-2
lines changed

6 files changed

+43
-2
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
LC_ALL: en_US.UTF-8 # consistent sort order
1414

1515
strategy:
16-
matrix: { ruby: ['2.4', '2.5', '2.6', '2.7', '3.0'] }
16+
matrix: { ruby: ['2.7', '3.0'] }
1717

1818
steps:
1919
- name: Checkout code

bashly.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ Gem::Specification.new do |s|
1515
s.executables = ['bashly']
1616
s.homepage = 'https://github.com/dannyben/bashly'
1717
s.license = 'MIT'
18-
s.required_ruby_version = ">= 2.3.0"
18+
s.required_ruby_version = ">= 2.7.0"
1919

2020
s.add_runtime_dependency 'colsole', '~> 0.6'
21+
s.add_runtime_dependency 'completely', '~> 0.1'
2122
s.add_runtime_dependency 'mister_bin', '~> 0.7'
2223
s.add_runtime_dependency 'requires', '~> 0.1'
2324
end

lib/bashly/commands/add.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def yaml_command
4141
end
4242

4343
private
44+
4445
def safe_copy_lib(libfile)
4546
safe_copy asset("templates/lib/#{libfile}"), "#{Settings.source_dir}/lib/#{libfile}"
4647
end

lib/bashly/concerns/completions.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Bashly
2+
# This is a `Command` concern responsible for providing bash completion data
3+
module Completions
4+
def completion_data(with_version: true)
5+
result = { full_name => completion_words(with_version: with_version) }
6+
7+
commands.each do |command|
8+
result.merge! command.completion_data(with_version: false)
9+
end
10+
11+
result
12+
end
13+
14+
private
15+
16+
def completion_flag_names
17+
flags.map(&:name) + flags.map(&:short)
18+
end
19+
20+
def completion_actions
21+
completions ? completions.map { |c| "<#{c}>" } : []
22+
end
23+
24+
def completion_words(with_version: false)
25+
trivial_flags = %w[--help -h]
26+
trivial_flags += %w[--version -v] if with_version
27+
all = (
28+
command_names + trivial_flags +
29+
completion_flag_names + completion_actions
30+
)
31+
32+
all.uniq.sort
33+
end
34+
35+
end
36+
end

lib/bashly/models/base.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Base
99
allowed
1010
arg
1111
catch_all
12+
completions
1213
default
1314
dependencies
1415
description

lib/bashly/models/command.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Bashly
22
module Models
33
class Command < Base
4+
include Completions
5+
46
# Returns the name to be used as an action.
57
# - If it is the root command, the action is "root"
68
# - Else, it is all the parents, except the first tone (root) joined

0 commit comments

Comments
 (0)