class PowerStencil::Dsl::Completion

Attributes

generating_project_completion[RW]
script_name[RW]
encountered_types[R]

Public Class Methods

new(universe) click to toggle source
Calls superclass method PowerStencil::Dsl::Base::new
# File lib/power_stencil/dsl/completion.rb, line 21
def initialize(universe)
  super
  @encountered_types = {}
end

Public Instance Methods

command_representation(command, enclosing_dquote: true) click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 70
def command_representation(command, enclosing_dquote: true)
  cr = "#{command.name}[#{command_summary command}]"
  enclosing_dquote ? "\"#{cr}\"" : cr
end
command_summary(command) click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 62
def command_summary(command)
  text = command.help[0][0,60].tr("\n", ' ')
  text.match(/^\s*(?<sentence>[^\s][^\.]+)\./) do |md|
    return "#{md['sentence']} ..."
  end
  text
end
context_commands() click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 34
def context_commands
  if generating_user_completion?
    PowerStencil.command_line_manager.commands.select do |command|
      command.providers.include? PowerStencil
    end
  else
    PowerStencil.command_line_manager.commands
  end
end
context_options(command) click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 44
def context_options(command)
  if generating_user_completion?
    command.options.select do |option|
      option.providers.include? PowerStencil
    end
  else
    command.options
  end
end
continued_multilines(lines, number_spaces: 4, cr: "\n", spaces_in_first_line: true, str_cont: ' \\') click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 101
def continued_multilines(lines, number_spaces: 4, cr: "\n", spaces_in_first_line: true, str_cont: ' \\')
  return '' if lines.empty?
  spaces = ' ' * number_spaces
  first_line = true
  res = lines.map do |line|
    if first_line
      fline = if spaces_in_first_line
                "#{spaces}#{line}"
              else
                line
              end
      first_line = false
      fline
    else
      "#{spaces}#{line}"
    end
  end
  eol = str_cont + cr
  res.join eol
end
default_command_param_type(command, enclosing_dquote: true) click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 75
def default_command_param_type(command, enclosing_dquote: true)
  return '' if %i(bool boolean).include? command.type
  default_cpt = "*:#{command.type}:_#{script_name}_#{command.type} "
  type = command.type

  encountered_types[type] ||=[]
  encountered_types[type] << command unless encountered_types[type].include? command
  enclosing_dquote ? "\"#{default_cpt}\"" : default_cpt
end
generating_project_completion?() click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 26
def generating_project_completion?
  self.class.generating_project_completion
end
generating_user_completion?() click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 30
def generating_user_completion?
  !self.class.generating_project_completion
end
option_representation(option, enclosing_dquote: true) click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 85
def option_representation(option, enclosing_dquote: true)
  opr = "--#{option.name}[#{option.summary}]"
  opr = if option.type.nil? or option.type.empty?
          opr
        else
          if %i(bool boolean).include? option.type
            opr
          else
            encountered_types[option.type] ||=[]
            encountered_types[option.type] << option unless encountered_types[option.type].include? option
            "#{opr}:#{option.type}:_#{ zsh_completion_from_type option.type} "
          end
        end
  enclosing_dquote ? "\"#{opr}\"" : opr
end
root_command(commands) click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 58
def root_command(commands)
  commands.select { |c| c.name.empty? }.first
end
script_name() click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 54
def script_name
  self.class.script_name
end
timestamp() click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 13
def timestamp
  if generating_user_completion?
    " on the #{Time.now}"
  else
    ''
  end
end
zsh_completion_from_type(type) click to toggle source
# File lib/power_stencil/dsl/completion.rb, line 122
def zsh_completion_from_type(type)
  slop_class = type.to_s.split('_').map(&:capitalize).join + 'Option'
  klass = Slop.const_get slop_class
  klass::ZSH_TYPE
end