Class: SimpleScripting::TabCompletion::CommandlineProcessor

Inherits:
Struct
  • Object
show all
Defined in:
lib/simple_scripting/tab_completion/commandline_processor.rb

Constant Summary collapse

BASE_CURSOR_MARKER =

Arbitrary; can be anything (except an empty string).

"<tab>"
OPTIONS_TERMINATOR =
"--"
LONG_OPTIONS_PREFIX =
"--"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cursor_markerObject

Returns the value of attribute cursor_marker

Returns:

  • (Object)

    the current value of cursor_marker



11
12
13
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 11

def cursor_marker
  @cursor_marker
end

#processed_argvObject

Returns the value of attribute processed_argv

Returns:

  • (Object)

    the current value of processed_argv



11
12
13
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 11

def processed_argv
  @processed_argv
end

#switches_definitionObject

Returns the value of attribute switches_definition

Returns:

  • (Object)

    the current value of switches_definition



11
12
13
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 11

def switches_definition
  @switches_definition
end

Class Method Details

.process_commandline(source_commandline, cursor_position, switches_definition) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 19

def self.process_commandline(source_commandline, cursor_position, switches_definition)
  # An input string with infinite "<tabN>" substrings will cause an infinite cycle (hehe).
  0.upto(Float::INFINITY) do |i|
    cursor_marker = BASE_CURSOR_MARKER.sub(">", "#{i}>")

    if !source_commandline.include?(cursor_marker)
      commandline_with_marker = source_commandline[0...cursor_position] + cursor_marker + source_commandline[cursor_position..-1].to_s

      # Remove the executable.
      processed_argv = Shellwords.split(commandline_with_marker)[1..-1]

      return new(processed_argv, cursor_marker, switches_definition)
    end
  end
end

Instance Method Details

#completing_an_option?Boolean

We’re abstracted from the commandline, with this exception. This is because while an option is being completed, the decoder would not recognize the key.

Returns:

  • (Boolean)


38
39
40
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 38

def completing_an_option?
  processed_argv[marked_word_position].start_with?(LONG_OPTIONS_PREFIX) && marked_word_position < options_terminator_position
end

#completing_word_prefixObject



46
47
48
49
50
51
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 46

def completing_word_prefix
  word = processed_argv[marked_word_position]

  # Regex alternative: [/\A(.*?)#{cursor_marker}/m, 1]
  word[0, word.index(cursor_marker)]
end

#parsed_pairsObject

Returns key, value prefix (before marker), value suffix (after marker), other_pairs



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 55

def parsed_pairs
  parsed_pairs = parse_argv || raise("Parsing error")

  key, value = parsed_pairs.detect do |_, value|
    !boolean?(value) && value.include?(cursor_marker)
  end

  # Impossible case, unless there is a programmatic error.
  #
  key || raise("Guru meditation! (#{self.class}##{__method__}:#{__LINE__})")

  value_prefix, value_suffix = value.split(cursor_marker)

  parsed_pairs.delete(key)

  [key, value_prefix || "", value_suffix || "", parsed_pairs]
end

#parsing_error?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 42

def parsing_error?
  parse_argv.nil?
end