Class: RawLine::HistoryBuffer
- Inherits:
-
Array
- Object
- Array
- RawLine::HistoryBuffer
- 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
-
#cycle ⇒ Object
Returns the value of attribute cycle.
-
#duplicates ⇒ Object
Returns the value of attribute duplicates.
-
#exclude ⇒ Object
Returns the value of attribute exclude.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#back(options = {}) ⇒ Object
Decrement
@position. -
#clear_position ⇒ Object
Clears the current position on the history object.
-
#empty ⇒ Object
Clear the content of the buffer and reset
@positionto nil. -
#end? ⇒ Boolean
Return true if
@positionis at the end of the buffer. -
#forward(options = {}) ⇒ Object
Increment
@position. -
#get ⇒ Object
Retrieve a copy of the element at
@position. -
#initialize(size) {|_self| ... } ⇒ HistoryBuffer
constructor
Create an instance of RawLine::HistoryBuffer.
-
#push(item) ⇒ Object
(also: #<<)
Add a new item to the buffer.
-
#resize(new_size) ⇒ Object
Resize the buffer, resetting
@positionto nil. - #searching? ⇒ Boolean
-
#start? ⇒ Boolean
Return true if
@positionis at the start of the buffer.
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.
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
#cycle ⇒ Object
Returns the value of attribute cycle.
22 23 24 |
# File 'lib/rawline/history_buffer.rb', line 22 def cycle @cycle end |
#duplicates ⇒ Object
Returns the value of attribute duplicates.
22 23 24 |
# File 'lib/rawline/history_buffer.rb', line 22 def duplicates @duplicates end |
#exclude ⇒ Object
Returns the value of attribute exclude.
22 23 24 |
# File 'lib/rawline/history_buffer.rb', line 22 def exclude @exclude end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
21 22 23 |
# File 'lib/rawline/history_buffer.rb', line 21 def position @position end |
#size ⇒ Object (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(={}) 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_position ⇒ Object
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 |
#empty ⇒ Object
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.
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(={}) 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 |
#get ⇒ Object
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
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.
91 92 93 |
# File 'lib/rawline/history_buffer.rb', line 91 def start? @position == 0 end |