Module: HeapScope::CLI::Completion

Defined in:
lib/heapscope/cli/completion.rb

Overview

Shell completion script generators (local; no network).

Constant Summary collapse

NAMES =
Help::COMMANDS.keys.freeze

Class Method Summary collapse

Class Method Details

.bashObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/heapscope/cli/completion.rb', line 21

def bash
  list = NAMES.join(" ")
  <<~BASH
    # heapscope bash completion — eval "$(heapscope completion bash)"
    _heapscope() {
      local cur="${COMP_WORDS[COMP_CWORD]}"
      if [[ ${COMP_CWORD} -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "#{list}" -- "$cur") )
      else
        COMPREPLY=( $(compgen -f -- "$cur") )
      fi
    }
    complete -F _heapscope heapscope
  BASH
end

.generate(shell) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/heapscope/cli/completion.rb', line 11

def generate(shell)
  case shell.to_s.downcase
  when "bash" then bash
  when "zsh" then zsh
  when "powershell", "pwsh" then powershell
  else
    raise ArgumentError, "unsupported shell: #{shell} (bash|zsh|powershell)"
  end
end

.powershellObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/heapscope/cli/completion.rb', line 57

def powershell
  list = NAMES.map { |n| "'#{n}'" }.join(", ")
  <<~PS
    # heapscope PowerShell completion — heapscope completion powershell | Out-String | Invoke-Expression
    Register-ArgumentCompleter -CommandName heapscope -ScriptBlock {
      param($wordToComplete, $commandAst, $cursorPosition)
      $commands = @(#{list})
      $commands | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
        [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
      }
    }
  PS
end

.zshObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/heapscope/cli/completion.rb', line 37

def zsh
  descriptions = NAMES.map do |n|
    desc = Help::COMMANDS[n].to_s.tr("'", " ")
    "'#{n}:#{desc}'"
  end.join(" ")
  <<~ZSH
    # heapscope zsh completion — eval "$(heapscope completion zsh)"
    _heapscope() {
      local -a cmds
      cmds=(#{descriptions})
      if (( CURRENT == 2 )); then
        _describe 'command' cmds
      else
        _files
      fi
    }
    compdef _heapscope heapscope
  ZSH
end