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

#escaped_dashObject

Returns the value of attribute escaped_dash

Returns:

  • (Object)

    the current value of escaped_dash



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

def escaped_dash
  @escaped_dash
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
34
35
36
37
38
# 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]

      # Shellwords strips the backslash, losing the information that the word is a value, not
      # an option.
      #
      escaped_dash = commandline_with_marker.match?(/(?:\A|\s)\\-\S*#{Regexp.escape(cursor_marker)}/)

      return new(processed_argv, cursor_marker, switches_definition, escaped_dash)
    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)


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

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

#completing_word_prefixObject



51
52
53
54
55
56
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 51

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



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
# File 'lib/simple_scripting/tab_completion/commandline_processor.rb', line 60

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

  key, value = parsed_pairs.detect do |_, value|
    if value.is_a?(Array)
      value.any? { |entry| entry.include?(cursor_marker) }
    else
      !boolean?(value) && value.include?(cursor_marker)
    end
  end

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

  value = value.detect { |entry| entry.include?(cursor_marker) } if value.is_a?(Array)

  value_prefix, value_suffix = value.split(cursor_marker)

  value_prefix = "-#{value_prefix}" if escaped_dash

  parsed_pairs.delete(key)

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

#parsing_error?Boolean

Returns:

  • (Boolean)


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

def parsing_error?
  parse_argv.nil?
end