Class: Toys::Completion::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/completion.rb

Overview

The context in which to determine completion candidates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cli:, previous_words: [], fragment_prefix: "", fragment: "", **params) ⇒ Context

Create a completion context

Parameters:

  • cli (Toys::CLI)

    The CLI being run. Required.

  • previous_words (Array<String>) (defaults to: [])

    Array of complete strings that appeared prior to the fragment to complete.

  • fragment_prefix (String) (defaults to: "")

    A prefix in the fragment that does not participate in completion. (e.g. "key=")

  • fragment (String) (defaults to: "")

    The string fragment to complete.

  • params (Hash)

    Miscellaneous context data



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/toys/completion.rb', line 51

def initialize(cli:, previous_words: [], fragment_prefix: "", fragment: "", **params)
  @cli = cli
  @previous_words = previous_words
  @fragment_prefix = fragment_prefix
  @fragment = fragment
  extra_params = {
    cli: cli, previous_words: previous_words, fragment_prefix: fragment_prefix,
    fragment: fragment
  }
  @params = params.merge(extra_params)
  @tool = nil
  @args = nil
  @arg_parser = nil
end

Instance Attribute Details

#cliToys::CLI (readonly)

The CLI being run.

Returns:



80
81
82
# File 'lib/toys/completion.rb', line 80

def cli
  @cli
end

#fragmentString (readonly)

The current string fragment to complete

Returns:

  • (String)


98
99
100
# File 'lib/toys/completion.rb', line 98

def fragment
  @fragment
end

#fragment_prefixString (readonly)

A non-completed prefix for the current fragment.

Returns:

  • (String)


92
93
94
# File 'lib/toys/completion.rb', line 92

def fragment_prefix
  @fragment_prefix
end

#previous_wordsArray<String> (readonly)

All previous words.

Returns:

  • (Array<String>)


86
87
88
# File 'lib/toys/completion.rb', line 86

def previous_words
  @previous_words
end

Instance Method Details

#[](key) ⇒ Object Also known as: get

Get data for arbitrary key.

Parameters:

  • key (Symbol)

Returns:

  • (Object)


105
106
107
# File 'lib/toys/completion.rb', line 105

def [](key)
  @params[key]
end

#arg_parserToys::ArgParser

Current ArgParser indicating the status of argument parsing up to this point.

Returns:



135
136
137
138
# File 'lib/toys/completion.rb', line 135

def arg_parser
  lookup_tool
  @arg_parser ||= ArgParser.new(@cli, @tool).parse(@args)
end

#argsArray<String>

An array of complete arguments passed to the tool, prior to the fragment to complete.

Returns:

  • (Array<String>)


124
125
126
127
# File 'lib/toys/completion.rb', line 124

def args
  lookup_tool
  @args
end

#toolToys::Tool

The tool being invoked, which should control the completion.

Returns:



114
115
116
117
# File 'lib/toys/completion.rb', line 114

def tool
  lookup_tool
  @tool
end

#with(**delta_params) ⇒ Toys::Completion::Context

Create a new completion context with the given modifications.

Parameters:

  • delta_params (Hash)

    Replace context data.

Returns:



72
73
74
# File 'lib/toys/completion.rb', line 72

def with(**delta_params)
  Context.new(**@params.merge(delta_params))
end