Module: Doing::Completion

Defined in:
lib/doing/completion.rb

Overview

Completion script generator

Class Method Summary collapse

Class Method Details

.generate_completion(type: 'zsh', file: 'stdout') ⇒ Object

Generate a completion script and output to file or stdout

Parameters:

  • type (String) (defaults to: 'zsh')

    shell to generate for (zsh|bash|fish)

  • file (String) (defaults to: 'stdout')

    Path to save to, or 'stdout'



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/doing/completion.rb', line 21

def generate_completion(type: 'zsh', file: 'stdout')

  generator = case type.to_s
              when /^f/
                FishCompletions.new
              when /^b/
                BashCompletions.new
              else
                ZshCompletions.new
              end

  result = generator.generate_completions

  if file =~ /^stdout$/i
    $stdout.puts result
  else
    File.open(File.expand_path(file), 'w') do |f|
      f.puts result
    end
    Doing.logger.warn('File written:', "#{type} completions written to #{file}")
  end
end