Class: Doing::Completion::FishCompletions

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

Overview

Generate completions for Fish

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFishCompletions

Returns a new instance of FishCompletions.



231
232
233
234
235
236
237
238
# File 'lib/doing/completion/fish_completion.rb', line 231

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 Fish 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.



8
9
10
# File 'lib/doing/completion/fish_completion.rb', line 8

def commands
  @commands
end

#global_optionsObject

Returns the value of attribute global_options.



8
9
10
# File 'lib/doing/completion/fish_completion.rb', line 8

def global_options
  @global_options
end

Instance Method Details

#generate_completionsObject



240
241
242
243
244
245
246
247
248
249
# File 'lib/doing/completion/fish_completion.rb', line 240

def generate_completions
  @bar.start
  out = []
  out << generate_helpers
  out << generate_subcommand_completions
  out << generate_subcommand_option_completions
  @bar.advance(status: '✅')
  @bar.finish
  out.join("\n")
end

#generate_helpersObject



10
11
12
13
14
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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/doing/completion/fish_completion.rb', line 10

def generate_helpers
  "    function __fish_doing_needs_command\n      # Figure out if the current invocation already has a command.\n\n      set -l opts color h-help config_file= f-doing_file= n-notes v-version stdout debug default x-noauto no p-pager q-quiet yes\n      set cmd (commandline -opc)\n      set -e cmd[1]\n      argparse -s $opts -- $cmd 2>/dev/null\n      or return 0\n      # These flags function as commands, effectively.\n      if set -q argv[1]\n        # Also print the command, so this can be used to figure out what it is.\n        echo $argv[1]\n        return 1\n      end\n      return 0\n    end\n\n    function __fish_doing_using_command\n      set -l cmd (__fish_doing_needs_command)\n      test -z \"$cmd\"\n      and return 1\n      contains -- $cmd $argv\n      and return 0\n    end\n\n    function __fish_doing_cache_timer_expired\n      set -l timer __fish_doing_cache_timer_$argv[1]\n      if not set -q $timer\n        set -g $timer (date '+%s')\n      end\n\n      if test (math (date '+%s') - $$timer) -gt $argv[2]\n        set -g $timer (date '+%s')\n        return 1\n      end\n\n      return 0\n    end\n\n    function __fish_doing_subcommands\n      if not set -q __fish_doing_subcommands_cache\n        or __fish_doing_cache_timer_expired subcommands 86400\n        set -g -a __fish_doing_subcommands_cache (doing help -c)\n      end\n      printf '%s\\n' $__fish_doing_subcommands_cache\n    end\n\n    function __fish_doing_complete_sections\n      if not set -q __fish_doing_sections_cache\n        or __fish_doing_cache_timer_expired sections 3600\n        set -g -a __fish_doing_sections_cache (doing sections -c)\n      end\n      printf '%s\\n' $__fish_doing_sections_cache\n      __fish_doing_complete_show_tag\n    end\n\n    function __fish_doing_complete_views\n      if not set -q __fish_doing_views_cache\n        or __fish_doing_cache_timer_expired views 3600\n        set -g -a __fish_doing_views_cache (doing views -c)\n      end\n      printf '%s\\n' $__fish_doing_views_cache\n    end\n\n    function __fish_doing_export_plugin\n      if not set -q __fish_doing_export_plugin_cache\n        or __fish_doing_cache_timer_expired export_plugins 3600\n        set -g -a __fish_doing_export_plugin_cache (doing plugins --type export -c)\n      end\n      printf '%s\\n' $__fish_doing_export_plugin_cache\n    end\n\n    function __fish_doing_import_plugin\n      if not set -q __fish_doing_import_plugin_cache\n        or __fish_doing_cache_timer_expired import_plugins 3600\n        set -g -a __fish_doing_import_plugin_cache (doing plugins --type import -c)\n      end\n      printf '%s\\n' $__fish_doing_import_plugin_cache\n    end\n\n    function __fish_doing_complete_template\n      if not set -q __fish_doing_template_cache\n        or __fish_doing_cache_timer_expired template 3600\n        set -g -a __fish_doing_template_cache (doing template -c)\n      end\n      printf '%s\\n' $__fish_doing_template_cache\n    end\n\n    function __fish_doing_complete_tag\n      if not set -q __fish_doing_tag_cache\n        or __fish_doing_cache_timer_expired tags 60\n        set -g -a __fish_doing_tag_cache (doing tags)\n      end\n      printf '%s\\n' $__fish_doing_tag_cache\n    end\n\n    function __fish_doing_complete_show_tag\n      if not set -q __fish_doing_tag_cache\n        or __fish_doing_cache_timer_expired tags 60\n        set -g -a __fish_doing_tag_cache (doing tags)\n      end\n      printf '@%s\\n' $__fish_doing_tag_cache\n    end\n\n    function __fish_doing_complete_args\n      for cmd in (doing commands_accepting -c $argv[1])\n        complete -x -c doing -l $argv[1] -n \"__fish_doing_using_command $cmd\" -a \"(__fish_doing_complete_$argv[1])\"\n      end\n    end\n\n    complete -c doing -f\n    complete -xc doing -n '__fish_doing_needs_command' -a '(__fish_doing_subcommands)'\n\n    complete -f -c doing -n '__fish_doing_using_command show' -a '(__fish_doing_complete_sections)'\n    complete -f -c doing -n '__fish_doing_using_command view' -a '(__fish_doing_complete_views)'\n    complete -f -c doing -n '__fish_doing_using_command template' -a '(__fish_doing_complete_templates)'\n    complete -f -c doing -s t -l type -x -n '__fish_doing_using_command import' -a '(__fish_doing_import_plugins)'\n    complete -f -c doing -n '__fish_doing_using_command help' -a '(__fish_doing_subcommands)'\n\n    # complete -xc doing -n '__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from (doing help -c)' -a \"(doing help -c)\"\n\n    function __fish_doing_complete_args\n      for cmd in (doing commands_accepting -c $argv[1])\n        complete -x -c doing -l $argv[1] -n \"__fish_doing_using_command $cmd\" -a \"(__fish_doing_complete_$argv[1])\"\n      end\n    end\n\n    __fish_doing_complete_args tag\n  EOFUNCTIONS\nend\n"

