Class: Doing::Completion::ZshCompletions

Inherits:
Object
  • Object
show all
Defined in:
lib/doing/completion/zsh_completion.rb

Overview

Generate completions for zsh

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeZshCompletions

Returns a new instance of ZshCompletions.



92
93
94
95
96
97
98
99
# File 'lib/doing/completion/zsh_completion.rb', line 92

def initialize
  data = Completion.get_help_sections
  @global_options = Completion.parse_options(data[:global_options])
  @commands = Completion.parse_commands(data[:commands])
  @bar = TTY::ProgressBar.new(" \033[0;0;33mGenerating Zsh completions: \033[0;35;40m[:bar] :status\033[0m", total: @commands.count + 1, bar_format: :square, hide_cursor: true, status: 'processing subcommands')
  width = TTY::Screen.columns - 45
  @bar.resize(width)
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



13
14
15
# File 'lib/doing/completion/zsh_completion.rb', line 13

def commands
  @commands
end

#global_optionsObject

Returns the value of attribute global_options.



13
14
15
# File 'lib/doing/completion/zsh_completion.rb', line 13

def global_options
  @global_options
end

Instance Method Details

#generate_completionsObject



101
102
103
104
# File 'lib/doing/completion/zsh_completion.rb', line 101

def generate_completions
  @bar.start
  generate_helpers
end

#generate_helpersObject



15
16
17
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
46
47
48
# File 'lib/doing/completion/zsh_completion.rb', line 15

def generate_helpers
  out=<<~EOFUNCTIONS
    compdef _doing doing

    function _doing() {
        local line state

        function _commands {
            local -a commands

            commands=(
                      #{generate_subcommand_completions.join("\n                  ")}
            )
            _describe 'command' commands
        }

        _arguments -C \
                "1: :_commands" \
                "*::arg:->args"



        case $line[1] in
            #{generate_subcommand_option_completions(indent: '            ').join("\n            ")}
        esac

        _arguments -s $args
    }

  EOFUNCTIONS
  @bar.advance(status: '')
  @bar.finish
  out
end

#generate_subcommand_completionsObject



50
51
52
53
54
55
56
57
58
# File 'lib/doing/completion/zsh_completion.rb', line 50

def generate_subcommand_completions
  out = []
  @commands.each_with_index do |cmd, i|
    cmd[:commands].each do |c|
      out << "'#{c}:#{cmd[:description].gsub(/'/, '\\\'')}'"
    end
  end
  out
end

#generate_subcommand_option_completions(indent: ' ') ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/doing/completion/zsh_completion.rb', line 60

def generate_subcommand_option_completions(indent: '        ')

  out = []

  @commands.each_with_index do |cmd, i|
    @bar.advance(status: cmd[:commands].first)

    data = Completion.get_help_sections(cmd[:commands].first)
    option_arr = []

    if data[:command_options]
      Completion.parse_options(data[:command_options]).each do |option|
        next if option.nil?

        arg = option[:arg] ? ":#{option[:arg]}:" : ''

        option_arr << if option[:short]
                        %({'(--#{option[:long]})-#{option[:short]}','(-#{option[:short]})--#{option[:long]}'}"[#{option[:description].sanitize}]#{arg}")
                      else
                        %("--#{option[:long]}[#{option[:description].sanitize}]#{arg}")
                      end
      end
    end

    cmd[:commands].each do |c|
      out << "#{c}) \n#{indent}    args=( #{option_arr.join(' ')} )\n#{indent};;"
    end
  end

  out
end