Class: TTY2::Reader::Completions Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/tty2/reader/completions.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for storing and navigating completion suggestions

Instance Method Summary collapse

Constructor Details

#initializeCompletions

Create a Completions collection



19
20
21
22
# File 'lib/tty2/reader/completions.rb', line 19

def initialize
  @completions = []
  @index = 0
end

Instance Method Details

#clearObject

Clear current completions



27
28
29
30
# File 'lib/tty2/reader/completions.rb', line 27

def clear
  @completions.clear
  @index = 0
end

#concat(suggestions) ⇒ Object

Add completion suggestions

Parameters:

  • suggestions (Array<String>)

    the suggestions to add



56
57
58
# File 'lib/tty2/reader/completions.rb', line 56

def concat(suggestions)
  suggestions.each { |suggestion| @completions << suggestion.dup }
end

#each(&block) ⇒ Object

Iterate over all completions



63
64
65
66
67
68
69
# File 'lib/tty2/reader/completions.rb', line 63

def each(&block)
  if block_given?
    @completions.each(&block)
  else
    @completions.to_enum
  end
end

#first?Boolean

Check whether the current index is at the first completion or not

Returns:

  • (Boolean)


37
38
39
# File 'lib/tty2/reader/completions.rb', line 37

def first?
  @index.zero?
end

#getString

Retrieve completion at the current index

Returns:

  • (String)


76
77
78
# File 'lib/tty2/reader/completions.rb', line 76

def get
  @completions[@index]
end

#last?Boolean

Check whether the current index is at the last completion or not

Returns:

  • (Boolean)


46
47
48
# File 'lib/tty2/reader/completions.rb', line 46

def last?
  @index == size - 1
end

#nextObject

Move index to the next completion



83
84
85
86
87
88
89
90
91
# File 'lib/tty2/reader/completions.rb', line 83

def next
  return if size.zero?

  if @index == size - 1
    @index = 0
  else
    @index += 1
  end
end

#previousObject

Move index to the previous completion



96
97
98
99
100
101
102
103
104
# File 'lib/tty2/reader/completions.rb', line 96

def previous
  return if size.zero?

  if @index.zero?
    @index = size - 1
  else
    @index -= 1
  end
end