#generate_subcommand_completionsObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/doing/completion/fish_completion.rb', line 143

def generate_subcommand_completions
  out = []
  @commands.each do |cmd|
    desc = Shellwords.escape(cmd[:description])
    cmds = cmd[:commands].join(' ')
    out << "complete -xc doing -n '__fish_doing_needs_command' -a '#{cmds}' -d #{desc}"
  end

  out.join("\n")
end

#generate_subcommand_option_completionsObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/doing/completion/fish_completion.rb', line 154

def generate_subcommand_option_completions

  out = []
  need_export = []
  need_bool = []
  need_case = []
  need_sort = []
  need_tag_sort = []
  need_tag_order = []
  need_age = []
  need_section = []

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

    if data[:synopsis].join(' ').strip.split(/ /).last =~ /(path|file)/i
      out << "complete -c doing -F -n '__fish_doing_using_command #{cmd[:commands].join(" ")}'"
    end

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

        arg = option[:arg] ? '-r' : ''
        short = option[:short] ? "-s #{option[:short]}" : ''
        long = option[:long] ? "-l #{option[:long]}" : ''
        out << "complete -c doing #{long} #{short} -f #{arg} -n '__fish_doing_using_command #{cmd[:commands].join(' ')}' -d #{Shellwords.escape(option[:description])}"

        need_export.concat(cmd[:commands]) if option[:long] == 'output'
        need_bool.concat(cmd[:commands]) if option[:long] == 'bool'
        need_case.concat(cmd[:commands]) if option[:long] == 'case'
        need_sort.concat(cmd[:commands]) if option[:long] == 'sort'
        need_tag_sort.concat(cmd[:commands]) if option[:long] == 'tag_sort'
        need_tag_order.concat(cmd[:commands]) if option[:long] == 'tag_order'
        need_age.concat(cmd[:commands]) if option[:long] == 'age'
        need_section.concat(cmd[:commands]) if option[:long] == 'section'
      end
    end
  end

  unless need_export.empty?
    out << "complete -f -c doing -s o -l output -x -n '__fish_doing_using_command #{need_export.join(' ')}' -a '(__fish_doing_export_plugin)'"
  end

  unless need_bool.empty?
    out << "complete -f -c doing -s b -l bool -x -n '__fish_doing_using_command #{need_bool.join(' ')}' -a 'and or not pattern'"
  end

  unless need_case.empty?
    out << "complete -f -c doing -l case -x -n '__fish_doing_using_command #{need_case.join(' ')}' -a 'case-sensitive ignore smart'"
  end

  unless need_sort.empty?
    out << "complete -f -c doing -l sort -x -n '__fish_doing_using_command #{need_sort.join(' ')}' -a 'asc desc'"
  end

  unless need_tag_sort.empty?
    out << "complete -f -c doing -l tag_sort -x -n '__fish_doing_using_command #{need_tag_sort.join(' ')}' -a 'name time'"
  end

  unless need_tag_order.empty?
    out << "complete -f -c doing -l tag_order -x -n '__fish_doing_using_command #{need_tag_order.join(' ')}' -a 'asc desc'"
  end

  unless need_age.empty?
    out << "complete -f -c doing -s a -l age -x -n '__fish_doing_using_command #{need_age.join(' ')}' -a 'oldest newest'"
  end

  unless need_section.empty?
    out << "complete -f -c doing -s s -l section -x -n '__fish_doing_using_command #{need_section.join(' ')}' -a '(__fish_doing_complete_sections)'"
  end

  # clear
  out.join("\n")
end