Module: MilkTea::CLI::CommandCompletions

Included in:
MilkTea::CLI
Defined in:
lib/milk_tea/tooling/cli/commands/completions.rb

Instance Method Summary collapse

Instance Method Details

#completion_script(shell) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/milk_tea/tooling/cli/commands/completions.rb', line 18

def completion_script(shell)
  names = COMMANDS.map(&:first)
  case shell
  when "bash"
    [
      "# mtc bash completion. Source this file or install it into your bash",
      "# completion directory (e.g. /etc/bash_completion.d/mtc).",
      "_mtc() {",
      %(  local cur="${COMP_WORDS[COMP_CWORD]}"),
      %(  if [ "${COMP_CWORD}" -eq 1 ]; then),
      %(    COMPREPLY=( $(compgen -W "#{(names + %w[help version]).join(' ')}" -- "${cur}") )),
      "  fi",
      "}",
      "complete -F _mtc mtc",
    ].join("\n")
  when "zsh"
    lines = ["#compdef mtc", "# mtc zsh completion. Install onto your $fpath as _mtc.", "_mtc() {", "  local -a commands", "  commands=("]
    COMMANDS.each { |name, summary| lines << "    '#{name}:#{summary}'" }
    lines.concat(["  )", "  if (( CURRENT == 2 )); then", "    _describe 'mtc command' commands", "  fi", "}", %(_mtc "$@")])
    lines.join("\n")
  when "fish"
    lines = ["# mtc fish completion. Install into ~/.config/fish/completions/mtc.fish."]
    COMMANDS.each do |name, summary|
      lines << "complete -c mtc -f -n '__fish_use_subcommand' -a #{name} -d '#{summary}'"
    end
    lines.join("\n")
  end
end

#completions_commandObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/milk_tea/tooling/cli/commands/completions.rb', line 6

def completions_command
  shell = @argv.shift
  unless %w[bash zsh fish].include?(shell)
    @err.puts("completions: shell must be bash, zsh, or fish")
    print_command_help("completions", @err)
    return 1
  end

  @out.puts(completion_script(shell))
  0
end