class Clamp::Completion::ParamsSatisfiedHelper

Generates a fish function that checks whether required parameters have been provided before offering subcommand completions.

Public Class Methods

new(executable_name, path, required_count) click to toggle source
# File lib/clamp/completion/fish_generator.rb, line 157
def initialize(executable_name, path, required_count)
  @executable_name = executable_name
  @path = path
  @required_count = required_count
end

Public Instance Methods

function_name() click to toggle source
# File lib/clamp/completion/fish_generator.rb, line 163
def function_name
  parts = [@executable_name]
  @path.each { |sub| parts << sub.names.first }
  "_clamp_complete_#{parts.join('_')}_params_satisfied"
end
to_s() click to toggle source
# File lib/clamp/completion/fish_generator.rb, line 169
def to_s
  lines = ["function #{function_name}", "    set -l tokens (commandline -opc)", "    set -l positional 0"]
  if @path.empty?
    count_positional_root(lines)
  else
    count_positional_nested(lines)
  end
  lines.push("    test $positional -ge #{@required_count}", "end")
  lines.join("\n")
end

Private Instance Methods

count_positional_nested(lines) click to toggle source
# File lib/clamp/completion/fish_generator.rb, line 189
def count_positional_nested(lines)
  lines.push("    set -l depth 0", "    for token in $tokens[2..]", "        switch $depth")
  @path.each_with_index do |sub, i|
    all_names = sub.names.join(" ")
    lines.push("            case #{i}",
               "                if contains -- $token #{all_names}",
               "                    set depth #{i + 1}", "                end")
  end
  lines.push("            case #{@path.length}",
             "                if not string match -q -- '-*' $token",
             "                    set positional (math $positional + 1)",
             "                end", "        end", "    end")
end
count_positional_root(lines) click to toggle source
# File lib/clamp/completion/fish_generator.rb, line 182
def count_positional_root(lines)
  lines.push("    for token in $tokens[2..]",
             "        if not string match -q -- '-*' $token",
             "            set positional (math $positional + 1)",
             "        end", "    end")
end