Class: TTY::Prompt::Reader::History Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/tty/prompt/reader/history.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.

A class responsible for storing a history of all lines entered by user when interacting with shell prompt.

Constant Summary collapse

DEFAULT_SIZE =

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

Default maximum size

32 << 4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = DEFAULT_SIZE, options = {}) {|_self| ... } ⇒ History

Create a History buffer

param [Integer] max_size

the maximum size for history buffer

param [Hash] options

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :duplicates (Boolean)

    whether or not to store duplicates, true by default

Yields:

  • (_self)

Yield Parameters:



42
43
44
45
46
47
48
49
50
# File 'lib/tty/prompt/reader/history.rb', line 42

def initialize(max_size = DEFAULT_SIZE, options = {})
  @max_size   = max_size
  @index      = 0
  @history    = []
  @duplicates = options.fetch(:duplicates) { true }
  @exclude    = options.fetch(:exclude) { proc {} }
  @cycle      = options.fetch(:cycle) { false }
  yield self if block_given?
end

Instance Attribute Details

#cycleObject

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



26
27
28
# File 'lib/tty/prompt/reader/history.rb', line 26

def cycle
  @cycle
end

#duplicatesObject

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



28
29
30
# File 'lib/tty/prompt/reader/history.rb', line 28

def duplicates
  @duplicates
end

#excludeObject

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



30
31
32
# File 'lib/tty/prompt/reader/history.rb', line 30

def exclude
  @exclude
end

#indexObject (readonly)

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



24
25
26
# File 'lib/tty/prompt/reader/history.rb', line 24

def index
  @index
end

#max_sizeObject

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

Set and retrieve the maximum size of the buffer



22
23
24
# File 'lib/tty/prompt/reader/history.rb', line 22

def max_size
  @max_size
end

Instance Method Details

#[](index) ⇒ Object

Return line at the specified index

Raises:

  • (IndexError)

    index out of range



115
116
117
118
119
120
121
122
123
124
# File 'lib/tty/prompt/reader/history.rb', line 115

def [](index)
  if index < 0
    index += @history.size if index < 0
  end
  line = @history[index]
  if line.nil?
    raise IndexError, 'invalid index'
  end
  line.dup
end

#clearObject

Empty all history lines



138
139
140
141
# File 'lib/tty/prompt/reader/history.rb', line 138

def clear
  @history.clear
  @index = 0
end

#eachObject

Iterates over history lines



55
56
57
58
59
60
61
# File 'lib/tty/prompt/reader/history.rb', line 55

def each
  if block_given?
    @history.each { |line| yield line }
  else
    @history.to_enum
  end
end

#getObject

Get current line



129
130
131
132
133
# File 'lib/tty/prompt/reader/history.rb', line 129

def get
  return if size.zero?

  self[@index]
end

#nextObject

Move the pointer to the next line in the history



83
84
85
86
87
88
89
90
# File 'lib/tty/prompt/reader/history.rb', line 83

def next
  return if size.zero?
  if @index == size - 1
    @index = 0 if @cycle
  else
    @index += 1
  end
end

#next?Boolean

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/tty/prompt/reader/history.rb', line 92

def next?
  size > 0 && !(@index == size - 1 && !@cycle)
end

#previousObject

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

Move the pointer to the previous line in the history



97
98
99
100
101
102
103
104
# File 'lib/tty/prompt/reader/history.rb', line 97

def previous
  return if size.zero?
  if @index.zero?
    @index = size - 1 if @cycle
  else
    @index -= 1
  end
end

#previous?Boolean

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

Returns:

  • (Boolean)


106
107
108
# File 'lib/tty/prompt/reader/history.rb', line 106

def previous?
  size > 0 && !(@index < 0 && !@cycle)
end

#push(line) ⇒ Object Also known as: <<

Add the last typed line to history buffer

Parameters:

  • line (String)


68
69
70
71
72
73
74
75
76
77
# File 'lib/tty/prompt/reader/history.rb', line 68

def push(line)
  @history.delete(line) unless @duplicates
  return if line.to_s.empty? || @exclude[line]

  @history.shift if size >= max_size
  @history << line
  @index = @history.size - 1

  self
end