Class: RawLine::HistoryBuffer

Inherits:
Array
  • Object
show all
Defined in:
lib/rawline/history_buffer.rb

Overview

The HistoryBuffer class is used to hold the editor and line histories, as well as word completion matches.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) {|_self| ... } ⇒ HistoryBuffer

Create an instance of RawLine::HistoryBuffer. This method takes an optional block used to override the following instance attributes:

  • @duplicates - whether or not duplicate items will be stored in the buffer.

  • @exclude - a Proc object defining exclusion rules to prevent items from being added to the buffer.

  • @cycle - Whether or not the buffer is cyclic.

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
# File 'lib/rawline/history_buffer.rb', line 32

def initialize(size)
  @duplicates = true
  @exclude = lambda{|a|}
  @cycle = false
  yield self if block_given?
  @size = size
  @position = nil
end

Instance Attribute Details

#cycleObject

Returns the value of attribute cycle.



22
23
24
# File 'lib/rawline/history_buffer.rb', line 22

def cycle
  @cycle
end

#duplicatesObject

Returns the value of attribute duplicates.



22
23
24
# File 'lib/rawline/history_buffer.rb', line 22

def duplicates
  @duplicates
end

#excludeObject

Returns the value of attribute exclude.



22
23
24
# File 'lib/rawline/history_buffer.rb', line 22

def exclude
  @exclude
end

#positionObject (readonly)

Returns the value of attribute position.



21
22
23
# File 'lib/rawline/history_buffer.rb', line 21

def position
  @position
end

#sizeObject (readonly)

Returns the value of attribute size.



21
22
23
# File 'lib/rawline/history_buffer.rb', line 21

def size
  @size
end

Instance Method Details

#back(options = {}) ⇒ Object

Decrement @position. By default the history will become positioned at the previous item.

If @cycle is set to true then the history will cycle to the end when it finds itself at the beginning. If false calling this when at the beginning will result in the position not changing.

If a search strategy is assigned then the method search_backward will be called on the search strategy to determine the position. This method is given any passed in options as well as a :history option. The :history option will be a reference to self.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rawline/history_buffer.rb', line 108

def back(options={})
  return nil unless length > 0

  case @position
  when nil then
    @position = length-1
  when 0 then
    @position = length-1 if @cycle
  else
    @position -= 1
  end
end

#clear_positionObject

Clears the current position on the history object. Useful when deciding to cancel/reset history navigation.



45
46
47
# File 'lib/rawline/history_buffer.rb', line 45

def clear_position
  @position = nil
end

#emptyObject

Clear the content of the buffer and reset @position to nil.



67
68
69
70
# File 'lib/rawline/history_buffer.rb', line 67

def empty
  @position = nil
  clear
end

#end?Boolean

Return true if @position is at the end of the buffer.

Returns:

  • (Boolean)


84
85
86
# File 'lib/rawline/history_buffer.rb', line 84

def end?
  @position == length-1
end

#forward(options = {}) ⇒ Object

Increment @position. By default the history will become positioned at the next item.

If @cycle is set to true then the history will cycle back to the beginning when it finds itself at the end. If false calling this when at the end will result in the position not changing.

If a search strategy is assigned then the method search_forward will be called on the search strategy to determine the position. This method is given any passed in options as well as a :history option. The :history option will be a reference to self. If <tt>



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rawline/history_buffer.rb', line 134

def forward(options={})
  return nil unless length > 0

  case @position
  when nil then
    @position = 0
  when length-1 then
    @position = 0 if @cycle
  else
    @position += 1
  end
end

#getObject

Retrieve a copy of the element at @position.



75
76
77
78
79
# File 'lib/rawline/history_buffer.rb', line 75

def get
  return nil unless length > 0
  return nil unless @position
  at(@position).dup
end

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

Add a new item to the buffer.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rawline/history_buffer.rb', line 150

def push(item)

  if !@duplicates && self[-1] == item
    # skip adding this line
    return
  end

  unless @exclude.call(item)
    # Remove the oldest element if size is exceeded
    if @size <= length
      reverse!.pop
      reverse!
    end
    # Add the new item and reset the position
    super(item)
    @position = nil
  end
end

#resize(new_size) ⇒ Object

Resize the buffer, resetting @position to nil.



56
57
58
59
60
61
62
# File 'lib/rawline/history_buffer.rb', line 56

def resize(new_size)
  if new_size < @size
    @size-new_size.times { pop }
  end
  @size = new_size
  @position = nil
end

#searching?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rawline/history_buffer.rb', line 49

def searching?
  !!@position
end

#start?Boolean

Return true if @position is at the start of the buffer.

Returns:

  • (Boolean)


91
92
93
# File 'lib/rawline/history_buffer.rb', line 91

def start?
  @position == 0
end