Module: Qcmd::InputCompleter

Defined in:
lib/qcmd/input_completer.rb

Constant Summary collapse

ReservedWords =

the commands listed here should represent every possible legal command

%w[
  connect exit quit workspace workspaces disconnect help
]
ReservedWorkspaceWords =
Qcmd::Commands::WORKSPACE
ReservedCueWords =
Qcmd::Commands::ALL_CUES
CompletionProc =
Proc.new {|input|
  # puts "input: #{ input }"

  matcher  = /^#{Regexp.escape(input)}/
  commands = ReservedWords.grep(matcher)

  if Qcmd.connected?
    # have selected a machine
    if Qcmd.context.workspace_connected?
      # have selected a workspace
      cue_numbers = Qcmd.context.workspace.cues.map(&:number)
      commands    = commands +
                    cue_numbers.grep(matcher) +
                    ReservedCueWords.grep(matcher) +
                    ReservedWorkspaceWords.grep(matcher)
    else
      # haven't selected a workspace yet
      names           = Qcmd.context.machine.workspace_names
      quoted_names    = names.map {|wn| %["#{wn}"]}
      workspace_names = (names + quoted_names).grep(matcher)
      workspace_names = quote_if_necessary workspace_names
      commands = commands + workspace_names
    end
  else
    # haven't selected a machine yet
    machine_names = Qcmd::Network.names
    quoted_names = machine_names.map {|mn| %["#{mn}"]}
    names = (quoted_names + machine_names).grep(matcher)
    names = quote_if_necessary(names)

    # unquote
    commands = commands + names
  end

  commands
}

Class Method Summary collapse

Class Method Details

.add_command(command) ⇒ Object



19
20
21
# File 'lib/qcmd/input_completer.rb', line 19

def self.add_command command
  add_commands([command])
end

.add_commands(commands) ⇒ Object



14
15
16
17
# File 'lib/qcmd/input_completer.rb', line 14

def self.add_commands commands
  ReservedWords.push(*commands)
  ReservedWords.uniq!
end

.quote_if_necessary(names) ⇒ Object

if the name of a thing has a space in it, it must be surrounded by double quotes to be properly handled by the parser. so before we pass back unquoted space-containing results, we must quote them.



64
65
66
67
68
69
70
71
72
73
# File 'lib/qcmd/input_completer.rb', line 64

def quote_if_necessary names
  names.map do |name|
    if / / =~ name && /"/ !~ name
      # if name has a space and is not already quoted
      %["#{ name }"]
    else
      name
    end
  end
end