Class: Doing::Completion::FigCompletions

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

Overview

Generate completions for zsh

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFigCompletions

Returns a new instance of FigCompletions.



106
107
108
109
110
111
112
113
# File 'lib/doing/completion/fig_completion.rb', line 106

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 Fig 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/fig_completion.rb', line 13

def commands
  @commands
end

#global_optionsObject

Returns the value of attribute global_options.



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

def global_options
  @global_options
end

Instance Method Details

#generate_completionsObject



115
116
117
118
# File 'lib/doing/completion/fig_completion.rb', line 115

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
# File 'lib/doing/completion/fig_completion.rb', line 15

def generate_helpers
  out=<<~EOFUNCTIONS
  const completionSpec: Fig.Spec = {
    name: "doing",
    description: "A CLI for a What Was I Doing system",
    subcommands: [
      #{generate_subcommand_completions.join("\n    ")}
    ],
  };
  export default completionSpec;
  EOFUNCTIONS
  @bar.advance(status: '')
  @bar.finish
  out
end

#generate_subcommand_completionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/doing/completion/fig_completion.rb', line 31

def generate_subcommand_completions
  out = []
  indent = '      '
  @commands.each do |cmd|
    cmd[:commands].each do |c|
      out << <<~EOCOMMAND
        {
        #{indent}name: "#{c}",
        #{indent}description: "#{cmd[:description].sanitize}",
        #{indent}#{generate_subcommand_option_completions(cmd)}
            },
      EOCOMMAND
    end
  end

  out
end

#generate_subcommand_option_completions(cmd, indent: ' ') ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/doing/completion/fig_completion.rb', line 49

def generate_subcommand_option_completions(cmd, indent: '          ')
  out = []

  @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 = ''

      if option[:arg]
        arg =<<~EOARG
        args: {
        #{indent}        name: "#{option[:arg]}",
        #{indent}        description: "#{option[:arg]}",
        #{indent}  },
        EOARG
      end

      if option[:short]
        opt_data =<<~EOOPT
        {
        #{indent}  name: ["-#{option[:short]}", "--#{option[:long]}"],
        #{indent}  description: "#{option[:description].sanitize}",
        #{indent}  #{arg}
        #{indent}},
        EOOPT
      else
        opt_data = <<~EOOPT
        {
        #{indent}  name: ["--#{option[:long]}"],
        #{indent}  description: "#{option[:description].sanitize}",
        #{indent}  #{arg}
        #{indent}},
        EOOPT
      end

      option_arr << opt_data

    end

    cmd_opts = <<~EOCMD
      options: [
      #{indent}#{option_arr.join("\n#{indent}")}
              ],
    EOCMD
    out << cmd_opts
  end

  out.join("\n")
end