Module: SimpleCov::CLI::Completions::Scripts

Extended by:
Scripts
Included in:
Scripts
Defined in:
lib/simplecov/cli/completions/scripts.rb

Overview

The per-shell renderers. Each receives the same parsed usage data and returns the complete script text. The completions command's own shell-name argument is the one thing the usage tables don't carry, so each renderer wires it in by hand.

Constant Summary collapse

BASH_TAIL =
[
  "  esac",
  '  [[ "$cur" == -* ]] && COMPREPLY=( $(compgen -W "$opts" -- "$cur") )',
  "}",
  "complete -o default -F _simplecov simplecov"
].freeze
ZSH_MIDDLE =
[
  "  )",
  "  if (( CURRENT == 2 )); then",
  "    _describe -t commands 'simplecov command' commands",
  "    return",
  "  fi",
  '  case "$words[2]" in'
].freeze
ZSH_TAIL =
["  esac", "}", '_simplecov "$@"'].freeze

Instance Method Summary collapse

Instance Method Details

#bash(commands, options) ⇒ Object



43
44
45
# File 'lib/simplecov/cli/completions/scripts.rb', line 43

def bash(commands, options)
  (bash_head(commands) + bash_cases(options) + BASH_TAIL).join("\n")
end

#bash_cases(options) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/simplecov/cli/completions/scripts.rb', line 59

def bash_cases(options)
  shells = '    completions) COMPREPLY=( $(compgen -W "fish bash zsh -h --help" -- "$cur") ); return;;'
  cases = [shells] #: Array[String]
  options.each do |name, list|
    next if name.eql?("completions")

    words = list.flat_map { |option| [option.fetch(:short), option.fetch(:long)] }.compact.join(" ")
    cases << "    #{name}) opts=\"#{words}\";;"
  end
  cases
end

#bash_head(commands) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/simplecov/cli/completions/scripts.rb', line 47

def bash_head(commands)
  [
    "# Completions for simplecov. Generated by `simplecov completions bash`.",
    "_simplecov() {",
    '  local cur="${COMP_WORDS[COMP_CWORD]}" opts=""',
    '  if [ "$COMP_CWORD" -eq 1 ]; then',
    "    COMPREPLY=( $(compgen -W \"#{commands.collect(&:first).join(" ")}\" -- \"$cur\") ); return",
    "  fi",
    '  case "${COMP_WORDS[1]}" in'
  ]
end

#fish(commands, options) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/simplecov/cli/completions/scripts.rb', line 13

def fish(commands, options)
  lines = ["# Completions for simplecov. Generated by `simplecov completions fish`."] #: Array[String]
  commands.each do |name, desc|
    lines << "complete -c simplecov -f -n __fish_use_subcommand -a #{name} -d #{fish_quote(desc)}"
  end
  options.each { |name, list| list.each { |option| lines << fish_option(name, option) } }
  lines << "complete -c simplecov -f -n '__fish_seen_subcommand_from completions' -a 'fish bash zsh'"
  lines.join("\n")
end

#fish_option(name, option) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/simplecov/cli/completions/scripts.rb', line 23

def fish_option(name, option)
  parts = ["complete -c simplecov -n '__fish_seen_subcommand_from #{name}'"] #: Array[String]
  parts << "-s #{(_ = option.fetch(:short)).delete_prefix("-")}" if option.fetch(:short)
  parts << "-l #{option.fetch(:long).delete_prefix("--")}"
  parts << "-r" if option.fetch(:arg)
  parts << "-d #{fish_quote(option.fetch(:desc))}"
  parts.join(" ")
end

#fish_quote(text) ⇒ Object



32
33
34
# File 'lib/simplecov/cli/completions/scripts.rb', line 32

def fish_quote(text)
  "'#{text.gsub("\\") { "\\\\" }.gsub("'") { "\\'" }}'"
end

#zsh(commands, options) ⇒ Object



82
83
84
85
86
87
# File 'lib/simplecov/cli/completions/scripts.rb', line 82

def zsh(commands, options)
  head = ["#compdef simplecov", "# Completions for simplecov. Generated by `simplecov completions zsh`.",
    "_simplecov() {", "  local -a commands", "  commands=("]
  rows = commands.collect { |name, desc| "    '#{name}:#{zsh_quote(desc)}'" }
  (head + rows + ZSH_MIDDLE + zsh_cases(options) + ZSH_TAIL).join("\n")
end

#zsh_cases(options) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/simplecov/cli/completions/scripts.rb', line 89

def zsh_cases(options)
  options.collect do |name, list|
    specs = +""
    specs << "'1:shell:(fish bash zsh)' " if name.eql?("completions")
    list.each { |option| zsh_specs(option).each { |spec| specs << spec << " " } }
    "    #{name}) _arguments #{specs.chop};;"
  end
end

#zsh_quote(text) ⇒ Object



108
109
110
# File 'lib/simplecov/cli/completions/scripts.rb', line 108

def zsh_quote(text)
  text.gsub("'") { "'\\''" }
end

#zsh_specs(option) ⇒ Object

'--input=[desc]': the = marks an argument-taking switch, which optparse accepts in that spelling too. Brackets would end the description early, so they are dropped from it.



101
102
103
104
105
106
# File 'lib/simplecov/cli/completions/scripts.rb', line 101

def zsh_specs(option)
  desc = "[#{zsh_quote(option.fetch(:desc).delete("[]"))}]"
  specs = [] #: Array[String]
  specs << "'#{option.fetch(:short)}#{desc}'" if option.fetch(:short)
  specs << "'#{option.fetch(:long)}#{"=" if option.fetch(:arg)}#{desc}'"
end