Module: Laborantin::Metaprog::Completeable
- Included in:
- Command, Command::Option
- Defined in:
- lib/laborantin/core/completeable.rb
Instance Attribute Summary collapse
-
#completion_block ⇒ Object
readonly
A block to propose completion on this option.
Instance Method Summary collapse
-
#complete(&blk) ⇒ Object
Stores the block argument in the completion_block, usage is for DSLs.
-
#completion_propositions_iterating_on(cmd, list) ⇒ Object
Provides completion facility for comma-separated lists of args, and returns the propositions - removes items already in list - prepends commas for items not already in list.
Instance Attribute Details
#completion_block ⇒ Object (readonly)
A block to propose completion on this option
6 7 8 |
# File 'lib/laborantin/core/completeable.rb', line 6 def completion_block @completion_block end |
Instance Method Details
#complete(&blk) ⇒ Object
Stores the block argument in the completion_block, usage is for DSLs
9 10 11 |
# File 'lib/laborantin/core/completeable.rb', line 9 def complete(&blk) @completion_block = blk end |
#completion_propositions_iterating_on(cmd, list) ⇒ Object
Provides completion facility for comma-separated lists of args, and returns the propositions
-
removes items already in list
-
prepends commas for items not already in list
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/laborantin/core/completeable.rb', line 17 def (cmd, list) envs_on_cli = cmd.split.last.split(',').reject{|s| s.start_with?('-')} last_env_on_cli = envs_on_cli.last unless cmd.end_with?(',') last_env_on_cli ||= '' complete_envs_on_cli = envs_on_cli - [last_env_on_cli] list = list.select{|str| str.start_with?(last_env_on_cli)} candidate_envs = list - envs_on_cli candidate_envs.map{|str| (complete_envs_on_cli + [str]).join(',') } end |